tazusb view tazusb @ rev 180

Fix i18n, use libtaz i18n functions, make pot and msgmerge, add Russian translations.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Sat Aug 01 22:49:04 2015 +0300 (2015-08-01)
parents 7cdf12d25925
children 3ae895c89885
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 # Export package name for gettext.
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 # Format target device and label partition.
107 mkfs_ext3() {
108 _n 'Please specify a label for the partition (TazUSB): '
109 read label
111 [ -z "$label" ] && label='TazUSB'
113 _ 'Label: %s' "$label"
114 echo "Mkfs: mkfs.ext3 -L \"$label\" $DEVICE"
115 newline; sleep 2
116 mkfs.ext3 -L "$label" $DEVICE
117 }
120 # Get label for device
122 get_label() {
123 _n 'Please specify a label for the partition (TazUSB): '
124 read label
125 [ -z "$label" ] && label='TazUSB'
126 }
129 # Get fs type. Supported fs are ext3, ext2, fat32
131 get_fs_type() {
132 _n 'Please specify a filesystem type ext2, ext3 or fat32 (ext3): '
133 read fs_type
134 [ -z "$fs_type" ] && fs_type='ext3'
135 }
138 # We can chose the filesystem type.
140 ask_for_fs_type() {
141 _ 'Please specify the filesystem type to %s.' "$COMMAND"
142 _ 'Available formats are ext3(default), ext2 or fat32.'
143 _ 'Press enter to keep the default value.'
144 newline
145 _n 'File system type: '; read answer
146 if [ -z "$answer" ]; then
147 FS_TYPE='ext3'
148 else
149 FS_TYPE="$answer"
150 fi
151 }
154 # Format target device in ext3, ext2 or fat32.
155 # Usage: make_fs ext2|fat32|ext3 (default)
157 make_fs() {
158 local answer
160 FS=$(echo $fs_type | tr '[A-Z]' '[a-z]')
161 case "$FS" in
162 ext3|ext2)
163 newline; _ 'Processing...'
164 _ 'Label: %s' "$label"
165 echo "Mkfs: mkfs.$FS -L \"$label\" $DEVICE"
166 newline; sleep 2
167 mkfs.$FS -L "$label" $DEVICE > $LOG 2>&1
168 ;;
169 fat32)
170 if [ -x '/sbin/mkdosfs' ];then
171 newline; _ 'Processing...'
172 _ 'Label: %s' "$label"
173 echo "Mkfs: mkdosfs -F 32 -n \"$label\" $DEVICE"
174 newline; sleep 2
175 mkdosfs -F 32 -n "$label" $DEVICE
176 else
177 _ "Can't find %s tool." 'mkdosfs'
178 _n 'Would you like to install %s from repository [y/N]? ' 'dosfstools'
179 read answer
180 case "$answer" in
181 y|Y)
182 yes | tazpkg get-install dosfstools && make_fs fat32;;
183 *)
184 exit 1 ;;
185 esac
186 fi
187 ;;
188 *)
189 _ 'Sorry. Filesystem %s is not supported.' "$FS"
190 exit
191 ;;
192 esac
193 }
196 # Mount an existing USB device.
198 unmount_target_usb() {
199 # If mount point is in use, unmount
200 if mount | grep -q $TARGET_ROOT; then
201 umount $TARGET_ROOT
202 fi
204 # Device could be mounted elsewhere, so unmount
205 if mount | grep -q $DEVICE; then
206 _ 'Unmounting USB target device...'
207 umount $DEVICE
208 fi
209 }
212 # Mount an existing USB device.
214 mount_target_usb() {
215 _ 'Mounting USB target device...'
216 mkdir -p $TARGET_ROOT
217 mount $DEVICE $TARGET_ROOT 2>/dev/null
218 }
221 # Mount SliTaz LiveCD to get needed files.
223 mount_cdrom() {
224 _ 'Mounting CD-ROM device...'
226 if mount | grep /media/cdrom; then
227 umount /media/cdrom
228 fi
230 mkdir -p /media/cdrom
231 mount -r -t iso9660 $CDROM /media/cdrom 2>/dev/null
233 if [ ! -f /media/cdrom/boot/rootfs.gz -a \
234 ! -f /media/cdrom/boot/rootfs1.gz ]; then
235 newline
236 longline "$(_ 'Unable to mount CD-ROM or to find a filesystem on it (%s).' 'rootfs.gz')"
237 exit 0
238 fi
239 }
242 # Mount SliTaz ISO to get needed files.
244 mount_iso() {
245 if mount | grep /media/cdrom ; then
246 umount /media/cdrom
247 fi
249 test -d /media/cdrom || mkdir -p /media/cdrom
251 # Add support to other distros.
252 if [ ! -f /etc/slitaz-version ]; then
253 OPTIONS='-o loop'
254 else
255 OPTIONS=''
256 fi
258 _ 'Mounting %s...' "$(basename $ISO)"
259 mount $OPTIONS $ISO /media/cdrom 2>/dev/null
261 if [ ! -f /media/cdrom/boot/rootfs.gz -a \
262 ! -f /media/cdrom/boot/rootfs1.gz ]; then
263 longline "$(_ 'Unable to mount ISO or to find a filesystem on it (%s).' 'rootfs.gz')"
264 exit 0
265 fi
266 }
269 # All needed files are in the boot directory of the CD-ROM.
271 copy_cdrom_files() {
272 _n 'Copying needed files from CD-ROM...'
273 mkdir -p $TARGET_ROOT/boot
274 cp /media/cdrom/boot/bzImage* $TARGET_ROOT/boot
275 cp /media/cdrom/boot/rootfs*.gz* $TARGET_ROOT/boot
276 cp /media/cdrom/boot/memtest $TARGET_ROOT/boot 2>/dev/null
277 cp /media/cdrom/boot/*pxe $TARGET_ROOT/boot 2>/dev/null
278 status
279 }
282 install_mbr() {
283 # MBR
284 DISK=${DEVICE%[1-99]}
285 if [ -f /usr/share/boot/mbr.bin ]; then
286 _n 'Installing a new MBR to %s' "$DISK"
287 cat /usr/share/boot/mbr.bin > $DISK
288 status
289 else
290 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
291 _ 'No new MBR installed to %s' "$DISK"
292 fi
293 }
296 # ext/syslinux install
298 install_boot() {
299 # Decide if we're installing syslinux or extlinux
300 if [ "$FSTYPE" == 'vfat' ]; then
301 ST='syslinux'
302 STC="syslinux --install -d /boot/syslinux/ $DEVICE"
303 STE='cfg'
304 else
305 ST='extlinux'
306 STC="extlinux --install $TARGET_ROOT/boot/$ST"
307 STE='conf'
308 fi
310 _ 'Installing bootloader: %s' "$ST"
311 mkdir -p $TARGET_ROOT/boot/$ST
312 $STC
314 # Use existing isolinux.cfg for extlinux.conf or syslinux.cfg
315 cp /media/cdrom/boot/isolinux/isolinux.cfg $TARGET_ROOT/boot/$ST/$ST.$STE
317 # Update DVD autoinstall
318 sed -i "s/LABEL=packages-[^,]*/UUID=$UUID/g" $(grep -l append $TARGET_ROOT/boot/$ST/*)
320 # Add home=$UUID to kernel line in extlinux or syslinux.cfg
321 sed -i -e "s/\(root=.*\)/\1 home=$UUID/" $(grep -l append $TARGET_ROOT/boot/$ST/*)
323 # Splash screen and help files.
324 cp /media/cdrom/boot/isolinux/splash.* $TARGET_ROOT/boot/$ST
325 cp /media/cdrom/boot/isolinux/*.cfg $TARGET_ROOT/boot/$ST
326 cp /media/cdrom/boot/isolinux/*.kbd $TARGET_ROOT/boot/$ST
327 cp /media/cdrom/boot/isolinux/*.c32 $TARGET_ROOT/boot/$ST
328 sed -i -e s/'SliTaz GNU\/Linux'/'SliTaz GNU\/Linux LiveUSB'/ \
329 -e "s/isolinux/$ST/" $TARGET_ROOT/boot/$ST/$ST.$STE
330 }
333 # Let user exit or reboot.
335 exit_or_reboot() {
336 separator
337 newline
338 longline "$(_n 'Do you want to exit Tazusb or reboot system (Exit/reboot)? ')"
339 read answer
340 if [ "$answer" == 'reboot' ]; then
341 unmount_target_usb
342 reboot || reboot -f
343 else
344 unmount_target_usb
345 newline; exit 0
346 fi
347 }
350 set_bootable() {
351 # As the boot flag is toggable, need to check it before hand
352 DISK=${DEVICE%[1-99]}
353 ISSET="$(fdisk -l $DISK | grep $DEVICE | grep "\*")"
355 # If not set, set bootable
356 if [ -z "$ISSET" ]; then
357 umount $TARGET_ROOT 2>/dev/null
358 _n 'Setting %s as bootable...' "$DEVICE"
359 fdisk $DISK >/dev/null << EOF
360 a
361 1
362 w
363 EOF
364 status
365 fi
366 }
369 # Generate a virtual swap file in /home/swap. SliTaz boot scripts
370 # will activate it, useful for low memory systems
372 gen_swap_file() {
373 newline
374 boldify "$(_n 'Gen swap')"
375 separator
376 longline "$(_ "Generate a swap file in %s that will be activated on each \
377 boot to have more memory available (empty value to exit)." '/home/swap')"
378 newline
379 _n 'Swap file in MB: '
380 read size
381 if [ -z "$size" ]; then
382 _ 'Empty value. Exiting...'
383 exit 0
384 fi
385 cd /home
386 # Sanity check
387 if [ -f swap ]; then
388 swapoff swap 2>/dev/null
389 fi
390 # DD to gen a virtual disk.
391 dd if=/dev/zero of=swap bs=1M count=$size
392 # Make swap filesystem.
393 mkswap swap
394 swapon swap
395 newline
396 }
399 # Clean out old backups to save disk space
401 clean_usb() {
402 newline
403 boldify "$(_n 'Clean')"
404 separator
405 longline "$(_n 'Remove old %s backup filesystems to free up disk space.' \
406 'rootfs.gz.unixtimestamp')"
407 newline
408 # Locate and interactively remove old filesystems from /home directory
409 for file in $(find /home/boot/rootfs.gz.[0-9]*); do
410 _n 'Do you wish to remove: %s (Yes/no/exit)? ' "$(basename $file)"
411 read answer
412 case $answer in
413 e|E|"exit"|Exit)
414 exit 0 ;;
415 y|Y|yes|Yes)
416 rm -f $file ;;
417 *)
418 _ 'No filesystems selected, exiting...' ;;
419 esac
420 done
421 }
424 #
425 # Tazusb sequence
426 #
428 case $COMMAND in
429 writefs)
430 # Writefs to rootfs.gz
431 check_root
432 if [ -z "$2" ]; then
433 COMPRESSION='none'
434 else
435 COMPRESSION="$2"
436 fi
437 # Start info
438 title 'Write filesystem'
440 longline "$(_ "The command writefs will write all the current \
441 filesystem into a suitable cpio archive (%s) usable on a bootable \
442 LiveUSB media." 'rootfs.gz')"
443 newline
444 _ 'Archive compression: %s' "$(colorize 36 "$COMPRESSION")"
446 # Clear out tazpkg cache
447 rm /var/lib/tazpkg/*.bak /var/cache/tazpkg/* -r -f
449 # Optionally remove sound card selection and screen resolution.
450 _ 'Do you wish to remove the sound card and screen configs?'
451 _n 'Press ENTER to keep or answer (No|yes|exit): '
452 read anser
453 case $anser in
454 e|E|"exit"|Exit)
455 exit 0 ;;
456 y|Y|yes|Yes)
457 _n 'Removing current sound card and screen configurations...'
458 rm -f /var/lib/sound-card-driver
459 rm -f /var/lib/alsa/asound.state
460 rm -f /etc/X11/xorg.conf ;;
461 *)
462 _n 'Keeping current sound card and screen configurations...' ;;
463 esac
464 status
465 newline
467 # Optionally remove i18n settings
468 _ 'Do you wish to remove local/keymap settings?'
469 _n 'Press ENTER to keep or answer (No|yes|exit): '
470 read anser
471 case $anser in
472 e|E|"exit"|Exit)
473 exit 0 ;;
474 y|Y|yes|Yes)
475 _n 'Removing current locale/keymap settings...'
476 echo > /etc/locale.conf
477 echo > /etc/keymap.conf ;;
478 *)
479 _n 'Keeping current locale/keymap settings...'
480 grep -qs '^INCLUDE i18n.cfg' /home/boot/*linux/*linux.c* &&
481 sed -i 's/^INCLUDE i18n.cfg/# &/' /home/boot/*linux/*linux.c* ;;
482 esac
483 status
485 # Clean-up files by default
486 mv -f /var/log/wtmp /tmp/tazusb-wtmp
487 touch /var/log/wtmp
488 echo > /etc/udev/rules.d/70-persistent-net.rules
489 echo > /etc/udev/rules.d/70-persistant-cd.rules
491 # Create list of files
492 # find / -xdev | sed '/^\/home\//d;/^\/tmp\//d' >/tmp/list
493 # for dev in console null tty tty1
494 # do
495 # echo /dev/$dev >>/tmp/list
496 # done
498 find /bin /etc /init /sbin /var /dev /lib /root /usr /opt >/tmp/list
500 for dir in /home /proc /run /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
501 do
502 echo $dir >>/tmp/list
503 done
504 sed -i '/^\/var\/run\/.*pid$/d' /tmp/list
506 for removelog in auth boot messages dmesg daemon utmp slim Xorg ; do
507 sed -i "\/var\/log\/$removelog/d" /tmp/list
508 done
510 # Generate initramfs with specified compression
511 if [ "$COMPRESSION" == 'lzma' ]; then
512 _n 'Creating %s with lzma compression... ' 'rootfs.gz'
513 cpio -o -H newc | lzma e -si -so > /rootfs.gz
515 elif [ "$COMPRESSION" == 'gzip' ]; then
516 _n 'Creating %s with gzip compression... ' 'rootfs.gz'
517 cpio -o -H newc | gzip -9 > /rootfs.gz
519 else
520 _n 'Creating %s without compression... ' 'rootfs.gz'
521 cpio -o -H newc > /rootfs.gz
522 fi < /tmp/list
524 mv -f /tmp/tazusb-wtmp /var/log/wtmp
526 # Get initramfs size
527 size=$(du -sh /rootfs.gz | cut -f1)
529 # If the bootable medium is where it should be, copy across
530 if (test -e /home/boot/bzImage); then
531 longline "$(_ 'Moving %s to media. Remember to unmount for delayed writes!' 'rootfs.gz')"
533 # Move the old filesystem with the unix timestamp for reference
534 if (test -e /home/boot/previous.gz); then
535 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
536 fi
538 mv /home/boot/rootfs.gz /home/boot/previous.gz
539 mv /rootfs.gz /home/boot/.
540 else
541 _ '%s is located in /' 'rootfs.gz'
542 fi
544 separator
545 _ 'Root filesystem size: %s' "$size"
546 separator '-'
547 _n 'ENTER to continue...'; read i
548 ;;
551 format)
552 # Format a partition.
553 check_root
555 title 'Format a device'
557 DEVICE="$2"
558 label="$3"
559 fs_type="$4"
560 if [ -z "$DEVICE" ]; then
561 ask_for_device
562 check_for_device
563 else
564 _ 'Device: %s' "$DEVICE"
565 fi
566 [ -z "$fs_type" ] && get_fs_type
567 get_label
568 unmount_target_usb
569 make_fs "$fs_type"
570 # mkfs_ext3
571 separator
572 longline "$(_ 'Device %s is ready to use as LiveUSB and/or /home partition.' "$label ($DEVICE)")"
573 ;;
576 gen-liveusb)
577 # Generate a LiveUSB media using files from a LiveCD.
578 check_root
580 title 'Gen a LiveUSB media'
582 DEVICE="$2"
583 if [ -z "$DEVICE" ]; then
584 ask_for_device
585 fi
587 check_for_device
588 mount_cdrom
589 get_part_info
590 unmount_target_usb
591 install_mbr
592 set_bootable
593 mount_target_usb
594 copy_cdrom_files
595 install_boot
596 exit_or_reboot
597 ;;
600 gen-swap)
601 check_root
602 gen_swap_file
603 ;;
606 gen-iso2usb|iso2usb)
607 check_root
608 # Check if file exists
609 ISO="$2"
610 if [ -z "$ISO" -o ! -f "$ISO" ]; then
611 _ 'Please specify a valid filename on the command line.'
612 exit 1
613 fi
615 title 'Copy ISO file to SliTaz LiveUSB media'
617 DEVICE="$3"
618 if [ -z "$DEVICE" ]; then
619 ask_for_device
620 fi
621 check_for_device
622 mount_iso
623 get_part_info
624 unmount_target_usb
625 install_mbr
626 set_bootable
627 mount_target_usb
628 copy_cdrom_files
629 install_boot
630 umount /media/cdrom
631 losetup -d /dev/loop0
632 exit_or_reboot
633 ;;
636 clean)
637 check_root
638 clean_usb
639 ;;
642 usage|*)
643 # Display usage by default.
644 usage
645 ;;
646 esac
648 exit 0