tazusb view tazusb @ rev 36

Add a tiny GUI to gen LiveUSB (tazusbbox)
author Christophe Lincoln <pankso@slitaz.org>
date Fri Feb 20 01:38:24 2009 +0100 (2009-02-20)
parents c972d94bee82
children 0f20a4a82c78
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=1.2.2
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
18 #
19 # Tazusb functions
20 #
22 # Print the usage.
23 usage ()
24 {
25 echo -e "\nSliTaz Live USB - Version: $VERSION\n
26 \033[1mUsage: \033[0m `basename $0` [command] [compression|device]
27 \033[1mCommands: \033[0m\n
28 usage Print this short usage.
29 writefs Write the current filesystem to rootfs.gz.
30 tazSupported compression: lzma. gzip, none.
31 format Format and label device with ext3 filesystem
32 (for LiveUSB or /home).
33 gen-liveusb Generate a bootable LiveUSB using files from the LiveCD.
34 gen-swap Create or recreate and activate additional swap memory.
35 gen-iso2usb Generate a bootable LiveUSB using files from ISO file.
36 clean Remove old backup filesystems to free disk space.\n"
37 }
39 # Status function.
40 status()
41 {
42 local CHECK=$?
43 echo -en "\\033[70G[ "
44 if [ $CHECK = 0 ]; then
45 echo -en "\\033[1;33mOK"
46 else
47 echo -en "\\033[1;31mFailed"
48 fi
49 echo -e "\\033[0;39m ]"
50 }
52 # Exit if user is not root.
53 check_root()
54 {
55 if test $(id -u) != 0 ; then
56 echo -e "\nThis program requires being run as root.\n"
57 exit 0
58 fi
59 }
61 # Display a list of available partition.
62 fdisk_list()
63 {
64 echo "----"
65 fdisk -l | grep ^/dev/sd*
66 echo "----"
67 }
69 # We need a USB media to install.
70 ask_for_device()
71 {
72 echo -n "\
73 Please specify the target USB device to $COMMAND. You can type 'list' to
74 get a list of devices, type 'exit' or give an empty value to exit.
76 Device to use : "; read anser
77 while [ "$anser" == "list" ]; do
78 fdisk_list
79 echo -n "Device to use : "; read anser
80 done
81 if [ "$anser" = "" -o "$anser" = "exit" ]; then
82 echo -e "\nNo specified device or exit.\n"
83 exit 0
84 else
85 DEVICE=$anser
86 fi
87 }
89 # Verify a device exists before format or install
90 check_for_device()
91 {
92 IFDEV=`fdisk -l | grep $DEVICE`
93 if [ -z "$IFDEV" ]; then
94 echo -e "\nUnable to find device: $DEVICE\n"
95 exit 0
96 fi
97 }
99 #gets the UUID and filesystem type
100 get_part_info()
101 {
102 UUID=`blkid -s UUID -o value $DEVICE`
103 FSTYPE=`blkid -s TYPE -o value $DEVICE`
104 }
106 # Format target device and label partition.
107 mkfs_ext3()
108 {
109 echo -n "Please specify a label for the partition (TazUSB): "
110 read label
112 if [ -z $label ]; then
113 label=TazUSB
114 fi
116 echo "Label : $label"
117 echo "Mkfs : mkfs.ext3 -L \"$label\" $DEVICE"
118 echo "" && sleep 2
119 mkfs.ext3 -L "$label" $DEVICE
121 }
123 # Mount an existing USB device.
124 unmount_target_usb()
125 {
126 # If mount point is in use, unmount
127 if mount | grep $TARGET_ROOT; then
128 umount $TARGET_ROOT
129 fi
131 # Device could be mounted elsewhere, so unmount
132 if mount | grep $DEVICE; then
133 echo "Unmounting USB target device..."
134 umount $DEVICE
135 fi
136 }
138 # Mount an existing USB device.
139 mount_target_usb()
140 {
141 echo "Mounting USB target device..."
142 mkdir -p $TARGET_ROOT
143 mount $DEVICE $TARGET_ROOT 2>/dev/null
144 }
146 # Mount SliTaz LiveCD to get needed files.
147 mount_cdrom()
148 {
149 echo "Mounting cdrom device..."
151 if mount | grep /media/cdrom; then
152 umount /media/cdrom
153 fi
155 mkdir -p /media/cdrom
156 mount -r -t iso9660 $CDROM /media/cdrom 2>/dev/null
158 if [ ! -f /media/cdrom/boot/rootfs.gz ]; then
159 echo -e "\nUnable to mount cdrom or to find a filesystem on it (rootfs.gz).\n"
160 exit 0
161 fi
162 }
164 # Mount SliTaz ISO to get needed files.
165 mount_iso()
166 {
167 if mount | grep /media/cdrom ; then
168 umount /media/cdrom
169 fi
171 test -d /media/cdrom || mkdir -p /media/cdrom
173 # Add support to other distros.
174 if [ ! -f /etc/slitaz-version ]; then
175 OPTIONS="-o loop"
176 else
177 OPTIONS=""
178 fi
180 echo "Mounting `basename $ISO`..."
181 mount $OPTIONS $ISO /media/cdrom 2>/dev/null
183 if [ ! -f /media/cdrom/boot/rootfs.gz ]; then
184 echo -e "\nUnable to mount iso or to find a filesystem on it (rootfs.gz).\n"
185 exit 0
186 fi
187 }
190 # All needed files are in the boot direcory of the cdrom.
191 copy_cdrom_files()
192 {
193 echo -n "Copying needed files from cdrom..."
194 mkdir -p $TARGET_ROOT/boot
195 cp /media/cdrom/boot/bzImage $TARGET_ROOT/boot
196 cp /media/cdrom/boot/rootfs.gz $TARGET_ROOT/boot
197 status
198 }
200 install_mbr()
201 {
202 # MBR
203 DISK=${DEVICE%[1-99]}
204 if [ -f /usr/share/boot/mbr.bin ]; then
205 echo -n "Installing a new MBR to: $DISK"
206 cat /usr/share/boot/mbr.bin > $DISK
207 status
208 else
209 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
210 echo "No new MBR installed to: $DISK"
211 fi
212 }
214 # ext/syslinux install
215 install_boot()
216 {
217 # Decide if we're installing syslinux or extlinux
218 if [ "$FSTYPE" = "vfat" ]; then
219 ST=syslinux
220 STC="syslinux -d /boot/syslinux/ $DEVICE"
221 STE=cfg
222 else
223 ST=extlinux
224 STC="extlinux --install $TARGET_ROOT/boot/$ST"
225 STE=conf
226 fi
228 echo "Installing bootloader: $ST"
229 mkdir -p $TARGET_ROOT/boot/$ST
230 $STC
232 # Use existing isolinux.cfg for extlinux.conf or syslinux.cfg
233 cp /media/cdrom/boot/isolinux/isolinux.cfg $TARGET_ROOT/boot/$ST/$ST.$STE
234 sed -i -e "s/isolinux.msg/$ST.msg/" $TARGET_ROOT/boot/$ST/$ST.$STE
236 # Add home= to append in extlinux or syslinux.cfg
237 sed -i -e "s/\(append.*\)/\1 home=$UUID/" $TARGET_ROOT/boot/$ST/$ST.$STE
239 # Splash screen and help files.
240 cp /media/cdrom/boot/isolinux/isolinux.msg $TARGET_ROOT/boot/$ST/$ST.msg
241 sed -i s/'SliTaz GNU\/Linux'/'SliTaz GNU\/Linux LiveUSB'/ $TARGET_ROOT/boot/$ST/$ST.msg
242 cp /media/cdrom/boot/isolinux/splash.lss $TARGET_ROOT/boot/$ST
243 cp /media/cdrom/boot/isolinux/*.txt $TARGET_ROOT/boot/$ST
244 cp /media/cdrom/boot/isolinux/*.cfg $TARGET_ROOT/boot/$ST
245 cp /media/cdrom/boot/isolinux/*.inc $TARGET_ROOT/boot/$ST
246 cp /media/cdrom/boot/isolinux/*.kbd $TARGET_ROOT/boot/$ST
247 cp /media/cdrom/boot/isolinux/*.c32 $TARGET_ROOT/boot/$ST
249 # Modifing all cfg files.
250 for cfg in $TARGET_ROOT/boot/$ST/*.cfg
251 do
252 sed -i s/isolinux.msg/$ST.msg/ $cfg
253 done
255 # Modifing include file if exists.
256 if [ -f $TARGET_ROOT/boot/$ST/common.inc ]; then
257 sed -i -e "s/isolinux.msg/$ST.msg/" $TARGET_ROOT/boot/$ST/common.inc
258 fi
260 }
262 # Let user exit or reboot.
263 exit_or_reboot()
264 {
265 echo "==============================================================================="
266 echo ""
267 echo -n "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
268 read anser
269 if [ "$anser" == "reboot" ]; then
270 umount $TARGET_ROOT
271 umount /media/cdrom
272 reboot || reboot -f
273 else
274 umount /media/cdrom
275 echo ""
276 exit 0
277 fi
278 }
280 set_bootable()
281 {
282 # As the boot flag is toggable, need to check it before hand
283 DISK=${DEVICE%[1-99]}
284 ISSET=`fdisk -l $DISK | grep $DEVICE | grep "\*"`
286 # If not set, set bootable
287 if [ "$ISSET" == "" ]; then
288 umount $TARGET_ROOT 2>/dev/null
289 echo -n "Setting $DEVICE as bootable..."
290 fdisk $DISK >/dev/null << EOF
291 a
292 1
293 w
294 EOF
295 status
296 fi
297 }
299 # Generate a virtual swap file in /home/swap. SliTaz boot script
300 # will active it, useful for low memory system
301 gen_swap_file()
302 {
303 echo ""
304 echo -en "\033[1mGen swap\033[0m
305 ===============================================================================
306 Generate a swap file in /home/swap that will be activated on each boot to have
307 more memory available (Empty value to exit).
309 Swap file in Mb : "
310 read size
311 if [ -z "$size" ]; then
312 echo -e "\nEmpty value. Exiting...\n"
313 exit 0
314 fi
315 cd /home
316 # Sanity check
317 if [ -f swap ]; then
318 swapoff swap 2>/dev/null
319 fi
320 # DD to gen a virtual disk.
321 dd if=/dev/zero of=swap bs=1M count=$size
322 # Make swap filesystem.
323 mkswap swap
324 swapon swap
325 echo ""
326 }
328 # Clean out old backups to save disk space
329 clean_usb()
330 {
331 echo ""
332 echo -en "\033[1mClean\033[0m
333 ===============================================================================
334 Remove old rootfs.gz.unixtimestamp backup filesystems to free up disk space.\n\n"
335 # Locate and interactively remove old filesystems from /home directory
336 for file in `find /home/boot/rootfs.gz.[0-9]*`
337 do
338 echo -n "Do you wish to remove: `basename $file` (Yes/no/exit) ? "
339 read answer
340 case $answer in
341 e|E|"exit"|Exit)
342 exit 0 ;;
343 y|Y|yes|Yes)
344 rm -f $file ;;
345 *)
346 echo -en "No filesystems selected, exiting...\n" ;;
347 esac
348 done
349 }
351 #
352 # Tazusb sequence
353 #
355 case $COMMAND in
356 writefs)
357 # Writefs to rootfs.gz
358 check_root
359 if [ -z $2 ]; then
360 COMPRESSION=none
361 else
362 COMPRESSION=$2
363 fi
364 # Start info
365 echo ""
366 echo -e "\033[1mWrite filesystem\033[0m
367 ===============================================================================
368 The command writefs will write all the current filesystem into a suitable cpio
369 archive (rootfs.gz) usable on a bootable LiveUSB media.
371 Archive compression: $COMPRESSION"
372 echo ""
374 # Clear out tazpkg cache
375 rm /var/cache/tazpkg/* -r -f
377 # Optionally remove sound card selection
378 echo -n "Do you wish to remove the sound card selection (No/yes/exit) ? "
379 read anser
380 case $anser in
381 e|E|"exit"|Exit)
382 exit 0 ;;
383 y|Y|yes|Yes)
384 echo -n "Removing current sound card selection..."
385 rm -f /var/lib/sound-card-driver
386 rm -f /etc/asound.state ;;
387 *)
388 echo -n "Keeping current sound card selection..." ;;
389 esac
390 status
391 # Optionally remove screen resolution
392 echo -n "Do you wish to remove the screen resolution (No/yes/exit) ? "
393 read anser
394 case $anser in
395 e|E|"exit"|Exit)
396 exit 0 ;;
397 y|Y|yes|Yes)
398 echo -n "Removing current screen resolution..."
399 rm -f /etc/X11/screen.conf ;;
400 *)
401 echo -n "Keeping current screen resolution..." ;;
402 esac
403 status
405 # Create list of files
406 find /bin /etc /init /sbin /var /dev /lib /root /usr >/tmp/list
408 for dir in /home /proc /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
409 do
410 echo $dir >>/tmp/list
411 done
413 # Generate initramfs with specified compression
414 if [ "$COMPRESSION" = "lzma" ]; then
415 echo -n "Creating rootfs.gz with lzma compression... "
416 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
418 elif [ "$COMPRESSION" = "gzip" ]; then
419 echo -n "Creating rootfs.gz with gzip compression... "
420 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
422 else
423 echo -n "Creating rootfs.gz without compression... "
424 cat /tmp/list | cpio -o -H newc > /rootfs.gz
425 fi
427 # Get initramfs size
428 size=`du -sh /rootfs.gz | cut -f 1`
430 # If the bootable medium is where it should be, copy across
431 if (test -e /home/boot/bzImage); then
432 echo "Moving rootfs.gz to media. Remember to unmount for delayed writes!"
434 # Move the old filesystem with the unix timestamp for reference
435 if (test -e /home/boot/previous.gz); then
436 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
437 fi
439 mv /home/boot/rootfs.gz /home/boot/previous.gz
440 mv /rootfs.gz /home/boot/.
441 else
442 echo "rootfs.gz is located in /"
443 fi
445 echo "==============================================================================="
446 echo "Root filesystem size: $size"
447 echo ""
448 echo -en "----\nENTER to continue..."; read i
449 ;;
450 format)
451 # Format a partitions in ext3.
452 check_root
453 echo ""
454 echo -e "\033[1mFormat a device\033[0m"
455 echo "==============================================================================="
456 DEVICE=$2
457 if [ -z $DEVICE ]; then
458 ask_for_device
459 check_for_device
460 else
461 echo "Device : $DEVICE"
462 fi
463 mkfs_ext3
464 echo "==============================================================================="
465 echo "Device $label ($DEVICE) is ready to use as LiveUSB and/or /home partition."
466 echo ""
467 ;;
468 gen-liveusb)
469 # Generate a LiveUSB media using files from a LiveCD.
470 check_root
471 echo ""
472 echo -e "\033[1mGen a LiveUSB media\033[0m"
473 echo "==============================================================================="
474 DEVICE=$2
475 if [ -z $DEVICE ]; then
476 ask_for_device
477 fi
479 check_for_device
480 mount_cdrom
481 get_part_info
482 unmount_target_usb
483 install_mbr
484 set_bootable
485 mount_target_usb
486 copy_cdrom_files
487 install_boot
488 exit_or_reboot
489 ;;
490 gen-swap)
491 check_root
492 gen_swap_file
493 ;;
494 gen-iso2usb)
495 check_root
497 # Check if file exists
498 ISO=$2
499 if [ -z $ISO ] || [ ! -f $ISO ]; then
500 echo -e "\nPlease specify a valid filename on the command line.\n"
501 exit 1
502 fi
503 echo ""
504 echo -e "\033[1mCopy ISO file to SliTaz LiveUSB media\033[0m"
505 echo "==============================================================================="
506 echo ""
507 DEVICE=$3
508 if [ -z $DEVICE ]; then
509 ask_for_device
510 fi
511 check_for_device
512 mount_iso
513 get_part_info
514 unmount_target_usb
515 install_mbr
516 set_bootable
517 mount_target_usb
518 copy_cdrom_files
519 install_boot
520 exit_or_reboot
521 ;;
522 clean)
523 check_root
524 clean_usb
525 ;;
526 usage|*)
527 # Display usage by default.
528 usage
529 ;;
530 esac
532 exit 0