tazusb view tazusb @ rev 46

Added tag 2.1 for changeset 7ae406aae003
author Christophe Lincoln <pankso@slitaz.org>
date Thu Apr 16 02:01:20 2009 +0200 (2009-04-16)
parents 5d78b19d2436
children 054c0ab2988a
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
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 partitions.
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/" $(grep -l append $TARGET_ROOT/boot/$ST/*)
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 $TARGET_ROOT
275 umount /media/cdrom
276 echo ""
277 exit 0
278 fi
279 }
281 set_bootable()
282 {
283 # As the boot flag is toggable, need to check it before hand
284 DISK=${DEVICE%[1-99]}
285 ISSET=`fdisk -l $DISK | grep $DEVICE | grep "\*"`
287 # If not set, set bootable
288 if [ "$ISSET" == "" ]; then
289 umount $TARGET_ROOT 2>/dev/null
290 echo -n "Setting $DEVICE as bootable..."
291 fdisk $DISK >/dev/null << EOF
292 a
293 1
294 w
295 EOF
296 status
297 fi
298 }
300 # Generate a virtual swap file in /home/swap. SliTaz boot scripts
301 # will activate it, useful for low memory systems
302 gen_swap_file()
303 {
304 echo ""
305 echo -en "\033[1mGen swap\033[0m
306 ===============================================================================
307 Generate a swap file in /home/swap that will be activated on each boot to have
308 more memory available (Empty value to exit).
310 Swap file in Mb : "
311 read size
312 if [ -z "$size" ]; then
313 echo -e "\nEmpty value. Exiting...\n"
314 exit 0
315 fi
316 cd /home
317 # Sanity check
318 if [ -f swap ]; then
319 swapoff swap 2>/dev/null
320 fi
321 # DD to gen a virtual disk.
322 dd if=/dev/zero of=swap bs=1M count=$size
323 # Make swap filesystem.
324 mkswap swap
325 swapon swap
326 echo ""
327 }
329 # Clean out old backups to save disk space
330 clean_usb()
331 {
332 echo ""
333 echo -en "\033[1mClean\033[0m
334 ===============================================================================
335 Remove old rootfs.gz.unixtimestamp backup filesystems to free up disk space.\n\n"
336 # Locate and interactively remove old filesystems from /home directory
337 for file in `find /home/boot/rootfs.gz.[0-9]*`
338 do
339 echo -n "Do you wish to remove: `basename $file` (Yes/no/exit) ? "
340 read answer
341 case $answer in
342 e|E|"exit"|Exit)
343 exit 0 ;;
344 y|Y|yes|Yes)
345 rm -f $file ;;
346 *)
347 echo -en "No filesystems selected, exiting...\n" ;;
348 esac
349 done
350 }
352 #
353 # Tazusb sequence
354 #
356 case $COMMAND in
357 writefs)
358 # Writefs to rootfs.gz
359 check_root
360 if [ -z $2 ]; then
361 COMPRESSION=none
362 else
363 COMPRESSION=$2
364 fi
365 # Start info
366 echo ""
367 echo -e "\033[1mWrite filesystem\033[0m
368 ===============================================================================
369 The command writefs will write all the current filesystem into a suitable cpio
370 archive (rootfs.gz) usable on a bootable LiveUSB media.
372 Archive compression: $COMPRESSION"
373 echo ""
375 # Clear out tazpkg cache
376 rm /var/cache/tazpkg/* -r -f
378 # Optionally remove sound card selection
379 echo -n "Do you wish to remove the sound card selection (No/yes/exit) ? "
380 read anser
381 case $anser in
382 e|E|"exit"|Exit)
383 exit 0 ;;
384 y|Y|yes|Yes)
385 echo -n "Removing current sound card selection..."
386 rm -f /var/lib/sound-card-driver
387 rm -f /etc/asound.state ;;
388 *)
389 echo -n "Keeping current sound card selection..." ;;
390 esac
391 status
392 # Optionally remove screen resolution
393 echo -n "Do you wish to remove the screen resolution (No/yes/exit) ? "
394 read anser
395 case $anser in
396 e|E|"exit"|Exit)
397 exit 0 ;;
398 y|Y|yes|Yes)
399 echo -n "Removing current screen resolution..."
400 rm -f /etc/X11/screen.conf ;;
401 *)
402 echo -n "Keeping current screen resolution..." ;;
403 esac
404 status
406 # Create list of files
407 find /bin /etc /init /sbin /var /dev /lib /root /usr >/tmp/list
409 for dir in /home /proc /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
410 do
411 echo $dir >>/tmp/list
412 done
414 # Generate initramfs with specified compression
415 if [ "$COMPRESSION" = "lzma" ]; then
416 echo -n "Creating rootfs.gz with lzma compression... "
417 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
419 elif [ "$COMPRESSION" = "gzip" ]; then
420 echo -n "Creating rootfs.gz with gzip compression... "
421 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
423 else
424 echo -n "Creating rootfs.gz without compression... "
425 cat /tmp/list | cpio -o -H newc > /rootfs.gz
426 fi
428 # Get initramfs size
429 size=`du -sh /rootfs.gz | cut -f 1`
431 # If the bootable medium is where it should be, copy across
432 if (test -e /home/boot/bzImage); then
433 echo "Moving rootfs.gz to media. Remember to unmount for delayed writes!"
435 # Move the old filesystem with the unix timestamp for reference
436 if (test -e /home/boot/previous.gz); then
437 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
438 fi
440 mv /home/boot/rootfs.gz /home/boot/previous.gz
441 mv /rootfs.gz /home/boot/.
442 else
443 echo "rootfs.gz is located in /"
444 fi
446 echo "==============================================================================="
447 echo "Root filesystem size: $size"
448 echo ""
449 echo -en "----\nENTER to continue..."; read i
450 ;;
451 format)
452 # Format a partitions in ext3.
453 check_root
454 echo ""
455 echo -e "\033[1mFormat a device\033[0m"
456 echo "==============================================================================="
457 DEVICE=$2
458 if [ -z $DEVICE ]; then
459 ask_for_device
460 check_for_device
461 else
462 echo "Device : $DEVICE"
463 fi
464 mkfs_ext3
465 echo "==============================================================================="
466 echo "Device $label ($DEVICE) is ready to use as LiveUSB and/or /home partition."
467 echo ""
468 ;;
469 gen-liveusb)
470 # Generate a LiveUSB media using files from a LiveCD.
471 check_root
472 echo ""
473 echo -e "\033[1mGen a LiveUSB media\033[0m"
474 echo "==============================================================================="
475 DEVICE=$2
476 if [ -z $DEVICE ]; then
477 ask_for_device
478 fi
480 check_for_device
481 mount_cdrom
482 get_part_info
483 unmount_target_usb
484 install_mbr
485 set_bootable
486 mount_target_usb
487 copy_cdrom_files
488 install_boot
489 exit_or_reboot
490 ;;
491 gen-swap)
492 check_root
493 gen_swap_file
494 ;;
495 gen-iso2usb)
496 check_root
498 # Check if file exists
499 ISO=$2
500 if [ -z $ISO ] || [ ! -f $ISO ]; then
501 echo -e "\nPlease specify a valid filename on the command line.\n"
502 exit 1
503 fi
504 echo ""
505 echo -e "\033[1mCopy ISO file to SliTaz LiveUSB media\033[0m"
506 echo "==============================================================================="
507 echo ""
508 DEVICE=$3
509 if [ -z $DEVICE ]; then
510 ask_for_device
511 fi
512 check_for_device
513 mount_iso
514 get_part_info
515 unmount_target_usb
516 install_mbr
517 set_bootable
518 mount_target_usb
519 copy_cdrom_files
520 install_boot
521 exit_or_reboot
522 losetup -d /dev/loop0
523 ;;
524 clean)
525 check_root
526 clean_usb
527 ;;
528 usage|*)
529 # Display usage by default.
530 usage
531 ;;
532 esac
534 exit 0