tazusb view tazusb @ rev 206

Create missing partition & syslinux.cfg is not case sensitive
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Aug 19 16:02:37 2020 +0000 (2020-08-19)
parents 8d3a1a983aae
children 836bf3399896
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 check_partition_table() {
141 DISK=${DEVICE%[1-99]}
142 fdisk -l $DISK | grep -q ^$DEVICE: || fdisk $DISK <<EOT
143 n
144 p
145 1
148 t
149 $1
150 w
151 EOT
152 }
155 # Format target device in ext4, ext3, ext2 or fat32.
156 # Usage: make_fs ext2|ext4|fat32|ext3 (default)
158 make_fs() {
159 local answer
161 FS=$(echo $fs_type | tr '[A-Z]' '[a-z]')
162 case "$FS" in
163 ext4|ext3|ext2)
164 check_partition_table 83
165 newline; _ 'Processing...'
166 _ 'Label: %s' "$label"
167 echo "Mkfs: mkfs.$FS -L \"$label\" $DEVICE"
168 newline; sleep 2
169 mkfs.$FS -L "$label" $DEVICE > $LOG 2>&1
170 ;;
171 fat32)
172 if [ -x '/sbin/mkdosfs' ];then
173 check_partition_table C
174 newline; _ 'Processing...'
175 _ 'Label: %s' "$label"
176 echo "Mkfs: mkdosfs -F 32 -n \"$label\" $DEVICE"
177 newline; sleep 2
178 mkdosfs -F 32 -n "$label" $DEVICE
179 else
180 _ "Can't find %s tool." 'mkdosfs'
181 _n 'Would you like to install %s from repository [y/N]? ' 'dosfstools'
182 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 _ 'Sorry. Filesystem %s is not supported.' "$FS"
193 exit
194 ;;
195 esac
196 }
199 # Mount an existing USB device.
201 unmount_target_usb() {
202 # If mount point is in use, unmount
203 if mount | grep -q $TARGET_ROOT; then
204 umount $TARGET_ROOT
205 fi
207 # Device could be mounted elsewhere, so unmount
208 if mount | grep -q $DEVICE; then
209 _ 'Unmounting USB target device...'
210 umount $DEVICE
211 fi
212 }
215 # Mount an existing USB device.
217 mount_target_usb() {
218 _ 'Mounting USB target device...'
219 mkdir -p $TARGET_ROOT
220 mount $DEVICE $TARGET_ROOT 2>/dev/null
221 }
224 # Mount SliTaz LiveCD to get needed files.
226 mount_cdrom() {
227 _ 'Mounting CD-ROM device...'
229 if mount | grep /media/cdrom; then
230 umount /media/cdrom
231 fi
233 mkdir -p /media/cdrom
234 mount -r -t iso9660 $CDROM /media/cdrom 2>/dev/null
236 if [ ! -f /media/cdrom/boot/rootfs.gz -a \
237 ! -f /media/cdrom/boot/rootfs1.gz ]; then
238 newline
239 longline "$(_ 'Unable to mount CD-ROM or to find a filesystem on it (%s).' 'rootfs.gz')"
240 exit 0
241 fi
242 }
245 # Mount SliTaz ISO to get needed files.
247 mount_iso() {
248 if mount | grep /media/cdrom ; then
249 umount /media/cdrom
250 fi
252 test -d /media/cdrom || mkdir -p /media/cdrom
254 # Add support to other distros.
255 if [ ! -f /etc/slitaz-version ]; then
256 OPTIONS='-o loop'
257 else
258 OPTIONS=''
259 fi
261 _ 'Mounting %s...' "$(basename $ISO)"
262 mount $OPTIONS $ISO /media/cdrom 2>/dev/null
264 if [ ! -f /media/cdrom/boot/rootfs.gz -a \
265 ! -f /media/cdrom/boot/rootfs1.gz ]; then
266 longline "$(_ 'Unable to mount ISO or to find a filesystem on it (%s).' 'rootfs.gz')"
267 exit 0
268 fi
269 }
272 # All needed files are in the boot directory of the CD-ROM.
274 copy_cdrom_files() {
275 _n 'Copying needed files from CD-ROM...'
276 mkdir -p $TARGET_ROOT/boot
277 cp /media/cdrom/boot/bzImage* $TARGET_ROOT/boot
278 cp /media/cdrom/boot/rootfs*.gz* $TARGET_ROOT/boot
279 cp /media/cdrom/boot/memtest* $TARGET_ROOT/boot 2>/dev/null
280 cp /media/cdrom/boot/*xe* $TARGET_ROOT/boot 2>/dev/null
281 status
282 }
285 install_mbr() {
286 # MBR
287 DISK=${DEVICE%[1-99]}
288 if [ -f /usr/share/boot/mbr.bin ]; then
289 _n 'Installing a new MBR to %s' "$DISK"
290 cat /usr/share/boot/mbr.bin > $DISK
291 status
292 else
293 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
294 _ 'No new MBR installed to %s' "$DISK"
295 fi
296 }
299 # ext/syslinux install
301 install_boot() {
302 # Decide if we're installing syslinux or extlinux
303 if [ "$FSTYPE" = 'vfat' ]; then
304 ST='syslinux'
305 STC="syslinux --install -d /boot/syslinux/ $DEVICE"
306 STE='cfg'
307 else
308 ST='extlinux'
309 STC="extlinux --install $TARGET_ROOT/boot/$ST"
310 STE='conf'
311 fi
313 _ 'Installing bootloader: %s' "$ST"
314 mkdir -p $TARGET_ROOT/boot/$ST
315 $STC
317 # Use existing isolinux.cfg for extlinux.conf or syslinux.cfg
318 cp /media/cdrom/boot/isolinux/isolinux.cfg $TARGET_ROOT/boot/$ST/$ST.$STE
320 # Update DVD autoinstall
321 sed -i "s/LABEL=packages-[^,]*/UUID=$UUID/g" $(grep -il append $TARGET_ROOT/boot/$ST/*)
323 # Add home=$UUID to kernel line in extlinux or syslinux.cfg
324 sed -i -e "s/\(root=.*\)/\1 home=$UUID/" $(grep -il append $TARGET_ROOT/boot/$ST/*)
326 # Splash screen and help files.
327 cp /media/cdrom/boot/isolinux/splash.* $TARGET_ROOT/boot/$ST
328 cp /media/cdrom/boot/isolinux/*.cfg $TARGET_ROOT/boot/$ST
329 cp /media/cdrom/boot/isolinux/*kbd $TARGET_ROOT/boot/$ST
330 cp /media/cdrom/boot/isolinux/*.c32 $TARGET_ROOT/boot/$ST
331 cp /media/cdrom/boot/isolinux/opts.* $TARGET_ROOT/boot/$ST
332 cp /media/cdrom/boot/isolinux/help.* $TARGET_ROOT/boot/$ST
333 sed -i -e s/'SliTaz GNU\/Linux'/'SliTaz GNU\/Linux LiveUSB'/ \
334 -e "s/isolinux/$ST/" $TARGET_ROOT/boot/$ST/$ST.$STE
335 }
338 # Let user exit or reboot.
340 exit_or_reboot() {
341 separator
342 newline
343 longline "$(_n 'Do you want to exit Tazusb or reboot system (Exit/reboot)? ')"
344 read answer
345 if [ "$answer" = 'reboot' ]; then
346 unmount_target_usb
347 reboot || reboot -f
348 else
349 unmount_target_usb
350 newline; exit 0
351 fi
352 }
355 set_bootable() {
356 # As the boot flag is toggable, need to check it before hand
357 DISK=${DEVICE%[1-99]}
358 ISSET="$(fdisk -l $DISK | grep $DEVICE | grep "\*")"
360 # If not set, set bootable
361 if [ -z "$ISSET" ]; then
362 umount $TARGET_ROOT 2>/dev/null
363 _n 'Setting %s as bootable...' "$DEVICE"
364 fdisk $DISK >/dev/null << EOF
365 $(fdisk -l $DISK | grep $DISK | sed "/\\*/!ds#$DISK##;s# .*##;s#.*#&\na#")
366 a
367 ${DEVICE#$DISK}
368 w
369 EOF
370 status
371 fi
372 }
375 # Generate a virtual swap file in /home/swap. SliTaz boot scripts
376 # will activate it, useful for low memory systems
378 gen_swap_file() {
379 title "$(_n 'Gen swap')"
381 longline "$(_ "Generate a swap file in %s that will be activated on each \
382 boot to have more memory available (empty value to exit)." '/home/swap')"
383 newline
384 _n 'Swap file in MB: '
385 read size
386 if [ -z "$size" ]; then
387 _ 'Empty value. Exiting...'
388 exit 0
389 fi
390 cd /home
391 # Sanity check
392 if [ -f swap ]; then
393 swapoff swap 2>/dev/null
394 fi
395 # DD to gen a virtual disk.
396 dd if=/dev/zero of=swap bs=1M count=$size
397 # Make swap filesystem.
398 mkswap swap
399 swapon swap
400 newline
401 }
404 # Clean out old backups to save disk space
406 clean_usb() {
407 title "$(_n 'Clean')"
409 longline "$(_n 'Remove old %s backup filesystems to free up disk space.' \
410 'rootfs.gz.unixtimestamp')"
411 newline
412 # Locate and interactively remove old filesystems from /home directory
413 for file in $(find /home/boot/rootfs.gz.[0-9]*); do
414 _n 'Do you wish to remove: %s (Yes/no/exit)? ' "$(basename $file)"
415 read answer
416 case $answer in
417 e|E|"exit"|Exit)
418 exit 0 ;;
419 y|Y|yes|Yes)
420 rm -f $file ;;
421 *)
422 _ 'No filesystems selected, exiting...' ;;
423 esac
424 done
425 }
428 #
429 # Tazusb sequence
430 #
432 case $COMMAND in
433 writefs)
434 # Writefs to rootfs.gz
435 check_root
436 # Compression type (optional): lzma, gzip, none. Default is none
437 COMPRESSION="${2:-none}"
438 # Full path to rootfs.gz (optional). Default is /rootfs.gz
439 ROOTFS_PATH="${3:-/rootfs.gz}"
440 # File name
441 ROOTFS="$(basename "$ROOTFS_PATH")"
443 # Start info
444 title 'Write filesystem'
446 longline "$(_ "The command writefs will write all the current \
447 filesystem into a suitable cpio archive (%s) usable on a bootable \
448 LiveUSB media." "$ROOTFS")"
449 newline
450 _ 'Archive compression: %s' "$(colorize 36 "$COMPRESSION")"
451 newline
453 # Clear out tazpkg cache
454 rm /var/lib/tazpkg/*.bak /var/cache/tazpkg/* -r -f
456 # Optionally remove sound card selection and screen resolution.
457 _ 'Do you wish to remove the sound card and screen configs?'
458 _n 'Press ENTER to keep or answer (No|yes|exit): '
459 read answer
460 case $answer in
461 e|E|"exit"|Exit)
462 exit 0 ;;
463 y|Y|yes|Yes)
464 _n 'Removing current sound card and screen configurations...'
465 rm -f /var/lib/sound-card-driver
466 rm -f /var/lib/alsa/asound.state
467 rm -f /etc/X11/xorg.conf ;;
468 *)
469 _n 'Keeping current sound card and screen configurations...' ;;
470 esac
471 status
472 newline
474 # Optionally remove i18n settings
475 _ 'Do you wish to remove local/keymap settings?'
476 _n 'Press ENTER to keep or answer (No|yes|exit): '
477 read answer
478 case $answer in
479 e|E|"exit"|Exit)
480 exit 0 ;;
481 y|Y|yes|Yes)
482 _n 'Removing current locale/keymap settings...'
483 echo > /etc/locale.conf
484 echo > /etc/keymap.conf ;;
485 *)
486 _n 'Keeping current locale/keymap settings...'
487 grep -qs '^INCLUDE i18n.cfg' /home/boot/*linux/*linux.c* &&
488 sed -i 's/^INCLUDE i18n.cfg/# &/' /home/boot/*linux/*linux.c* ;;
489 esac
490 status
491 newline
493 # Clean-up files by default
494 mv -f /var/log/wtmp /tmp/tazusb-wtmp
495 touch /var/log/wtmp
496 echo > /etc/udev/rules.d/70-persistent-net.rules
497 echo > /etc/udev/rules.d/70-persistant-cd.rules
499 # Create list of files
500 # find / -xdev | sed '/^\/home\//d;/^\/tmp\//d' >/tmp/list
501 # for dev in console null tty tty1
502 # do
503 # echo /dev/$dev >>/tmp/list
504 # done
506 for dir in /bin /etc /init /sbin /var /dev /lib /root /usr /opt
507 do
508 [ -d $dir -o -f $dir ] && find $dir
509 done >/tmp/list
511 for dir in /home /proc /run /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
512 do
513 echo $dir >>/tmp/list
514 done
515 sed -i '/^\/var\/run\/.*pid$/d' /tmp/list
517 for removelog in auth boot messages dmesg daemon utmp slim Xorg tazpanel cups; do
518 sed -i "\/var\/log\/$removelog/d" /tmp/list
519 done
521 # Generate initramfs with specified compression
522 case "$COMPRESSION" in
523 lzma)
524 _n 'Creating %s with lzma compression... ' "$ROOTFS"
525 cpio -o -H newc | lzma e -si -so > "$ROOTFS_PATH"
526 ;;
527 gzip)
528 _n 'Creating %s with gzip compression... ' "$ROOTFS"
529 cpio -o -H newc | gzip -9 > "$ROOTFS_PATH"
530 [ -x /usr/bin/advdef ] && advdef -z4 "$ROOTFS_PATH"
531 ;;
532 none|*)
533 _n 'Creating %s without compression... ' "$ROOTFS"
534 cpio -o -H newc > "$ROOTFS_PATH"
535 ;;
536 esac < /tmp/list
538 mv -f /tmp/tazusb-wtmp /var/log/wtmp
540 # Get initramfs size
541 size=$(du -sh "$ROOTFS_PATH" | cut -f1)
543 # If the bootable medium is where it should be, copy across
544 if [ -e /home/boot/bzImage* ]; then
545 longline "$(_ 'Moving %s to media. Remember to unmount for delayed writes!' "$ROOTFS")"
547 # Move the old filesystem with the unix timestamp for reference
548 if [ -e /home/boot/previous.gz ]; then
549 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date -r /home/boot/previous.gz +%s)
550 fi
552 mv /home/boot/rootfs.gz /home/boot/previous.gz
553 mv "$ROOTFS_PATH" /home/boot/rootfs.gz
554 _ '%s is located in %s' 'rootfs.gz' '/home/boot'
555 else
556 _ '%s is located in %s' "$ROOTFS" "$(dirname "$ROOTFS_PATH")"
557 fi
559 separator
560 _ 'Root filesystem size: %s' "$size"
561 separator '-'
562 _n 'ENTER to continue...'; read i
563 ;;
566 format)
567 # Format a partition.
568 check_root
570 title 'Format a device'
572 DEVICE="$2"
573 label="$3"
574 fs_type="$4"
575 if [ -z "$DEVICE" ]; then
576 ask_for_device
577 check_for_device
578 else
579 _ 'Device: %s' "$DEVICE"
580 fi
581 [ -z "$fs_type" ] && get_fs_type
582 get_label
583 unmount_target_usb
584 make_fs "$fs_type"
585 separator
586 longline "$(_ 'Device %s is ready to use as LiveUSB and/or /home partition.' "$label ($DEVICE)")"
587 ;;
590 gen-liveusb)
591 # Generate a LiveUSB media using files from a LiveCD.
592 check_root
594 title 'Gen a LiveUSB media'
596 DEVICE="$2"
597 if [ -z "$DEVICE" ]; then
598 ask_for_device
599 fi
601 check_for_device
602 mount_cdrom
603 get_part_info
604 unmount_target_usb
605 install_mbr
606 set_bootable
607 mount_target_usb
608 copy_cdrom_files
609 install_boot
610 exit_or_reboot
611 ;;
614 gen-swap)
615 check_root
616 gen_swap_file
617 ;;
620 gen-iso2usb|iso2usb)
621 check_root
622 # Check if file exists
623 ISO="$2"
624 if [ -z "$ISO" -o ! -f "$ISO" ]; then
625 _ 'Please specify a valid filename on the command line.'
626 exit 1
627 fi
629 title 'Copy ISO file to SliTaz LiveUSB media'
631 DEVICE="$3"
632 if [ -z "$DEVICE" ]; then
633 ask_for_device
634 fi
635 check_for_device
636 mount_iso
637 get_part_info
638 unmount_target_usb
639 install_mbr
640 set_bootable
641 mount_target_usb
642 copy_cdrom_files
643 install_boot
644 umount /media/cdrom
645 losetup -d /dev/loop0
646 exit_or_reboot
647 ;;
650 clean)
651 check_root
652 clean_usb
653 ;;
656 usage|*)
657 # Display usage by default.
658 usage
659 ;;
660 esac
662 exit 0