tazusb view tazusb @ rev 138

tazusb: update VERSION
author Erjo <erjo@openema.com>
date Sat Jan 05 15:08:52 2013 +0100 (2013-01-05)
parents c9d9883c6368
children fb57b8839aa2
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.1
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= to APPEND 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/*.txt $TARGET_ROOT/boot/$ST
311 cp /media/cdrom/boot/isolinux/*.cfg $TARGET_ROOT/boot/$ST
312 cp /media/cdrom/boot/isolinux/*.kbd $TARGET_ROOT/boot/$ST
313 cp /media/cdrom/boot/isolinux/*.c32 $TARGET_ROOT/boot/$ST
314 sed -i -e s/'SliTaz GNU\/Linux'/'SliTaz GNU\/Linux LiveUSB'/ \
315 -e s/'isolinux'/'extlinux'/ $TARGET_ROOT/boot/$ST/$ST.$STE
316 }
318 # Let user exit or reboot.
319 exit_or_reboot()
320 {
321 separator
322 echo ""
323 gettext "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
324 read anser
325 if [ "$anser" == "reboot" ]; then
326 unmount_target_usb
327 reboot || reboot -f
328 else
329 unmount_target_usb
330 echo "" && exit 0
331 fi
332 }
334 set_bootable()
335 {
336 # As the boot flag is toggable, need to check it before hand
337 DISK=${DEVICE%[1-99]}
338 ISSET=`fdisk -l $DISK | grep $DEVICE | grep "\*"`
340 # If not set, set bootable
341 if [ "$ISSET" == "" ]; then
342 umount $TARGET_ROOT 2>/dev/null
343 gettext "Setting $DEVICE as bootable..."
344 fdisk $DISK >/dev/null << EOF
345 a
346 1
347 w
348 EOF
349 status
350 fi
351 }
353 # Generate a virtual swap file in /home/swap. SliTaz boot scripts
354 # will activate it, useful for low memory systems
355 gen_swap_file()
356 {
357 echo ""
358 echo -en "\033[1m`gettext \"Gen swap\"`\033[0m
359 ==============================================================================="
360 gettext "Generate a swap file in /home/swap that will be activated on each boot to have
361 more memory available (Empty value to exit).
363 Swap file in Mb : "
364 read size
365 if [ -z "$size" ]; then
366 gettext "Empty value. Exiting..."; echo ""
367 exit 0
368 fi
369 cd /home
370 # Sanity check
371 if [ -f swap ]; then
372 swapoff swap 2>/dev/null
373 fi
374 # DD to gen a virtual disk.
375 dd if=/dev/zero of=swap bs=1M count=$size
376 # Make swap filesystem.
377 mkswap swap
378 swapon swap
379 echo ""
380 }
382 # Clean out old backups to save disk space
383 clean_usb()
384 {
385 echo ""
386 echo -en "\033[1m`gettext \"Clean\"`\033[0m
387 ==============================================================================="
388 gettext "Remove old rootfs.gz.unixtimestamp backup filesystems to free up disk space."; echo -e "\n\n"
389 # Locate and interactively remove old filesystems from /home directory
390 for file in `find /home/boot/rootfs.gz.[0-9]*`
391 do
392 gettext "Do you wish to remove: `basename $file` (Yes/no/exit) ? "
393 read answer
394 case $answer in
395 e|E|"exit"|Exit)
396 exit 0 ;;
397 y|Y|yes|Yes)
398 rm -f $file ;;
399 *)
400 gettext "No filesystems selected, exiting..." ; echo "" ;;
401 esac
402 done
403 }
405 #
406 # Tazusb sequence
407 #
409 case $COMMAND in
410 writefs)
411 # Writefs to rootfs.gz
412 check_root
413 if [ -z $2 ]; then
414 COMPRESSION=none
415 else
416 COMPRESSION=$2
417 fi
418 # Start info
419 echo ""
420 echo -e "\n\033[1m`gettext "Write filesystem"`\033[0m
421 ==============================================================================="
422 gettext "The command writefs will write all the current filesystem into a suitable cpio
423 archive (rootfs.gz) usable on a bootable LiveUSB media.
425 Archive compression: $COMPRESSION"
426 echo ""
428 # Clear out tazpkg cache
429 rm /var/cache/tazpkg/* -r -f
431 # Optionally remove sound card selection
432 gettext "Do you wish to remove the sound card selection (No/yes/exit) ? "
433 read anser
434 case $anser in
435 e|E|"exit"|Exit)
436 exit 0 ;;
437 y|Y|yes|Yes)
438 gettext "Removing current sound card selection..."
439 rm -f /var/lib/sound-card-driver
440 rm -f /var/lib/alsa/asound.state ;;
441 *)
442 gettext "Keeping current sound card selection..." ;;
443 esac
444 status
445 # Optionally remove screen resolution
446 gettext "Do you wish to remove the screen resolution (No/yes/exit) ? "
447 read anser
448 case $anser in
449 e|E|"exit"|Exit)
450 exit 0 ;;
451 y|Y|yes|Yes)
452 gettext "Removing current screen resolution..."
453 rm -f /etc/X11/screen.conf ;;
454 *)
455 gettext "Keeping current screen resolution..." ;;
456 esac
457 status
459 # Create list of files
460 find /bin /etc /init /sbin /var /dev /lib /root /usr >/tmp/list
462 for dir in /home /proc /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
463 do
464 echo $dir >>/tmp/list
465 done
467 # Generate initramfs with specified compression
468 if [ "$COMPRESSION" = "lzma" ]; then
469 gettext "Creating rootfs.gz with lzma compression... "
470 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
472 elif [ "$COMPRESSION" = "gzip" ]; then
473 gettext "Creating rootfs.gz with gzip compression... "
474 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
476 else
477 gettext "Creating rootfs.gz without compression... "
478 cat /tmp/list | cpio -o -H newc > /rootfs.gz
479 fi
481 # Get initramfs size
482 size=`du -sh /rootfs.gz | cut -f 1`
484 # If the bootable medium is where it should be, copy across
485 if (test -e /home/boot/bzImage); then
486 gettext "Moving rootfs.gz to media. Remember to unmount for delayed writes!"; echo ""
488 # Move the old filesystem with the unix timestamp for reference
489 if (test -e /home/boot/previous.gz); then
490 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
491 fi
493 mv /home/boot/rootfs.gz /home/boot/previous.gz
494 mv /rootfs.gz /home/boot/.
495 else
496 gettext "rootfs.gz is located in /"; echo ""
497 fi
499 separator
500 gettext "Root filesystem size: $size"; echo ""
501 echo ""
502 echo "----"
503 gettext "ENTER to continue..."; read i
504 ;;
505 format)
506 # Format a partition.
507 check_root
508 echo ""
509 echo -e "\033[1m`gettext \"Format a device\"`\033[0m"
510 separator
511 DEVICE=$2
512 label=$3
513 fs_type=$4
514 if [ -z $DEVICE ]; then
515 ask_for_device
516 check_for_device
517 else
518 echo "Device : $DEVICE"
519 fi
520 test -z $fs_type && get_fs_type
521 get_label
522 unmount_target_usb
523 make_fs "$fs_type"
524 # mkfs_ext3
525 separator
526 gettext "Device $label ($DEVICE) is ready to use as LiveUSB and/or /home partition."
527 echo ""
528 ;;
529 gen-liveusb)
530 # Generate a LiveUSB media using files from a LiveCD.
531 check_root
532 echo ""
533 echo -e "\033[1m`gettext \"Gen a LiveUSB media\"`\033[0m"
534 separator
535 DEVICE=$2
536 if [ -z $DEVICE ]; then
537 ask_for_device
538 fi
540 check_for_device
541 mount_cdrom
542 get_part_info
543 unmount_target_usb
544 install_mbr
545 set_bootable
546 mount_target_usb
547 copy_cdrom_files
548 install_boot
549 exit_or_reboot
550 ;;
551 gen-swap)
552 check_root
553 gen_swap_file ;;
554 gen-iso2usb|iso2usb)
555 check_root
556 # Check if file exists
557 ISO=$2
558 if [ -z $ISO ] || [ ! -f $ISO ]; then
559 gettext "Please specify a valid filename on the command line."; echo ""
560 exit 1
561 fi
562 echo ""
563 boldify "$(gettext "Copy ISO file to SliTaz LiveUSB media")"
564 separator
565 DEVICE=$3
566 if [ -z $DEVICE ]; then
567 ask_for_device
568 fi
569 check_for_device
570 mount_iso
571 get_part_info
572 unmount_target_usb
573 install_mbr
574 set_bootable
575 mount_target_usb
576 copy_cdrom_files
577 install_boot
578 umount /media/cdrom
579 losetup -d /dev/loop0
580 exit_or_reboot
581 ;;
582 clean)
583 check_root
584 clean_usb
585 ;;
586 usage|*)
587 # Display usage by default.
588 usage
589 ;;
590 esac
592 exit 0