tazusb view tazusb @ rev 153

Tiny edits
author Paul Issott <paul@slitaz.org>
date Sun Feb 23 10:50:02 2014 +0000 (2014-02-23)
parents c5bb97d97d94
children b158b5aefa5d
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.2.2
13 . /lib/libtaz.sh
15 # Include gettext helper script.
16 . /usr/bin/gettext.sh
18 # Export package name for gettext.
19 TEXTDOMAIN='tazusb'
20 export TEXTDOMAIN
22 COMMAND=$1
23 TARGET_ROOT=/media/flash
24 DRIVE_NAME=$(cat /proc/sys/dev/cdrom/info | grep "drive name" | cut -f 3)
25 CDROM=/dev/$DRIVE_NAME
26 LOG=/tmp/$(basename $0).log
28 #
29 # Tazusb functions
30 #
32 # Print the usage.
33 usage ()
34 {
35 echo -e "`gettext \"SliTaz Live USB - Version:\"` $VERSION\n
36 \033[1m`gettext \"Usage: \"`\033[0m `basename $0` [command] [compression|device]
37 \033[1m`gettext \"Commands:\"`\033[0m\n
38 usage `gettext \"Print this short usage.\"`
39 writefs `gettext \"Write the current filesystem to rootfs.gz.
40 Supported compression: lzma. gzip, none.\"`
41 format `gettext \"Format and label device with ext3, ext2 or fat32 filesystem
42 (for LiveUSB or /home). Default is ext3.\"`
43 gen-liveusb `gettext \"Generate a bootable LiveUSB using files from the LiveCD.\"`
44 gen-swap `gettext \"Create or recreate and activate additional swap memory.\"`
45 gen-iso2usb `gettext \"Generate a bootable LiveUSB using files from ISO file.\"`
46 clean `gettext \"Remove old backup filesystems to free disk space.\"`\n"
47 }
49 # Display a list of available partitions.
50 fdisk_list()
51 {
52 echo "----"
53 fdisk -l | grep ^/dev/sd*
54 echo "----"
55 }
57 # We need a USB media to install.
58 ask_for_device()
59 {
60 gettext "\
61 Please specify the target USB device to $COMMAND. You can type 'list' to
62 get a list of devices, type 'exit' or give an empty value to exit.
64 Device to use: "; read anser
65 while [ "$anser" == "list" ]; do
66 fdisk_list
67 gettext "Device to use: "; read anser
68 done
69 if [ "$anser" = "" -o "$anser" = "exit" ]; then
70 echo ""
71 gettext "No specified device or exit."
72 echo ""
73 exit 0
74 else
75 DEVICE=$anser
76 fi
77 }
79 # Verify a device exists before format or install
80 check_for_device()
81 {
82 IFDEV=`fdisk -l | grep $DEVICE`
83 if [ -z "$IFDEV" ]; then
84 echo ""
85 gettext "Unable to find device: $DEVICE"
86 echo ""
87 exit 0
88 fi
89 }
91 # gets the UUID and filesystem type
92 get_part_info()
93 {
94 UUID=`blkid -s UUID -o value $DEVICE`
95 FSTYPE=`blkid -s TYPE -o value $DEVICE`
96 }
98 # Format target device and label partition.
99 mkfs_ext3()
100 {
101 gettext "Please specify a label for the partition (TazUSB): "
102 read label
104 if [ -z $label ]; then
105 label=TazUSB
106 fi
108 gettext "Label : $label"; echo ""
109 gettext "Mkfs : mkfs.ext3 -L \"$label\" $DEVICE" ; echo ""
110 echo "" && sleep 2
111 mkfs.ext3 -L "$label" $DEVICE
112 }
114 # Get label for device
115 get_label()
116 {
117 gettext "Please specify a label for the partition (TazUSB): "
118 read label
119 if [ -z $label ]; then
120 label=TazUSB
121 fi
122 }
124 # Get fs type. Supported fs are ext3, ext2, fat32
125 get_fs_type()
126 {
127 gettext "Please specify a filesystem type ext2, ext3 or fat32 (ext3): "
128 read fs_type
129 if [ -z $fs_type ]; then
130 fs_type=ext3
131 fi
132 }
134 # We can chose the filesystem type.
135 ask_for_fs_type()
136 {
137 gettext "\
138 Please specify the filesystem type to $COMMAND.
139 Available formats are ext3(default), ext2 or fat32.
140 Press enter to keep the default value.
142 File system type: "; read anser
143 if [ "$anser" = "" ]; then
144 FS_TYPE=ext3
145 else
146 FS_TYPE=$anser
147 fi
148 }
150 # Format target device in ext3, ext2 or fat32.
151 # Usage: make_fs ext2|fat32|ext3 (default)
152 make_fs()
153 {
154 local answer
156 FS=$(echo $fs_type | tr [A-Z] [a-z])
157 case "$FS" in
158 ext3|ext2)
159 echo ""; gettext "Processing..." ; echo ""
160 gettext "Label : $label" ; echo ""
161 gettext "Mkfs : mkfs.$FS -L \"$label\" $DEVICE"
162 echo "" && sleep 2
163 mkfs.$@ -L "$label" $DEVICE > $LOG 2>&1
164 ;;
165 [Ff]at32)
166 if [ -x /sbin/mkdosfs ];then
167 echo "" ; gettext "Processing..."; echo ""
168 gettext "Label : $label" ; echo ""
169 gettext "Mkfs : mkdosfs -F 32 -n \"$label\" $DEVICE"; echo ""
170 echo "" && sleep 2
171 mkdosfs -F 32 -n "$label" $DEVICE
172 else
173 gettext "Can't find mkdosfs tool.\nWould you like to install dosfstools from repository [y/N] ? "; read answer
174 case $answer in
175 y|Y)
176 yes | tazpkg get-install dosfstools && make_fs fat32;;
177 *)
178 exit 1 ;;
179 esac
180 fi
181 ;;
182 *)
183 gettext "Sorry. Filesystem $FS is not supported."; echo ""
184 exit
185 esac
186 }
188 # Mount an existing USB device.
189 unmount_target_usb()
190 {
191 # If mount point is in use, unmount
192 if mount | grep -q $TARGET_ROOT; then
193 umount $TARGET_ROOT
194 fi
196 # Device could be mounted elsewhere, so unmount
197 if mount | grep -q $DEVICE; then
198 gettext "Unmounting USB target device..."; echo ""
199 umount $DEVICE
200 fi
201 }
203 # Mount an existing USB device.
204 mount_target_usb()
205 {
206 gettext "Mounting USB target device..." ; echo ""
207 mkdir -p $TARGET_ROOT
208 mount $DEVICE $TARGET_ROOT 2>/dev/null
209 }
211 # Mount SliTaz LiveCD to get needed files.
212 mount_cdrom()
213 {
214 gettext "Mounting cdrom device..."; echo ""
216 if mount | grep /media/cdrom; then
217 umount /media/cdrom
218 fi
220 mkdir -p /media/cdrom
221 mount -r -t iso9660 $CDROM /media/cdrom 2>/dev/null
223 if [ ! -f /media/cdrom/boot/rootfs.gz -a \
224 ! -f /media/cdrom/boot/rootfs1.gz ]; then
225 echo ""; gettext "Unable to mount cdrom or to find a filesystem on it (rootfs.gz)."; echo ""
226 exit 0
227 fi
228 }
230 # Mount SliTaz ISO to get needed files.
231 mount_iso()
232 {
233 if mount | grep /media/cdrom ; then
234 umount /media/cdrom
235 fi
237 test -d /media/cdrom || mkdir -p /media/cdrom
239 # Add support to other distros.
240 if [ ! -f /etc/slitaz-version ]; then
241 OPTIONS="-o loop"
242 else
243 OPTIONS=""
244 fi
246 gettext "Mounting "
247 echo "`basename $ISO`..."; echo ""
248 mount $OPTIONS $ISO /media/cdrom 2>/dev/null
250 if [ ! -f /media/cdrom/boot/rootfs.gz -a \
251 ! -f /media/cdrom/boot/rootfs1.gz ]; then
252 gettext "Unable to mount iso or to find a filesystem on it (rootfs.gz)."; echo ""
253 exit 0
254 fi
255 }
257 # All needed files are in the boot directory of the cdrom.
258 copy_cdrom_files()
259 {
260 gettext "Copying needed files from cdrom..."
261 mkdir -p $TARGET_ROOT/boot
262 cp /media/cdrom/boot/bzImage $TARGET_ROOT/boot
263 cp /media/cdrom/boot/rootfs*.gz $TARGET_ROOT/boot
264 status
265 }
267 install_mbr()
268 {
269 # MBR
270 DISK=${DEVICE%[1-99]}
271 if [ -f /usr/share/boot/mbr.bin ]; then
272 gettext "Installing a new MBR to: $DISK"
273 cat /usr/share/boot/mbr.bin > $DISK
274 status
275 else
276 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
277 gettext "No new MBR installed to: $DISK"; echo ""
278 fi
279 }
281 # ext/syslinux install
282 install_boot()
283 {
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 gettext "Installing bootloader: $ST"; echo ""
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'/'extlinux'/ $TARGET_ROOT/boot/$ST/$ST.$STE
315 }
317 # Let user exit or reboot.
318 exit_or_reboot()
319 {
320 separator
321 echo ""
322 gettext "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
323 read anser
324 if [ "$anser" == "reboot" ]; then
325 unmount_target_usb
326 reboot || reboot -f
327 else
328 unmount_target_usb
329 echo "" && exit 0
330 fi
331 }
333 set_bootable()
334 {
335 # As the boot flag is toggable, need to check it before hand
336 DISK=${DEVICE%[1-99]}
337 ISSET=`fdisk -l $DISK | grep $DEVICE | grep "\*"`
339 # If not set, set bootable
340 if [ "$ISSET" == "" ]; then
341 umount $TARGET_ROOT 2>/dev/null
342 gettext "Setting $DEVICE as bootable..."
343 fdisk $DISK >/dev/null << EOF
344 a
345 1
346 w
347 EOF
348 status
349 fi
350 }
352 # Generate a virtual swap file in /home/swap. SliTaz boot scripts
353 # will activate it, useful for low memory systems
354 gen_swap_file()
355 {
356 echo ""
357 echo -en "\033[1m`gettext \"Gen swap\"`\033[0m
358 ================================================================================"
359 gettext "Generate a swap file in /home/swap that will be activated on each boot to have
360 more memory available (Empty value to exit).
362 Swap file in Mb : "
363 read size
364 if [ -z "$size" ]; then
365 gettext "Empty value. Exiting..."; echo ""
366 exit 0
367 fi
368 cd /home
369 # Sanity check
370 if [ -f swap ]; then
371 swapoff swap 2>/dev/null
372 fi
373 # DD to gen a virtual disk.
374 dd if=/dev/zero of=swap bs=1M count=$size
375 # Make swap filesystem.
376 mkswap swap
377 swapon swap
378 echo ""
379 }
381 # Clean out old backups to save disk space
382 clean_usb()
383 {
384 echo ""
385 echo -en "\033[1m`gettext \"Clean\"`\033[0m
386 ================================================================================"
387 gettext "Remove old rootfs.gz.unixtimestamp backup filesystems to free up disk space."; echo -e "\n\n"
388 # Locate and interactively remove old filesystems from /home directory
389 for file in `find /home/boot/rootfs.gz.[0-9]*`
390 do
391 gettext "Do you wish to remove: `basename $file` (Yes/no/exit) ? "
392 read answer
393 case $answer in
394 e|E|"exit"|Exit)
395 exit 0 ;;
396 y|Y|yes|Yes)
397 rm -f $file ;;
398 *)
399 gettext "No filesystems selected, exiting..." ; echo "" ;;
400 esac
401 done
402 }
404 #
405 # Tazusb sequence
406 #
408 case $COMMAND in
409 writefs)
410 # Writefs to rootfs.gz
411 check_root
412 if [ -z $2 ]; then
413 COMPRESSION=none
414 else
415 COMPRESSION=$2
416 fi
417 # Start info
418 newline
419 boldify "$(gettext 'Write filesystem')"
420 separator
421 cat << EOT
422 $(gettext "The command writefs will write all the current filesystem into a suitable
423 cpio archive (rootfs.gz) usable on a bootable LiveUSB media.")
425 $(boldify "Archive compression:") $(colorize 36 "$COMPRESSION")
427 EOT
428 # Clear out tazpkg cache
429 rm /var/cache/tazpkg/* -r -f
431 # Optionally remove sound card selection and screen resolution.
432 gettext "Do you wish to remove the sound card and screen configs ? "; echo
433 gettext "Press ENTER to keep or answer (No|yes|exit): "
434 read anser
435 case $anser in
436 e|E|"exit"|Exit)
437 exit 0 ;;
438 y|Y|yes|Yes)
439 gettext "Removing current sound card and screen configurations..."
440 rm -f /var/lib/sound-card-driver
441 rm -f /var/lib/alsa/asound.state
442 rm -f /etc/X11/xorg.conf ;;
443 *)
444 gettext "Keeping current sound card and screen configurations..." ;;
445 esac
446 status && newline
448 # Optionally remove i18n settings
449 gettext "Do you wish to remove local/keymap settings ? "; echo
450 gettext "Press ENTER to keep or answer (No|yes|exit): "
451 read anser
452 case $anser in
453 e|E|"exit"|Exit)
454 exit 0 ;;
455 y|Y|yes|Yes)
456 gettext "Removing current locale/keymap settings..."
457 echo "" > /etc/locale.conf
458 echo "" > /etc/keymap.conf ;;
459 *)
460 gettext "Keeping current locale/keymap settings..." ;;
461 esac
462 status
464 # Clean-up files by default
465 echo "" > /etc/udev/rules.d/70-persistent-net.rules
466 echo "" > /etc/udev/rules.d/70-persistant-cd.rules
468 # Create list of files
469 find /bin /etc /init /sbin /var /dev /lib /root /usr >/tmp/list
471 for dir in /home /proc /run /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
472 do
473 echo $dir >>/tmp/list
474 done
476 # Generate initramfs with specified compression
477 if [ "$COMPRESSION" = "lzma" ]; then
478 gettext "Creating rootfs.gz with lzma compression... "
479 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
481 elif [ "$COMPRESSION" = "gzip" ]; then
482 gettext "Creating rootfs.gz with gzip compression... "
483 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
485 else
486 gettext "Creating rootfs.gz without compression... "
487 cat /tmp/list | cpio -o -H newc > /rootfs.gz
488 fi
490 # Get initramfs size
491 size=`du -sh /rootfs.gz | cut -f 1`
493 # If the bootable medium is where it should be, copy across
494 if (test -e /home/boot/bzImage); then
495 gettext "Moving rootfs.gz to media. Remember to unmount for delayed writes!"; echo ""
497 # Move the old filesystem with the unix timestamp for reference
498 if (test -e /home/boot/previous.gz); then
499 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
500 fi
502 mv /home/boot/rootfs.gz /home/boot/previous.gz
503 mv /rootfs.gz /home/boot/.
504 else
505 gettext "rootfs.gz is located in /"; echo ""
506 fi
508 separator
509 gettext "Root filesystem size: $size"; echo ""
510 echo ""
511 echo "----"
512 gettext "ENTER to continue..."; read i
513 ;;
514 format)
515 # Format a partition.
516 check_root
517 echo ""
518 echo -e "\033[1m`gettext \"Format a device\"`\033[0m"
519 separator
520 DEVICE=$2
521 label=$3
522 fs_type=$4
523 if [ -z $DEVICE ]; then
524 ask_for_device
525 check_for_device
526 else
527 echo "Device : $DEVICE"
528 fi
529 test -z $fs_type && get_fs_type
530 get_label
531 unmount_target_usb
532 make_fs "$fs_type"
533 # mkfs_ext3
534 separator
535 gettext "Device $label ($DEVICE) is ready to use as LiveUSB and/or /home partition."
536 echo ""
537 ;;
538 gen-liveusb)
539 # Generate a LiveUSB media using files from a LiveCD.
540 check_root
541 echo ""
542 echo -e "\033[1m`gettext \"Gen a LiveUSB media\"`\033[0m"
543 separator
544 DEVICE=$2
545 if [ -z $DEVICE ]; then
546 ask_for_device
547 fi
549 check_for_device
550 mount_cdrom
551 get_part_info
552 unmount_target_usb
553 install_mbr
554 set_bootable
555 mount_target_usb
556 copy_cdrom_files
557 install_boot
558 exit_or_reboot
559 ;;
560 gen-swap)
561 check_root
562 gen_swap_file ;;
563 gen-iso2usb|iso2usb)
564 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 boldify "$(gettext "Copy ISO file to SliTaz LiveUSB media")"
573 separator
574 DEVICE=$3
575 if [ -z $DEVICE ]; then
576 ask_for_device
577 fi
578 check_for_device
579 mount_iso
580 get_part_info
581 unmount_target_usb
582 install_mbr
583 set_bootable
584 mount_target_usb
585 copy_cdrom_files
586 install_boot
587 umount /media/cdrom
588 losetup -d /dev/loop0
589 exit_or_reboot
590 ;;
591 clean)
592 check_root
593 clean_usb
594 ;;
595 usage|*)
596 # Display usage by default.
597 usage
598 ;;
599 esac
601 exit 0