tazusb view tazusb @ rev 0

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