tazusb view tazusb @ rev 10

Updated Documentation to match code update
author Andrew Miller <spode@thinkbikes.com>
date Wed Mar 12 22:14:18 2008 +0000 (2008-03-12)
parents 132de4a8e0af
children bc7ca2e2616e
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 IFDEV=`fdisk -l | grep $DEVICE`
62 if [ -z "$IFDEV" ]; then
63 echo -e "\nUnable to find device: $DEVICE\n"
64 exit 0
65 fi
66 DEVID=${DEVID%?}
67 }
69 #gets the UUID and filesystem type
70 get_part_info()
71 {
72 UUID=`blkid -s UUID -o value $DEVICE`
73 FSTYPE=`blkid -s TYPE -o value $DEVICE`
74 }
76 # Format target device and label partition.
77 mkfs_ext3()
78 {
79 echo -n "Please specify a label for the partition (TazUSB): "
80 read label
82 if [ -z $label ]; then
83 label=TazUSB
84 fi
86 echo "Label : $label"
87 echo "Mkfs : mkfs.ext3 -L \"$label\" $DEVICE"
88 echo "" && sleep 2
89 mkfs.ext3 -L "$label" $DEVICE
91 }
93 # Mount an existing USB device.
94 unmount_target_usb()
95 {
96 # If mount point is in use, unmount
97 if mount | grep $TARGET_ROOT; then
98 umount $TARGET_ROOT
99 fi
101 # Device could be mounted elsewhere, so unmount
102 if mount | grep $DEVICE; then
103 echo "Unmounting USB target device..."
104 umount $DEVICE
105 fi
106 }
108 # Mount an existing USB device.
109 mount_target_usb()
110 {
111 echo "Mounting USB target device..."
112 mkdir -p $TARGET_ROOT
113 mount $DEVICE $TARGET_ROOT 2>/dev/null
114 }
116 # Mount SliTaz LiveCD to get needed files.
117 mount_cdrom()
118 {
119 echo "Mounting cdrom device..."
121 if mount | grep /media/cdrom; then
122 umount /media/cdrom
123 fi
125 mkdir -p /media/cdrom
126 mount -t iso9660 $CDROM /media/cdrom
128 if [ ! -f /media/cdrom/boot/rootfs.gz ]; then
129 echo -e "\nUnable to find a filesystem on the cdrom (rootfs.gz).\n"
130 exit 0
131 fi
132 }
134 # All needed files are in the boot direcory of the cdrom.
135 copy_cdrom_files()
136 {
137 echo -n "Copying needed files from cdrom..."
138 mkdir -p $TARGET_ROOT/boot
139 cp /media/cdrom/boot/bzImage $TARGET_ROOT/boot
140 cp /media/cdrom/boot/rootfs.gz $TARGET_ROOT/boot
141 status
142 }
144 install_mbr()
145 {
146 # MBR
147 if [ -f /usr/share/syslinux/mbr.bin ]; then
148 echo -n "Installing a new MBR to: $DEVICE"
149 cat /usr/share/syslinux/mbr.bin > $DEVID
150 status
151 else
152 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
153 echo "No new MBR installed to: $DEVID"
154 fi
155 }
157 # ext/syslinux install
158 install_boot()
159 {
160 #decide if we're installing syslinux or extlinux
161 if [ "$FSTYPE" = "vfat" ]; then
162 ST=syslinux
163 STC="syslinux -d /boot/syslinux/ $DEVICE"
164 STE=cfg
165 else
166 ST=extlinux
167 STC="extlinux --install $TARGET_ROOT/boot/$ST"
168 STE=conf
169 fi
171 echo "Installing bootloader: $ST"
172 mkdir -p $TARGET_ROOT/boot/$ST
173 $STC
175 # extlinux.conf / syslinux.cfg
176 cat > $TARGET_ROOT/boot/$ST/$ST.$STE << _EOT_
177 display display.txt
178 default slitaz
179 timeout 20
180 label slitaz
181 kernel /boot/bzImage
182 append initrd=/boot/rootfs.gz rw root=/dev/null home=$UUID
184 label previous
185 kernel /boot/bzImage
186 append initrd=/boot/previous.gz rw root=/dev/null home=$UUID
188 _EOT_
190 # display.txt
191 cat > $TARGET_ROOT/boot/$ST/display.txt << "EOT"
192 _______. __ __ .___________. ___ ________
193 / || | | | | | / \ | /
194 | (----`| | | | `---| |---` / ^ \ `---/ /
195 \ \ | | | | | | / /_\ \ / /
196 .----) | | `----.| | | | / _____ \ / /----.
197 |_______/ |_______||__| |__| /__/ \__\ /________|
200 SliTaz GNU/Linux LiveUSB
201 Simple Light Incredible Temporary Autonomus Zone
204 EOT
205 status
206 }
208 # Let user exit or reboot.
209 exit_or_reboot()
210 {
211 echo ""
212 echo -n "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
213 read anser
214 if [ "$anser" == "reboot" ]; then
215 umount $TARGET_ROOT
216 umount /media/cdrom
217 reboot || reboot -f
218 else
219 umount /media/cdrom
220 echo "==============================================================================="
221 echo ""
222 exit 0
223 fi
224 }
226 set_bootable()
227 {
228 # As the boot flag is toggable, need to check it before hand
229 ISSET=`fdisk -l $DEVICE | grep $DEVICE | grep "*"`
231 # If not set, set bootable
232 if [ -z "$ISSET" ]; then
233 umount $DEVICE
234 echo "Setting $DEVICE as bootable..."
235 fdisk $DEVICE >/dev/null << EOF
236 a
237 1
238 w
239 EOF
240 status
241 fi
242 }
244 #
245 # Tazusb sequence
246 #
248 case $COMMAND in
249 writefs)
250 #writefs to rootfs.gz
251 check_root
252 if [ -z $2 ]; then
253 COMPRESSION=none
254 else
255 COMPRESSION=$2
256 fi
257 #start info
258 echo ""
259 echo -e "\033[1mWrite filesystem\033[0m
260 ===============================================================================
261 The command writefs will write all the current filesystem into a suitable cpio
262 archive (rootfs.gz) usable on a bootable LiveUSB media.
264 Archive compression: $COMPRESSION"
265 echo ""
267 #clear out tazpkg cache
268 rm /var/cache/tazpkg/* -r -f
270 #optionally remove sound card selection
271 echo -n "Do you wish to remove the sound card selection (Yes/no/exit) ? "
272 read anser
273 case $anser in
274 e|E|"exit"|Exit)
275 exit 0
276 ;;
277 y|Y|yes|Yes)
278 echo -n "Removing current sound card selection..."
279 rm -f /var/lib/sound-card-driver
280 rm -f /etc/asound.state
281 ;;
282 *)
283 echo -n "Keeping current sound card selection..."
284 ;;
285 esac
286 status
288 #create list of files
289 find /bin /etc /init /sbin /var /dev /lib /mnt /root /usr >/tmp/list
291 for dir in /home /proc /sys /tmp /media /media/cdrom /media/flash /media/usbdisk
292 do
293 echo $dir >>/tmp/list
294 done
296 #gen initramfs with specified compression
297 if [ "$COMPRESSION" = "lzma" ]; then
298 echo -n "Creating rootfs.gz with lzma compression... "
299 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
301 elif [ "$COMPRESSION" = "gzip" ]; then
302 echo -n "Creating rootfs.gz with gzip compression... "
303 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
305 else
306 echo -n "Creating rootfs.gz without compression... "
307 cat /tmp/list | cpio -o -H newc > /rootfs.gz
308 fi
310 #get initramfs size
311 size=`du -sh /rootfs.gz | cut -f 1`
313 #if the bootable medium is where it should be, copy across
315 if (test -e /home/boot/bzImage); then
316 echo "Moving rootfs.gz to media. Remember to unmount for delayed writes!"
318 #move the old filesystem with the unix timestamp for reference
319 if (test -e /home/boot/previous.gz); then
320 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
321 fi
323 mv /home/boot/rootfs.gz /home/boot/previous.gz
324 mv /rootfs.gz /home/boot/.
325 else
326 echo "rootfs.gz is located in /"
327 fi
329 echo "==============================================================================="
330 echo "Root filesystem size: $size"
331 echo ""
332 ;;
333 format)
334 # Format a partitions in ext3.
335 check_root
336 echo ""
337 echo -e "\033[1mFormat a device\033[0m"
338 echo "==============================================================================="
339 DEVICE=$2
340 if [ -z $DEVICE ]; then
341 echo -e "\nPlease specify a device to format: tazusb $COMMAND /dev/name\n"
342 exit 0
343 fi
344 check_for_device
345 echo "Device : $DEVICE"
346 mkfs_ext3
347 echo "==============================================================================="
348 echo "Device $label ($DEVICE) is ready to use as LiveUSB and/or /home partition."
349 echo ""
350 ;;
351 gen-liveusb)
352 # Generate a LiveUSB media using files from a LiveCD.
353 check_root
354 echo ""
355 echo -e "\033[1mGen a LiveUSB media\033[0m"
356 echo "==============================================================================="
357 DEVICE=$2
358 if [ -z $DEVICE ]; then
359 echo -e "\No device specified. Usage: tazusb $CAMMAND /dev/name\n"
360 exit 0
361 fi
363 check_for_device
364 get_part_info
365 unmount_target_usb
366 install_mbr
367 set_bootable
368 mount_target_usb
369 mount_cdrom
370 copy_cdrom_files
371 install_boot
372 exit_or_reboot
373 ;;
374 usage|*)
375 # Display usage by default.
376 usage
377 ;;
378 esac
380 exit 0