tazusb view tazusb @ rev 86

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