tazusb view tazusb @ rev 90

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