tazusb view tazusb @ rev 91

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