tazusb view tazusb @ rev 17

Changed release string, for release again...
author Christophe Lincoln <pankso@slitaz.org>
date Thu Mar 13 21:11:09 2008 +0100 (2008-03-13)
parents b0ed320cc6f2
children a501498d2bc6
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
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.\n"
35 }
37 # Status function.
38 status()
39 {
40 local CHECK=$?
41 echo -en "\\033[70G[ "
42 if [ $CHECK = 0 ]; then
43 echo -en "\\033[1;33mOK"
44 else
45 echo -en "\\033[1;31mFailed"
46 fi
47 echo -e "\\033[0;39m ]"
48 }
50 # Exit if user is not root.
51 check_root()
52 {
53 if test $(id -u) != 0 ; then
54 echo -e "\nThis program requires being run as root.\n"
55 exit 0
56 fi
57 }
59 # Display a list of available partition.
60 fdisk_list()
61 {
62 echo "----"
63 fdisk -l | grep ^/dev/sd*
64 echo "----"
65 }
67 # We need a USB media to install.
68 ask_for_device()
69 {
70 echo -n "\
71 Please specify the target USB device to $COMMAND. You can type 'list' to
72 get a list of devices, type 'exit' or give an empty value to exit.
74 Device to use : "; read anser
75 while [ "$anser" == "list" ]; do
76 fdisk_list
77 echo -n "Device to use : "; read anser
78 done
79 if [ "$anser" = "" -o "$anser" = "exit" ]; then
80 echo -e "\nNo specified device or exit.\n"
81 exit 0
82 else
83 DEVICE=$anser
84 fi
85 }
87 # Verify a device exists before format or install
88 check_for_device()
89 {
90 IFDEV=`fdisk -l | grep $DEVICE`
91 if [ -z "$IFDEV" ]; then
92 echo -e "\nUnable to find device: $DEVICE\n"
93 exit 0
94 fi
95 }
97 #gets the UUID and filesystem type
98 get_part_info()
99 {
100 UUID=`blkid -s UUID -o value $DEVICE`
101 FSTYPE=`blkid -s TYPE -o value $DEVICE`
102 }
104 # Format target device and label partition.
105 mkfs_ext3()
106 {
107 echo -n "Please specify a label for the partition (TazUSB): "
108 read label
110 if [ -z $label ]; then
111 label=TazUSB
112 fi
114 echo "Label : $label"
115 echo "Mkfs : mkfs.ext3 -L \"$label\" $DEVICE"
116 echo "" && sleep 2
117 mkfs.ext3 -L "$label" $DEVICE
119 }
121 # Mount an existing USB device.
122 unmount_target_usb()
123 {
124 # If mount point is in use, unmount
125 if mount | grep $TARGET_ROOT; then
126 umount $TARGET_ROOT
127 fi
129 # Device could be mounted elsewhere, so unmount
130 if mount | grep $DEVICE; then
131 echo "Unmounting USB target device..."
132 umount $DEVICE
133 fi
134 }
136 # Mount an existing USB device.
137 mount_target_usb()
138 {
139 echo "Mounting USB target device..."
140 mkdir -p $TARGET_ROOT
141 mount $DEVICE $TARGET_ROOT 2>/dev/null
142 }
144 # Mount SliTaz LiveCD to get needed files.
145 mount_cdrom()
146 {
147 echo "Mounting cdrom device..."
149 if mount | grep /media/cdrom; then
150 umount /media/cdrom
151 fi
153 mkdir -p /media/cdrom
154 mount -r -t iso9660 $CDROM /media/cdrom 2>/dev/null
156 if [ ! -f /media/cdrom/boot/rootfs.gz ]; then
157 echo -e "\nUnable to mount cdrom or to find a filesystem on it (rootfs.gz).\n"
158 exit 0
159 fi
160 }
162 # All needed files are in the boot direcory of the cdrom.
163 copy_cdrom_files()
164 {
165 echo -n "Copying needed files from cdrom..."
166 mkdir -p $TARGET_ROOT/boot
167 cp /media/cdrom/boot/bzImage $TARGET_ROOT/boot
168 cp /media/cdrom/boot/rootfs.gz $TARGET_ROOT/boot
169 status
170 }
172 install_mbr()
173 {
174 # MBR
175 DISK=${DEVICE%[1-99]}
176 if [ -f /usr/share/syslinux/mbr.bin ]; then
177 echo -n "Installing a new MBR to: $DISK"
178 cat /usr/share/syslinux/mbr.bin > $DISK
179 status
180 else
181 # Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
182 echo "No new MBR installed to: $DISK"
183 fi
184 }
186 # ext/syslinux install
187 install_boot()
188 {
189 #decide if we're installing syslinux or extlinux
190 if [ "$FSTYPE" = "vfat" ]; then
191 ST=syslinux
192 STC="syslinux -d /boot/syslinux/ $DEVICE"
193 STE=cfg
194 else
195 ST=extlinux
196 STC="extlinux --install $TARGET_ROOT/boot/$ST"
197 STE=conf
198 fi
200 echo "Installing bootloader: $ST"
201 mkdir -p $TARGET_ROOT/boot/$ST
202 $STC
204 # extlinux.conf / syslinux.cfg
205 cat > $TARGET_ROOT/boot/$ST/$ST.$STE << _EOT_
206 display $ST.msg
207 default slitaz
208 timeout 40
209 implicit 0
210 prompt 1
211 label slitaz
212 kernel /boot/bzImage
213 append initrd=/boot/rootfs.gz rw root=/dev/null home=$UUID
215 label previous
216 kernel /boot/bzImage
217 append initrd=/boot/previous.gz rw root=/dev/null home=$UUID
219 _EOT_
221 # Splash screen and help files.
222 cp /media/cdrom/boot/isolinux/isolinux.msg $TARGET_ROOT/boot/$ST/extlinux.msg
223 sed -i s/'SliTaz GNU/Linux'/'SliTaz GNU/Linux LiveUSB'/ $TARGET_ROOT/boot/$ST/extlinux.msg
224 cp /media/cdrom/boot/isolinux/splash.lss $TARGET_ROOT/boot/$ST
225 cp /media/cdrom/boot/isolinux/*.txt $TARGET_ROOT/boot/$ST
226 }
228 # Let user exit or reboot.
229 exit_or_reboot()
230 {
231 echo "==============================================================================="
232 echo ""
233 echo -n "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
234 read anser
235 if [ "$anser" == "reboot" ]; then
236 umount $TARGET_ROOT
237 umount /media/cdrom
238 reboot || reboot -f
239 else
240 umount /media/cdrom
241 echo ""
242 exit 0
243 fi
244 }
246 set_bootable()
247 {
248 # As the boot flag is toggable, need to check it before hand
249 DISK=${DEVICE%[1-99]}
250 ISSET=`fdisk -l $DISK | grep $DEVICE | grep "\*"`
252 # If not set, set bootable
253 if [ "$ISSET" == "" ]; then
254 umount $TARGET_ROOT 2>/dev/null
255 echo -n "Setting $DEVICE as bootable..."
256 fdisk $DISK >/dev/null << EOF
257 a
258 1
259 w
260 EOF
261 status
262 fi
263 }
265 # Generate a virtual swap file in /home/swap. SliTaz boot script
266 # will active it, usefull for low memory system
267 gen_swap_file()
268 {
269 echo ""
270 echo -en "\033[1mGen swap\033[0m
271 ===============================================================================
272 Generate a swap file in /home/swap that will be activate on each boot to have
273 more memory available (Empty value to exit).
275 Swap file in Mb : "
276 read size
277 if [ -z "$size" ]; then
278 echo -e "\nEmpty value. Exiting...\n"
279 exit 0
280 fi
281 cd /home
282 # Sanity check
283 if [ -f swap ]; then
284 swapoff swap 2>/dev/null
285 fi
286 # DD to gen a virtual disk.
287 dd if=/dev/zero of=swap bs=1M count=$size
288 # Make swap filesystem.
289 mkswap swap
290 swapon swap
291 echo ""
292 }
294 #
295 # Tazusb sequence
296 #
298 case $COMMAND in
299 writefs)
300 #writefs to rootfs.gz
301 check_root
302 if [ -z $2 ]; then
303 COMPRESSION=none
304 else
305 COMPRESSION=$2
306 fi
307 #start info
308 echo ""
309 echo -e "\033[1mWrite filesystem\033[0m
310 ===============================================================================
311 The command writefs will write all the current filesystem into a suitable cpio
312 archive (rootfs.gz) usable on a bootable LiveUSB media.
314 Archive compression: $COMPRESSION"
315 echo ""
317 #clear out tazpkg cache
318 rm /var/cache/tazpkg/* -r -f
320 #optionally remove sound card selection
321 echo -n "Do you wish to remove the sound card selection (Yes/no/exit) ? "
322 read anser
323 case $anser in
324 e|E|"exit"|Exit)
325 exit 0
326 ;;
327 y|Y|yes|Yes)
328 echo -n "Removing current sound card selection..."
329 rm -f /var/lib/sound-card-driver
330 rm -f /etc/asound.state
331 ;;
332 *)
333 echo -n "Keeping current sound card selection..."
334 ;;
335 esac
336 status
338 #create list of files
339 find /bin /etc /init /sbin /var /dev /lib /mnt /root /usr >/tmp/list
341 for dir in /home /proc /sys /tmp /media /media/cdrom /media/flash /media/usbdisk
342 do
343 echo $dir >>/tmp/list
344 done
346 #gen initramfs with specified compression
347 if [ "$COMPRESSION" = "lzma" ]; then
348 echo -n "Creating rootfs.gz with lzma compression... "
349 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
351 elif [ "$COMPRESSION" = "gzip" ]; then
352 echo -n "Creating rootfs.gz with gzip compression... "
353 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
355 else
356 echo -n "Creating rootfs.gz without compression... "
357 cat /tmp/list | cpio -o -H newc > /rootfs.gz
358 fi
360 #get initramfs size
361 size=`du -sh /rootfs.gz | cut -f 1`
363 #if the bootable medium is where it should be, copy across
365 if (test -e /home/boot/bzImage); then
366 echo "Moving rootfs.gz to media. Remember to unmount for delayed writes!"
368 #move the old filesystem with the unix timestamp for reference
369 if (test -e /home/boot/previous.gz); then
370 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
371 fi
373 mv /home/boot/rootfs.gz /home/boot/previous.gz
374 mv /rootfs.gz /home/boot/.
375 else
376 echo "rootfs.gz is located in /"
377 fi
379 echo "==============================================================================="
380 echo "Root filesystem size: $size"
381 echo ""
382 echo -en "----\nENTER to continue..."; read i
383 ;;
384 format)
385 # Format a partitions in ext3.
386 check_root
387 echo ""
388 echo -e "\033[1mFormat a device\033[0m"
389 echo "==============================================================================="
390 DEVICE=$2
391 if [ -z $DEVICE ]; then
392 ask_for_device
393 check_for_device
394 else
395 echo "Device : $DEVICE"
396 fi
397 mkfs_ext3
398 echo "==============================================================================="
399 echo "Device $label ($DEVICE) is ready to use as LiveUSB and/or /home partition."
400 echo ""
401 ;;
402 gen-liveusb)
403 # Generate a LiveUSB media using files from a LiveCD.
404 check_root
405 echo ""
406 echo -e "\033[1mGen a LiveUSB media\033[0m"
407 echo "==============================================================================="
408 DEVICE=$2
409 if [ -z $DEVICE ]; then
410 ask_for_device
411 fi
413 check_for_device
414 mount_cdrom
415 get_part_info
416 unmount_target_usb
417 install_mbr
418 set_bootable
419 mount_target_usb
420 copy_cdrom_files
421 install_boot
422 exit_or_reboot
423 ;;
424 gen-swap)
425 check_root
426 gen_swap_file
427 ;;
428 usage|*)
429 # Display usage by default.
430 usage
431 ;;
432 esac
434 exit 0