tazusb view tazusb @ rev 205

busybox blkid support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Aug 19 10:56:11 2020 +0000 (2020-08-19)
parents ed82aa3f1853
children ac809a573f3c
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=4.2.6
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 ext4, 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 if [ -z "$(blkid | grep "^$DEVICE:")" ]; then
89 newline
90 _ 'Unable to find device %s' "$DEVICE"
91 exit 0
92 fi
93 }
96 # gets the UUID and filesystem type
98 get_part_info() {
99 UUID="$(blkid $DEVICE | sed 's|.* UUID="||;s|".*||')"
100 FSTYPE="$(blkid $DEVICE | sed 's|.* TYPE="||;s|".*||')"
101 }
104 # Get label for device
106 get_label() {
107 _n 'Please specify a label for the partition (TazUSB): '
108 read label
109 [ -z "$label" ] && label='TazUSB'
110 }
113 # Get fs type. Supported fs are ext4, ext3, ext2, fat32
115 get_fs_type() {
116 _n 'Please specify a filesystem type ext2, ext3, ext4 or fat32 (ext3): '
117 read fs_type
118 case "$fs_type" in
119 ext2|ext4|fat32);;
120 *) fs_type='ext3'
121 esac
122 }
125 # We can chose the filesystem type.
127 ask_for_fs_type() {
128 _ 'Please specify the filesystem type to %s.' "$COMMAND"
129 _ 'Available formats are ext4, ext3(default), ext2 or fat32.'
130 _ 'Press enter to keep the default value.'
131 newline
132 _n 'File system type: '; read FS_TYPE
133 case "$FS_TYPE" in
134 ext2|ext4|fat32);;
135 *) FS_TYPE='ext3'
136 esac
137 }
140 # Format target device in ext4, ext3, ext2 or fat32.
141 # Usage: make_fs ext2|ext4|fat32|ext3 (default)
143 make_fs() {
144 local answer
146 FS=$(echo $fs_type | tr '[A-Z]' '[a-z]')
147 case "$FS" in
148 ext4|ext3|ext2)
149 newline; _ 'Processing...'
150 _ 'Label: %s' "$label"
151 echo "Mkfs: mkfs.$FS -L \"$label\" $DEVICE"
152 newline; sleep 2
153 mkfs.$FS -L "$label" $DEVICE > $LOG 2>&1
154 ;;
155 fat32)
156 if [ -x '/sbin/mkdosfs' ];then
157 newline; _ 'Processing...'
158 _ 'Label: %s' "$label"
159 echo "Mkfs: mkdosfs -F 32 -n \"$label\" $DEVICE"
160 newline; sleep 2
161 mkdosfs -F 32 -n "$label" $DEVICE
162 else
163 _ "Can't find %s tool." 'mkdosfs'
164 _n 'Would you like to install %s from repository [y/N]? ' 'dosfstools'
165 read answer
166 case "$answer" in
167 y|Y)
168 yes | tazpkg get-install dosfstools && make_fs fat32;;
169 *)
170 exit 1 ;;
171 esac
172 fi
173 ;;
174 *)
175 _ 'Sorry. Filesystem %s is not supported.' "$FS"
176 exit
177 ;;
178 esac
179 }
182 # Mount an existing USB device.
184 unmount_target_usb() {
185 # If mount point is in use, unmount
186 if mount | grep -q $TARGET_ROOT; then
187 umount $TARGET_ROOT
188 fi
190 # Device could be mounted elsewhere, so unmount
191 if mount | grep -q $DEVICE; then
192 _ 'Unmounting USB target device...'
193 umount $DEVICE
194 fi
195 }
198 # Mount an existing USB device.
200 mount_target_usb() {
201 _ 'Mounting USB target device...'
202 mkdir -p $TARGET_ROOT
203 mount $DEVICE $TARGET_ROOT 2>/dev/null
204 }
207 # Mount SliTaz LiveCD to get needed files.
209 mount_cdrom() {
210 _ 'Mounting CD-ROM device...'
212 if mount | grep /media/cdrom; then
213 umount /media/cdrom
214 fi
216 mkdir -p /media/cdrom
217 mount -r -t iso9660 $CDROM /media/cdrom 2>/dev/null
219 if [ ! -f /media/cdrom/boot/rootfs.gz -a \
220 ! -f /media/cdrom/boot/rootfs1.gz ]; then
221 newline
222 longline "$(_ 'Unable to mount CD-ROM or to find a filesystem on it (%s).' 'rootfs.gz')"
223 exit 0
224 fi
225 }
228 # Mount SliTaz ISO to get needed files.
230 mount_iso() {
231 if mount | grep /media/cdrom ; then
232 umount /media/cdrom
233 fi
235 test -d /media/cdrom || mkdir -p /media/cdrom
237 # Add support to other distros.
238 if [ ! -f /etc/slitaz-version ]; then
239 OPTIONS='-o loop'
240 else
241 OPTIONS=''
242 fi
244 _ 'Mounting %s...' "$(basename $ISO)"
245 mount $OPTIONS $ISO /media/cdrom 2>/dev/null
247 if [ ! -f /media/cdrom/boot/rootfs.gz -a \
248 ! -f /media/cdrom/boot/rootfs1.gz ]; then
249 longline "$(_ 'Unable to mount ISO or to find a filesystem on it (%s).' 'rootfs.gz')"
250 exit 0
251 fi
252 }
255 # All needed files are in the boot directory of the CD-ROM.
257 copy_cdrom_files() {
258 _n 'Copying needed files from CD-ROM...'
259 mkdir -p $TARGET_ROOT/boot
260 cp /media/cdrom/boot/bzImage* $TARGET_ROOT/boot
261 cp /media/cdrom/boot/rootfs*.gz* $TARGET_ROOT/boot
262 cp /media/cdrom/boot/memtest* $TARGET_ROOT/boot 2>/dev/null
263 cp /media/cdrom/boot/*pxe* $TARGET_ROOT/boot 2>/dev/null
264 status
265 }
268 install_mbr() {
269 # MBR
270 DISK=${DEVICE%[1-99]}
271 if [ -f /usr/share/boot/mbr.bin ]; then
272 _n 'Installing a new MBR to %s' "$DISK"
273 cat /usr/share/boot/mbr.bin > $DISK
274 status
275 else
276 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
277 _ 'No new MBR installed to %s' "$DISK"
278 fi
279 }
282 # ext/syslinux install
284 install_boot() {
285 # Decide if we're installing syslinux or extlinux
286 if [ "$FSTYPE" = 'vfat' ]; then
287 ST='syslinux'
288 STC="syslinux --install -d /boot/syslinux/ $DEVICE"
289 STE='cfg'
290 else
291 ST='extlinux'
292 STC="extlinux --install $TARGET_ROOT/boot/$ST"
293 STE='conf'
294 fi
296 _ 'Installing bootloader: %s' "$ST"
297 mkdir -p $TARGET_ROOT/boot/$ST
298 $STC
300 # Use existing isolinux.cfg for extlinux.conf or syslinux.cfg
301 cp /media/cdrom/boot/isolinux/isolinux.cfg $TARGET_ROOT/boot/$ST/$ST.$STE
303 # Update DVD autoinstall
304 sed -i "s/LABEL=packages-[^,]*/UUID=$UUID/g" $(grep -l append $TARGET_ROOT/boot/$ST/*)
306 # Add home=$UUID to kernel line in extlinux or syslinux.cfg
307 sed -i -e "s/\(root=.*\)/\1 home=$UUID/" $(grep -l append $TARGET_ROOT/boot/$ST/*)
309 # Splash screen and help files.
310 cp /media/cdrom/boot/isolinux/splash.* $TARGET_ROOT/boot/$ST
311 cp /media/cdrom/boot/isolinux/*.cfg $TARGET_ROOT/boot/$ST
312 cp /media/cdrom/boot/isolinux/*kbd $TARGET_ROOT/boot/$ST
313 cp /media/cdrom/boot/isolinux/*.c32 $TARGET_ROOT/boot/$ST
314 cp /media/cdrom/boot/isolinux/opts.* $TARGET_ROOT/boot/$ST
315 cp /media/cdrom/boot/isolinux/help.* $TARGET_ROOT/boot/$ST
316 sed -i -e s/'SliTaz GNU\/Linux'/'SliTaz GNU\/Linux LiveUSB'/ \
317 -e "s/isolinux/$ST/" $TARGET_ROOT/boot/$ST/$ST.$STE
318 }
321 # Let user exit or reboot.
323 exit_or_reboot() {
324 separator
325 newline
326 longline "$(_n 'Do you want to exit Tazusb or reboot system (Exit/reboot)? ')"
327 read answer
328 if [ "$answer" = 'reboot' ]; then
329 unmount_target_usb
330 reboot || reboot -f
331 else
332 unmount_target_usb
333 newline; exit 0
334 fi
335 }
338 set_bootable() {
339 # As the boot flag is toggable, need to check it before hand
340 DISK=${DEVICE%[1-99]}
341 ISSET="$(fdisk -l $DISK | grep $DEVICE | grep "\*")"
343 # If not set, set bootable
344 if [ -z "$ISSET" ]; then
345 umount $TARGET_ROOT 2>/dev/null
346 _n 'Setting %s as bootable...' "$DEVICE"
347 fdisk $DISK >/dev/null << EOF
348 $(fdisk -l $DISK | grep $DISK | sed "/\\*/!ds#$DISK##;s# .*##;s#.*#&\na#")
349 a
350 ${DEVICE#$DISK}
351 w
352 EOF
353 status
354 fi
355 }
358 # Generate a virtual swap file in /home/swap. SliTaz boot scripts
359 # will activate it, useful for low memory systems
361 gen_swap_file() {
362 title "$(_n 'Gen swap')"
364 longline "$(_ "Generate a swap file in %s that will be activated on each \
365 boot to have more memory available (empty value to exit)." '/home/swap')"
366 newline
367 _n 'Swap file in MB: '
368 read size
369 if [ -z "$size" ]; then
370 _ 'Empty value. Exiting...'
371 exit 0
372 fi
373 cd /home
374 # Sanity check
375 if [ -f swap ]; then
376 swapoff swap 2>/dev/null
377 fi
378 # DD to gen a virtual disk.
379 dd if=/dev/zero of=swap bs=1M count=$size
380 # Make swap filesystem.
381 mkswap swap
382 swapon swap
383 newline
384 }
387 # Clean out old backups to save disk space
389 clean_usb() {
390 title "$(_n 'Clean')"
392 longline "$(_n 'Remove old %s backup filesystems to free up disk space.' \
393 'rootfs.gz.unixtimestamp')"
394 newline
395 # Locate and interactively remove old filesystems from /home directory
396 for file in $(find /home/boot/rootfs.gz.[0-9]*); do
397 _n 'Do you wish to remove: %s (Yes/no/exit)? ' "$(basename $file)"
398 read answer
399 case $answer in
400 e|E|"exit"|Exit)
401 exit 0 ;;
402 y|Y|yes|Yes)
403 rm -f $file ;;
404 *)
405 _ 'No filesystems selected, exiting...' ;;
406 esac
407 done
408 }
411 #
412 # Tazusb sequence
413 #
415 case $COMMAND in
416 writefs)
417 # Writefs to rootfs.gz
418 check_root
419 # Compression type (optional): lzma, gzip, none. Default is none
420 COMPRESSION="${2:-none}"
421 # Full path to rootfs.gz (optional). Default is /rootfs.gz
422 ROOTFS_PATH="${3:-/rootfs.gz}"
423 # File name
424 ROOTFS="$(basename "$ROOTFS_PATH")"
426 # Start info
427 title 'Write filesystem'
429 longline "$(_ "The command writefs will write all the current \
430 filesystem into a suitable cpio archive (%s) usable on a bootable \
431 LiveUSB media." "$ROOTFS")"
432 newline
433 _ 'Archive compression: %s' "$(colorize 36 "$COMPRESSION")"
434 newline
436 # Clear out tazpkg cache
437 rm /var/lib/tazpkg/*.bak /var/cache/tazpkg/* -r -f
439 # Optionally remove sound card selection and screen resolution.
440 _ 'Do you wish to remove the sound card and screen configs?'
441 _n 'Press ENTER to keep or answer (No|yes|exit): '
442 read answer
443 case $answer in
444 e|E|"exit"|Exit)
445 exit 0 ;;
446 y|Y|yes|Yes)
447 _n 'Removing current sound card and screen configurations...'
448 rm -f /var/lib/sound-card-driver
449 rm -f /var/lib/alsa/asound.state
450 rm -f /etc/X11/xorg.conf ;;
451 *)
452 _n 'Keeping current sound card and screen configurations...' ;;
453 esac
454 status
455 newline
457 # Optionally remove i18n settings
458 _ 'Do you wish to remove local/keymap settings?'
459 _n 'Press ENTER to keep or answer (No|yes|exit): '
460 read answer
461 case $answer in
462 e|E|"exit"|Exit)
463 exit 0 ;;
464 y|Y|yes|Yes)
465 _n 'Removing current locale/keymap settings...'
466 echo > /etc/locale.conf
467 echo > /etc/keymap.conf ;;
468 *)
469 _n 'Keeping current locale/keymap settings...'
470 grep -qs '^INCLUDE i18n.cfg' /home/boot/*linux/*linux.c* &&
471 sed -i 's/^INCLUDE i18n.cfg/# &/' /home/boot/*linux/*linux.c* ;;
472 esac
473 status
474 newline
476 # Clean-up files by default
477 mv -f /var/log/wtmp /tmp/tazusb-wtmp
478 touch /var/log/wtmp
479 echo > /etc/udev/rules.d/70-persistent-net.rules
480 echo > /etc/udev/rules.d/70-persistant-cd.rules
482 # Create list of files
483 # find / -xdev | sed '/^\/home\//d;/^\/tmp\//d' >/tmp/list
484 # for dev in console null tty tty1
485 # do
486 # echo /dev/$dev >>/tmp/list
487 # done
489 for dir in /bin /etc /init /sbin /var /dev /lib /root /usr /opt
490 do
491 [ -d $dir -o -f $dir ] && find $dir
492 done >/tmp/list
494 for dir in /home /proc /run /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
495 do
496 echo $dir >>/tmp/list
497 done
498 sed -i '/^\/var\/run\/.*pid$/d' /tmp/list
500 for removelog in auth boot messages dmesg daemon utmp slim Xorg tazpanel cups; do
501 sed -i "\/var\/log\/$removelog/d" /tmp/list
502 done
504 # Generate initramfs with specified compression
505 case "$COMPRESSION" in
506 lzma)
507 _n 'Creating %s with lzma compression... ' "$ROOTFS"
508 cpio -o -H newc | lzma e -si -so > "$ROOTFS_PATH"
509 ;;
510 gzip)
511 _n 'Creating %s with gzip compression... ' "$ROOTFS"
512 cpio -o -H newc | gzip -9 > "$ROOTFS_PATH"
513 [ -x /usr/bin/advdef ] && advdef -z4 "$ROOTFS_PATH"
514 ;;
515 none|*)
516 _n 'Creating %s without compression... ' "$ROOTFS"
517 cpio -o -H newc > "$ROOTFS_PATH"
518 ;;
519 esac < /tmp/list
521 mv -f /tmp/tazusb-wtmp /var/log/wtmp
523 # Get initramfs size
524 size=$(du -sh "$ROOTFS_PATH" | cut -f1)
526 # If the bootable medium is where it should be, copy across
527 if [ -e /home/boot/bzImage* ]; then
528 longline "$(_ 'Moving %s to media. Remember to unmount for delayed writes!' "$ROOTFS")"
530 # Move the old filesystem with the unix timestamp for reference
531 if [ -e /home/boot/previous.gz ]; then
532 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date -r /home/boot/previous.gz +%s)
533 fi
535 mv /home/boot/rootfs.gz /home/boot/previous.gz
536 mv "$ROOTFS_PATH" /home/boot/rootfs.gz
537 _ '%s is located in %s' 'rootfs.gz' '/home/boot'
538 else
539 _ '%s is located in %s' "$ROOTFS" "$(dirname "$ROOTFS_PATH")"
540 fi
542 separator
543 _ 'Root filesystem size: %s' "$size"
544 separator '-'
545 _n 'ENTER to continue...'; read i
546 ;;
549 format)
550 # Format a partition.
551 check_root
553 title 'Format a device'
555 DEVICE="$2"
556 label="$3"
557 fs_type="$4"
558 if [ -z "$DEVICE" ]; then
559 ask_for_device
560 check_for_device
561 else
562 _ 'Device: %s' "$DEVICE"
563 fi
564 [ -z "$fs_type" ] && get_fs_type
565 get_label
566 unmount_target_usb
567 make_fs "$fs_type"
568 separator
569 longline "$(_ 'Device %s is ready to use as LiveUSB and/or /home partition.' "$label ($DEVICE)")"
570 ;;
573 gen-liveusb)
574 # Generate a LiveUSB media using files from a LiveCD.
575 check_root
577 title 'Gen a LiveUSB media'
579 DEVICE="$2"
580 if [ -z "$DEVICE" ]; then
581 ask_for_device
582 fi
584 check_for_device
585 mount_cdrom
586 get_part_info
587 unmount_target_usb
588 install_mbr
589 set_bootable
590 mount_target_usb
591 copy_cdrom_files
592 install_boot
593 exit_or_reboot
594 ;;
597 gen-swap)
598 check_root
599 gen_swap_file
600 ;;
603 gen-iso2usb|iso2usb)
604 check_root
605 # Check if file exists
606 ISO="$2"
607 if [ -z "$ISO" -o ! -f "$ISO" ]; then
608 _ 'Please specify a valid filename on the command line.'
609 exit 1
610 fi
612 title 'Copy ISO file to SliTaz LiveUSB media'
614 DEVICE="$3"
615 if [ -z "$DEVICE" ]; then
616 ask_for_device
617 fi
618 check_for_device
619 mount_iso
620 get_part_info
621 unmount_target_usb
622 install_mbr
623 set_bootable
624 mount_target_usb
625 copy_cdrom_files
626 install_boot
627 umount /media/cdrom
628 losetup -d /dev/loop0
629 exit_or_reboot
630 ;;
633 clean)
634 check_root
635 clean_usb
636 ;;
639 usage|*)
640 # Display usage by default.
641 usage
642 ;;
643 esac
645 exit 0