tazusb view tazusb @ rev 185

writefs: use tazlito-writeiso code to create list of files
author Xander Ziiryanoff <psychomaniak@xakep.ru>
date Fri Feb 19 16:07:14 2016 +0200 (2016-02-19)
parents bce29264a97b
children 22d3ffdc6ab6
line source
1 #!/bin/sh
2 #
3 # Tazusb - SliTaz LiveUSB utility to generate, configure and manipulate
4 # SliTaz LiveUSB bootable media and/or USB /home partition, such as
5 # flash keys, SD card or USB harddisk.
6 #
7 # Copyright (C) 2014 SliTaz GNU/Linux - GNU gpl v2
8 #
9 # Authors: see AUTHORS file
10 #
12 VERSION=182
14 . /lib/libtaz.sh
16 # i18n
17 export TEXTDOMAIN='tazusb'
19 COMMAND="$1"
20 TARGET_ROOT='/media/flash'
21 DRIVE_NAME="$(grep "drive name" < /proc/sys/dev/cdrom/info | cut -f3)"
22 CDROM="/dev/$DRIVE_NAME"
23 LOG="/tmp/$(basename $0).log"
26 #
27 # Tazusb functions
28 #
31 # Print the usage.
33 usage () {
34 _ 'SliTaz Live USB - Version: %s' "$VERSION"
35 newline
36 boldify "$(_n 'Usage:')"
37 echo -n ' '; _ '%s [command] [compression|device]' "$(basename $0)"
38 boldify "$(_n 'Commands:')"
39 optlist "\
40 usage $(_ 'Print this short usage.')
41 writefs $(_ 'Write the current filesystem to rootfs.gz. Supported compression: lzma, gzip, none.')
42 format $(_ 'Format and label device with ext3, ext2 or fat32 filesystem (for LiveUSB or /home). Default is ext3.')
43 gen-liveusb $(_ 'Generate a bootable LiveUSB using files from the LiveCD.')
44 gen-swap $(_ 'Create or recreate and activate additional swap memory.')
45 gen-iso2usb $(_ 'Generate a bootable LiveUSB using files from ISO file.')
46 clean $(_ 'Remove old backup filesystems to free disk space.')
47 "
48 newline
49 }
52 # Display a list of available partitions.
54 fdisk_list() {
55 separator '-'
56 fdisk -l | grep ^/dev/sd*
57 separator '-'
58 }
61 # We need a USB media to install.
63 ask_for_device() {
64 longline "$(_ "Please specify the target USB device to %s. You can type \
65 'list' to get a list of devices, type 'exit' or give an empty value to exit." \
66 "$COMMAND")"
67 newline
68 _n 'Device to use: '; read answer
70 while [ "$answer" == 'list' ]; do
71 fdisk_list
72 _n 'Device to use: '; read answer
73 done
75 if [ -z "$answer" -o "$answer" == 'exit' ]; then
76 newline
77 _ 'No specified device or exit.'
78 exit 0
79 else
80 DEVICE="$answer"
81 fi
82 }
85 # Verify a device exists before format or install
87 check_for_device() {
88 IFDEV="$(fdisk -l | grep $DEVICE)"
89 if [ -z "$IFDEV" ]; then
90 newline
91 _ 'Unable to find device %s' "$DEVICE"
92 exit 0
93 fi
94 }
97 # gets the UUID and filesystem type
99 get_part_info() {
100 UUID="$(blkid -s UUID -o value $DEVICE)"
101 FSTYPE="$(blkid -s TYPE -o value $DEVICE)"
102 }
105 # Get label for device
107 get_label() {
108 _n 'Please specify a label for the partition (TazUSB): '
109 read label
110 [ -z "$label" ] && label='TazUSB'
111 }
114 # Format target device and label partition.
116 mkfs_ext3() {
117 get_label
119 _ 'Label: %s' "$label"
120 echo "Mkfs: mkfs.ext3 -L \"$label\" $DEVICE"
121 newline; sleep 2
122 mkfs.ext3 -L "$label" $DEVICE
123 }
126 # Get fs type. Supported fs are ext3, ext2, fat32
128 get_fs_type() {
129 _n 'Please specify a filesystem type ext2, ext3 or fat32 (ext3): '
130 read fs_type
131 [ -z "$fs_type" ] && fs_type='ext3'
132 }
135 # We can chose the filesystem type.
137 ask_for_fs_type() {
138 _ 'Please specify the filesystem type to %s.' "$COMMAND"
139 _ 'Available formats are ext3(default), ext2 or fat32.'
140 _ 'Press enter to keep the default value.'
141 newline
142 _n 'File system type: '; read answer
143 if [ -z "$answer" ]; then
144 FS_TYPE='ext3'
145 else
146 FS_TYPE="$answer"
147 fi
148 }
151 # Format target device in ext3, ext2 or fat32.
152 # Usage: make_fs ext2|fat32|ext3 (default)
154 make_fs() {
155 local answer
157 FS=$(echo $fs_type | tr '[A-Z]' '[a-z]')
158 case "$FS" in
159 ext3|ext2)
160 newline; _ 'Processing...'
161 _ 'Label: %s' "$label"
162 echo "Mkfs: mkfs.$FS -L \"$label\" $DEVICE"
163 newline; sleep 2
164 mkfs.$FS -L "$label" $DEVICE > $LOG 2>&1
165 ;;
166 fat32)
167 if [ -x '/sbin/mkdosfs' ];then
168 newline; _ 'Processing...'
169 _ 'Label: %s' "$label"
170 echo "Mkfs: mkdosfs -F 32 -n \"$label\" $DEVICE"
171 newline; sleep 2
172 mkdosfs -F 32 -n "$label" $DEVICE
173 else
174 _ "Can't find %s tool." 'mkdosfs'
175 _n 'Would you like to install %s from repository [y/N]? ' 'dosfstools'
176 read answer
177 case "$answer" in
178 y|Y)
179 yes | tazpkg get-install dosfstools && make_fs fat32;;
180 *)
181 exit 1 ;;
182 esac
183 fi
184 ;;
185 *)
186 _ 'Sorry. Filesystem %s is not supported.' "$FS"
187 exit
188 ;;
189 esac
190 }
193 # Mount an existing USB device.
195 unmount_target_usb() {
196 # If mount point is in use, unmount
197 if mount | grep -q $TARGET_ROOT; then
198 umount $TARGET_ROOT
199 fi
201 # Device could be mounted elsewhere, so unmount
202 if mount | grep -q $DEVICE; then
203 _ 'Unmounting USB target device...'
204 umount $DEVICE
205 fi
206 }
209 # Mount an existing USB device.
211 mount_target_usb() {
212 _ 'Mounting USB target device...'
213 mkdir -p $TARGET_ROOT
214 mount $DEVICE $TARGET_ROOT 2>/dev/null
215 }
218 # Mount SliTaz LiveCD to get needed files.
220 mount_cdrom() {
221 _ 'Mounting CD-ROM device...'
223 if mount | grep /media/cdrom; then
224 umount /media/cdrom
225 fi
227 mkdir -p /media/cdrom
228 mount -r -t iso9660 $CDROM /media/cdrom 2>/dev/null
230 if [ ! -f /media/cdrom/boot/rootfs.gz -a \
231 ! -f /media/cdrom/boot/rootfs1.gz ]; then
232 newline
233 longline "$(_ 'Unable to mount CD-ROM or to find a filesystem on it (%s).' 'rootfs.gz')"
234 exit 0
235 fi
236 }
239 # Mount SliTaz ISO to get needed files.
241 mount_iso() {
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 _ 'Mounting %s...' "$(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 longline "$(_ 'Unable to mount ISO or to find a filesystem on it (%s).' 'rootfs.gz')"
261 exit 0
262 fi
263 }
266 # All needed files are in the boot directory of the CD-ROM.
268 copy_cdrom_files() {
269 _n 'Copying needed files from CD-ROM...'
270 mkdir -p $TARGET_ROOT/boot
271 cp /media/cdrom/boot/bzImage* $TARGET_ROOT/boot
272 cp /media/cdrom/boot/rootfs*.gz* $TARGET_ROOT/boot
273 cp /media/cdrom/boot/memtest $TARGET_ROOT/boot 2>/dev/null
274 cp /media/cdrom/boot/*pxe $TARGET_ROOT/boot 2>/dev/null
275 status
276 }
279 install_mbr() {
280 # MBR
281 DISK=${DEVICE%[1-99]}
282 if [ -f /usr/share/boot/mbr.bin ]; then
283 _n 'Installing a new MBR to %s' "$DISK"
284 cat /usr/share/boot/mbr.bin > $DISK
285 status
286 else
287 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
288 _ 'No new MBR installed to %s' "$DISK"
289 fi
290 }
293 # ext/syslinux install
295 install_boot() {
296 # Decide if we're installing syslinux or extlinux
297 if [ "$FSTYPE" == 'vfat' ]; then
298 ST='syslinux'
299 STC="syslinux --install -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 _ 'Installing bootloader: %s' "$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
314 # Update DVD autoinstall
315 sed -i "s/LABEL=packages-[^,]*/UUID=$UUID/g" $(grep -l append $TARGET_ROOT/boot/$ST/*)
317 # Add home=$UUID to kernel line in extlinux or syslinux.cfg
318 sed -i -e "s/\(root=.*\)/\1 home=$UUID/" $(grep -l append $TARGET_ROOT/boot/$ST/*)
320 # Splash screen and help files.
321 cp /media/cdrom/boot/isolinux/splash.* $TARGET_ROOT/boot/$ST
322 cp /media/cdrom/boot/isolinux/*.cfg $TARGET_ROOT/boot/$ST
323 cp /media/cdrom/boot/isolinux/*.kbd $TARGET_ROOT/boot/$ST
324 cp /media/cdrom/boot/isolinux/*.c32 $TARGET_ROOT/boot/$ST
325 sed -i -e s/'SliTaz GNU\/Linux'/'SliTaz GNU\/Linux LiveUSB'/ \
326 -e "s/isolinux/$ST/" $TARGET_ROOT/boot/$ST/$ST.$STE
327 }
330 # Let user exit or reboot.
332 exit_or_reboot() {
333 separator
334 newline
335 longline "$(_n 'Do you want to exit Tazusb or reboot system (Exit/reboot)? ')"
336 read answer
337 if [ "$answer" == 'reboot' ]; then
338 unmount_target_usb
339 reboot || reboot -f
340 else
341 unmount_target_usb
342 newline; exit 0
343 fi
344 }
347 set_bootable() {
348 # As the boot flag is toggable, need to check it before hand
349 DISK=${DEVICE%[1-99]}
350 ISSET="$(fdisk -l $DISK | grep $DEVICE | grep "\*")"
352 # If not set, set bootable
353 if [ -z "$ISSET" ]; then
354 umount $TARGET_ROOT 2>/dev/null
355 _n 'Setting %s as bootable...' "$DEVICE"
356 fdisk $DISK >/dev/null << EOF
357 a
358 1
359 w
360 EOF
361 status
362 fi
363 }
366 # Generate a virtual swap file in /home/swap. SliTaz boot scripts
367 # will activate it, useful for low memory systems
369 gen_swap_file() {
370 title "$(_n 'Gen swap')"
372 longline "$(_ "Generate a swap file in %s that will be activated on each \
373 boot to have more memory available (empty value to exit)." '/home/swap')"
374 newline
375 _n 'Swap file in MB: '
376 read size
377 if [ -z "$size" ]; then
378 _ 'Empty value. Exiting...'
379 exit 0
380 fi
381 cd /home
382 # Sanity check
383 if [ -f swap ]; then
384 swapoff swap 2>/dev/null
385 fi
386 # DD to gen a virtual disk.
387 dd if=/dev/zero of=swap bs=1M count=$size
388 # Make swap filesystem.
389 mkswap swap
390 swapon swap
391 newline
392 }
395 # Clean out old backups to save disk space
397 clean_usb() {
398 title "$(_n 'Clean')"
400 longline "$(_n 'Remove old %s backup filesystems to free up disk space.' \
401 'rootfs.gz.unixtimestamp')"
402 newline
403 # Locate and interactively remove old filesystems from /home directory
404 for file in $(find /home/boot/rootfs.gz.[0-9]*); do
405 _n 'Do you wish to remove: %s (Yes/no/exit)? ' "$(basename $file)"
406 read answer
407 case $answer in
408 e|E|"exit"|Exit)
409 exit 0 ;;
410 y|Y|yes|Yes)
411 rm -f $file ;;
412 *)
413 _ 'No filesystems selected, exiting...' ;;
414 esac
415 done
416 }
419 #
420 # Tazusb sequence
421 #
423 case $COMMAND in
424 writefs)
425 # Writefs to rootfs.gz
426 check_root
427 # Compression type (optional): lzma, gzip, none. Default is none
428 COMPRESSION="${2:-none}"
429 # Full path to rootfs.gz (optional). Default is /rootfs.gz
430 ROOTFS_PATH="${3:-/rootfs.gz}"
431 # File name
432 ROOTFS="$(basename "$ROOTFS_PATH")"
434 # Start info
435 title 'Write filesystem'
437 longline "$(_ "The command writefs will write all the current \
438 filesystem into a suitable cpio archive (%s) usable on a bootable \
439 LiveUSB media." "$ROOTFS")"
440 newline
441 _ 'Archive compression: %s' "$(colorize 36 "$COMPRESSION")"
442 newline
444 # Clear out tazpkg cache
445 rm /var/lib/tazpkg/*.bak /var/cache/tazpkg/* -r -f
447 # Optionally remove sound card selection and screen resolution.
448 _ 'Do you wish to remove the sound card and screen configs?'
449 _n 'Press ENTER to keep or answer (No|yes|exit): '
450 read answer
451 case $answer in
452 e|E|"exit"|Exit)
453 exit 0 ;;
454 y|Y|yes|Yes)
455 _n 'Removing current sound card and screen configurations...'
456 rm -f /var/lib/sound-card-driver
457 rm -f /var/lib/alsa/asound.state
458 rm -f /etc/X11/xorg.conf ;;
459 *)
460 _n 'Keeping current sound card and screen configurations...' ;;
461 esac
462 status
463 newline
465 # Optionally remove i18n settings
466 _ 'Do you wish to remove local/keymap settings?'
467 _n 'Press ENTER to keep or answer (No|yes|exit): '
468 read answer
469 case $answer in
470 e|E|"exit"|Exit)
471 exit 0 ;;
472 y|Y|yes|Yes)
473 _n 'Removing current locale/keymap settings...'
474 echo > /etc/locale.conf
475 echo > /etc/keymap.conf ;;
476 *)
477 _n 'Keeping current locale/keymap settings...'
478 grep -qs '^INCLUDE i18n.cfg' /home/boot/*linux/*linux.c* &&
479 sed -i 's/^INCLUDE i18n.cfg/# &/' /home/boot/*linux/*linux.c* ;;
480 esac
481 status
482 newline
484 # Clean-up files by default
485 mv -f /var/log/wtmp /tmp/tazusb-wtmp
486 touch /var/log/wtmp
487 echo > /etc/udev/rules.d/70-persistent-net.rules
488 echo > /etc/udev/rules.d/70-persistant-cd.rules
490 # Create list of files
491 # find / -xdev | sed '/^\/home\//d;/^\/tmp\//d' >/tmp/list
492 # for dev in console null tty tty1
493 # do
494 # echo /dev/$dev >>/tmp/list
495 # done
497 for dir in /bin /etc /init /sbin /var /dev /lib /root /usr /opt
498 do
499 [ -d $dir -o -f $dir ] && find $dir
500 done >/tmp/list
502 for dir in /home /proc /run /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
503 do
504 echo $dir >>/tmp/list
505 done
506 sed -i '/^\/var\/run\/.*pid$/d' /tmp/list
508 for removelog in auth boot messages dmesg daemon utmp slim Xorg tazpanel cups; do
509 sed -i "\/var\/log\/$removelog/d" /tmp/list
510 done
512 # Generate initramfs with specified compression
513 case "$COMPRESSION" in
514 lzma)
515 _n 'Creating %s with lzma compression... ' "$ROOTFS"
516 cpio -o -H newc | lzma e -si -so > "$ROOTFS_PATH"
517 ;;
518 gzip)
519 _n 'Creating %s with gzip compression... ' "$ROOTFS"
520 cpio -o -H newc | gzip -9 > "$ROOTFS_PATH"
521 [ -x /usr/bin/advdef ] && advdef -z4 "$ROOTFS_PATH"
522 ;;
523 none|*)
524 _n 'Creating %s without compression... ' "$ROOTFS"
525 cpio -o -H newc > "$ROOTFS_PATH"
526 ;;
527 esac < /tmp/list
529 mv -f /tmp/tazusb-wtmp /var/log/wtmp
531 # Get initramfs size
532 size=$(du -sh "$ROOTFS_PATH" | cut -f1)
534 # If the bootable medium is where it should be, copy across
535 if [ -e /home/boot/bzImage ]; then
536 longline "$(_ 'Moving %s to media. Remember to unmount for delayed writes!' "$ROOTFS")"
538 # Move the old filesystem with the unix timestamp for reference
539 if [ -e /home/boot/previous.gz ]; then
540 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
541 fi
543 mv /home/boot/rootfs.gz /home/boot/previous.gz
544 mv "$ROOTFS_PATH" /home/boot/rootfs.gz
545 _ '%s is located in %s' 'rootfs.gz' '/home/boot'
546 else
547 _ '%s is located in %s' "$ROOTFS" "$(dirname "$ROOTFS_PATH")"
548 fi
550 separator
551 _ 'Root filesystem size: %s' "$size"
552 separator '-'
553 _n 'ENTER to continue...'; read i
554 ;;
557 format)
558 # Format a partition.
559 check_root
561 title 'Format a device'
563 DEVICE="$2"
564 label="$3"
565 fs_type="$4"
566 if [ -z "$DEVICE" ]; then
567 ask_for_device
568 check_for_device
569 else
570 _ 'Device: %s' "$DEVICE"
571 fi
572 [ -z "$fs_type" ] && get_fs_type
573 get_label
574 unmount_target_usb
575 make_fs "$fs_type"
576 # mkfs_ext3
577 separator
578 longline "$(_ 'Device %s is ready to use as LiveUSB and/or /home partition.' "$label ($DEVICE)")"
579 ;;
582 gen-liveusb)
583 # Generate a LiveUSB media using files from a LiveCD.
584 check_root
586 title 'Gen a LiveUSB media'
588 DEVICE="$2"
589 if [ -z "$DEVICE" ]; then
590 ask_for_device
591 fi
593 check_for_device
594 mount_cdrom
595 get_part_info
596 unmount_target_usb
597 install_mbr
598 set_bootable
599 mount_target_usb
600 copy_cdrom_files
601 install_boot
602 exit_or_reboot
603 ;;
606 gen-swap)
607 check_root
608 gen_swap_file
609 ;;
612 gen-iso2usb|iso2usb)
613 check_root
614 # Check if file exists
615 ISO="$2"
616 if [ -z "$ISO" -o ! -f "$ISO" ]; then
617 _ 'Please specify a valid filename on the command line.'
618 exit 1
619 fi
621 title 'Copy ISO file to SliTaz LiveUSB media'
623 DEVICE="$3"
624 if [ -z "$DEVICE" ]; then
625 ask_for_device
626 fi
627 check_for_device
628 mount_iso
629 get_part_info
630 unmount_target_usb
631 install_mbr
632 set_bootable
633 mount_target_usb
634 copy_cdrom_files
635 install_boot
636 umount /media/cdrom
637 losetup -d /dev/loop0
638 exit_or_reboot
639 ;;
642 clean)
643 check_root
644 clean_usb
645 ;;
648 usage|*)
649 # Display usage by default.
650 usage
651 ;;
652 esac
654 exit 0