tazusb view tazusb @ rev 47

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