tazusb view tazusb @ rev 143

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