tazusb view tazusb @ rev 95

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