tazusb view tazusb @ rev 209

Take care of isolinux.cfg
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Aug 20 07:27:26 2020 +0000 (2020-08-20)
parents 836bf3399896
children
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/" -e '/LABEL md5sum/,/append md5sum/d' \
325 $(grep -il append $TARGET_ROOT/boot/$ST/*)
327 # Splash screen and help files.
328 cp /media/cdrom/boot/isolinux/splash.* $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
332 cp /media/cdrom/boot/isolinux/opts.* $TARGET_ROOT/boot/$ST
333 cp /media/cdrom/boot/isolinux/help.* $TARGET_ROOT/boot/$ST
334 sed -i -e s/'SliTaz GNU\/Linux'/'SliTaz GNU\/Linux LiveUSB'/ \
335 -e "s/isolinux/$ST/" $TARGET_ROOT/boot/$ST/$ST.$STE
336 }
339 # Let user exit or reboot.
341 exit_or_reboot() {
342 separator
343 newline
344 longline "$(_n 'Do you want to exit Tazusb or reboot system (Exit/reboot)? ')"
345 read answer
346 if [ "$answer" = 'reboot' ]; then
347 unmount_target_usb
348 reboot || reboot -f
349 else
350 unmount_target_usb
351 newline; exit 0
352 fi
353 }
356 set_bootable() {
357 # As the boot flag is toggable, need to check it before hand
358 DISK=${DEVICE%[1-99]}
359 ISSET="$(fdisk -l $DISK | grep $DEVICE | grep "\*")"
361 # If not set, set bootable
362 if [ -z "$ISSET" ]; then
363 umount $TARGET_ROOT 2>/dev/null
364 _n 'Setting %s as bootable...' "$DEVICE"
365 fdisk $DISK >/dev/null << EOF
366 $(fdisk -l $DISK | grep $DISK | sed "/\\*/!ds#$DISK##;s# .*##;s#.*#&\na#")
367 a
368 ${DEVICE#$DISK}
369 w
370 EOF
371 status
372 fi
373 }
376 # Generate a virtual swap file in /home/swap. SliTaz boot scripts
377 # will activate it, useful for low memory systems
379 gen_swap_file() {
380 title "$(_n 'Gen swap')"
382 longline "$(_ "Generate a swap file in %s that will be activated on each \
383 boot to have more memory available (empty value to exit)." '/home/swap')"
384 newline
385 _n 'Swap file in MB: '
386 read size
387 if [ -z "$size" ]; then
388 _ 'Empty value. Exiting...'
389 exit 0
390 fi
391 cd /home
392 # Sanity check
393 if [ -f swap ]; then
394 swapoff swap 2>/dev/null
395 fi
396 # DD to gen a virtual disk.
397 dd if=/dev/zero of=swap bs=1M count=$size
398 # Make swap filesystem.
399 mkswap swap
400 swapon swap
401 newline
402 }
405 # Clean out old backups to save disk space
407 clean_usb() {
408 title "$(_n 'Clean')"
410 longline "$(_n 'Remove old %s backup filesystems to free up disk space.' \
411 'rootfs.gz.unixtimestamp')"
412 newline
413 # Locate and interactively remove old filesystems from /home directory
414 for file in $(find /home/boot/rootfs.gz.[0-9]*); do
415 _n 'Do you wish to remove: %s (Yes/no/exit)? ' "$(basename $file)"
416 read answer
417 case $answer in
418 e|E|"exit"|Exit)
419 exit 0 ;;
420 y|Y|yes|Yes)
421 rm -f $file ;;
422 *)
423 _ 'No filesystems selected, exiting...' ;;
424 esac
425 done
426 }
429 #
430 # Tazusb sequence
431 #
433 case $COMMAND in
434 writefs)
435 # Writefs to rootfs.gz
436 check_root
437 # Compression type (optional): lzma, gzip, none. Default is none
438 COMPRESSION="${2:-none}"
439 # Full path to rootfs.gz (optional). Default is /rootfs.gz
440 ROOTFS_PATH="${3:-/rootfs.gz}"
441 # File name
442 ROOTFS="$(basename "$ROOTFS_PATH")"
444 # Start info
445 title 'Write filesystem'
447 longline "$(_ "The command writefs will write all the current \
448 filesystem into a suitable cpio archive (%s) usable on a bootable \
449 LiveUSB media." "$ROOTFS")"
450 newline
451 _ 'Archive compression: %s' "$(colorize 36 "$COMPRESSION")"
452 newline
454 # Clear out tazpkg cache
455 rm /var/lib/tazpkg/*.bak /var/cache/tazpkg/* -r -f
457 # Optionally remove sound card selection and screen resolution.
458 _ 'Do you wish to remove the sound card and screen configs?'
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 sound card and screen configurations...'
466 rm -f /var/lib/sound-card-driver
467 rm -f /var/lib/alsa/asound.state
468 rm -f /etc/X11/xorg.conf ;;
469 *)
470 _n 'Keeping current sound card and screen configurations...' ;;
471 esac
472 status
473 newline
475 # Optionally remove i18n settings
476 _ 'Do you wish to remove local/keymap settings?'
477 _n 'Press ENTER to keep or answer (No|yes|exit): '
478 read answer
479 case $answer in
480 e|E|"exit"|Exit)
481 exit 0 ;;
482 y|Y|yes|Yes)
483 _n 'Removing current locale/keymap settings...'
484 echo > /etc/locale.conf
485 echo > /etc/keymap.conf ;;
486 *)
487 _n 'Keeping current locale/keymap settings...'
488 grep -qs '^INCLUDE i18n.cfg' /home/boot/*linux/[se]*linux.c* &&
489 sed -i 's/^INCLUDE i18n.cfg/# &/' /home/boot/*linux/[se]*linux.c* ;;
490 esac
491 status
492 newline
494 # Clean-up files by default
495 mv -f /var/log/wtmp /tmp/tazusb-wtmp
496 touch /var/log/wtmp
497 echo > /etc/udev/rules.d/70-persistent-net.rules
498 echo > /etc/udev/rules.d/70-persistant-cd.rules
500 # Create list of files
501 # find / -xdev | sed '/^\/home\//d;/^\/tmp\//d' >/tmp/list
502 # for dev in console null tty tty1
503 # do
504 # echo /dev/$dev >>/tmp/list
505 # done
507 for dir in /bin /etc /init /sbin /var /dev /lib /root /usr /opt
508 do
509 [ -d $dir -o -f $dir ] && find $dir
510 done >/tmp/list
512 for dir in /home /proc /run /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
513 do
514 echo $dir >>/tmp/list
515 done
516 sed -i '/^\/var\/run\/.*pid$/d' /tmp/list
518 for removelog in auth boot messages dmesg daemon utmp slim Xorg tazpanel cups; do
519 sed -i "\/var\/log\/$removelog/d" /tmp/list
520 done
522 # Generate initramfs with specified compression
523 case "$COMPRESSION" in
524 lzma)
525 _n 'Creating %s with lzma compression... ' "$ROOTFS"
526 cpio -o -H newc | lzma e -si -so > "$ROOTFS_PATH"
527 ;;
528 gzip)
529 _n 'Creating %s with gzip compression... ' "$ROOTFS"
530 cpio -o -H newc | gzip -9 > "$ROOTFS_PATH"
531 [ -x /usr/bin/advdef ] && advdef -z4 "$ROOTFS_PATH"
532 ;;
533 none|*)
534 _n 'Creating %s without compression... ' "$ROOTFS"
535 cpio -o -H newc > "$ROOTFS_PATH"
536 ;;
537 esac < /tmp/list
539 mv -f /tmp/tazusb-wtmp /var/log/wtmp
541 # Get initramfs size
542 size=$(du -sh "$ROOTFS_PATH" | cut -f1)
544 # If the bootable medium is where it should be, copy across
545 if [ "$(ls /home/boot/bzImage*)" ]; then
546 longline "$(_ 'Moving %s to media. Remember to unmount for delayed writes!' "$ROOTFS")"
548 CONFIG=$(ls /home/boot/*linux/[se]*linux.c*)
549 if grep -qs 'gz,' $CONFIG; then
551 # Disable loram & many-in-1
552 [ -s /home/boot/rootfs.gz ] && mv /home/boot/rootfs.gz /home/boot/rootfs.gz.loram
553 cp $CONFIG $CONFIG.org
554 sed -i 's|rootfs.*\.gz|rootfs.gz|' $CONFIG
555 else
556 # Move the old filesystem with the unix timestamp for reference
557 if [ -e /home/boot/previous.gz ]; then
558 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date -r /home/boot/previous.gz +%s)
559 fi
561 mv /home/boot/rootfs.gz /home/boot/previous.gz
562 fi
563 mv "$ROOTFS_PATH" /home/boot/rootfs.gz
564 _ '%s is located in %s' 'rootfs.gz' '/home/boot'
565 else
566 _ '%s is located in %s' "$ROOTFS" "$(dirname "$ROOTFS_PATH")"
567 fi
569 separator
570 _ 'Root filesystem size: %s' "$size"
571 separator '-'
572 _n 'ENTER to continue...'; read i
573 ;;
576 format)
577 # Format a partition.
578 check_root
580 title 'Format a device'
582 DEVICE="$2"
583 label="$3"
584 fs_type="$4"
585 if [ -z "$DEVICE" ]; then
586 ask_for_device
587 check_for_device
588 else
589 _ 'Device: %s' "$DEVICE"
590 fi
591 [ -z "$fs_type" ] && get_fs_type
592 get_label
593 unmount_target_usb
594 make_fs "$fs_type"
595 separator
596 longline "$(_ 'Device %s is ready to use as LiveUSB and/or /home partition.' "$label ($DEVICE)")"
597 ;;
600 gen-liveusb)
601 # Generate a LiveUSB media using files from a LiveCD.
602 check_root
604 title 'Gen a LiveUSB media'
606 DEVICE="$2"
607 if [ -z "$DEVICE" ]; then
608 ask_for_device
609 fi
611 check_for_device
612 mount_cdrom
613 get_part_info
614 unmount_target_usb
615 install_mbr
616 set_bootable
617 mount_target_usb
618 copy_cdrom_files
619 install_boot
620 exit_or_reboot
621 ;;
624 gen-swap)
625 check_root
626 gen_swap_file
627 ;;
630 gen-iso2usb|iso2usb)
631 check_root
632 # Check if file exists
633 ISO="$2"
634 if [ -z "$ISO" -o ! -f "$ISO" ]; then
635 _ 'Please specify a valid filename on the command line.'
636 exit 1
637 fi
639 title 'Copy ISO file to SliTaz LiveUSB media'
641 DEVICE="$3"
642 if [ -z "$DEVICE" ]; then
643 ask_for_device
644 fi
645 check_for_device
646 mount_iso
647 get_part_info
648 unmount_target_usb
649 install_mbr
650 set_bootable
651 mount_target_usb
652 copy_cdrom_files
653 install_boot
654 umount /media/cdrom
655 losetup -d /dev/loop0
656 exit_or_reboot
657 ;;
660 clean)
661 check_root
662 clean_usb
663 ;;
666 usage|*)
667 # Display usage by default.
668 usage
669 ;;
670 esac
672 exit 0