tazusb view tazusb @ rev 3

Fix usage output (80 cols is standard) add timeout at boot
author Christophe Lincoln <pankso@slitaz.org>
date Wed Mar 05 11:28:30 2008 +0100 (2008-03-05)
parents 068f0611d8b5
children 189f70337617
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=20080304
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.\n"
34 }
36 # Status function.
37 status()
38 {
39 local CHECK=$?
40 echo -en "\\033[70G[ "
41 if [ $CHECK = 0 ]; then
42 echo -en "\\033[1;33mOK"
43 else
44 echo -en "\\033[1;31mFailed"
45 fi
46 echo -e "\\033[0;39m ]"
47 }
49 # Exit if user is not root.
50 check_root()
51 {
52 if test $(id -u) != 0 ; then
53 echo -e "\nThis program requires being run as root.\n"
54 exit 0
55 fi
56 }
58 # Verify a device exists before format or install
59 check_for_device()
60 {
61 DEVID=`fdisk -l | grep -w $DEVICE | cut -d: -f1 | cut -d/ -f3`
62 if [ -z "$DEVID" ]; then
63 echo -e "\nUnable to find device: $DEVICE\n"
64 exit 0
65 fi
67 PARTID=/dev/"$DEVID"1
68 }
70 #gets the UUID and filesystem type
71 get_part_info()
72 {
73 UUID=`blkid -s UUID -o value $PARTID`
74 FSTYPE=`blkid -s TYPE -o value $PARTID`
75 }
77 # Format target device and label partition.
78 mkfs_ext3()
79 {
80 echo -n "Please specify a label for the partition (TazUSB): "
81 read label
83 if [ -z $label ]; then
84 label=TazUSB
85 fi
87 echo "Label : $label"
88 echo "Mkfs : mkfs.ext3 -L \"$label\" $PARTID"
89 echo "" && sleep 2
90 mkfs.ext3 -L "$label" $PARTID
92 }
94 # Mount an existing USB device.
95 unmount_target_usb()
96 {
97 # If mount point is in use, unmount
98 if mount | grep $TARGET_ROOT; then
99 umount $TARGET_ROOT
100 fi
102 # Device could be mounted elsewhere, so unmount
103 if mount | grep $PARTID; then
104 echo "Unmounting USB target device..."
105 umount $PARTID
106 fi
107 }
109 # Mount an existing USB device.
110 mount_target_usb()
111 {
112 # If mount point is in use, unmount
113 if mount | grep $TARGET_ROOT; then
114 umount $TARGET_ROOT
115 fi
117 # Device could be mounted elsewhere, so unmount
118 if mount | grep $PARTID; then
119 umount $PARTID
120 fi
122 echo "Mounting USB target device..."
123 mkdir -p $TARGET_ROOT
124 mount $PARTID $TARGET_ROOT 2>/dev/null
125 }
127 # Mount SliTaz LiveCD to get needed files.
128 mount_cdrom()
129 {
130 echo "Mounting cdrom device..."
132 if mount | grep /media/cdrom; then
133 umount /media/cdrom
134 fi
136 mkdir -p /media/cdrom
137 mount -t iso9660 $CDROM /media/cdrom
139 if [ ! -f /media/cdrom/boot/rootfs.gz ]; then
140 echo -e "\nUnable to find a filesystem on the cdrom (rootfs.gz).\n"
141 exit 0
142 fi
143 }
145 # All needed files are in the boot direcory of the cdrom.
146 copy_cdrom_files()
147 {
148 echo -n "Copying needed files from cdrom..."
149 mkdir -p $TARGET_ROOT/boot
150 cp /media/cdrom/boot/bzImage $TARGET_ROOT/boot
151 cp /media/cdrom/boot/rootfs.gz $TARGET_ROOT/boot
152 status
153 }
155 install_mbr()
156 {
157 # MBR
158 if [ -f /usr/share/syslinux/mbr.bin ]; then
159 echo -n "Installing a new MBR to: $DEVICE"
160 cat /usr/share/syslinux/mbr.bin > $DEVICE
161 status
162 else
163 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
164 echo "No new MBR installed to: $DEVICE"
165 fi
166 }
168 # ext/syslinux install
169 install_boot()
170 {
171 #decide if we're installing syslinux or extlinux
172 if [ "$FSTYPE" = "vfat" ]; then
173 ST=syslinux
174 STC="syslinux -d /boot/syslinux/ $PARTID"
175 STE=cfg
176 else
177 ST=extlinux
178 STC="extlinux --install $TARGET_ROOT/boot/$ST"
179 STE=conf
180 fi
182 echo "Installing bootloader: $ST"
183 mkdir -p $TARGET_ROOT/boot/$ST
184 $STC
186 # extlinux.conf / syslinux.cfg
187 cat > $TARGET_ROOT/boot/$ST/$ST.$STE << _EOT_
188 display display.txt
189 default slitaz
190 timeout 20
191 label slitaz
192 kernel /boot/bzImage
193 append initrd=/boot/rootfs.gz rw root=/dev/null home=$UUID
195 label previous
196 kernel /boot/bzImage
197 append initrd=/boot/previous.gz rw root=/dev/null home=$UUID
199 _EOT_
201 # display.txt
202 cat > $TARGET_ROOT/boot/$ST/display.txt << "EOT"
203 _______. __ __ .___________. ___ ________
204 / || | | | | | / \ | /
205 | (----`| | | | `---| |---` / ^ \ `---/ /
206 \ \ | | | | | | / /_\ \ / /
207 .----) | | `----.| | | | / _____ \ / /----.
208 |_______/ |_______||__| |__| /__/ \__\ /________|
211 SliTaz GNU/Linux LiveUSB
212 Simple Light Incredible Temporary Autonomus Zone
215 EOT
216 status
217 }
219 # Let user exit or reboot.
220 exit_or_reboot()
221 {
222 echo ""
223 echo -n "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
224 read anser
225 if [ "$anser" == "reboot" ]; then
226 umount $TARGET_ROOT
227 umount /media/cdrom
228 reboot || reboot -f
229 else
230 umount /media/cdrom
231 echo "==============================================================================="
232 echo ""
233 exit 0
234 fi
235 }
237 set_bootable()
238 {
239 # As the boot flag is toggable, need to check it before hand
240 ISSET=`fdisk -l $DEVICE | grep $DEVICE | grep "*"`
242 # If not set, set bootable
243 if [ -z "$ISSET" ]; then
244 umount $DEVICE
245 echo "Setting $PARTID as bootable..."
246 fdisk $DEVICE >/dev/null << EOF
247 a
248 1
249 w
250 EOF
251 status
252 fi
253 }
255 #
256 # Tazusb sequence
257 #
259 case $COMMAND in
260 writefs)
261 #writefs to rootfs.gz
262 check_root
263 if [ -z $2 ]; then
264 COMPRESSION=none
265 else
266 COMPRESSION=$2
267 fi
268 #start info
269 echo ""
270 echo -e "\033[1mWrite filesystem\033[0m
271 ===============================================================================
272 The command writefs will write all the current filesystem into a suitable cpio
273 archive (rootfs.gz) usable on a bootable LiveUSB media.
275 Archive compression: $COMPRESSION"
276 echo ""
278 #clear out tazpkg cache
279 rm /var/cache/tazpkg/* -r -f
281 #optionally remove sound card selection
282 echo -n "Do you wish to remove the sound card selection (Yes/no/exit) ? "
283 read answer
284 answer=`echo "$answer:0:1}" | tr A-Z a-z`
286 if [ "$answer" = "e" ]; then
287 exit 0
288 fi
290 if [ "$answer" = "y" ]; then
291 echo -n "Removing current sound card selection..."
292 rm -f /var/lib/sound-card-driver
293 rm -f /etc/asound.state
294 else
295 echo -n "Keeping current sound card selection..."
296 fi
297 status
299 #create list of files
300 find /bin /etc /init /sbin /var /dev /lib /mnt /root /usr >/tmp/list
302 for dir in /home /proc /sys /tmp /media /media/cdrom /media/flash /media/usbdisk
303 do
304 echo $dir >>/tmp/list
305 done
307 #gen initramfs with specified compression
308 if [ "$COMPRESSION" = "lzma" ]; then
309 echo -n "Creating rootfs.gz with lzma compression... "
310 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
312 elif [ "$COMPRESSION" = "gzip" ]; then
313 echo -n "Creating rootfs.gz with gzip compression... "
314 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
316 else
317 echo -n "Creating rootfs.gz without compression... "
318 cat /tmp/list | cpio -o -H newc > /rootfs.gz
319 fi
321 #get initramfs size
322 size=`du -sh /rootfs.gz | cut -f 1`
324 #if the bootable medium is where it should be, copy across
326 if (test -e /home/boot/bzImage); then
327 echo "Moving rootfs.gz to media. Remember to unmount for delayed writes!"
329 #move the old filesystem with the unix timestamp for reference
330 if (test -e /home/boot/previous.gz); then
331 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
332 fi
334 mv /home/boot/rootfs.gz /home/boot/previous.gz
335 mv /rootfs.gz /home/boot/.
336 else
337 echo "rootfs.gz is located in /"
338 fi
340 echo "==============================================================================="
341 echo "Root filesystem size: $size"
342 echo ""
343 ;;
344 format)
345 # Format a partitions in ext3.
346 check_root
347 echo ""
348 echo -e "\033[1mFormat a device\033[0m"
349 echo "==============================================================================="
350 DEVICE=$2
351 if [ -z $DEVICE ]; then
352 echo -e "\nPlease specify a device to format: tazusb $COMMAND /dev/name\n"
353 exit 0
354 fi
355 check_for_device
356 echo "Device : $DEVICE"
357 mkfs_ext3
358 echo "==============================================================================="
359 echo "Device $label ($PARTID) is ready to use as LiveUSB and/or /home partition."
360 echo ""
361 ;;
362 gen-liveusb)
363 # Generate a LiveUSB media using files from a LiveCD.
364 check_root
365 echo ""
366 echo -e "\033[1mGen a LiveUSB media\033[0m"
367 echo "==============================================================================="
368 DEVICE=$2
369 if [ -z $DEVICE ]; then
370 echo -e "\No device specified. Usage: tazusb $CAMMAND /dev/name\n"
371 exit 0
372 fi
374 check_for_device
375 get_part_info
376 unmount_target_usb
377 install_mbr
378 set_bootable
379 mount_target_usb
380 mount_cdrom
381 copy_cdrom_files
382 install_boot
383 exit_or_reboot
384 ;;
385 usage|*)
386 # Display usage by default.
387 usage
388 ;;
389 esac
391 exit 0