tazusb view tazusb @ rev 25

Fix display splashimage with extlinux.
author Eric Joseph-Alexandre <erjo@slitaz.org>
date Sun May 18 22:53:10 2008 +0200 (2008-05-18)
parents a8ea91ebcfba
children cb7070e045e4
line source
1 #!/bin/sh
2 # Tazusb - SliTaz LiveUSB
3 #
4 # Tazusb is an utility to generate, configure and manipulate SliTaz LiveUSB
5 # bootable media and/or USB /home partition, such as flash keys, SD card or
6 # USB harddisk.
7 #
8 # Authors : Christophe Lincoln (Pankso) <pankso@slitaz.org>
9 # Andrew Miller (Spode) <spode@spodesabode.com>
10 #
11 VERSION=1.2.1
13 COMMAND=$1
14 TARGET_ROOT=/media/flash
15 DRIVE_NAME=`cat /proc/sys/dev/cdrom/info | grep "drive name" | cut -f 3`
16 CDROM=/dev/$DRIVE_NAME
18 #
19 # Tazusb functions
20 #
22 # Print the usage.
23 usage ()
24 {
25 echo -e "\nSliTaz Live USB - Version: $VERSION\n
26 \033[1mUsage: \033[0m `basename $0` [command] [compression|device]
27 \033[1mCommands: \033[0m\n
28 usage Print this short usage.
29 writefs Write the current filesystem to rootfs.gz.
30 tazSupported compression: lzma. gzip, none.
31 format Format and label device with ext3 filesystem
32 (for LiveUSB or /home).
33 gen-liveusb Generate a bootable LiveUSB using files from the LiveCD.
34 gen-swap Creat or recreat and active additionnal swap memory.
35 gen-iso2usb Generate a bootable LiveUSB using files from ISO file\n"
36 }
38 # Status function.
39 status()
40 {
41 local CHECK=$?
42 echo -en "\\033[70G[ "
43 if [ $CHECK = 0 ]; then
44 echo -en "\\033[1;33mOK"
45 else
46 echo -en "\\033[1;31mFailed"
47 fi
48 echo -e "\\033[0;39m ]"
49 }
51 # Exit if user is not root.
52 check_root()
53 {
54 if test $(id -u) != 0 ; then
55 echo -e "\nThis program requires being run as root.\n"
56 exit 0
57 fi
58 }
60 # Display a list of available partition.
61 fdisk_list()
62 {
63 echo "----"
64 fdisk -l | grep ^/dev/sd*
65 echo "----"
66 }
68 # We need a USB media to install.
69 ask_for_device()
70 {
71 echo -n "\
72 Please specify the target USB device to $COMMAND. You can type 'list' to
73 get a list of devices, type 'exit' or give an empty value to exit.
75 Device to use : "; read anser
76 while [ "$anser" == "list" ]; do
77 fdisk_list
78 echo -n "Device to use : "; read anser
79 done
80 if [ "$anser" = "" -o "$anser" = "exit" ]; then
81 echo -e "\nNo specified device or exit.\n"
82 exit 0
83 else
84 DEVICE=$anser
85 fi
86 }
88 # Verify a device exists before format or install
89 check_for_device()
90 {
91 IFDEV=`fdisk -l | grep $DEVICE`
92 if [ -z "$IFDEV" ]; then
93 echo -e "\nUnable to find device: $DEVICE\n"
94 exit 0
95 fi
96 }
98 #gets the UUID and filesystem type
99 get_part_info()
100 {
101 UUID=`blkid -s UUID -o value $DEVICE`
102 FSTYPE=`blkid -s TYPE -o value $DEVICE`
103 }
105 # Format target device and label partition.
106 mkfs_ext3()
107 {
108 echo -n "Please specify a label for the partition (TazUSB): "
109 read label
111 if [ -z $label ]; then
112 label=TazUSB
113 fi
115 echo "Label : $label"
116 echo "Mkfs : mkfs.ext3 -L \"$label\" $DEVICE"
117 echo "" && sleep 2
118 mkfs.ext3 -L "$label" $DEVICE
120 }
122 # Mount an existing USB device.
123 unmount_target_usb()
124 {
125 # If mount point is in use, unmount
126 if mount | grep $TARGET_ROOT; then
127 umount $TARGET_ROOT
128 fi
130 # Device could be mounted elsewhere, so unmount
131 if mount | grep $DEVICE; then
132 echo "Unmounting USB target device..."
133 umount $DEVICE
134 fi
135 }
137 # Mount an existing USB device.
138 mount_target_usb()
139 {
140 echo "Mounting USB target device..."
141 mkdir -p $TARGET_ROOT
142 mount $DEVICE $TARGET_ROOT 2>/dev/null
143 }
145 # Mount SliTaz LiveCD to get needed files.
146 mount_cdrom()
147 {
148 echo "Mounting cdrom device..."
150 if mount | grep /media/cdrom; then
151 umount /media/cdrom
152 fi
154 mkdir -p /media/cdrom
155 mount -r -t iso9660 $CDROM /media/cdrom 2>/dev/null
157 if [ ! -f /media/cdrom/boot/rootfs.gz ]; then
158 echo -e "\nUnable to mount cdrom or to find a filesystem on it (rootfs.gz).\n"
159 exit 0
160 fi
161 }
163 # Mount SliTaz ISO to get needed files.
164 mount_iso()
165 {
166 if mount | grep /media/cdrom ; then
167 umount /media/cdrom
168 fi
170 test -d /media/cdrom || mkdir -p /media/cdrom
172 echo "Mounting `basename $ISO`..."
173 mount $ISO /media/cdrom 2>/dev/null
175 if [ ! -f /media/cdrom/boot/rootfs.gz ]; then
176 echo -e "\nUnable to mount iso or to find a filesystem on it (rootfs.gz).\n"
177 exit 0
178 fi
179 }
182 # All needed files are in the boot direcory of the cdrom.
183 copy_cdrom_files()
184 {
185 echo -n "Copying needed files from cdrom..."
186 mkdir -p $TARGET_ROOT/boot
187 cp /media/cdrom/boot/bzImage $TARGET_ROOT/boot
188 cp /media/cdrom/boot/rootfs.gz $TARGET_ROOT/boot
189 status
190 }
192 install_mbr()
193 {
194 # MBR
195 DISK=${DEVICE%[1-99]}
196 if [ -f /usr/share/boot/mbr.bin ]; then
197 echo -n "Installing a new MBR to: $DISK"
198 cat /usr/share/boot/mbr.bin > $DISK
199 status
200 else
201 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
202 echo "No new MBR installed to: $DISK"
203 fi
204 }
206 # ext/syslinux install
207 install_boot()
208 {
209 #decide if we're installing syslinux or extlinux
210 if [ "$FSTYPE" = "vfat" ]; then
211 ST=syslinux
212 STC="syslinux -d /boot/syslinux/ $DEVICE"
213 STE=cfg
214 else
215 ST=extlinux
216 STC="extlinux --install $TARGET_ROOT/boot/$ST"
217 STE=conf
218 fi
220 echo "Installing bootloader: $ST"
221 mkdir -p $TARGET_ROOT/boot/$ST
222 $STC
224 # Use existing isolinux.cfg for extlinux.conf or syslinux.cfg
225 cp /media/cdrom/boot/isolinux/isolinux.cfg $TARGET_ROOT/boot/$ST/$ST.$STE
226 sed -i -e "s/isolinux.msg/$ST.msg/" $TARGET_ROOT/boot/$ST/$ST.$STE
228 # Add home= to apppend in extlinux or syslinux.cfg
229 sed -i -e "s/\(append.*\)/\1 home=$UUID/" $TARGET_ROOT/boot/$ST/$ST.$STE
231 # Splash screen and help files.
232 cp /media/cdrom/boot/isolinux/isolinux.msg $TARGET_ROOT/boot/$ST/$ST.msg
233 sed -i s/'SliTaz GNU\/Linux'/'SliTaz GNU\/Linux LiveUSB'/ $TARGET_ROOT/boot/$ST/$ST.msg
234 cp /media/cdrom/boot/isolinux/splash.lss $TARGET_ROOT/boot/$ST
235 cp /media/cdrom/boot/isolinux/*.txt $TARGET_ROOT/boot/$ST
236 cp /media/cdrom/boot/isolinux/*.cfg $TARGET_ROOT/boot/$ST
237 cp /media/cdrom/boot/isolinux/*.kbd $TARGET_ROOT/boot/$ST
238 cp /media/cdrom/boot/isolinux/*.c32 $TARGET_ROOT/boot/$ST
240 # Modifing all cfg files.
241 for cfg in $TARGET_ROOT/boot/$ST/*.cfg
242 do
243 sed -i s/isolinux.msg/$ST.msg/ $cfg
244 done
247 }
249 # Let user exit or reboot.
250 exit_or_reboot()
251 {
252 echo "==============================================================================="
253 echo ""
254 echo -n "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
255 read anser
256 if [ "$anser" == "reboot" ]; then
257 umount $TARGET_ROOT
258 umount /media/cdrom
259 reboot || reboot -f
260 else
261 umount /media/cdrom
262 echo ""
263 exit 0
264 fi
265 }
267 set_bootable()
268 {
269 # As the boot flag is toggable, need to check it before hand
270 DISK=${DEVICE%[1-99]}
271 ISSET=`fdisk -l $DISK | grep $DEVICE | grep "\*"`
273 # If not set, set bootable
274 if [ "$ISSET" == "" ]; then
275 umount $TARGET_ROOT 2>/dev/null
276 echo -n "Setting $DEVICE as bootable..."
277 fdisk $DISK >/dev/null << EOF
278 a
279 1
280 w
281 EOF
282 status
283 fi
284 }
286 # Generate a virtual swap file in /home/swap. SliTaz boot script
287 # will active it, usefull for low memory system
288 gen_swap_file()
289 {
290 echo ""
291 echo -en "\033[1mGen swap\033[0m
292 ===============================================================================
293 Generate a swap file in /home/swap that will be activate on each boot to have
294 more memory available (Empty value to exit).
296 Swap file in Mb : "
297 read size
298 if [ -z "$size" ]; then
299 echo -e "\nEmpty value. Exiting...\n"
300 exit 0
301 fi
302 cd /home
303 # Sanity check
304 if [ -f swap ]; then
305 swapoff swap 2>/dev/null
306 fi
307 # DD to gen a virtual disk.
308 dd if=/dev/zero of=swap bs=1M count=$size
309 # Make swap filesystem.
310 mkswap swap
311 swapon swap
312 echo ""
313 }
315 #
316 # Tazusb sequence
317 #
319 case $COMMAND in
320 writefs)
321 #writefs to rootfs.gz
322 check_root
323 if [ -z $2 ]; then
324 COMPRESSION=none
325 else
326 COMPRESSION=$2
327 fi
328 #start info
329 echo ""
330 echo -e "\033[1mWrite filesystem\033[0m
331 ===============================================================================
332 The command writefs will write all the current filesystem into a suitable cpio
333 archive (rootfs.gz) usable on a bootable LiveUSB media.
335 Archive compression: $COMPRESSION"
336 echo ""
338 #clear out tazpkg cache
339 rm /var/cache/tazpkg/* -r -f
341 #optionally remove sound card selection
342 echo -n "Do you wish to remove the sound card selection (Yes/no/exit) ? "
343 read anser
344 case $anser in
345 e|E|"exit"|Exit)
346 exit 0
347 ;;
348 y|Y|yes|Yes)
349 echo -n "Removing current sound card selection..."
350 rm -f /var/lib/sound-card-driver
351 rm -f /etc/asound.state
352 ;;
353 *)
354 echo -n "Keeping current sound card selection..."
355 ;;
356 esac
357 status
359 #create list of files
360 find /bin /etc /init /sbin /var /dev /lib /mnt /root /usr >/tmp/list
362 for dir in /home /proc /sys /tmp /media /media/cdrom /media/flash /media/usbdisk
363 do
364 echo $dir >>/tmp/list
365 done
367 #gen initramfs with specified compression
368 if [ "$COMPRESSION" = "lzma" ]; then
369 echo -n "Creating rootfs.gz with lzma compression... "
370 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
372 elif [ "$COMPRESSION" = "gzip" ]; then
373 echo -n "Creating rootfs.gz with gzip compression... "
374 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
376 else
377 echo -n "Creating rootfs.gz without compression... "
378 cat /tmp/list | cpio -o -H newc > /rootfs.gz
379 fi
381 #get initramfs size
382 size=`du -sh /rootfs.gz | cut -f 1`
384 #if the bootable medium is where it should be, copy across
386 if (test -e /home/boot/bzImage); then
387 echo "Moving rootfs.gz to media. Remember to unmount for delayed writes!"
389 #move the old filesystem with the unix timestamp for reference
390 if (test -e /home/boot/previous.gz); then
391 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
392 fi
394 mv /home/boot/rootfs.gz /home/boot/previous.gz
395 mv /rootfs.gz /home/boot/.
396 else
397 echo "rootfs.gz is located in /"
398 fi
400 echo "==============================================================================="
401 echo "Root filesystem size: $size"
402 echo ""
403 echo -en "----\nENTER to continue..."; read i
404 ;;
405 format)
406 # Format a partitions in ext3.
407 check_root
408 echo ""
409 echo -e "\033[1mFormat a device\033[0m"
410 echo "==============================================================================="
411 DEVICE=$2
412 if [ -z $DEVICE ]; then
413 ask_for_device
414 check_for_device
415 else
416 echo "Device : $DEVICE"
417 fi
418 mkfs_ext3
419 echo "==============================================================================="
420 echo "Device $label ($DEVICE) is ready to use as LiveUSB and/or /home partition."
421 echo ""
422 ;;
423 gen-liveusb)
424 # Generate a LiveUSB media using files from a LiveCD.
425 check_root
426 echo ""
427 echo -e "\033[1mGen a LiveUSB media\033[0m"
428 echo "==============================================================================="
429 DEVICE=$2
430 if [ -z $DEVICE ]; then
431 ask_for_device
432 fi
434 check_for_device
435 mount_cdrom
436 get_part_info
437 unmount_target_usb
438 install_mbr
439 set_bootable
440 mount_target_usb
441 copy_cdrom_files
442 install_boot
443 exit_or_reboot
444 ;;
445 gen-swap)
446 check_root
447 gen_swap_file
448 ;;
449 gen-iso2usb)
450 check_root
452 # Check if file exists
453 ISO=$2
454 if [ -z $ISO ] || [ ! -f $ISO ]; then
455 echo -e "\nPlease specify a valid filename on the command line.\n"
456 exit 1
457 fi
458 echo ""
459 echo -e "\033[1mCopy ISO file to SliTaz LiveUSB media\033[0m"
460 echo "==============================================================================="
461 echo ""
462 DEVICE=$3
463 if [ -z $DEVICE ]; then
464 ask_for_device
465 fi
466 check_for_device
467 mount_iso
468 get_part_info
469 unmount_target_usb
470 install_mbr
471 set_bootable
472 mount_target_usb
473 copy_cdrom_files
474 install_boot
475 exit_or_reboot
476 ;;
477 usage|*)
478 # Display usage by default.
479 usage
480 ;;
481 esac
483 exit 0