tazusb view tazusb @ rev 103

Added tag 3.0.3 for changeset bfb5bc01902b
author Eric Joseph-Alexandre <erjo@slitaz.org>
date Mon Feb 20 01:34:42 2012 +0100 (2012-02-20)
parents e1123d4a9a68
children 6de164926943
line source
1 #!/bin/sh
2 # Tazusb - SliTaz LiveUSB
3 #
4 # Tazusb is an utility to generate, configure and manipulate SliTaz LiveUSB
5 # bootable media and/or USB /home partition, such as flash keys, SD card or
6 # USB harddisk. Authors : see AUTHORS
7 #
8 VERSION=3.0.2
10 # Include gettext helper script.
11 . /usr/bin/gettext.sh
13 # Export package name for gettext.
14 TEXTDOMAIN='tazusb'
15 export TEXTDOMAIN
17 COMMAND=$1
18 TARGET_ROOT=/media/flash
19 DRIVE_NAME=`cat /proc/sys/dev/cdrom/info | grep "drive name" | cut -f 3`
20 CDROM=/dev/$DRIVE_NAME
21 LOG=/tmp/$(basename $0).log
23 #
24 # Tazusb functions
25 #
27 # Print the usage.
28 usage ()
29 {
30 echo -e "`gettext \"SliTaz Live USB - Version:\"` $VERSION\n
31 \033[1m`gettext \"Usage: \"`\033[0m `basename $0` [command] [compression|device]
32 \033[1m`gettext \"Commands:\"`\033[0m\n
33 usage `gettext \"Print this short usage.\"`
34 writefs `gettext \"Write the current filesystem to rootfs.gz.
35 Supported compression: lzma. gzip, none.\"`
36 format `gettext \"Format and label device with ext3, ext2 or fat32 filesystem
37 (for LiveUSB or /home). Default is ext3.\"`
38 gen-liveusb `gettext \"Generate a bootable LiveUSB using files from the LiveCD.\"`
39 gen-swap `gettext \"Create or recreate and activate additional swap memory.\"`
40 gen-iso2usb `gettext \"Generate a bootable LiveUSB using files from ISO file.\"`
41 clean `gettext \"Remove old backup filesystems to free disk space.\"`\n"
42 }
44 # Status function.
45 status()
46 {
47 local CHECK=$?
48 echo -en "\\033[70G[ "
49 if [ $CHECK = 0 ]; then
50 echo -en "\\033[1;33m`gettext \"OK\"`"
51 else
52 echo -en "\\033[1;31m`gettext \"Failed\"`"
53 fi
54 echo -e "\\033[0;39m ]"
55 }
57 # Exit if user is not root.
58 check_root()
59 {
60 if test $(id -u) != 0 ; then
61 # echo -e "\nThis program requires being run as root.\n"
62 gettext "This program requires being run as root."
63 echo ""
64 exit 0
65 fi
66 }
68 # Display a list of available partitions.
69 fdisk_list()
70 {
71 echo "----"
72 fdisk -l | grep ^/dev/sd*
73 echo "----"
74 }
76 # We need a USB media to install.
77 ask_for_device()
78 {
79 gettext "\
80 Please specify the target USB device to $COMMAND. You can type 'list' to
81 get a list of devices, type 'exit' or give an empty value to exit.
83 Device to use : "; read anser
84 while [ "$anser" == "list" ]; do
85 fdisk_list
86 gettext "Device to use : "; read anser
87 done
88 if [ "$anser" = "" -o "$anser" = "exit" ]; then
89 echo ""
90 gettext "No specified device or exit."
91 echo ""
92 exit 0
93 else
94 DEVICE=$anser
95 fi
96 }
98 # Verify a device exists before format or install
99 check_for_device()
100 {
101 IFDEV=`fdisk -l | grep $DEVICE`
102 if [ -z "$IFDEV" ]; then
103 echo ""
104 gettext "Unable to find device: $DEVICE"
105 echo ""
106 exit 0
107 fi
108 }
110 # gets the UUID and filesystem type
111 get_part_info()
112 {
113 UUID=`blkid -s UUID -o value $DEVICE`
114 FSTYPE=`blkid -s TYPE -o value $DEVICE`
115 }
117 # Format target device and label partition.
118 mkfs_ext3()
119 {
120 gettext "Please specify a label for the partition (TazUSB): "
121 read label
123 if [ -z $label ]; then
124 label=TazUSB
125 fi
127 gettext "Label : $label"; echo ""
128 gettext "Mkfs : mkfs.ext3 -L \"$label\" $DEVICE" ; echo ""
129 echo "" && sleep 2
130 mkfs.ext3 -L "$label" $DEVICE
132 }
134 # Get label for device
135 get_label()
136 {
137 gettext "Please specify a label for the partition (TazUSB): "
138 read label
140 if [ -z $label ]; then
141 label=TazUSB
142 fi
143 }
145 # Get fs type. Supported fs are ext3, ext2, fat32
146 get_fs_type()
147 {
148 gettext "Please specify a filesystem type ext2, ext3 or fat32 (ext3): "
149 read fs_type
151 if [ -z $fs_type ]; then
152 fs_type=ext3
153 fi
154 }
156 # We can chose the filesystem type.
157 ask_for_fs_type()
158 {
159 gettext "\
160 Please specify the filesystem type to $COMMAND.
161 Available formats are ext3(default), ext2 or fat32.
162 Press enter to keep the default value.
164 File system type : "; read anser
165 if [ "$anser" = "" ]; then
166 FS_TYPE=ext3
167 else
168 FS_TYPE=$anser
169 fi
170 }
172 # Format target device in ext3, ext2 or fat32.
173 # Usage: make_fs ext2|fat32|ext3 (default)
174 make_fs()
175 {
176 local answer
178 FS=$(echo $fs_type | tr [A-Z] [a-z])
179 case "$FS" in
180 ext3|ext2)
181 echo ""; gettext "Processing..." ; echo ""
182 gettext "Label : $label" ; echo ""
183 gettext "Mkfs : mkfs.$FS -L \"$label\" $DEVICE"
184 echo "" && sleep 2
185 mkfs.$@ -L "$label" $DEVICE > $LOG 2>&1
186 ;;
187 [Ff]at32)
188 if [ -x /sbin/mkdosfs ];then
189 echo "" ; gettext "Processing..."; echo ""
190 gettext "Label : $label" ; echo ""
191 gettext "Mkfs : mkdosfs -F 32 -n \"$label\" $DEVICE"; echo ""
192 echo "" && sleep 2
193 mkdosfs -F 32 -n "$label" $DEVICE
194 else
195 gettext "Can't find mkdosfs tool.\nWould you like to install dosfstools from repository [y/N] ? "; read answer
196 case $answer in
197 y|Y)
198 yes | tazpkg get-install dosfstools && make_fs fat32;;
199 *)
200 exit 1 ;;
201 esac
202 fi
203 ;;
204 *)
205 gettext "Sorry. Filesystem $FS is not supported."; echo ""
206 exit
207 esac
208 }
210 # Mount an existing USB device.
211 unmount_target_usb()
212 {
213 # If mount point is in use, unmount
214 if mount | grep -q $TARGET_ROOT; then
215 umount $TARGET_ROOT
216 fi
218 # Device could be mounted elsewhere, so unmount
219 if mount | grep -q $DEVICE; then
220 gettext "Unmounting USB target device..."; echo ""
221 umount $DEVICE
222 fi
223 }
225 # Mount an existing USB device.
226 mount_target_usb()
227 {
228 gettext "Mounting USB target device..." ; echo ""
229 mkdir -p $TARGET_ROOT
230 mount $DEVICE $TARGET_ROOT 2>/dev/null
231 }
233 # Mount SliTaz LiveCD to get needed files.
234 mount_cdrom()
235 {
236 gettext "Mounting cdrom device..."; echo ""
238 if mount | grep /media/cdrom; then
239 umount /media/cdrom
240 fi
242 mkdir -p /media/cdrom
243 mount -r -t iso9660 $CDROM /media/cdrom 2>/dev/null
245 if [ ! -f /media/cdrom/boot/rootfs.gz -a \
246 ! -f /media/cdrom/boot/rootfs1.gz ]; then
247 echo ""; gettext "Unable to mount cdrom or to find a filesystem on it (rootfs.gz)."; ehco ""
248 exit 0
249 fi
250 }
252 # Mount SliTaz ISO to get needed files.
253 mount_iso()
254 {
255 if mount | grep /media/cdrom ; then
256 umount /media/cdrom
257 fi
259 test -d /media/cdrom || mkdir -p /media/cdrom
261 # Add support to other distros.
262 if [ ! -f /etc/slitaz-version ]; then
263 OPTIONS="-o loop"
264 else
265 OPTIONS=""
266 fi
268 gettext "Mounting "
269 echo "`basename $ISO`..."; echo ""
270 mount $OPTIONS $ISO /media/cdrom 2>/dev/null
272 if [ ! -f /media/cdrom/boot/rootfs.gz -a \
273 ! -f /media/cdrom/boot/rootfs1.gz ]; then
274 gettext "Unable to mount iso or to find a filesystem on it (rootfs.gz)."; echo ""
275 exit 0
276 fi
277 }
279 # All needed files are in the boot directory of the cdrom.
280 copy_cdrom_files()
281 {
282 gettext "Copying needed files from cdrom..."
283 mkdir -p $TARGET_ROOT/boot
284 cp /media/cdrom/boot/bzImage $TARGET_ROOT/boot
285 cp /media/cdrom/boot/rootfs*.gz $TARGET_ROOT/boot
286 #rem=0
287 #~ for i in $(ls /media/cdrom/boot/rootfs*.gz | sort -r); do
288 #~ [ $rem -ne 0 ] &&
289 #~ dd if=/dev/zero bs=1 count=$((4 - $rem)) 2> /dev/null
290 #~ cat $i
291 #~ rem=$(stat -c %s $i)
292 #~ rem=$(($rem % 4))
293 #~ done > $TARGET_ROOT/boot/rootfs.gz
294 status
295 }
297 install_mbr()
298 {
299 # MBR
300 DISK=${DEVICE%[1-99]}
301 if [ -f /usr/share/boot/mbr.bin ]; then
302 gettext "Installing a new MBR to: $DISK"
303 cat /usr/share/boot/mbr.bin > $DISK
304 status
305 else
306 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
307 gettext "No new MBR installed to: $DISK"; echo ""
308 fi
309 }
311 # ext/syslinux install
312 install_boot()
313 {
314 # Decide if we're installing syslinux or extlinux
315 if [ "$FSTYPE" = "vfat" ]; then
316 ST=syslinux
317 STC="syslinux --install -d /boot/syslinux/ $DEVICE"
318 STE=cfg
319 else
320 ST=extlinux
321 STC="extlinux --install $TARGET_ROOT/boot/$ST"
322 STE=conf
323 fi
325 gettext "Installing bootloader: $ST"; echo ""
326 mkdir -p $TARGET_ROOT/boot/$ST
327 $STC
329 # Use existing isolinux.cfg for extlinux.conf or syslinux.cfg
330 cp /media/cdrom/boot/isolinux/isolinux.cfg $TARGET_ROOT/boot/$ST/$ST.$STE
331 sed -i "s/isolinux.msg/$ST.msg/" $TARGET_ROOT/boot/$ST/$ST.$STE
333 # Update DVD autoinstall
334 sed -i "s/LABEL=packages-[^,]*/UUID=$UUID/g" $(grep -l append $TARGET_ROOT/boot/$ST/*)
336 # Add home= to append in extlinux or syslinux.cfg
337 sed -i -e "s/\(append.*\)/\1 home=$UUID/" $(grep -l append $TARGET_ROOT/boot/$ST/*)
339 # Splash screen and help files.
340 if [ -f /media/cdrom/boot/isolinux/isolinux.msg ]; then
341 cp /media/cdrom/boot/isolinux/isolinux.msg $TARGET_ROOT/boot/$ST/$ST.msg
342 sed -i s/'SliTaz GNU\/Linux'/'SliTaz GNU\/Linux LiveUSB'/ $TARGET_ROOT/boot/$ST/$ST.msg
343 fi
344 if [ -f /media/cdrom/boot/isolinux/splash.lss ]; then
345 cp /media/cdrom/boot/isolinux/splash.lss $TARGET_ROOT/boot/$ST
346 fi
347 cp /media/cdrom/boot/isolinux/*.txt $TARGET_ROOT/boot/$ST
348 cp /media/cdrom/boot/isolinux/*.cfg $TARGET_ROOT/boot/$ST
349 cp /media/cdrom/boot/isolinux/*.kbd $TARGET_ROOT/boot/$ST
350 cp /media/cdrom/boot/isolinux/*.c32 $TARGET_ROOT/boot/$ST
352 # Modifing all cfg files.
353 for cfg in $TARGET_ROOT/boot/$ST/*.cfg
354 do
355 sed -i s/isolinux.msg/$ST.msg/ $cfg
356 done
358 # Modifing include file if exists.
359 if [ -f $TARGET_ROOT/boot/$ST/common.inc ]; then
360 sed -i -e "s/isolinux.msg/$ST.msg/" $TARGET_ROOT/boot/$ST/common.inc
361 fi
363 # Un-meta-ize a multi-in-one flavor
364 if grep -qs "label slitaz" $TARGET_ROOT/boot/$ST/common.cfg ; then
365 sed -i "s/isolinux/$ST/;s/label slitaz/label multi/" $TARGET_ROOT/boot/$ST/common.cfg
366 if [ -f "$TARGET_ROOT/boot/$ST/$ST.msg" ]; then
367 sed -i 's/\(.*\), flavors.*/ \1/' \
368 $TARGET_ROOT/boot/$ST/$ST.msg
369 fi
370 for i in $TARGET_ROOT/boot/$ST/$ST.$STE \
371 $TARGET_ROOT/boot/$ST/??.$STE \
372 $TARGET_ROOT/boot/$ST/??_??.$STE; do
373 sed '/label /!d;/label /{s/label .*/\nlabel slitaz/;NN;s/rootfs..gz.*gz /rootfs.gz /;N;q}' \
374 < $i >> $i
375 done
376 fi
378 }
380 # Let user exit or reboot.
381 exit_or_reboot()
382 {
383 echo "==============================================================================="
384 echo ""
385 gettext "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
386 read anser
387 if [ "$anser" == "reboot" ]; then
388 umount $TARGET_ROOT
389 umount /media/cdrom
390 reboot || reboot -f
391 else
392 umount $TARGET_ROOT
393 umount /media/cdrom
394 echo ""
395 exit 0
396 fi
397 }
399 set_bootable()
400 {
401 # As the boot flag is toggable, need to check it before hand
402 DISK=${DEVICE%[1-99]}
403 ISSET=`fdisk -l $DISK | grep $DEVICE | grep "\*"`
405 # If not set, set bootable
406 if [ "$ISSET" == "" ]; then
407 umount $TARGET_ROOT 2>/dev/null
408 gettext "Setting $DEVICE as bootable..."
409 fdisk $DISK >/dev/null << EOF
410 a
411 1
412 w
413 EOF
414 status
415 fi
416 }
418 # Generate a virtual swap file in /home/swap. SliTaz boot scripts
419 # will activate it, useful for low memory systems
420 gen_swap_file()
421 {
422 echo ""
423 echo -en "\033[1m`gettext \"Gen swap\"`\033[0m
424 ==============================================================================="
425 gettext "Generate a swap file in /home/swap that will be activated on each boot to have
426 more memory available (Empty value to exit).
428 Swap file in Mb : "
429 read size
430 if [ -z "$size" ]; then
431 gettext "Empty value. Exiting..."; echo ""
432 exit 0
433 fi
434 cd /home
435 # Sanity check
436 if [ -f swap ]; then
437 swapoff swap 2>/dev/null
438 fi
439 # DD to gen a virtual disk.
440 dd if=/dev/zero of=swap bs=1M count=$size
441 # Make swap filesystem.
442 mkswap swap
443 swapon swap
444 echo ""
445 }
447 # Clean out old backups to save disk space
448 clean_usb()
449 {
450 echo ""
451 echo -en "\033[1m`gettext \"Clean\"`\033[0m
452 ==============================================================================="
453 gettext "Remove old rootfs.gz.unixtimestamp backup filesystems to free up disk space."; echo -e "\n\n"
454 # Locate and interactively remove old filesystems from /home directory
455 for file in `find /home/boot/rootfs.gz.[0-9]*`
456 do
457 gettext "Do you wish to remove: `basename $file` (Yes/no/exit) ? "
458 read answer
459 case $answer in
460 e|E|"exit"|Exit)
461 exit 0 ;;
462 y|Y|yes|Yes)
463 rm -f $file ;;
464 *)
465 gettext "No filesystems selected, exiting..." ; echo "" ;;
466 esac
467 done
468 }
470 #
471 # Tazusb sequence
472 #
474 case $COMMAND in
475 writefs)
476 # Writefs to rootfs.gz
477 check_root
478 if [ -z $2 ]; then
479 COMPRESSION=none
480 else
481 COMPRESSION=$2
482 fi
483 # Start info
484 echo ""
485 echo -e "\033[1m`gettext \nWrite filesystem\"`\033[0m
486 ==============================================================================="
487 gettext "The command writefs will write all the current filesystem into a suitable cpio
488 archive (rootfs.gz) usable on a bootable LiveUSB media.
490 Archive compression: $COMPRESSION"
491 echo ""
493 # Clear out tazpkg cache
494 rm /var/cache/tazpkg/* -r -f
496 # Optionally remove sound card selection
497 gettext "Do you wish to remove the sound card selection (No/yes/exit) ? "
498 read anser
499 case $anser in
500 e|E|"exit"|Exit)
501 exit 0 ;;
502 y|Y|yes|Yes)
503 gettext "Removing current sound card selection..."
504 rm -f /var/lib/sound-card-driver
505 rm -f /var/lib/alsa/asound.state ;;
506 *)
507 gettext "Keeping current sound card selection..." ;;
508 esac
509 status
510 # Optionally remove screen resolution
511 gettext "Do you wish to remove the screen resolution (No/yes/exit) ? "
512 read anser
513 case $anser in
514 e|E|"exit"|Exit)
515 exit 0 ;;
516 y|Y|yes|Yes)
517 gettext "Removing current screen resolution..."
518 rm -f /etc/X11/screen.conf ;;
519 *)
520 gettext "Keeping current screen resolution..." ;;
521 esac
522 status
524 # Create list of files
525 find /bin /etc /init /sbin /var /dev /lib /root /usr >/tmp/list
527 for dir in /home /proc /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
528 do
529 echo $dir >>/tmp/list
530 done
532 # Generate initramfs with specified compression
533 if [ "$COMPRESSION" = "lzma" ]; then
534 gettext "Creating rootfs.gz with lzma compression... "
535 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
537 elif [ "$COMPRESSION" = "gzip" ]; then
538 gettext "Creating rootfs.gz with gzip compression... "
539 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
541 else
542 gettext "Creating rootfs.gz without compression... "
543 cat /tmp/list | cpio -o -H newc > /rootfs.gz
544 fi
546 # Get initramfs size
547 size=`du -sh /rootfs.gz | cut -f 1`
549 # If the bootable medium is where it should be, copy across
550 if (test -e /home/boot/bzImage); then
551 gettext "Moving rootfs.gz to media. Remember to unmount for delayed writes!"; echo ""
553 # Move the old filesystem with the unix timestamp for reference
554 if (test -e /home/boot/previous.gz); then
555 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
556 fi
558 mv /home/boot/rootfs.gz /home/boot/previous.gz
559 mv /rootfs.gz /home/boot/.
560 else
561 gettext "rootfs.gz is located in /"; echo ""
562 fi
564 echo "==============================================================================="
565 gettext "Root filesystem size: $size"; echo ""
566 echo ""
567 echo "----"
568 gettext "ENTER to continue..."; read i
569 ;;
570 format)
571 # Format a partition.
572 check_root
573 echo ""
574 echo -e "\033[1m`gettext \"Format a device\"`\033[0m"
575 echo "==============================================================================="
576 DEVICE=$2
577 label=$3
578 fs_type=$4
579 if [ -z $DEVICE ]; then
580 ask_for_device
581 check_for_device
582 else
583 echo "Device : $DEVICE"
584 fi
585 test -z $fs_type && get_fs_type
586 get_label
587 unmount_target_usb
588 make_fs "$fs_type"
589 # mkfs_ext3
590 echo "==============================================================================="
591 gettext "Device $label ($DEVICE) is ready to use as LiveUSB and/or /home partition."
592 echo ""
593 ;;
594 gen-liveusb)
595 # Generate a LiveUSB media using files from a LiveCD.
596 check_root
597 echo ""
598 echo -e "\033[1m`gettext \"Gen a LiveUSB media\"`\033[0m"
599 echo "==============================================================================="
600 DEVICE=$2
601 if [ -z $DEVICE ]; then
602 ask_for_device
603 fi
605 check_for_device
606 mount_cdrom
607 get_part_info
608 unmount_target_usb
609 install_mbr
610 set_bootable
611 mount_target_usb
612 copy_cdrom_files
613 install_boot
614 exit_or_reboot
615 ;;
616 gen-swap)
617 check_root
618 gen_swap_file
619 ;;
620 gen-iso2usb|iso2usb)
621 check_root
623 # Check if file exists
624 ISO=$2
625 if [ -z $ISO ] || [ ! -f $ISO ]; then
626 gettext "Please specify a valid filename on the command line."; echo ""
627 exit 1
628 fi
629 echo ""
630 echo -e "\033[1m`gettext \"Copy ISO file to SliTaz LiveUSB media\"`\033[0m"
631 echo "==============================================================================="
632 echo ""
633 DEVICE=$3
634 if [ -z $DEVICE ]; then
635 ask_for_device
636 fi
637 check_for_device
638 mount_iso
639 get_part_info
640 unmount_target_usb
641 install_mbr
642 set_bootable
643 mount_target_usb
644 copy_cdrom_files
645 install_boot
646 umount /media/cdrom
647 losetup -d /dev/loop0
648 exit_or_reboot
649 ;;
650 clean)
651 check_root
652 clean_usb
653 ;;
654 usage|*)
655 # Display usage by default.
656 usage
657 ;;
658 esac
660 exit 0