tazusb view tazusb @ rev 83

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