tazusb view tazusb @ rev 200

Add Italian; make pot; make msgmerge; make clean
author Aleksej Bobylev <al.bobylev@gmail.com>
date Tue Jan 30 12:20:07 2018 +0200 (2018-01-30)
parents 00209e613d86
children e9c96f617368
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 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 # Get fs type. Supported fs are ext3, ext2, fat32
116 get_fs_type() {
117 _n 'Please specify a filesystem type ext2, ext3, ext4 or fat32 (ext3): '
118 read fs_type
119 [ -z "$fs_type" ] && fs_type='ext3'
120 }
123 # We can chose the filesystem type.
125 ask_for_fs_type() {
126 _ 'Please specify the filesystem type to %s.' "$COMMAND"
127 _ 'Available formats are ext4, ext3(default), ext2 or fat32.'
128 _ 'Press enter to keep the default value.'
129 newline
130 _n 'File system type: '; read answer
131 if [ -z "$answer" ]; then
132 FS_TYPE='ext3'
133 else
134 FS_TYPE="$answer"
135 fi
136 }
139 # Format target device in ext4, ext3, ext2 or fat32.
140 # Usage: make_fs ext2|ext4|fat32|ext3 (default)
142 make_fs() {
143 local answer
145 FS=$(echo $fs_type | tr '[A-Z]' '[a-z]')
146 case "$FS" in
147 ext4|ext3|ext2)
148 newline; _ 'Processing...'
149 _ 'Label: %s' "$label"
150 echo "Mkfs: mkfs.$FS -L \"$label\" $DEVICE"
151 newline; sleep 2
152 mkfs.$FS -L "$label" $DEVICE > $LOG 2>&1
153 ;;
154 fat32)
155 if [ -x '/sbin/mkdosfs' ];then
156 newline; _ 'Processing...'
157 _ 'Label: %s' "$label"
158 echo "Mkfs: mkdosfs -F 32 -n \"$label\" $DEVICE"
159 newline; sleep 2
160 mkdosfs -F 32 -n "$label" $DEVICE
161 else
162 _ "Can't find %s tool." 'mkdosfs'
163 _n 'Would you like to install %s from repository [y/N]? ' 'dosfstools'
164 read answer
165 case "$answer" in
166 y|Y)
167 yes | tazpkg get-install dosfstools && make_fs fat32;;
168 *)
169 exit 1 ;;
170 esac
171 fi
172 ;;
173 *)
174 _ 'Sorry. Filesystem %s is not supported.' "$FS"
175 exit
176 ;;
177 esac
178 }
181 # Mount an existing USB device.
183 unmount_target_usb() {
184 # If mount point is in use, unmount
185 if mount | grep -q $TARGET_ROOT; then
186 umount $TARGET_ROOT
187 fi
189 # Device could be mounted elsewhere, so unmount
190 if mount | grep -q $DEVICE; then
191 _ 'Unmounting USB target device...'
192 umount $DEVICE
193 fi
194 }
197 # Mount an existing USB device.
199 mount_target_usb() {
200 _ 'Mounting USB target device...'
201 mkdir -p $TARGET_ROOT
202 mount $DEVICE $TARGET_ROOT 2>/dev/null
203 }
206 # Mount SliTaz LiveCD to get needed files.
208 mount_cdrom() {
209 _ 'Mounting CD-ROM device...'
211 if mount | grep /media/cdrom; then
212 umount /media/cdrom
213 fi
215 mkdir -p /media/cdrom
216 mount -r -t iso9660 $CDROM /media/cdrom 2>/dev/null
218 if [ ! -f /media/cdrom/boot/rootfs.gz -a \
219 ! -f /media/cdrom/boot/rootfs1.gz ]; then
220 newline
221 longline "$(_ 'Unable to mount CD-ROM or to find a filesystem on it (%s).' 'rootfs.gz')"
222 exit 0
223 fi
224 }
227 # Mount SliTaz ISO to get needed files.
229 mount_iso() {
230 if mount | grep /media/cdrom ; then
231 umount /media/cdrom
232 fi
234 test -d /media/cdrom || mkdir -p /media/cdrom
236 # Add support to other distros.
237 if [ ! -f /etc/slitaz-version ]; then
238 OPTIONS='-o loop'
239 else
240 OPTIONS=''
241 fi
243 _ 'Mounting %s...' "$(basename $ISO)"
244 mount $OPTIONS $ISO /media/cdrom 2>/dev/null
246 if [ ! -f /media/cdrom/boot/rootfs.gz -a \
247 ! -f /media/cdrom/boot/rootfs1.gz ]; then
248 longline "$(_ 'Unable to mount ISO or to find a filesystem on it (%s).' 'rootfs.gz')"
249 exit 0
250 fi
251 }
254 # All needed files are in the boot directory of the CD-ROM.
256 copy_cdrom_files() {
257 _n 'Copying needed files from CD-ROM...'
258 mkdir -p $TARGET_ROOT/boot
259 cp /media/cdrom/boot/bzImage* $TARGET_ROOT/boot
260 cp /media/cdrom/boot/rootfs*.gz* $TARGET_ROOT/boot
261 cp /media/cdrom/boot/memtest $TARGET_ROOT/boot 2>/dev/null
262 cp /media/cdrom/boot/*pxe $TARGET_ROOT/boot 2>/dev/null
263 status
264 }
267 install_mbr() {
268 # MBR
269 DISK=${DEVICE%[1-99]}
270 if [ -f /usr/share/boot/mbr.bin ]; then
271 _n 'Installing a new MBR to %s' "$DISK"
272 cat /usr/share/boot/mbr.bin > $DISK
273 status
274 else
275 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
276 _ 'No new MBR installed to %s' "$DISK"
277 fi
278 }
281 # ext/syslinux install
283 install_boot() {
284 # Decide if we're installing syslinux or extlinux
285 if [ "$FSTYPE" == 'vfat' ]; then
286 ST='syslinux'
287 STC="syslinux --install -d /boot/syslinux/ $DEVICE"
288 STE='cfg'
289 else
290 ST='extlinux'
291 STC="extlinux --install $TARGET_ROOT/boot/$ST"
292 STE='conf'
293 fi
295 _ 'Installing bootloader: %s' "$ST"
296 mkdir -p $TARGET_ROOT/boot/$ST
297 $STC
299 # Use existing isolinux.cfg for extlinux.conf or syslinux.cfg
300 cp /media/cdrom/boot/isolinux/isolinux.cfg $TARGET_ROOT/boot/$ST/$ST.$STE
302 # Update DVD autoinstall
303 sed -i "s/LABEL=packages-[^,]*/UUID=$UUID/g" $(grep -l append $TARGET_ROOT/boot/$ST/*)
305 # Add home=$UUID to kernel line in extlinux or syslinux.cfg
306 sed -i -e "s/\(root=.*\)/\1 home=$UUID/" $(grep -l append $TARGET_ROOT/boot/$ST/*)
308 # Splash screen and help files.
309 cp /media/cdrom/boot/isolinux/splash.* $TARGET_ROOT/boot/$ST
310 cp /media/cdrom/boot/isolinux/*.cfg $TARGET_ROOT/boot/$ST
311 cp /media/cdrom/boot/isolinux/*kbd $TARGET_ROOT/boot/$ST
312 cp /media/cdrom/boot/isolinux/*.c32 $TARGET_ROOT/boot/$ST
313 sed -i -e s/'SliTaz GNU\/Linux'/'SliTaz GNU\/Linux LiveUSB'/ \
314 -e "s/isolinux/$ST/" $TARGET_ROOT/boot/$ST/$ST.$STE
315 }
318 # Let user exit or reboot.
320 exit_or_reboot() {
321 separator
322 newline
323 longline "$(_n 'Do you want to exit Tazusb or reboot system (Exit/reboot)? ')"
324 read answer
325 if [ "$answer" == 'reboot' ]; then
326 unmount_target_usb
327 reboot || reboot -f
328 else
329 unmount_target_usb
330 newline; exit 0
331 fi
332 }
335 set_bootable() {
336 # As the boot flag is toggable, need to check it before hand
337 DISK=${DEVICE%[1-99]}
338 ISSET="$(fdisk -l $DISK | grep $DEVICE | grep "\*")"
340 # If not set, set bootable
341 if [ -z "$ISSET" ]; then
342 umount $TARGET_ROOT 2>/dev/null
343 _n 'Setting %s as bootable...' "$DEVICE"
344 fdisk $DISK >/dev/null << EOF
345 $(fdisk -l $DISK | grep $DISK | sed "/\\*/!ds#$DISK##;s# .*##;s#.*#&\na#")
346 a
347 ${DEVICE#$DISK}
348 w
349 EOF
350 status
351 fi
352 }
355 # Generate a virtual swap file in /home/swap. SliTaz boot scripts
356 # will activate it, useful for low memory systems
358 gen_swap_file() {
359 title "$(_n 'Gen swap')"
361 longline "$(_ "Generate a swap file in %s that will be activated on each \
362 boot to have more memory available (empty value to exit)." '/home/swap')"
363 newline
364 _n 'Swap file in MB: '
365 read size
366 if [ -z "$size" ]; then
367 _ 'Empty value. Exiting...'
368 exit 0
369 fi
370 cd /home
371 # Sanity check
372 if [ -f swap ]; then
373 swapoff swap 2>/dev/null
374 fi
375 # DD to gen a virtual disk.
376 dd if=/dev/zero of=swap bs=1M count=$size
377 # Make swap filesystem.
378 mkswap swap
379 swapon swap
380 newline
381 }
384 # Clean out old backups to save disk space
386 clean_usb() {
387 title "$(_n 'Clean')"
389 longline "$(_n 'Remove old %s backup filesystems to free up disk space.' \
390 'rootfs.gz.unixtimestamp')"
391 newline
392 # Locate and interactively remove old filesystems from /home directory
393 for file in $(find /home/boot/rootfs.gz.[0-9]*); do
394 _n 'Do you wish to remove: %s (Yes/no/exit)? ' "$(basename $file)"
395 read answer
396 case $answer in
397 e|E|"exit"|Exit)
398 exit 0 ;;
399 y|Y|yes|Yes)
400 rm -f $file ;;
401 *)
402 _ 'No filesystems selected, exiting...' ;;
403 esac
404 done
405 }
408 #
409 # Tazusb sequence
410 #
412 case $COMMAND in
413 writefs)
414 # Writefs to rootfs.gz
415 check_root
416 # Compression type (optional): lzma, gzip, none. Default is none
417 COMPRESSION="${2:-none}"
418 # Full path to rootfs.gz (optional). Default is /rootfs.gz
419 ROOTFS_PATH="${3:-/rootfs.gz}"
420 # File name
421 ROOTFS="$(basename "$ROOTFS_PATH")"
423 # Start info
424 title 'Write filesystem'
426 longline "$(_ "The command writefs will write all the current \
427 filesystem into a suitable cpio archive (%s) usable on a bootable \
428 LiveUSB media." "$ROOTFS")"
429 newline
430 _ 'Archive compression: %s' "$(colorize 36 "$COMPRESSION")"
431 newline
433 # Clear out tazpkg cache
434 rm /var/lib/tazpkg/*.bak /var/cache/tazpkg/* -r -f
436 # Optionally remove sound card selection and screen resolution.
437 _ 'Do you wish to remove the sound card and screen configs?'
438 _n 'Press ENTER to keep or answer (No|yes|exit): '
439 read answer
440 case $answer in
441 e|E|"exit"|Exit)
442 exit 0 ;;
443 y|Y|yes|Yes)
444 _n 'Removing current sound card and screen configurations...'
445 rm -f /var/lib/sound-card-driver
446 rm -f /var/lib/alsa/asound.state
447 rm -f /etc/X11/xorg.conf ;;
448 *)
449 _n 'Keeping current sound card and screen configurations...' ;;
450 esac
451 status
452 newline
454 # Optionally remove i18n settings
455 _ 'Do you wish to remove local/keymap settings?'
456 _n 'Press ENTER to keep or answer (No|yes|exit): '
457 read answer
458 case $answer in
459 e|E|"exit"|Exit)
460 exit 0 ;;
461 y|Y|yes|Yes)
462 _n 'Removing current locale/keymap settings...'
463 echo > /etc/locale.conf
464 echo > /etc/keymap.conf ;;
465 *)
466 _n 'Keeping current locale/keymap settings...'
467 grep -qs '^INCLUDE i18n.cfg' /home/boot/*linux/*linux.c* &&
468 sed -i 's/^INCLUDE i18n.cfg/# &/' /home/boot/*linux/*linux.c* ;;
469 esac
470 status
471 newline
473 # Clean-up files by default
474 mv -f /var/log/wtmp /tmp/tazusb-wtmp
475 touch /var/log/wtmp
476 echo > /etc/udev/rules.d/70-persistent-net.rules
477 echo > /etc/udev/rules.d/70-persistant-cd.rules
479 # Create list of files
480 # find / -xdev | sed '/^\/home\//d;/^\/tmp\//d' >/tmp/list
481 # for dev in console null tty tty1
482 # do
483 # echo /dev/$dev >>/tmp/list
484 # done
486 for dir in /bin /etc /init /sbin /var /dev /lib /root /usr /opt
487 do
488 [ -d $dir -o -f $dir ] && find $dir
489 done >/tmp/list
491 for dir in /home /proc /run /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
492 do
493 echo $dir >>/tmp/list
494 done
495 sed -i '/^\/var\/run\/.*pid$/d' /tmp/list
497 for removelog in auth boot messages dmesg daemon utmp slim Xorg tazpanel cups; do
498 sed -i "\/var\/log\/$removelog/d" /tmp/list
499 done
501 # Generate initramfs with specified compression
502 case "$COMPRESSION" in
503 lzma)
504 _n 'Creating %s with lzma compression... ' "$ROOTFS"
505 cpio -o -H newc | lzma e -si -so > "$ROOTFS_PATH"
506 ;;
507 gzip)
508 _n 'Creating %s with gzip compression... ' "$ROOTFS"
509 cpio -o -H newc | gzip -9 > "$ROOTFS_PATH"
510 [ -x /usr/bin/advdef ] && advdef -z4 "$ROOTFS_PATH"
511 ;;
512 none|*)
513 _n 'Creating %s without compression... ' "$ROOTFS"
514 cpio -o -H newc > "$ROOTFS_PATH"
515 ;;
516 esac < /tmp/list
518 mv -f /tmp/tazusb-wtmp /var/log/wtmp
520 # Get initramfs size
521 size=$(du -sh "$ROOTFS_PATH" | cut -f1)
523 # If the bootable medium is where it should be, copy across
524 if [ -e /home/boot/bzImage ]; then
525 longline "$(_ 'Moving %s to media. Remember to unmount for delayed writes!' "$ROOTFS")"
527 # Move the old filesystem with the unix timestamp for reference
528 if [ -e /home/boot/previous.gz ]; then
529 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
530 fi
532 mv /home/boot/rootfs.gz /home/boot/previous.gz
533 mv "$ROOTFS_PATH" /home/boot/rootfs.gz
534 _ '%s is located in %s' 'rootfs.gz' '/home/boot'
535 else
536 _ '%s is located in %s' "$ROOTFS" "$(dirname "$ROOTFS_PATH")"
537 fi
539 separator
540 _ 'Root filesystem size: %s' "$size"
541 separator '-'
542 _n 'ENTER to continue...'; read i
543 ;;
546 format)
547 # Format a partition.
548 check_root
550 title 'Format a device'
552 DEVICE="$2"
553 label="$3"
554 fs_type="$4"
555 if [ -z "$DEVICE" ]; then
556 ask_for_device
557 check_for_device
558 else
559 _ 'Device: %s' "$DEVICE"
560 fi
561 [ -z "$fs_type" ] && get_fs_type
562 get_label
563 unmount_target_usb
564 make_fs "$fs_type"
565 separator
566 longline "$(_ 'Device %s is ready to use as LiveUSB and/or /home partition.' "$label ($DEVICE)")"
567 ;;
570 gen-liveusb)
571 # Generate a LiveUSB media using files from a LiveCD.
572 check_root
574 title 'Gen a LiveUSB media'
576 DEVICE="$2"
577 if [ -z "$DEVICE" ]; then
578 ask_for_device
579 fi
581 check_for_device
582 mount_cdrom
583 get_part_info
584 unmount_target_usb
585 install_mbr
586 set_bootable
587 mount_target_usb
588 copy_cdrom_files
589 install_boot
590 exit_or_reboot
591 ;;
594 gen-swap)
595 check_root
596 gen_swap_file
597 ;;
600 gen-iso2usb|iso2usb)
601 check_root
602 # Check if file exists
603 ISO="$2"
604 if [ -z "$ISO" -o ! -f "$ISO" ]; then
605 _ 'Please specify a valid filename on the command line.'
606 exit 1
607 fi
609 title 'Copy ISO file to SliTaz LiveUSB media'
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 ;;
630 clean)
631 check_root
632 clean_usb
633 ;;
636 usage|*)
637 # Display usage by default.
638 usage
639 ;;
640 esac
642 exit 0