tazusb view tazusb @ rev 119

Fix echo in tazusb and fix fr.po file
author Christophe Lincoln <pankso@slitaz.org>
date Wed Mar 07 14:05:07 2012 +0100 (2012-03-07)
parents 22f3a9b70ec8
children bee939ce59f7
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) 2012 SliTaz GNU/Linux - GNU gpl v2
8 #
9 # Authors: see AUTHORS file
10 #
11 VERSION=4.0.1
13 # Include gettext helper script.
14 . /usr/bin/gettext.sh
16 # Export package name for gettext.
17 TEXTDOMAIN='tazusb'
18 export TEXTDOMAIN
20 COMMAND=$1
21 TARGET_ROOT=/media/flash
22 DRIVE_NAME=`cat /proc/sys/dev/cdrom/info | grep "drive name" | cut -f 3`
23 CDROM=/dev/$DRIVE_NAME
24 LOG=/tmp/$(basename $0).log
26 #
27 # Tazusb functions
28 #
30 source /usr/lib/slitaz/libtaz
31 source_lib commons
33 # Print the usage.
34 usage ()
35 {
36 echo -e "`gettext \"SliTaz Live USB - Version:\"` $VERSION\n
37 \033[1m`gettext \"Usage: \"`\033[0m `basename $0` [command] [compression|device]
38 \033[1m`gettext \"Commands:\"`\033[0m\n
39 usage `gettext \"Print this short usage.\"`
40 writefs `gettext \"Write the current filesystem to rootfs.gz.
41 Supported compression: lzma. gzip, none.\"`
42 format `gettext \"Format and label device with ext3, ext2 or fat32 filesystem
43 (for LiveUSB or /home). Default is ext3.\"`
44 gen-liveusb `gettext \"Generate a bootable LiveUSB using files from the LiveCD.\"`
45 gen-swap `gettext \"Create or recreate and activate additional swap memory.\"`
46 gen-iso2usb `gettext \"Generate a bootable LiveUSB using files from ISO file.\"`
47 clean `gettext \"Remove old backup filesystems to free disk space.\"`\n"
48 }
50 # Display a list of available partitions.
51 fdisk_list()
52 {
53 echo "----"
54 fdisk -l | grep ^/dev/sd*
55 echo "----"
56 }
58 # We need a USB media to install.
59 ask_for_device()
60 {
61 gettext "\
62 Please specify the target USB device to $COMMAND. You can type 'list' to
63 get a list of devices, type 'exit' or give an empty value to exit.
65 Device to use : "; read anser
66 while [ "$anser" == "list" ]; do
67 fdisk_list
68 gettext "Device to use : "; read anser
69 done
70 if [ "$anser" = "" -o "$anser" = "exit" ]; then
71 echo ""
72 gettext "No specified device or exit."
73 echo ""
74 exit 0
75 else
76 DEVICE=$anser
77 fi
78 }
80 # Verify a device exists before format or install
81 check_for_device()
82 {
83 IFDEV=`fdisk -l | grep $DEVICE`
84 if [ -z "$IFDEV" ]; then
85 echo ""
86 gettext "Unable to find device: $DEVICE"
87 echo ""
88 exit 0
89 fi
90 }
92 # gets the UUID and filesystem type
93 get_part_info()
94 {
95 UUID=`blkid -s UUID -o value $DEVICE`
96 FSTYPE=`blkid -s TYPE -o value $DEVICE`
97 }
99 # Format target device and label partition.
100 mkfs_ext3()
101 {
102 gettext "Please specify a label for the partition (TazUSB): "
103 read label
105 if [ -z $label ]; then
106 label=TazUSB
107 fi
109 gettext "Label : $label"; echo ""
110 gettext "Mkfs : mkfs.ext3 -L \"$label\" $DEVICE" ; echo ""
111 echo "" && sleep 2
112 mkfs.ext3 -L "$label" $DEVICE
114 }
116 # Get label for device
117 get_label()
118 {
119 gettext "Please specify a label for the partition (TazUSB): "
120 read label
122 if [ -z $label ]; then
123 label=TazUSB
124 fi
125 }
127 # Get fs type. Supported fs are ext3, ext2, fat32
128 get_fs_type()
129 {
130 gettext "Please specify a filesystem type ext2, ext3 or fat32 (ext3): "
131 read fs_type
133 if [ -z $fs_type ]; then
134 fs_type=ext3
135 fi
136 }
138 # We can chose the filesystem type.
139 ask_for_fs_type()
140 {
141 gettext "\
142 Please specify the filesystem type to $COMMAND.
143 Available formats are ext3(default), ext2 or fat32.
144 Press enter to keep the default value.
146 File system type : "; read anser
147 if [ "$anser" = "" ]; then
148 FS_TYPE=ext3
149 else
150 FS_TYPE=$anser
151 fi
152 }
154 # Format target device in ext3, ext2 or fat32.
155 # Usage: make_fs ext2|fat32|ext3 (default)
156 make_fs()
157 {
158 local answer
160 FS=$(echo $fs_type | tr [A-Z] [a-z])
161 case "$FS" in
162 ext3|ext2)
163 echo ""; gettext "Processing..." ; echo ""
164 gettext "Label : $label" ; echo ""
165 gettext "Mkfs : mkfs.$FS -L \"$label\" $DEVICE"
166 echo "" && sleep 2
167 mkfs.$@ -L "$label" $DEVICE > $LOG 2>&1
168 ;;
169 [Ff]at32)
170 if [ -x /sbin/mkdosfs ];then
171 echo "" ; gettext "Processing..."; echo ""
172 gettext "Label : $label" ; echo ""
173 gettext "Mkfs : mkdosfs -F 32 -n \"$label\" $DEVICE"; echo ""
174 echo "" && sleep 2
175 mkdosfs -F 32 -n "$label" $DEVICE
176 else
177 gettext "Can't find mkdosfs tool.\nWould you like to install dosfstools from repository [y/N] ? "; read answer
178 case $answer in
179 y|Y)
180 yes | tazpkg get-install dosfstools && make_fs fat32;;
181 *)
182 exit 1 ;;
183 esac
184 fi
185 ;;
186 *)
187 gettext "Sorry. Filesystem $FS is not supported."; echo ""
188 exit
189 esac
190 }
192 # Mount an existing USB device.
193 unmount_target_usb()
194 {
195 # If mount point is in use, unmount
196 if mount | grep -q $TARGET_ROOT; then
197 umount $TARGET_ROOT
198 fi
200 # Device could be mounted elsewhere, so unmount
201 if mount | grep -q $DEVICE; then
202 gettext "Unmounting USB target device..."; echo ""
203 umount $DEVICE
204 fi
205 }
207 # Mount an existing USB device.
208 mount_target_usb()
209 {
210 gettext "Mounting USB target device..." ; echo ""
211 mkdir -p $TARGET_ROOT
212 mount $DEVICE $TARGET_ROOT 2>/dev/null
213 }
215 # Mount SliTaz LiveCD to get needed files.
216 mount_cdrom()
217 {
218 gettext "Mounting cdrom device..."; echo ""
220 if mount | grep /media/cdrom; then
221 umount /media/cdrom
222 fi
224 mkdir -p /media/cdrom
225 mount -r -t iso9660 $CDROM /media/cdrom 2>/dev/null
227 if [ ! -f /media/cdrom/boot/rootfs.gz -a \
228 ! -f /media/cdrom/boot/rootfs1.gz ]; then
229 echo ""; gettext "Unable to mount cdrom or to find a filesystem on it (rootfs.gz)."; echo ""
230 exit 0
231 fi
232 }
234 # Mount SliTaz ISO to get needed files.
235 mount_iso()
236 {
237 if mount | grep /media/cdrom ; then
238 umount /media/cdrom
239 fi
241 test -d /media/cdrom || mkdir -p /media/cdrom
243 # Add support to other distros.
244 if [ ! -f /etc/slitaz-version ]; then
245 OPTIONS="-o loop"
246 else
247 OPTIONS=""
248 fi
250 gettext "Mounting "
251 echo "`basename $ISO`..."; echo ""
252 mount $OPTIONS $ISO /media/cdrom 2>/dev/null
254 if [ ! -f /media/cdrom/boot/rootfs.gz -a \
255 ! -f /media/cdrom/boot/rootfs1.gz ]; then
256 gettext "Unable to mount iso or to find a filesystem on it (rootfs.gz)."; echo ""
257 exit 0
258 fi
259 }
261 # All needed files are in the boot directory of the cdrom.
262 copy_cdrom_files()
263 {
264 gettext "Copying needed files from cdrom..."
265 mkdir -p $TARGET_ROOT/boot
266 cp /media/cdrom/boot/bzImage $TARGET_ROOT/boot
267 cp /media/cdrom/boot/rootfs*.gz $TARGET_ROOT/boot
268 status
269 }
271 install_mbr()
272 {
273 # MBR
274 DISK=${DEVICE%[1-99]}
275 if [ -f /usr/share/boot/mbr.bin ]; then
276 gettext "Installing a new MBR to: $DISK"
277 cat /usr/share/boot/mbr.bin > $DISK
278 status
279 else
280 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
281 gettext "No new MBR installed to: $DISK"; echo ""
282 fi
283 }
285 # ext/syslinux install
286 install_boot()
287 {
288 # Decide if we're installing syslinux or extlinux
289 if [ "$FSTYPE" = "vfat" ]; then
290 ST=syslinux
291 STC="syslinux --install -d /boot/syslinux/ $DEVICE"
292 STE=cfg
293 else
294 ST=extlinux
295 STC="extlinux --install $TARGET_ROOT/boot/$ST"
296 STE=conf
297 fi
299 gettext "Installing bootloader: $ST"; echo ""
300 mkdir -p $TARGET_ROOT/boot/$ST
301 $STC
303 # Use existing isolinux.cfg for extlinux.conf or syslinux.cfg
304 cp /media/cdrom/boot/isolinux/isolinux.cfg $TARGET_ROOT/boot/$ST/$ST.$STE
306 # Update DVD autoinstall
307 sed -i "s/LABEL=packages-[^,]*/UUID=$UUID/g" $(grep -l append $TARGET_ROOT/boot/$ST/*)
309 # Add home= to append in extlinux or syslinux.cfg
310 sed -i -e "s/\(append.*\)/\1 home=$UUID/" $(grep -l append $TARGET_ROOT/boot/$ST/*)
312 # Splash screen and help files.
313 cp /media/cdrom/boot/isolinux/splash.* $TARGET_ROOT/boot/$ST
314 cp /media/cdrom/boot/isolinux/*.txt $TARGET_ROOT/boot/$ST
315 cp /media/cdrom/boot/isolinux/*.cfg $TARGET_ROOT/boot/$ST
316 cp /media/cdrom/boot/isolinux/*.kbd $TARGET_ROOT/boot/$ST
317 cp /media/cdrom/boot/isolinux/*.c32 $TARGET_ROOT/boot/$ST
318 sed -i -e s/'SliTaz GNU\/Linux'/'SliTaz GNU\/Linux LiveUSB'/ \
319 -e s/'isolinux'/'extlinux'/ $TARGET_ROOT/boot/$ST/$ST.$STE
320 }
322 # Let user exit or reboot.
323 exit_or_reboot()
324 {
325 echo "==============================================================================="
326 echo ""
327 gettext "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
328 read anser
329 if [ "$anser" == "reboot" ]; then
330 umount $TARGET_ROOT
331 umount /media/cdrom
332 reboot || reboot -f
333 else
334 umount $TARGET_ROOT
335 umount /media/cdrom
336 echo ""
337 exit 0
338 fi
339 }
341 set_bootable()
342 {
343 # As the boot flag is toggable, need to check it before hand
344 DISK=${DEVICE%[1-99]}
345 ISSET=`fdisk -l $DISK | grep $DEVICE | grep "\*"`
347 # If not set, set bootable
348 if [ "$ISSET" == "" ]; then
349 umount $TARGET_ROOT 2>/dev/null
350 gettext "Setting $DEVICE as bootable..."
351 fdisk $DISK >/dev/null << EOF
352 a
353 1
354 w
355 EOF
356 status
357 fi
358 }
360 # Generate a virtual swap file in /home/swap. SliTaz boot scripts
361 # will activate it, useful for low memory systems
362 gen_swap_file()
363 {
364 echo ""
365 echo -en "\033[1m`gettext \"Gen swap\"`\033[0m
366 ==============================================================================="
367 gettext "Generate a swap file in /home/swap that will be activated on each boot to have
368 more memory available (Empty value to exit).
370 Swap file in Mb : "
371 read size
372 if [ -z "$size" ]; then
373 gettext "Empty value. Exiting..."; echo ""
374 exit 0
375 fi
376 cd /home
377 # Sanity check
378 if [ -f swap ]; then
379 swapoff swap 2>/dev/null
380 fi
381 # DD to gen a virtual disk.
382 dd if=/dev/zero of=swap bs=1M count=$size
383 # Make swap filesystem.
384 mkswap swap
385 swapon swap
386 echo ""
387 }
389 # Clean out old backups to save disk space
390 clean_usb()
391 {
392 echo ""
393 echo -en "\033[1m`gettext \"Clean\"`\033[0m
394 ==============================================================================="
395 gettext "Remove old rootfs.gz.unixtimestamp backup filesystems to free up disk space."; echo -e "\n\n"
396 # Locate and interactively remove old filesystems from /home directory
397 for file in `find /home/boot/rootfs.gz.[0-9]*`
398 do
399 gettext "Do you wish to remove: `basename $file` (Yes/no/exit) ? "
400 read answer
401 case $answer in
402 e|E|"exit"|Exit)
403 exit 0 ;;
404 y|Y|yes|Yes)
405 rm -f $file ;;
406 *)
407 gettext "No filesystems selected, exiting..." ; echo "" ;;
408 esac
409 done
410 }
412 #
413 # Tazusb sequence
414 #
416 case $COMMAND in
417 writefs)
418 # Writefs to rootfs.gz
419 check_root
420 if [ -z $2 ]; then
421 COMPRESSION=none
422 else
423 COMPRESSION=$2
424 fi
425 # Start info
426 echo ""
427 echo -e "\n\033[1m`gettext "Write filesystem"`\033[0m
428 ==============================================================================="
429 gettext "The command writefs will write all the current filesystem into a suitable cpio
430 archive (rootfs.gz) usable on a bootable LiveUSB media.
432 Archive compression: $COMPRESSION"
433 echo ""
435 # Clear out tazpkg cache
436 rm /var/cache/tazpkg/* -r -f
438 # Optionally remove sound card selection
439 gettext "Do you wish to remove the sound card selection (No/yes/exit) ? "
440 read anser
441 case $anser in
442 e|E|"exit"|Exit)
443 exit 0 ;;
444 y|Y|yes|Yes)
445 gettext "Removing current sound card selection..."
446 rm -f /var/lib/sound-card-driver
447 rm -f /var/lib/alsa/asound.state ;;
448 *)
449 gettext "Keeping current sound card selection..." ;;
450 esac
451 status
452 # Optionally remove screen resolution
453 gettext "Do you wish to remove the screen resolution (No/yes/exit) ? "
454 read anser
455 case $anser in
456 e|E|"exit"|Exit)
457 exit 0 ;;
458 y|Y|yes|Yes)
459 gettext "Removing current screen resolution..."
460 rm -f /etc/X11/screen.conf ;;
461 *)
462 gettext "Keeping current screen resolution..." ;;
463 esac
464 status
466 # Create list of files
467 find /bin /etc /init /sbin /var /dev /lib /root /usr >/tmp/list
469 for dir in /home /proc /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
470 do
471 echo $dir >>/tmp/list
472 done
474 # Generate initramfs with specified compression
475 if [ "$COMPRESSION" = "lzma" ]; then
476 gettext "Creating rootfs.gz with lzma compression... "
477 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
479 elif [ "$COMPRESSION" = "gzip" ]; then
480 gettext "Creating rootfs.gz with gzip compression... "
481 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
483 else
484 gettext "Creating rootfs.gz without compression... "
485 cat /tmp/list | cpio -o -H newc > /rootfs.gz
486 fi
488 # Get initramfs size
489 size=`du -sh /rootfs.gz | cut -f 1`
491 # If the bootable medium is where it should be, copy across
492 if (test -e /home/boot/bzImage); then
493 gettext "Moving rootfs.gz to media. Remember to unmount for delayed writes!"; echo ""
495 # Move the old filesystem with the unix timestamp for reference
496 if (test -e /home/boot/previous.gz); then
497 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
498 fi
500 mv /home/boot/rootfs.gz /home/boot/previous.gz
501 mv /rootfs.gz /home/boot/.
502 else
503 gettext "rootfs.gz is located in /"; echo ""
504 fi
506 echo "==============================================================================="
507 gettext "Root filesystem size: $size"; echo ""
508 echo ""
509 echo "----"
510 gettext "ENTER to continue..."; read i
511 ;;
512 format)
513 # Format a partition.
514 check_root
515 echo ""
516 echo -e "\033[1m`gettext \"Format a device\"`\033[0m"
517 echo "==============================================================================="
518 DEVICE=$2
519 label=$3
520 fs_type=$4
521 if [ -z $DEVICE ]; then
522 ask_for_device
523 check_for_device
524 else
525 echo "Device : $DEVICE"
526 fi
527 test -z $fs_type && get_fs_type
528 get_label
529 unmount_target_usb
530 make_fs "$fs_type"
531 # mkfs_ext3
532 echo "==============================================================================="
533 gettext "Device $label ($DEVICE) is ready to use as LiveUSB and/or /home partition."
534 echo ""
535 ;;
536 gen-liveusb)
537 # Generate a LiveUSB media using files from a LiveCD.
538 check_root
539 echo ""
540 echo -e "\033[1m`gettext \"Gen a LiveUSB media\"`\033[0m"
541 echo "==============================================================================="
542 DEVICE=$2
543 if [ -z $DEVICE ]; then
544 ask_for_device
545 fi
547 check_for_device
548 mount_cdrom
549 get_part_info
550 unmount_target_usb
551 install_mbr
552 set_bootable
553 mount_target_usb
554 copy_cdrom_files
555 install_boot
556 exit_or_reboot
557 ;;
558 gen-swap)
559 check_root
560 gen_swap_file
561 ;;
562 gen-iso2usb|iso2usb)
563 check_root
565 # Check if file exists
566 ISO=$2
567 if [ -z $ISO ] || [ ! -f $ISO ]; then
568 gettext "Please specify a valid filename on the command line."; echo ""
569 exit 1
570 fi
571 echo ""
572 echo -e "\033[1m`gettext \"Copy ISO file to SliTaz LiveUSB media\"`\033[0m"
573 echo "==============================================================================="
574 echo ""
575 DEVICE=$3
576 if [ -z $DEVICE ]; then
577 ask_for_device
578 fi
579 check_for_device
580 mount_iso
581 get_part_info
582 unmount_target_usb
583 install_mbr
584 set_bootable
585 mount_target_usb
586 copy_cdrom_files
587 install_boot
588 umount /media/cdrom
589 losetup -d /dev/loop0
590 exit_or_reboot
591 ;;
592 clean)
593 check_root
594 clean_usb
595 ;;
596 usage|*)
597 # Display usage by default.
598 usage
599 ;;
600 esac
602 exit 0