tazusb view tazusb @ rev 67

Improve Makefile. Change VERSION to 2.3
author Eric Joseph-Alexandre <erjo@slitaz.org>
date Sun Mar 21 22:40:11 2010 +0100 (2010-03-21)
parents ef6e1c9adb1f
children 671df117fb6f
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.3
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 formats are 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 # Usage: make_fs ext2|fat32|ext3 (default)
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 "\nProcessing..."
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 "\nProcessing..."
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 is 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 directory 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/*.kbd $TARGET_ROOT/boot/$ST
325 cp /media/cdrom/boot/isolinux/*.c32 $TARGET_ROOT/boot/$ST
327 # Modifing all cfg files.
328 for cfg in $TARGET_ROOT/boot/$ST/*.cfg
329 do
330 sed -i s/isolinux.msg/$ST.msg/ $cfg
331 done
333 # Modifing include file if exists.
334 if [ -f $TARGET_ROOT/boot/$ST/common.inc ]; then
335 sed -i -e "s/isolinux.msg/$ST.msg/" $TARGET_ROOT/boot/$ST/common.inc
336 fi
338 }
340 # Let user exit or reboot.
341 exit_or_reboot()
342 {
343 echo "==============================================================================="
344 echo ""
345 echo -n "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
346 read anser
347 if [ "$anser" == "reboot" ]; then
348 umount $TARGET_ROOT
349 umount /media/cdrom
350 reboot || reboot -f
351 else
352 umount $TARGET_ROOT
353 umount /media/cdrom
354 echo ""
355 exit 0
356 fi
357 }
359 set_bootable()
360 {
361 # As the boot flag is toggable, need to check it before hand
362 DISK=${DEVICE%[1-99]}
363 ISSET=`fdisk -l $DISK | grep $DEVICE | grep "\*"`
365 # If not set, set bootable
366 if [ "$ISSET" == "" ]; then
367 umount $TARGET_ROOT 2>/dev/null
368 echo -n "Setting $DEVICE as bootable..."
369 fdisk $DISK >/dev/null << EOF
370 a
371 1
372 w
373 EOF
374 status
375 fi
376 }
378 # Generate a virtual swap file in /home/swap. SliTaz boot scripts
379 # will activate it, useful for low memory systems
380 gen_swap_file()
381 {
382 echo ""
383 echo -en "\033[1mGen swap\033[0m
384 ===============================================================================
385 Generate a swap file in /home/swap that will be activated on each boot to have
386 more memory available (Empty value to exit).
388 Swap file in Mb : "
389 read size
390 if [ -z "$size" ]; then
391 echo -e "\nEmpty value. Exiting...\n"
392 exit 0
393 fi
394 cd /home
395 # Sanity check
396 if [ -f swap ]; then
397 swapoff swap 2>/dev/null
398 fi
399 # DD to gen a virtual disk.
400 dd if=/dev/zero of=swap bs=1M count=$size
401 # Make swap filesystem.
402 mkswap swap
403 swapon swap
404 echo ""
405 }
407 # Clean out old backups to save disk space
408 clean_usb()
409 {
410 echo ""
411 echo -en "\033[1mClean\033[0m
412 ===============================================================================
413 Remove old rootfs.gz.unixtimestamp backup filesystems to free up disk space.\n\n"
414 # Locate and interactively remove old filesystems from /home directory
415 for file in `find /home/boot/rootfs.gz.[0-9]*`
416 do
417 echo -n "Do you wish to remove: `basename $file` (Yes/no/exit) ? "
418 read answer
419 case $answer in
420 e|E|"exit"|Exit)
421 exit 0 ;;
422 y|Y|yes|Yes)
423 rm -f $file ;;
424 *)
425 echo -en "No filesystems selected, exiting...\n" ;;
426 esac
427 done
428 }
430 #
431 # Tazusb sequence
432 #
434 case $COMMAND in
435 writefs)
436 # Writefs to rootfs.gz
437 check_root
438 if [ -z $2 ]; then
439 COMPRESSION=none
440 else
441 COMPRESSION=$2
442 fi
443 # Start info
444 echo ""
445 echo -e "\033[1mWrite filesystem\033[0m
446 ===============================================================================
447 The command writefs will write all the current filesystem into a suitable cpio
448 archive (rootfs.gz) usable on a bootable LiveUSB media.
450 Archive compression: $COMPRESSION"
451 echo ""
453 # Clear out tazpkg cache
454 rm /var/cache/tazpkg/* -r -f
456 # Optionally remove sound card selection
457 echo -n "Do you wish to remove the sound card selection (No/yes/exit) ? "
458 read anser
459 case $anser in
460 e|E|"exit"|Exit)
461 exit 0 ;;
462 y|Y|yes|Yes)
463 echo -n "Removing current sound card selection..."
464 rm -f /var/lib/sound-card-driver
465 rm -f /etc/asound.state ;;
466 *)
467 echo -n "Keeping current sound card selection..." ;;
468 esac
469 status
470 # Optionally remove screen resolution
471 echo -n "Do you wish to remove the screen resolution (No/yes/exit) ? "
472 read anser
473 case $anser in
474 e|E|"exit"|Exit)
475 exit 0 ;;
476 y|Y|yes|Yes)
477 echo -n "Removing current screen resolution..."
478 rm -f /etc/X11/screen.conf ;;
479 *)
480 echo -n "Keeping current screen resolution..." ;;
481 esac
482 status
484 # Create list of files
485 find /bin /etc /init /sbin /var /dev /lib /root /usr >/tmp/list
487 for dir in /home /proc /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
488 do
489 echo $dir >>/tmp/list
490 done
492 # Generate initramfs with specified compression
493 if [ "$COMPRESSION" = "lzma" ]; then
494 echo -n "Creating rootfs.gz with lzma compression... "
495 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
497 elif [ "$COMPRESSION" = "gzip" ]; then
498 echo -n "Creating rootfs.gz with gzip compression... "
499 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
501 else
502 echo -n "Creating rootfs.gz without compression... "
503 cat /tmp/list | cpio -o -H newc > /rootfs.gz
504 fi
506 # Get initramfs size
507 size=`du -sh /rootfs.gz | cut -f 1`
509 # If the bootable medium is where it should be, copy across
510 if (test -e /home/boot/bzImage); then
511 echo "Moving rootfs.gz to media. Remember to unmount for delayed writes!"
513 # Move the old filesystem with the unix timestamp for reference
514 if (test -e /home/boot/previous.gz); then
515 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
516 fi
518 mv /home/boot/rootfs.gz /home/boot/previous.gz
519 mv /rootfs.gz /home/boot/.
520 else
521 echo "rootfs.gz is located in /"
522 fi
524 echo "==============================================================================="
525 echo "Root filesystem size: $size"
526 echo ""
527 echo -en "----\nENTER to continue..."; read i
528 ;;
529 format)
530 # Format a partition.
531 check_root
532 echo ""
533 echo -e "\033[1mFormat a device\033[0m"
534 echo "==============================================================================="
535 DEVICE=$2
536 label=$3
537 fs_type=$4
538 if [ -z $DEVICE ]; then
539 ask_for_device
540 check_for_device
541 else
542 echo "Device : $DEVICE"
543 fi
544 test -z $fs_type && get_fs_type
545 get_label
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 umount /dev/cdrom
606 losetup -d /dev/loop0
607 exit_or_reboot
608 ;;
609 clean)
610 check_root
611 clean_usb
612 ;;
613 usage|*)
614 # Display usage by default.
615 usage
616 ;;
617 esac
619 exit 0