tazusb view tazusb @ rev 166

tazusb: do not save pid files (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Jun 17 11:30:08 2014 +0200 (2014-06-17)
parents 0645a66ba186
children e43257bdb708
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 #
11 VERSION=4.2.5
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=$(grep "drive name" < /proc/sys/dev/cdrom/info | 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 cp /media/cdrom/boot/memtest $TARGET_ROOT/boot 2> /dev/null
265 cp /media/cdrom/boot/*pxe $TARGET_ROOT/boot 2> /dev/null
266 status
267 }
269 install_mbr()
270 {
271 # MBR
272 DISK=${DEVICE%[1-99]}
273 if [ -f /usr/share/boot/mbr.bin ]; then
274 gettext "Installing a new MBR to: $DISK"
275 cat /usr/share/boot/mbr.bin > $DISK
276 status
277 else
278 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
279 gettext "No new MBR installed to: $DISK"; echo ""
280 fi
281 }
283 # ext/syslinux install
284 install_boot()
285 {
286 # Decide if we're installing syslinux or extlinux
287 if [ "$FSTYPE" = "vfat" ]; then
288 ST=syslinux
289 STC="syslinux --install -d /boot/syslinux/ $DEVICE"
290 STE=cfg
291 else
292 ST=extlinux
293 STC="extlinux --install $TARGET_ROOT/boot/$ST"
294 STE=conf
295 fi
297 gettext "Installing bootloader: $ST"; echo ""
298 mkdir -p $TARGET_ROOT/boot/$ST
299 $STC
301 # Use existing isolinux.cfg for extlinux.conf or syslinux.cfg
302 cp /media/cdrom/boot/isolinux/isolinux.cfg $TARGET_ROOT/boot/$ST/$ST.$STE
304 # Update DVD autoinstall
305 sed -i "s/LABEL=packages-[^,]*/UUID=$UUID/g" $(grep -l append $TARGET_ROOT/boot/$ST/*)
307 # Add home=$UUID to kernel line in extlinux or syslinux.cfg
308 sed -i -e "s/\(root=.*\)/\1 home=$UUID/" $(grep -l append $TARGET_ROOT/boot/$ST/*)
310 # Splash screen and help files.
311 cp /media/cdrom/boot/isolinux/splash.* $TARGET_ROOT/boot/$ST
312 cp /media/cdrom/boot/isolinux/*.cfg $TARGET_ROOT/boot/$ST
313 cp /media/cdrom/boot/isolinux/*.kbd $TARGET_ROOT/boot/$ST
314 cp /media/cdrom/boot/isolinux/*.c32 $TARGET_ROOT/boot/$ST
315 sed -i -e s/'SliTaz GNU\/Linux'/'SliTaz GNU\/Linux LiveUSB'/ \
316 -e "s/isolinux/$ST/" $TARGET_ROOT/boot/$ST/$ST.$STE
317 }
319 # Let user exit or reboot.
320 exit_or_reboot()
321 {
322 separator
323 echo ""
324 gettext "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
325 read anser
326 if [ "$anser" == "reboot" ]; then
327 unmount_target_usb
328 reboot || reboot -f
329 else
330 unmount_target_usb
331 echo "" && exit 0
332 fi
333 }
335 set_bootable()
336 {
337 # As the boot flag is toggable, need to check it before hand
338 DISK=${DEVICE%[1-99]}
339 ISSET=`fdisk -l $DISK | grep $DEVICE | grep "\*"`
341 # If not set, set bootable
342 if [ "$ISSET" == "" ]; then
343 umount $TARGET_ROOT 2>/dev/null
344 gettext "Setting $DEVICE as bootable..."
345 fdisk $DISK >/dev/null << EOF
346 a
347 1
348 w
349 EOF
350 status
351 fi
352 }
354 # Generate a virtual swap file in /home/swap. SliTaz boot scripts
355 # will activate it, useful for low memory systems
356 gen_swap_file()
357 {
358 echo ""
359 echo -en "\033[1m`gettext \"Gen swap\"`\033[0m
360 ================================================================================"
361 gettext "Generate a swap file in /home/swap that will be activated on each boot to have
362 more memory available (Empty value to exit).
364 Swap file in Mb : "
365 read size
366 if [ -z "$size" ]; then
367 gettext "Empty value. Exiting..."; echo ""
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 echo ""
381 }
383 # Clean out old backups to save disk space
384 clean_usb()
385 {
386 echo ""
387 echo -en "\033[1m`gettext \"Clean\"`\033[0m
388 ================================================================================"
389 gettext "Remove old rootfs.gz.unixtimestamp backup filesystems to free up disk space."; echo -e "\n\n"
390 # Locate and interactively remove old filesystems from /home directory
391 for file in `find /home/boot/rootfs.gz.[0-9]*`
392 do
393 gettext "Do you wish to remove: `basename $file` (Yes/no/exit) ? "
394 read answer
395 case $answer in
396 e|E|"exit"|Exit)
397 exit 0 ;;
398 y|Y|yes|Yes)
399 rm -f $file ;;
400 *)
401 gettext "No filesystems selected, exiting..." ; echo "" ;;
402 esac
403 done
404 }
406 #
407 # Tazusb sequence
408 #
410 case $COMMAND in
411 writefs)
412 # Writefs to rootfs.gz
413 check_root
414 if [ -z $2 ]; then
415 COMPRESSION=none
416 else
417 COMPRESSION=$2
418 fi
419 # Start info
420 newline
421 boldify "$(gettext 'Write filesystem')"
422 separator
423 cat << EOT
424 $(gettext "The command writefs will write all the current filesystem into a suitable
425 cpio archive (rootfs.gz) usable on a bootable LiveUSB media.")
427 $(boldify "Archive compression:") $(colorize 36 "$COMPRESSION")
429 EOT
430 # Clear out tazpkg cache
431 rm /var/cache/tazpkg/* -r -f
433 # Optionally remove sound card selection and screen resolution.
434 gettext "Do you wish to remove the sound card and screen configs ? "; echo
435 gettext "Press ENTER to keep or answer (No|yes|exit): "
436 read anser
437 case $anser in
438 e|E|"exit"|Exit)
439 exit 0 ;;
440 y|Y|yes|Yes)
441 gettext "Removing current sound card and screen configurations..."
442 rm -f /var/lib/sound-card-driver
443 rm -f /var/lib/alsa/asound.state
444 rm -f /etc/X11/xorg.conf ;;
445 *)
446 gettext "Keeping current sound card and screen configurations..." ;;
447 esac
448 status && newline
450 # Optionally remove i18n settings
451 gettext "Do you wish to remove local/keymap settings ? "; echo
452 gettext "Press ENTER to keep or answer (No|yes|exit): "
453 read anser
454 case $anser in
455 e|E|"exit"|Exit)
456 exit 0 ;;
457 y|Y|yes|Yes)
458 gettext "Removing current locale/keymap settings..."
459 echo "" > /etc/locale.conf
460 echo "" > /etc/keymap.conf ;;
461 *)
462 gettext "Keeping current locale/keymap settings..."
463 grep -qs '^INCLUDE i18n.cfg' /home/boot/*linux/*linux.c* &&
464 sed -i 's/^INCLUDE i18n.cfg/# &/' /home/boot/*linux/*linux.c* ;;
465 esac
466 status
468 # Clean-up files by default
469 echo "" > /etc/udev/rules.d/70-persistent-net.rules
470 echo "" > /etc/udev/rules.d/70-persistant-cd.rules
472 # Create list of files
473 # find / -xdev | sed '/^\/home\//d;/^\/tmp\//d' >/tmp/list
474 # for dev in console null tty tty1
475 # do
476 # echo /dev/$dev >>/tmp/list
477 # done
479 find /bin /etc /init /sbin /var /dev /lib /root /usr >/tmp/list
481 for dir in /home /proc /run /sys /tmp /mnt /media /media/cdrom /media/flash /media/usbdisk
482 do
483 echo $dir >>/tmp/list
484 done
485 sed -i '/^\/var\/run\/.*pid$/d' /tmp/list
487 # Generate initramfs with specified compression
488 if [ "$COMPRESSION" = "lzma" ]; then
489 gettext "Creating rootfs.gz with lzma compression... "
490 cpio -o -H newc | lzma e -si -so > /rootfs.gz
492 elif [ "$COMPRESSION" = "gzip" ]; then
493 gettext "Creating rootfs.gz with gzip compression... "
494 cpio -o -H newc | gzip -9 > /rootfs.gz
496 else
497 gettext "Creating rootfs.gz without compression... "
498 cpio -o -H newc > /rootfs.gz
499 fi < /tmp/list
501 # Get initramfs size
502 size=`du -sh /rootfs.gz | cut -f 1`
504 # If the bootable medium is where it should be, copy across
505 if (test -e /home/boot/bzImage); then
506 gettext "Moving rootfs.gz to media. Remember to unmount for delayed writes!"; echo ""
508 # Move the old filesystem with the unix timestamp for reference
509 if (test -e /home/boot/previous.gz); then
510 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
511 fi
513 mv /home/boot/rootfs.gz /home/boot/previous.gz
514 mv /rootfs.gz /home/boot/.
515 else
516 gettext "rootfs.gz is located in /"; echo ""
517 fi
519 separator
520 gettext "Root filesystem size: $size"; echo ""
521 echo ""
522 echo "----"
523 gettext "ENTER to continue..."; read i
524 ;;
525 format)
526 # Format a partition.
527 check_root
528 echo ""
529 echo -e "\033[1m`gettext \"Format a device\"`\033[0m"
530 separator
531 DEVICE=$2
532 label=$3
533 fs_type=$4
534 if [ -z $DEVICE ]; then
535 ask_for_device
536 check_for_device
537 else
538 echo "Device : $DEVICE"
539 fi
540 test -z $fs_type && get_fs_type
541 get_label
542 unmount_target_usb
543 make_fs "$fs_type"
544 # mkfs_ext3
545 separator
546 gettext "Device $label ($DEVICE) is ready to use as LiveUSB and/or /home partition."
547 echo ""
548 ;;
549 gen-liveusb)
550 # Generate a LiveUSB media using files from a LiveCD.
551 check_root
552 echo ""
553 echo -e "\033[1m`gettext \"Gen a LiveUSB media\"`\033[0m"
554 separator
555 DEVICE=$2
556 if [ -z $DEVICE ]; then
557 ask_for_device
558 fi
560 check_for_device
561 mount_cdrom
562 get_part_info
563 unmount_target_usb
564 install_mbr
565 set_bootable
566 mount_target_usb
567 copy_cdrom_files
568 install_boot
569 exit_or_reboot
570 ;;
571 gen-swap)
572 check_root
573 gen_swap_file ;;
574 gen-iso2usb|iso2usb)
575 check_root
576 # Check if file exists
577 ISO=$2
578 if [ -z $ISO ] || [ ! -f $ISO ]; then
579 gettext "Please specify a valid filename on the command line."; echo ""
580 exit 1
581 fi
582 echo ""
583 boldify "$(gettext "Copy ISO file to SliTaz LiveUSB media")"
584 separator
585 DEVICE=$3
586 if [ -z $DEVICE ]; then
587 ask_for_device
588 fi
589 check_for_device
590 mount_iso
591 get_part_info
592 unmount_target_usb
593 install_mbr
594 set_bootable
595 mount_target_usb
596 copy_cdrom_files
597 install_boot
598 umount /media/cdrom
599 losetup -d /dev/loop0
600 exit_or_reboot
601 ;;
602 clean)
603 check_root
604 clean_usb
605 ;;
606 usage|*)
607 # Display usage by default.
608 usage
609 ;;
610 esac
612 exit 0