tazusb view tazusb @ rev 19

Fix sed on *linux.msg + F1,F2, etc for help files
author Christophe Lincoln <pankso@slitaz.org>
date Thu Mar 13 22:16:00 2008 +0100 (2008-03-13)
parents 479ea6c5562e
children 323918d9c714
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 label slitaz
209 kernel /boot/bzImage
210 append initrd=/boot/rootfs.gz rw root=/dev/null home=$UUID
212 label previous
213 kernel /boot/bzImage
214 append initrd=/boot/previous.gz rw root=/dev/null home=$UUID
216 implicit 0
217 prompt 1
218 timeout 40
219 F1 help.txt
220 F2 options.txt
221 F3 $ST.msg
222 F4 display.txt
223 _EOT_
225 # Splash screen and help files.
226 cp /media/cdrom/boot/isolinux/isolinux.msg $TARGET_ROOT/boot/$ST/extlinux.msg
227 sed -i s/'SliTaz GNU\/Linux'/'SliTaz GNU\/Linux LiveUSB'/ $TARGET_ROOT/boot/$ST/extlinux.msg
228 cp /media/cdrom/boot/isolinux/splash.lss $TARGET_ROOT/boot/$ST
229 cp /media/cdrom/boot/isolinux/*.txt $TARGET_ROOT/boot/$ST
230 }
232 # Let user exit or reboot.
233 exit_or_reboot()
234 {
235 echo "==============================================================================="
236 echo ""
237 echo -n "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
238 read anser
239 if [ "$anser" == "reboot" ]; then
240 umount $TARGET_ROOT
241 umount /media/cdrom
242 reboot || reboot -f
243 else
244 umount /media/cdrom
245 echo ""
246 exit 0
247 fi
248 }
250 set_bootable()
251 {
252 # As the boot flag is toggable, need to check it before hand
253 DISK=${DEVICE%[1-99]}
254 ISSET=`fdisk -l $DISK | grep $DEVICE | grep "\*"`
256 # If not set, set bootable
257 if [ "$ISSET" == "" ]; then
258 umount $TARGET_ROOT 2>/dev/null
259 echo -n "Setting $DEVICE as bootable..."
260 fdisk $DISK >/dev/null << EOF
261 a
262 1
263 w
264 EOF
265 status
266 fi
267 }
269 # Generate a virtual swap file in /home/swap. SliTaz boot script
270 # will active it, usefull for low memory system
271 gen_swap_file()
272 {
273 echo ""
274 echo -en "\033[1mGen swap\033[0m
275 ===============================================================================
276 Generate a swap file in /home/swap that will be activate on each boot to have
277 more memory available (Empty value to exit).
279 Swap file in Mb : "
280 read size
281 if [ -z "$size" ]; then
282 echo -e "\nEmpty value. Exiting...\n"
283 exit 0
284 fi
285 cd /home
286 # Sanity check
287 if [ -f swap ]; then
288 swapoff swap 2>/dev/null
289 fi
290 # DD to gen a virtual disk.
291 dd if=/dev/zero of=swap bs=1M count=$size
292 # Make swap filesystem.
293 mkswap swap
294 swapon swap
295 echo ""
296 }
298 #
299 # Tazusb sequence
300 #
302 case $COMMAND in
303 writefs)
304 #writefs to rootfs.gz
305 check_root
306 if [ -z $2 ]; then
307 COMPRESSION=none
308 else
309 COMPRESSION=$2
310 fi
311 #start info
312 echo ""
313 echo -e "\033[1mWrite filesystem\033[0m
314 ===============================================================================
315 The command writefs will write all the current filesystem into a suitable cpio
316 archive (rootfs.gz) usable on a bootable LiveUSB media.
318 Archive compression: $COMPRESSION"
319 echo ""
321 #clear out tazpkg cache
322 rm /var/cache/tazpkg/* -r -f
324 #optionally remove sound card selection
325 echo -n "Do you wish to remove the sound card selection (Yes/no/exit) ? "
326 read anser
327 case $anser in
328 e|E|"exit"|Exit)
329 exit 0
330 ;;
331 y|Y|yes|Yes)
332 echo -n "Removing current sound card selection..."
333 rm -f /var/lib/sound-card-driver
334 rm -f /etc/asound.state
335 ;;
336 *)
337 echo -n "Keeping current sound card selection..."
338 ;;
339 esac
340 status
342 #create list of files
343 find /bin /etc /init /sbin /var /dev /lib /mnt /root /usr >/tmp/list
345 for dir in /home /proc /sys /tmp /media /media/cdrom /media/flash /media/usbdisk
346 do
347 echo $dir >>/tmp/list
348 done
350 #gen initramfs with specified compression
351 if [ "$COMPRESSION" = "lzma" ]; then
352 echo -n "Creating rootfs.gz with lzma compression... "
353 cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
355 elif [ "$COMPRESSION" = "gzip" ]; then
356 echo -n "Creating rootfs.gz with gzip compression... "
357 cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
359 else
360 echo -n "Creating rootfs.gz without compression... "
361 cat /tmp/list | cpio -o -H newc > /rootfs.gz
362 fi
364 #get initramfs size
365 size=`du -sh /rootfs.gz | cut -f 1`
367 #if the bootable medium is where it should be, copy across
369 if (test -e /home/boot/bzImage); then
370 echo "Moving rootfs.gz to media. Remember to unmount for delayed writes!"
372 #move the old filesystem with the unix timestamp for reference
373 if (test -e /home/boot/previous.gz); then
374 mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
375 fi
377 mv /home/boot/rootfs.gz /home/boot/previous.gz
378 mv /rootfs.gz /home/boot/.
379 else
380 echo "rootfs.gz is located in /"
381 fi
383 echo "==============================================================================="
384 echo "Root filesystem size: $size"
385 echo ""
386 echo -en "----\nENTER to continue..."; read i
387 ;;
388 format)
389 # Format a partitions in ext3.
390 check_root
391 echo ""
392 echo -e "\033[1mFormat a device\033[0m"
393 echo "==============================================================================="
394 DEVICE=$2
395 if [ -z $DEVICE ]; then
396 ask_for_device
397 check_for_device
398 else
399 echo "Device : $DEVICE"
400 fi
401 mkfs_ext3
402 echo "==============================================================================="
403 echo "Device $label ($DEVICE) is ready to use as LiveUSB and/or /home partition."
404 echo ""
405 ;;
406 gen-liveusb)
407 # Generate a LiveUSB media using files from a LiveCD.
408 check_root
409 echo ""
410 echo -e "\033[1mGen a LiveUSB media\033[0m"
411 echo "==============================================================================="
412 DEVICE=$2
413 if [ -z $DEVICE ]; then
414 ask_for_device
415 fi
417 check_for_device
418 mount_cdrom
419 get_part_info
420 unmount_target_usb
421 install_mbr
422 set_bootable
423 mount_target_usb
424 copy_cdrom_files
425 install_boot
426 exit_or_reboot
427 ;;
428 gen-swap)
429 check_root
430 gen_swap_file
431 ;;
432 usage|*)
433 # Display usage by default.
434 usage
435 ;;
436 esac
438 exit 0