tazusb view tazusb @ rev 27

Do not backup files in /mnt
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue May 27 15:11:19 2008 +0000 (2008-05-27)
parents cb7070e045e4
children f7fe287be212
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.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 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 echo "Mounting `basename $ISO`..."
174 mount $ISO /media/cdrom 2>/dev/null
176 if [ ! -f /media/cdrom/boot/rootfs.gz ]; then
177 echo -e "\nUnable to mount iso or to find a filesystem on it (rootfs.gz).\n"
178 exit 0
179 fi
180 }
183 # All needed files are in the boot direcory of the cdrom.
184 copy_cdrom_files()
185 {
186 echo -n "Copying needed files from cdrom..."
187 mkdir -p $TARGET_ROOT/boot
188 cp /media/cdrom/boot/bzImage $TARGET_ROOT/boot
189 cp /media/cdrom/boot/rootfs.gz $TARGET_ROOT/boot
190 status
191 }
193 install_mbr()
194 {
195 # MBR
196 DISK=${DEVICE%[1-99]}
197 if [ -f /usr/share/boot/mbr.bin ]; then
198 echo -n "Installing a new MBR to: $DISK"
199 cat /usr/share/boot/mbr.bin > $DISK
200 status
201 else
202 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
203 echo "No new MBR installed to: $DISK"
204 fi
205 }
207 # ext/syslinux install
208 install_boot()
209 {
210 #decide if we're installing syslinux or extlinux
211 if [ "$FSTYPE" = "vfat" ]; then
212 ST=syslinux
213 STC="syslinux -d /boot/syslinux/ $DEVICE"
214 STE=cfg
215 else
216 ST=extlinux
217 STC="extlinux --install $TARGET_ROOT/boot/$ST"
218 STE=conf
219 fi
221 echo "Installing bootloader: $ST"
222 mkdir -p $TARGET_ROOT/boot/$ST
223 $STC
225 # Use existing isolinux.cfg for extlinux.conf or syslinux.cfg
226 cp /media/cdrom/boot/isolinux/isolinux.cfg $TARGET_ROOT/boot/$ST/$ST.$STE
227 sed -i -e "s/isolinux.msg/$ST.msg/" $TARGET_ROOT/boot/$ST/$ST.$STE
229 # Add home= to apppend in extlinux or syslinux.cfg
230 sed -i -e "s/\(append.*\)/\1 home=$UUID/" $TARGET_ROOT/boot/$ST/$ST.$STE
232 # Splash screen and help files.
233 cp /media/cdrom/boot/isolinux/isolinux.msg $TARGET_ROOT/boot/$ST/$ST.msg
234 sed -i s/'SliTaz GNU\/Linux'/'SliTaz GNU\/Linux LiveUSB'/ $TARGET_ROOT/boot/$ST/$ST.msg
235 cp /media/cdrom/boot/isolinux/splash.lss $TARGET_ROOT/boot/$ST
236 cp /media/cdrom/boot/isolinux/*.txt $TARGET_ROOT/boot/$ST
237 cp /media/cdrom/boot/isolinux/*.cfg $TARGET_ROOT/boot/$ST
238 cp /media/cdrom/boot/isolinux/*.kbd $TARGET_ROOT/boot/$ST
239 cp /media/cdrom/boot/isolinux/*.c32 $TARGET_ROOT/boot/$ST
241 # Modifing all cfg files.
242 for cfg in $TARGET_ROOT/boot/$ST/*.cfg
243 do
244 sed -i s/isolinux.msg/$ST.msg/ $cfg
245 done
248 }
250 # Let user exit or reboot.
251 exit_or_reboot()
252 {
253 echo "==============================================================================="
254 echo ""
255 echo -n "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
256 read anser
257 if [ "$anser" == "reboot" ]; then
258 umount $TARGET_ROOT
259 umount /media/cdrom
260 reboot || reboot -f
261 else
262 umount /media/cdrom
263 echo ""
264 exit 0
265 fi
266 }
268 set_bootable()
269 {
270 # As the boot flag is toggable, need to check it before hand
271 DISK=${DEVICE%[1-99]}
272 ISSET=`fdisk -l $DISK | grep $DEVICE | grep "\*"`
274 # If not set, set bootable
275 if [ "$ISSET" == "" ]; then
276 umount $TARGET_ROOT 2>/dev/null
277 echo -n "Setting $DEVICE as bootable..."
278 fdisk $DISK >/dev/null << EOF
279 a
280 1
281 w
282 EOF
283 status
284 fi
285 }
287 # Generate a virtual swap file in /home/swap. SliTaz boot script
288 # will active it, usefull for low memory system
289 gen_swap_file()
290 {
291 echo ""
292 echo -en "\033[1mGen swap\033[0m
293 ===============================================================================
294 Generate a swap file in /home/swap that will be activated on each boot to have
295 more memory available (Empty value to exit).
297 Swap file in Mb : "
298 read size
299 if [ -z "$size" ]; then
300 echo -e "\nEmpty value. Exiting...\n"
301 exit 0
302 fi
303 cd /home
304 # Sanity check
305 if [ -f swap ]; then
306 swapoff swap 2>/dev/null
307 fi
308 # DD to gen a virtual disk.
309 dd if=/dev/zero of=swap bs=1M count=$size
310 # Make swap filesystem.
311 mkswap swap
312 swapon swap
313 echo ""
314 }
316 # Clean out old backups to save disk space
317 clean_usb()
318 {
319 echo ""
320 echo -en "\033[1mClean\033[0m
321 ===============================================================================
322 Remove old rootfs.gz.unixtimestamp backup filesystems to free up disk space.\n\n"
323 # locate and interactively remove old filesystems from /home directory
324 for file in `find /home/boot/rootfs.gz.[0-9]*`
325 do
326 echo -n "Do you wish to remove: `basename $file` (Yes/no/exit) ? "
327 read answer
328 case $answer in
329 e|E|"exit"|Exit)
330 exit 0 ;;
331 y|Y|yes|Yes)
332 rm -f $file ;;
333 *)
334 echo -en "No filesystems selected, exiting...\n" ;;
335 esac
336 done
337 }
339 #
340 # Tazusb sequence
341 #
343 case $COMMAND in
344 writefs)
345 #writefs to rootfs.gz
346 check_root
347 if [ -z $2 ]; then
348 COMPRESSION=none
349 else
350 COMPRESSION=$2
351 fi
352 #start info
353 echo ""
354 echo -e "\033[1mWrite filesystem\033[0m
355 ===============================================================================
356 The command writefs will write all the current filesystem into a suitable cpio
357 archive (rootfs.gz) usable on a bootable LiveUSB media.
359 Archive compression: $COMPRESSION"
360 echo ""
362 #clear out tazpkg cache
363 rm /var/cache/tazpkg/* -r -f
365 #optionally remove sound card selection
366 echo -n "Do you wish to remove the sound card selection (Yes/no/exit) ? "
367 read anser
368 case $anser in
369 e|E|"exit"|Exit)
370 exit 0
371 ;;
372 y|Y|yes|Yes)
373 echo -n "Removing current sound card selection..."
374 rm -f /var/lib/sound-card-driver
375 rm -f /etc/asound.state
376 ;;
377 *)
378 echo -n "Keeping current sound card selection..."
379 ;;
380 esac
381 status
383 #create list of files
384 find /bin /etc /init /sbin /var /dev /lib /root /usr >/tmp/list
386 for dir in /home /proc /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
387 do
388 echo $dir >>/tmp/list
389 done
391 #gen initramfs with specified compression
392 if [ "$COMPRESSION" = "lzma" ]; then
393 echo -n "Creating rootfs.gz with lzma compression... "
394 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
396 elif [ "$COMPRESSION" = "gzip" ]; then
397 echo -n "Creating rootfs.gz with gzip compression... "
398 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
400 else
401 echo -n "Creating rootfs.gz without compression... "
402 cat /tmp/list | cpio -o -H newc > /rootfs.gz
403 fi
405 #get initramfs size
406 size=`du -sh /rootfs.gz | cut -f 1`
408 #if the bootable medium is where it should be, copy across
410 if (test -e /home/boot/bzImage); then
411 echo "Moving rootfs.gz to media. Remember to unmount for delayed writes!"
413 #move the old filesystem with the unix timestamp for reference
414 if (test -e /home/boot/previous.gz); then
415 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
416 fi
418 mv /home/boot/rootfs.gz /home/boot/previous.gz
419 mv /rootfs.gz /home/boot/.
420 else
421 echo "rootfs.gz is located in /"
422 fi
424 echo "==============================================================================="
425 echo "Root filesystem size: $size"
426 echo ""
427 echo -en "----\nENTER to continue..."; read i
428 ;;
429 format)
430 # Format a partitions in ext3.
431 check_root
432 echo ""
433 echo -e "\033[1mFormat a device\033[0m"
434 echo "==============================================================================="
435 DEVICE=$2
436 if [ -z $DEVICE ]; then
437 ask_for_device
438 check_for_device
439 else
440 echo "Device : $DEVICE"
441 fi
442 mkfs_ext3
443 echo "==============================================================================="
444 echo "Device $label ($DEVICE) is ready to use as LiveUSB and/or /home partition."
445 echo ""
446 ;;
447 gen-liveusb)
448 # Generate a LiveUSB media using files from a LiveCD.
449 check_root
450 echo ""
451 echo -e "\033[1mGen a LiveUSB media\033[0m"
452 echo "==============================================================================="
453 DEVICE=$2
454 if [ -z $DEVICE ]; then
455 ask_for_device
456 fi
458 check_for_device
459 mount_cdrom
460 get_part_info
461 unmount_target_usb
462 install_mbr
463 set_bootable
464 mount_target_usb
465 copy_cdrom_files
466 install_boot
467 exit_or_reboot
468 ;;
469 gen-swap)
470 check_root
471 gen_swap_file
472 ;;
473 gen-iso2usb)
474 check_root
476 # Check if file exists
477 ISO=$2
478 if [ -z $ISO ] || [ ! -f $ISO ]; then
479 echo -e "\nPlease specify a valid filename on the command line.\n"
480 exit 1
481 fi
482 echo ""
483 echo -e "\033[1mCopy ISO file to SliTaz LiveUSB media\033[0m"
484 echo "==============================================================================="
485 echo ""
486 DEVICE=$3
487 if [ -z $DEVICE ]; then
488 ask_for_device
489 fi
490 check_for_device
491 mount_iso
492 get_part_info
493 unmount_target_usb
494 install_mbr
495 set_bootable
496 mount_target_usb
497 copy_cdrom_files
498 install_boot
499 exit_or_reboot
500 ;;
501 clean)
502 check_root
503 clean_usb
504 ;;
505 usage|*)
506 # Display usage by default.
507 usage
508 ;;
509 esac
511 exit 0