tazusb view tazusb @ rev 8

Minor Bug
author hacker@slitaz
date Wed Mar 12 17:03:10 2008 +0000 (2008-03-12)
parents 189f70337617
children a43da2aa89b2
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 -m 1 -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 echo "Mounting USB target device..."
113 mkdir -p $TARGET_ROOT
114 mount $PARTID $TARGET_ROOT 2>/dev/null
115 }
117 # Mount SliTaz LiveCD to get needed files.
118 mount_cdrom()
119 {
120 echo "Mounting cdrom device..."
122 if mount | grep /media/cdrom; then
123 umount /media/cdrom
124 fi
126 mkdir -p /media/cdrom
127 mount -t iso9660 $CDROM /media/cdrom
129 if [ ! -f /media/cdrom/boot/rootfs.gz ]; then
130 echo -e "\nUnable to find a filesystem on the cdrom (rootfs.gz).\n"
131 exit 0
132 fi
133 }
135 # All needed files are in the boot direcory of the cdrom.
136 copy_cdrom_files()
137 {
138 echo -n "Copying needed files from cdrom..."
139 mkdir -p $TARGET_ROOT/boot
140 cp /media/cdrom/boot/bzImage $TARGET_ROOT/boot
141 cp /media/cdrom/boot/rootfs.gz $TARGET_ROOT/boot
142 status
143 }
145 install_mbr()
146 {
147 # MBR
148 if [ -f /usr/share/syslinux/mbr.bin ]; then
149 echo -n "Installing a new MBR to: $DEVICE"
150 cat /usr/share/syslinux/mbr.bin > $DEVICE
151 status
152 else
153 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
154 echo "No new MBR installed to: $DEVICE"
155 fi
156 }
158 # ext/syslinux install
159 install_boot()
160 {
161 #decide if we're installing syslinux or extlinux
162 if [ "$FSTYPE" = "vfat" ]; then
163 ST=syslinux
164 STC="syslinux -d /boot/syslinux/ $PARTID"
165 STE=cfg
166 else
167 ST=extlinux
168 STC="extlinux --install $TARGET_ROOT/boot/$ST"
169 STE=conf
170 fi
172 echo "Installing bootloader: $ST"
173 mkdir -p $TARGET_ROOT/boot/$ST
174 $STC
176 # extlinux.conf / syslinux.cfg
177 cat > $TARGET_ROOT/boot/$ST/$ST.$STE << _EOT_
178 display display.txt
179 default slitaz
180 timeout 20
181 label slitaz
182 kernel /boot/bzImage
183 append initrd=/boot/rootfs.gz rw root=/dev/null home=$UUID
185 label previous
186 kernel /boot/bzImage
187 append initrd=/boot/previous.gz rw root=/dev/null home=$UUID
189 _EOT_
191 # display.txt
192 cat > $TARGET_ROOT/boot/$ST/display.txt << "EOT"
193 _______. __ __ .___________. ___ ________
194 / || | | | | | / \ | /
195 | (----`| | | | `---| |---` / ^ \ `---/ /
196 \ \ | | | | | | / /_\ \ / /
197 .----) | | `----.| | | | / _____ \ / /----.
198 |_______/ |_______||__| |__| /__/ \__\ /________|
201 SliTaz GNU/Linux LiveUSB
202 Simple Light Incredible Temporary Autonomus Zone
205 EOT
206 status
207 }
209 # Let user exit or reboot.
210 exit_or_reboot()
211 {
212 echo ""
213 echo -n "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
214 read anser
215 if [ "$anser" == "reboot" ]; then
216 umount $TARGET_ROOT
217 umount /media/cdrom
218 reboot || reboot -f
219 else
220 umount /media/cdrom
221 echo "==============================================================================="
222 echo ""
223 exit 0
224 fi
225 }
227 set_bootable()
228 {
229 # As the boot flag is toggable, need to check it before hand
230 ISSET=`fdisk -l $DEVICE | grep $DEVICE | grep "*"`
232 # If not set, set bootable
233 if [ -z "$ISSET" ]; then
234 umount $DEVICE
235 echo "Setting $PARTID as bootable..."
236 fdisk $DEVICE >/dev/null << EOF
237 a
238 1
239 w
240 EOF
241 status
242 fi
243 }
245 #
246 # Tazusb sequence
247 #
249 case $COMMAND in
250 writefs)
251 #writefs to rootfs.gz
252 check_root
253 if [ -z $2 ]; then
254 COMPRESSION=none
255 else
256 COMPRESSION=$2
257 fi
258 #start info
259 echo ""
260 echo -e "\033[1mWrite filesystem\033[0m
261 ===============================================================================
262 The command writefs will write all the current filesystem into a suitable cpio
263 archive (rootfs.gz) usable on a bootable LiveUSB media.
265 Archive compression: $COMPRESSION"
266 echo ""
268 #clear out tazpkg cache
269 rm /var/cache/tazpkg/* -r -f
271 #optionally remove sound card selection
272 echo -n "Do you wish to remove the sound card selection (Yes/no/exit) ? "
273 read anser
274 case $anser in
275 e|E|"exit"|Exit)
276 exit 0
277 ;;
278 y|Y|yes|Yes)
279 echo -n "Removing current sound card selection..."
280 rm -f /var/lib/sound-card-driver
281 rm -f /etc/asound.state
282 ;;
283 *)
284 echo -n "Keeping current sound card selection..."
285 ;;
286 esac
287 status
289 #create list of files
290 find /bin /etc /init /sbin /var /dev /lib /mnt /root /usr >/tmp/list
292 for dir in /home /proc /sys /tmp /media /media/cdrom /media/flash /media/usbdisk
293 do
294 echo $dir >>/tmp/list
295 done
297 #gen initramfs with specified compression
298 if [ "$COMPRESSION" = "lzma" ]; then
299 echo -n "Creating rootfs.gz with lzma compression... "
300 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
302 elif [ "$COMPRESSION" = "gzip" ]; then
303 echo -n "Creating rootfs.gz with gzip compression... "
304 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
306 else
307 echo -n "Creating rootfs.gz without compression... "
308 cat /tmp/list | cpio -o -H newc > /rootfs.gz
309 fi
311 #get initramfs size
312 size=`du -sh /rootfs.gz | cut -f 1`
314 #if the bootable medium is where it should be, copy across
316 if (test -e /home/boot/bzImage); then
317 echo "Moving rootfs.gz to media. Remember to unmount for delayed writes!"
319 #move the old filesystem with the unix timestamp for reference
320 if (test -e /home/boot/previous.gz); then
321 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
322 fi
324 mv /home/boot/rootfs.gz /home/boot/previous.gz
325 mv /rootfs.gz /home/boot/.
326 else
327 echo "rootfs.gz is located in /"
328 fi
330 echo "==============================================================================="
331 echo "Root filesystem size: $size"
332 echo ""
333 ;;
334 format)
335 # Format a partitions in ext3.
336 check_root
337 echo ""
338 echo -e "\033[1mFormat a device\033[0m"
339 echo "==============================================================================="
340 DEVICE=$2
341 if [ -z $DEVICE ]; then
342 echo -e "\nPlease specify a device to format: tazusb $COMMAND /dev/name\n"
343 exit 0
344 fi
345 check_for_device
346 echo "Device : $DEVICE"
347 mkfs_ext3
348 echo "==============================================================================="
349 echo "Device $label ($PARTID) is ready to use as LiveUSB and/or /home partition."
350 echo ""
351 ;;
352 gen-liveusb)
353 # Generate a LiveUSB media using files from a LiveCD.
354 check_root
355 echo ""
356 echo -e "\033[1mGen a LiveUSB media\033[0m"
357 echo "==============================================================================="
358 DEVICE=$2
359 if [ -z $DEVICE ]; then
360 echo -e "\No device specified. Usage: tazusb $CAMMAND /dev/name\n"
361 exit 0
362 fi
364 check_for_device
365 get_part_info
366 unmount_target_usb
367 install_mbr
368 set_bootable
369 mount_target_usb
370 mount_cdrom
371 copy_cdrom_files
372 install_boot
373 exit_or_reboot
374 ;;
375 usage|*)
376 # Display usage by default.
377 usage
378 ;;
379 esac
381 exit 0