tazusb view tazusb @ rev 108

tazusb: fix typo
author Christophe Lincoln <pankso@slitaz.org>
date Fri Mar 02 19:02:09 2012 +0100 (2012-03-02)
parents 12a36d113296
children 215abeab51dc
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)."; echo ""
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'/ \
338 $TARGET_ROOT/boot/$ST/isolinux.cfg
340 #[ -f $TARGET_ROOT/boot/$ST/isolinux.msg ] &&
341 #mv $TARGET_ROOT/boot/$ST/isolinux.msg $TARGET_ROOT/boot/$ST/$ST.msg
342 # Modifing all cfg files and include file if exists.
343 #for cfg in $TARGET_ROOT/boot/$ST/*.cfg $TARGET_ROOT/boot/$ST/common.inc
344 #do
345 #[ -f $cfg ] && sed -i s/isolinux.msg/$ST.msg/ $cfg
346 #done
347 }
349 # Let user exit or reboot.
350 exit_or_reboot()
351 {
352 echo "==============================================================================="
353 echo ""
354 gettext "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
355 read anser
356 if [ "$anser" == "reboot" ]; then
357 umount $TARGET_ROOT
358 umount /media/cdrom
359 reboot || reboot -f
360 else
361 umount $TARGET_ROOT
362 umount /media/cdrom
363 echo ""
364 exit 0
365 fi
366 }
368 set_bootable()
369 {
370 # As the boot flag is toggable, need to check it before hand
371 DISK=${DEVICE%[1-99]}
372 ISSET=`fdisk -l $DISK | grep $DEVICE | grep "\*"`
374 # If not set, set bootable
375 if [ "$ISSET" == "" ]; then
376 umount $TARGET_ROOT 2>/dev/null
377 gettext "Setting $DEVICE as bootable..."
378 fdisk $DISK >/dev/null << EOF
379 a
380 1
381 w
382 EOF
383 status
384 fi
385 }
387 # Generate a virtual swap file in /home/swap. SliTaz boot scripts
388 # will activate it, useful for low memory systems
389 gen_swap_file()
390 {
391 echo ""
392 echo -en "\033[1m`gettext \"Gen swap\"`\033[0m
393 ==============================================================================="
394 gettext "Generate a swap file in /home/swap that will be activated on each boot to have
395 more memory available (Empty value to exit).
397 Swap file in Mb : "
398 read size
399 if [ -z "$size" ]; then
400 gettext "Empty value. Exiting..."; echo ""
401 exit 0
402 fi
403 cd /home
404 # Sanity check
405 if [ -f swap ]; then
406 swapoff swap 2>/dev/null
407 fi
408 # DD to gen a virtual disk.
409 dd if=/dev/zero of=swap bs=1M count=$size
410 # Make swap filesystem.
411 mkswap swap
412 swapon swap
413 echo ""
414 }
416 # Clean out old backups to save disk space
417 clean_usb()
418 {
419 echo ""
420 echo -en "\033[1m`gettext \"Clean\"`\033[0m
421 ==============================================================================="
422 gettext "Remove old rootfs.gz.unixtimestamp backup filesystems to free up disk space."; echo -e "\n\n"
423 # Locate and interactively remove old filesystems from /home directory
424 for file in `find /home/boot/rootfs.gz.[0-9]*`
425 do
426 gettext "Do you wish to remove: `basename $file` (Yes/no/exit) ? "
427 read answer
428 case $answer in
429 e|E|"exit"|Exit)
430 exit 0 ;;
431 y|Y|yes|Yes)
432 rm -f $file ;;
433 *)
434 gettext "No filesystems selected, exiting..." ; echo "" ;;
435 esac
436 done
437 }
439 #
440 # Tazusb sequence
441 #
443 case $COMMAND in
444 writefs)
445 # Writefs to rootfs.gz
446 check_root
447 if [ -z $2 ]; then
448 COMPRESSION=none
449 else
450 COMPRESSION=$2
451 fi
452 # Start info
453 echo ""
454 echo -e "\033[1m`gettext \nWrite filesystem\"`\033[0m
455 ==============================================================================="
456 gettext "The command writefs will write all the current filesystem into a suitable cpio
457 archive (rootfs.gz) usable on a bootable LiveUSB media.
459 Archive compression: $COMPRESSION"
460 echo ""
462 # Clear out tazpkg cache
463 rm /var/cache/tazpkg/* -r -f
465 # Optionally remove sound card selection
466 gettext "Do you wish to remove the sound card selection (No/yes/exit) ? "
467 read anser
468 case $anser in
469 e|E|"exit"|Exit)
470 exit 0 ;;
471 y|Y|yes|Yes)
472 gettext "Removing current sound card selection..."
473 rm -f /var/lib/sound-card-driver
474 rm -f /var/lib/alsa/asound.state ;;
475 *)
476 gettext "Keeping current sound card selection..." ;;
477 esac
478 status
479 # Optionally remove screen resolution
480 gettext "Do you wish to remove the screen resolution (No/yes/exit) ? "
481 read anser
482 case $anser in
483 e|E|"exit"|Exit)
484 exit 0 ;;
485 y|Y|yes|Yes)
486 gettext "Removing current screen resolution..."
487 rm -f /etc/X11/screen.conf ;;
488 *)
489 gettext "Keeping current screen resolution..." ;;
490 esac
491 status
493 # Create list of files
494 find /bin /etc /init /sbin /var /dev /lib /root /usr >/tmp/list
496 for dir in /home /proc /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
497 do
498 echo $dir >>/tmp/list
499 done
501 # Generate initramfs with specified compression
502 if [ "$COMPRESSION" = "lzma" ]; then
503 gettext "Creating rootfs.gz with lzma compression... "
504 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
506 elif [ "$COMPRESSION" = "gzip" ]; then
507 gettext "Creating rootfs.gz with gzip compression... "
508 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
510 else
511 gettext "Creating rootfs.gz without compression... "
512 cat /tmp/list | cpio -o -H newc > /rootfs.gz
513 fi
515 # Get initramfs size
516 size=`du -sh /rootfs.gz | cut -f 1`
518 # If the bootable medium is where it should be, copy across
519 if (test -e /home/boot/bzImage); then
520 gettext "Moving rootfs.gz to media. Remember to unmount for delayed writes!"; echo ""
522 # Move the old filesystem with the unix timestamp for reference
523 if (test -e /home/boot/previous.gz); then
524 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
525 fi
527 mv /home/boot/rootfs.gz /home/boot/previous.gz
528 mv /rootfs.gz /home/boot/.
529 else
530 gettext "rootfs.gz is located in /"; echo ""
531 fi
533 echo "==============================================================================="
534 gettext "Root filesystem size: $size"; echo ""
535 echo ""
536 echo "----"
537 gettext "ENTER to continue..."; read i
538 ;;
539 format)
540 # Format a partition.
541 check_root
542 echo ""
543 echo -e "\033[1m`gettext \"Format a device\"`\033[0m"
544 echo "==============================================================================="
545 DEVICE=$2
546 label=$3
547 fs_type=$4
548 if [ -z $DEVICE ]; then
549 ask_for_device
550 check_for_device
551 else
552 echo "Device : $DEVICE"
553 fi
554 test -z $fs_type && get_fs_type
555 get_label
556 unmount_target_usb
557 make_fs "$fs_type"
558 # mkfs_ext3
559 echo "==============================================================================="
560 gettext "Device $label ($DEVICE) is ready to use as LiveUSB and/or /home partition."
561 echo ""
562 ;;
563 gen-liveusb)
564 # Generate a LiveUSB media using files from a LiveCD.
565 check_root
566 echo ""
567 echo -e "\033[1m`gettext \"Gen a LiveUSB media\"`\033[0m"
568 echo "==============================================================================="
569 DEVICE=$2
570 if [ -z $DEVICE ]; then
571 ask_for_device
572 fi
574 check_for_device
575 mount_cdrom
576 get_part_info
577 unmount_target_usb
578 install_mbr
579 set_bootable
580 mount_target_usb
581 copy_cdrom_files
582 install_boot
583 exit_or_reboot
584 ;;
585 gen-swap)
586 check_root
587 gen_swap_file
588 ;;
589 gen-iso2usb|iso2usb)
590 check_root
592 # Check if file exists
593 ISO=$2
594 if [ -z $ISO ] || [ ! -f $ISO ]; then
595 gettext "Please specify a valid filename on the command line."; echo ""
596 exit 1
597 fi
598 echo ""
599 echo -e "\033[1m`gettext \"Copy ISO file to SliTaz LiveUSB media\"`\033[0m"
600 echo "==============================================================================="
601 echo ""
602 DEVICE=$3
603 if [ -z $DEVICE ]; then
604 ask_for_device
605 fi
606 check_for_device
607 mount_iso
608 get_part_info
609 unmount_target_usb
610 install_mbr
611 set_bootable
612 mount_target_usb
613 copy_cdrom_files
614 install_boot
615 umount /media/cdrom
616 losetup -d /dev/loop0
617 exit_or_reboot
618 ;;
619 clean)
620 check_root
621 clean_usb
622 ;;
623 usage|*)
624 # Display usage by default.
625 usage
626 ;;
627 esac
629 exit 0