tazusb view tazusb @ rev 104

tazusb: keep multi-in-one flavor
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Feb 20 13:40:49 2012 +0100 (2012-02-20)
parents bfb5bc01902b
children 12a36d113296
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 status
287 }
289 install_mbr()
290 {
291 # MBR
292 DISK=${DEVICE%[1-99]}
293 if [ -f /usr/share/boot/mbr.bin ]; then
294 gettext "Installing a new MBR to: $DISK"
295 cat /usr/share/boot/mbr.bin > $DISK
296 status
297 else
298 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
299 gettext "No new MBR installed to: $DISK"; echo ""
300 fi
301 }
303 # ext/syslinux install
304 install_boot()
305 {
306 # Decide if we're installing syslinux or extlinux
307 if [ "$FSTYPE" = "vfat" ]; then
308 ST=syslinux
309 STC="syslinux --install -d /boot/syslinux/ $DEVICE"
310 STE=cfg
311 else
312 ST=extlinux
313 STC="extlinux --install $TARGET_ROOT/boot/$ST"
314 STE=conf
315 fi
317 gettext "Installing bootloader: $ST"; echo ""
318 mkdir -p $TARGET_ROOT/boot/$ST
319 $STC
321 # Use existing isolinux.cfg for extlinux.conf or syslinux.cfg
322 cp /media/cdrom/boot/isolinux/isolinux.cfg $TARGET_ROOT/boot/$ST/$ST.$STE
323 sed -i "s/isolinux.msg/$ST.msg/" $TARGET_ROOT/boot/$ST/$ST.$STE
325 # Update DVD autoinstall
326 sed -i "s/LABEL=packages-[^,]*/UUID=$UUID/g" $(grep -l append $TARGET_ROOT/boot/$ST/*)
328 # Add home= to append in extlinux or syslinux.cfg
329 sed -i -e "s/\(append.*\)/\1 home=$UUID/" $(grep -l append $TARGET_ROOT/boot/$ST/*)
331 # Splash screen and help files.
332 cp /media/cdrom/boot/isolinux/splash.* $TARGET_ROOT/boot/$ST
333 cp /media/cdrom/boot/isolinux/*.txt $TARGET_ROOT/boot/$ST
334 cp /media/cdrom/boot/isolinux/*.cfg $TARGET_ROOT/boot/$ST
335 cp /media/cdrom/boot/isolinux/*.kbd $TARGET_ROOT/boot/$ST
336 cp /media/cdrom/boot/isolinux/*.c32 $TARGET_ROOT/boot/$ST
337 sed -i s/'SliTaz GNU\/Linux'/'SliTaz GNU\/Linux LiveUSB'/ $TARGET_ROOT/boot/$ST/isolinux.*g
339 [ -f $TARGET_ROOT/boot/$ST/isolinux.msg ] &&
340 mv $TARGET_ROOT/boot/$ST/isolinux.msg $TARGET_ROOT/boot/$ST/$ST.msg
341 # Modifing all cfg files and include file if exists.
342 for cfg in $TARGET_ROOT/boot/$ST/*.cfg $TARGET_ROOT/boot/$ST/common.inc
343 do
344 [ -f $cfg ] && sed -i s/isolinux.msg/$ST.msg/ $cfg
345 done
346 }
348 # Let user exit or reboot.
349 exit_or_reboot()
350 {
351 echo "==============================================================================="
352 echo ""
353 gettext "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
354 read anser
355 if [ "$anser" == "reboot" ]; then
356 umount $TARGET_ROOT
357 umount /media/cdrom
358 reboot || reboot -f
359 else
360 umount $TARGET_ROOT
361 umount /media/cdrom
362 echo ""
363 exit 0
364 fi
365 }
367 set_bootable()
368 {
369 # As the boot flag is toggable, need to check it before hand
370 DISK=${DEVICE%[1-99]}
371 ISSET=`fdisk -l $DISK | grep $DEVICE | grep "\*"`
373 # If not set, set bootable
374 if [ "$ISSET" == "" ]; then
375 umount $TARGET_ROOT 2>/dev/null
376 gettext "Setting $DEVICE as bootable..."
377 fdisk $DISK >/dev/null << EOF
378 a
379 1
380 w
381 EOF
382 status
383 fi
384 }
386 # Generate a virtual swap file in /home/swap. SliTaz boot scripts
387 # will activate it, useful for low memory systems
388 gen_swap_file()
389 {
390 echo ""
391 echo -en "\033[1m`gettext \"Gen swap\"`\033[0m
392 ==============================================================================="
393 gettext "Generate a swap file in /home/swap that will be activated on each boot to have
394 more memory available (Empty value to exit).
396 Swap file in Mb : "
397 read size
398 if [ -z "$size" ]; then
399 gettext "Empty value. Exiting..."; echo ""
400 exit 0
401 fi
402 cd /home
403 # Sanity check
404 if [ -f swap ]; then
405 swapoff swap 2>/dev/null
406 fi
407 # DD to gen a virtual disk.
408 dd if=/dev/zero of=swap bs=1M count=$size
409 # Make swap filesystem.
410 mkswap swap
411 swapon swap
412 echo ""
413 }
415 # Clean out old backups to save disk space
416 clean_usb()
417 {
418 echo ""
419 echo -en "\033[1m`gettext \"Clean\"`\033[0m
420 ==============================================================================="
421 gettext "Remove old rootfs.gz.unixtimestamp backup filesystems to free up disk space."; echo -e "\n\n"
422 # Locate and interactively remove old filesystems from /home directory
423 for file in `find /home/boot/rootfs.gz.[0-9]*`
424 do
425 gettext "Do you wish to remove: `basename $file` (Yes/no/exit) ? "
426 read answer
427 case $answer in
428 e|E|"exit"|Exit)
429 exit 0 ;;
430 y|Y|yes|Yes)
431 rm -f $file ;;
432 *)
433 gettext "No filesystems selected, exiting..." ; echo "" ;;
434 esac
435 done
436 }
438 #
439 # Tazusb sequence
440 #
442 case $COMMAND in
443 writefs)
444 # Writefs to rootfs.gz
445 check_root
446 if [ -z $2 ]; then
447 COMPRESSION=none
448 else
449 COMPRESSION=$2
450 fi
451 # Start info
452 echo ""
453 echo -e "\033[1m`gettext \nWrite filesystem\"`\033[0m
454 ==============================================================================="
455 gettext "The command writefs will write all the current filesystem into a suitable cpio
456 archive (rootfs.gz) usable on a bootable LiveUSB media.
458 Archive compression: $COMPRESSION"
459 echo ""
461 # Clear out tazpkg cache
462 rm /var/cache/tazpkg/* -r -f
464 # Optionally remove sound card selection
465 gettext "Do you wish to remove the sound card selection (No/yes/exit) ? "
466 read anser
467 case $anser in
468 e|E|"exit"|Exit)
469 exit 0 ;;
470 y|Y|yes|Yes)
471 gettext "Removing current sound card selection..."
472 rm -f /var/lib/sound-card-driver
473 rm -f /var/lib/alsa/asound.state ;;
474 *)
475 gettext "Keeping current sound card selection..." ;;
476 esac
477 status
478 # Optionally remove screen resolution
479 gettext "Do you wish to remove the screen resolution (No/yes/exit) ? "
480 read anser
481 case $anser in
482 e|E|"exit"|Exit)
483 exit 0 ;;
484 y|Y|yes|Yes)
485 gettext "Removing current screen resolution..."
486 rm -f /etc/X11/screen.conf ;;
487 *)
488 gettext "Keeping current screen resolution..." ;;
489 esac
490 status
492 # Create list of files
493 find /bin /etc /init /sbin /var /dev /lib /root /usr >/tmp/list
495 for dir in /home /proc /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
496 do
497 echo $dir >>/tmp/list
498 done
500 # Generate initramfs with specified compression
501 if [ "$COMPRESSION" = "lzma" ]; then
502 gettext "Creating rootfs.gz with lzma compression... "
503 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
505 elif [ "$COMPRESSION" = "gzip" ]; then
506 gettext "Creating rootfs.gz with gzip compression... "
507 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
509 else
510 gettext "Creating rootfs.gz without compression... "
511 cat /tmp/list | cpio -o -H newc > /rootfs.gz
512 fi
514 # Get initramfs size
515 size=`du -sh /rootfs.gz | cut -f 1`
517 # If the bootable medium is where it should be, copy across
518 if (test -e /home/boot/bzImage); then
519 gettext "Moving rootfs.gz to media. Remember to unmount for delayed writes!"; echo ""
521 # Move the old filesystem with the unix timestamp for reference
522 if (test -e /home/boot/previous.gz); then
523 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
524 fi
526 mv /home/boot/rootfs.gz /home/boot/previous.gz
527 mv /rootfs.gz /home/boot/.
528 else
529 gettext "rootfs.gz is located in /"; echo ""
530 fi
532 echo "==============================================================================="
533 gettext "Root filesystem size: $size"; echo ""
534 echo ""
535 echo "----"
536 gettext "ENTER to continue..."; read i
537 ;;
538 format)
539 # Format a partition.
540 check_root
541 echo ""
542 echo -e "\033[1m`gettext \"Format a device\"`\033[0m"
543 echo "==============================================================================="
544 DEVICE=$2
545 label=$3
546 fs_type=$4
547 if [ -z $DEVICE ]; then
548 ask_for_device
549 check_for_device
550 else
551 echo "Device : $DEVICE"
552 fi
553 test -z $fs_type && get_fs_type
554 get_label
555 unmount_target_usb
556 make_fs "$fs_type"
557 # mkfs_ext3
558 echo "==============================================================================="
559 gettext "Device $label ($DEVICE) is ready to use as LiveUSB and/or /home partition."
560 echo ""
561 ;;
562 gen-liveusb)
563 # Generate a LiveUSB media using files from a LiveCD.
564 check_root
565 echo ""
566 echo -e "\033[1m`gettext \"Gen a LiveUSB media\"`\033[0m"
567 echo "==============================================================================="
568 DEVICE=$2
569 if [ -z $DEVICE ]; then
570 ask_for_device
571 fi
573 check_for_device
574 mount_cdrom
575 get_part_info
576 unmount_target_usb
577 install_mbr
578 set_bootable
579 mount_target_usb
580 copy_cdrom_files
581 install_boot
582 exit_or_reboot
583 ;;
584 gen-swap)
585 check_root
586 gen_swap_file
587 ;;
588 gen-iso2usb|iso2usb)
589 check_root
591 # Check if file exists
592 ISO=$2
593 if [ -z $ISO ] || [ ! -f $ISO ]; then
594 gettext "Please specify a valid filename on the command line."; echo ""
595 exit 1
596 fi
597 echo ""
598 echo -e "\033[1m`gettext \"Copy ISO file to SliTaz LiveUSB media\"`\033[0m"
599 echo "==============================================================================="
600 echo ""
601 DEVICE=$3
602 if [ -z $DEVICE ]; then
603 ask_for_device
604 fi
605 check_for_device
606 mount_iso
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 umount /media/cdrom
615 losetup -d /dev/loop0
616 exit_or_reboot
617 ;;
618 clean)
619 check_root
620 clean_usb
621 ;;
622 usage|*)
623 # Display usage by default.
624 usage
625 ;;
626 esac
628 exit 0