tazusb view tazusb @ rev 115

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