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
files tazusb
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tazusb	Tue Mar 04 21:20:30 2008 +0000
     1.3 @@ -0,0 +1,389 @@
     1.4 +#!/bin/sh
     1.5 +# Tazusb - SliTaz LiveUSB
     1.6 +#
     1.7 +# Tazusb is an utility to generate, configure and manipulate SliTaz LiveUSB
     1.8 +# bootable media and/or USB /home partition, such as flash keys, SD card or 
     1.9 +# USB harddisk.
    1.10 +#
    1.11 +# Authors : Christophe Lincoln (Pankso) <pankso@slitaz.org>
    1.12 +#           Andrew Miller (Spode) <spode@spodesabode.com>
    1.13 +#
    1.14 +VERSION=20080304
    1.15 +
    1.16 +COMMAND=$1
    1.17 +TARGET_ROOT=/media/flash
    1.18 +DRIVE_NAME=`cat /proc/sys/dev/cdrom/info | grep "drive name" | cut -f 3`
    1.19 +CDROM=/dev/$DRIVE_NAME
    1.20 +
    1.21 +#
    1.22 +# Tazusb functions
    1.23 +#
    1.24 +
    1.25 +# Print the usage.
    1.26 +usage ()
    1.27 +{
    1.28 +	echo -e "\nSliTaz Live USB - Version: $VERSION\n
    1.29 +\033[1mUsage: \033[0m `basename $0` [command] [compression|device]
    1.30 +\033[1mCommands: \033[0m\n
    1.31 +  usage     	Print this short usage.
    1.32 +  writefs 	Write the current filesystem to rootfs.gz.
    1.33 +  		tazSupported compression: lzma. gzip, none.
    1.34 +  format	Format and label device with ext3 filesystem (for LiveUSB or /home).
    1.35 +  gen-liveusb 	Generate a bootable LiveUSB using files from the LiveCD.\n"
    1.36 +}
    1.37 +
    1.38 +# Status function.
    1.39 +status()
    1.40 +{
    1.41 +	local CHECK=$?
    1.42 +	echo -en "\\033[70G[ "
    1.43 +	if [ $CHECK = 0 ]; then
    1.44 +		echo -en "\\033[1;33mOK"
    1.45 +	else
    1.46 +		echo -en "\\033[1;31mFailed"
    1.47 +	fi
    1.48 +	echo -e "\\033[0;39m ]"
    1.49 +}
    1.50 +
    1.51 +# Exit if user is not root.
    1.52 +check_root()
    1.53 +{
    1.54 +	if test $(id -u) != 0 ; then
    1.55 +	   echo -e "\nThis program requires being run as root.\n"
    1.56 +	   exit 0
    1.57 +	fi
    1.58 +}
    1.59 +
    1.60 +# Verify a device exists before format or install
    1.61 +check_for_device()
    1.62 +{
    1.63 +	DEVID=`fdisk -l | grep -w $DEVICE | cut -d: -f1 | cut -d/ -f3`
    1.64 +	if [ -z "$DEVID" ]; then
    1.65 +		echo -e "\nUnable to find device: $DEVICE\n"
    1.66 +		exit 0
    1.67 +	fi
    1.68 +	
    1.69 +	PARTID=/dev/"$DEVID"1
    1.70 +}
    1.71 +
    1.72 +#gets the UUID and filesystem type
    1.73 +get_part_info()
    1.74 +{
    1.75 +	UUID=`blkid -s UUID -o value $PARTID`
    1.76 +	FSTYPE=`blkid -s TYPE -o value $PARTID`
    1.77 +}
    1.78 +
    1.79 +# Format target device and label partition.
    1.80 +mkfs_ext3()
    1.81 +{
    1.82 +	echo -n "Please specify a label for the partition (TazUSB): "
    1.83 +	read label
    1.84 +	
    1.85 +	if [ -z $label ]; then
    1.86 +		label=TazUSB
    1.87 +	fi
    1.88 +	
    1.89 +	echo "Label  : $label"
    1.90 +	echo "Mkfs   : mkfs.ext3 -L \"$label\" $PARTID"
    1.91 +	echo "" && sleep 2
    1.92 +	mkfs.ext3 -L "$label" $PARTID
    1.93 +	
    1.94 +}
    1.95 +
    1.96 +# Mount an existing USB device.
    1.97 +unmount_target_usb()
    1.98 +{
    1.99 +	# If mount point is in use, unmount
   1.100 +	if mount | grep $TARGET_ROOT; then
   1.101 +		umount $TARGET_ROOT
   1.102 +	fi
   1.103 +	
   1.104 +	# Device could be mounted elsewhere, so unmount
   1.105 +	if mount | grep $PARTID; then
   1.106 +		echo "Unmounting USB target device..."
   1.107 +		umount $PARTID
   1.108 +	fi
   1.109 +}
   1.110 +
   1.111 +# Mount an existing USB device.
   1.112 +mount_target_usb()
   1.113 +{
   1.114 +	# If mount point is in use, unmount
   1.115 +	if mount | grep $TARGET_ROOT; then
   1.116 +		umount $TARGET_ROOT
   1.117 +	fi
   1.118 +	
   1.119 +	# Device could be mounted elsewhere, so unmount
   1.120 +	if mount | grep $PARTID; then
   1.121 +		umount $PARTID
   1.122 +	fi
   1.123 +
   1.124 +	echo "Mounting USB target device..."	
   1.125 +	mkdir -p $TARGET_ROOT
   1.126 +	mount $PARTID $TARGET_ROOT 2>/dev/null
   1.127 +}
   1.128 +
   1.129 +# Mount SliTaz LiveCD to get needed files.
   1.130 +mount_cdrom()
   1.131 +{
   1.132 +	echo "Mounting cdrom device..."
   1.133 +	
   1.134 +	if mount | grep /media/cdrom; then
   1.135 +		umount /media/cdrom
   1.136 +	fi
   1.137 +	
   1.138 +	mkdir -p /media/cdrom
   1.139 +	mount -t iso9660 $CDROM /media/cdrom
   1.140 +	
   1.141 +	if [ ! -f /media/cdrom/boot/rootfs.gz ]; then
   1.142 +		echo -e "\nUnable to find a filesystem on the cdrom (rootfs.gz).\n"
   1.143 +		exit 0
   1.144 +	fi
   1.145 +}
   1.146 +
   1.147 +# All needed files are in the boot direcory of the cdrom.
   1.148 +copy_cdrom_files()
   1.149 +{
   1.150 +	echo -n "Copying needed files from cdrom..."
   1.151 +	mkdir -p $TARGET_ROOT/boot
   1.152 +	cp /media/cdrom/boot/bzImage $TARGET_ROOT/boot
   1.153 +	cp /media/cdrom/boot/rootfs.gz $TARGET_ROOT/boot
   1.154 +	status
   1.155 +}
   1.156 +
   1.157 +install_mbr()
   1.158 +{
   1.159 +	# MBR
   1.160 +	if [ -f /usr/share/syslinux/mbr.bin ]; then
   1.161 +		echo -n "Installing a new MBR to: $DEVICE"
   1.162 +		cat /usr/share/syslinux/mbr.bin > $DEVICE
   1.163 +		status
   1.164 +	else
   1.165 +		# Skip MBR install (tazpkg get-install syslinux-extra ? and then cat)
   1.166 +		echo "No new MBR installed to: $DEVICE"
   1.167 +	fi
   1.168 +}
   1.169 +
   1.170 +# ext/syslinux install
   1.171 +install_boot()
   1.172 +{
   1.173 +	#decide if we're installing syslinux or extlinux
   1.174 +	if [ "$FSTYPE" = "vfat" ]; then
   1.175 +		ST=syslinux
   1.176 +		STC="syslinux -d /boot/syslinux/ $PARTID"
   1.177 +		STE=cfg
   1.178 +	else
   1.179 +		ST=extlinux
   1.180 +		STC="extlinux --install $TARGET_ROOT/boot/$ST"
   1.181 +		STE=conf
   1.182 +	fi
   1.183 +	
   1.184 +	echo "Installing bootloader: $ST"
   1.185 +	mkdir -p $TARGET_ROOT/boot/$ST
   1.186 +	$STC
   1.187 +	
   1.188 +	# extlinux.conf / syslinux.cfg
   1.189 +	cat > $TARGET_ROOT/boot/$ST/$ST.$STE << _EOT_
   1.190 +display display.txt
   1.191 +default slitaz
   1.192 +label slitaz
   1.193 +    kernel /boot/bzImage
   1.194 +    append initrd=/boot/rootfs.gz rw root=/dev/null home=$UUID
   1.195 +
   1.196 +label previous
   1.197 +    kernel /boot/bzImage
   1.198 +    append initrd=/boot/previous.gz rw root=/dev/null home=$UUID
   1.199 +
   1.200 +_EOT_
   1.201 +
   1.202 +	# display.txt 
   1.203 +	cat > $TARGET_ROOT/boot/$ST/display.txt << "EOT"
   1.204 +       _______. __       __  .___________.    ___      ________  
   1.205 +      /       ||  |     |  | |          |   /   \    |       /  
   1.206 +     |   (----`|  |     |  | `---|  |---`  /  ^  \   `---/  /   
   1.207 +      \   \    |  |     |  |     |  |     /  /_\  \     /  /    
   1.208 +  .----)   |   |  `----.|  |     |  |    /  _____  \   /  /----.
   1.209 +  |_______/    |_______||__|     |__|   /__/     \__\ /________|
   1.210 +
   1.211 +
   1.212 +                       SliTaz GNU/Linux LiveUSB
   1.213 +            Simple Light Incredible Temporary Autonomus Zone
   1.214 +
   1.215 +
   1.216 +EOT
   1.217 +status
   1.218 +}
   1.219 +
   1.220 +# Let user exit or reboot.
   1.221 +exit_or_reboot()
   1.222 +{
   1.223 +	echo ""
   1.224 +	echo -n "Do you want to exit Tazusb or reboot system (Exit/reboot) ? "
   1.225 +	read anser
   1.226 +	if [ "$anser" == "reboot" ]; then
   1.227 +		umount $TARGET_ROOT
   1.228 +		umount /media/cdrom
   1.229 +		reboot || reboot -f
   1.230 +	else
   1.231 +		umount /media/cdrom
   1.232 +		echo "==============================================================================="
   1.233 +		echo ""
   1.234 +		exit 0
   1.235 +	fi
   1.236 +}
   1.237 +
   1.238 +set_bootable()
   1.239 +{
   1.240 +	# As the boot flag is toggable, need to check it before hand
   1.241 +	ISSET=`fdisk -l $DEVICE | grep $DEVICE | grep "*"`
   1.242 +	
   1.243 +	# If not set, set bootable
   1.244 +	if [ -z "$ISSET" ]; then
   1.245 +	umount $DEVICE
   1.246 +	echo "Setting $PARTID as bootable..."
   1.247 +	fdisk $DEVICE >/dev/null << EOF
   1.248 +a
   1.249 +1
   1.250 +w
   1.251 +EOF
   1.252 +	status
   1.253 +	fi
   1.254 +}
   1.255 +
   1.256 +#
   1.257 +# Tazusb sequence
   1.258 +#
   1.259 +
   1.260 +case $COMMAND in
   1.261 +	writefs)
   1.262 +		#writefs to rootfs.gz
   1.263 +		check_root
   1.264 +		if [ -z $2 ]; then
   1.265 +			COMPRESSION=none
   1.266 +		else
   1.267 +			COMPRESSION=$2
   1.268 +		fi
   1.269 +		#start info
   1.270 +		echo ""
   1.271 +		echo -e "\033[1mWrite filesystem\033[0m
   1.272 +===============================================================================
   1.273 +The command writefs will write all the current filesystem into a suitable cpio 
   1.274 +archive (rootfs.gz) usable on a bootable LiveUSB media. 
   1.275 +
   1.276 +Archive compression: $COMPRESSION"
   1.277 +		echo ""
   1.278 +		
   1.279 +		#clear out tazpkg cache
   1.280 +		rm /var/cache/tazpkg/* -r -f
   1.281 +
   1.282 +		#optionally remove sound card selection
   1.283 +		echo -n "Do you wish to remove the sound card selection (Yes/no/exit) ? "
   1.284 +		read answer
   1.285 +		answer=`echo "$answer:0:1}" | tr A-Z a-z`
   1.286 +		
   1.287 +		if [ "$answer" = "e"]; then
   1.288 +			exit 0
   1.289 +		fi
   1.290 +		
   1.291 +		if [ "$answer" = "y" ]; then
   1.292 +			echo -n "Removing current sound card selection..."
   1.293 +			rm -f /var/lib/sound-card-driver
   1.294 +			rm -f /etc/asound.state
   1.295 +		else
   1.296 +			echo -n "Keeping current sound card selection..."
   1.297 +		fi
   1.298 +		status
   1.299 +		
   1.300 +		#create list of files
   1.301 +		find /bin /etc /init /sbin /var /dev /lib /mnt /root /usr >/tmp/list
   1.302 +
   1.303 +		for dir in /home /proc /sys /tmp /media /media/cdrom /media/flash /media/usbdisk
   1.304 +		do
   1.305 +			echo $dir >>/tmp/list
   1.306 +		done
   1.307 +
   1.308 +		#gen initramfs with specified compression
   1.309 +		if [ "$COMPRESSION" = "lzma" ]; then
   1.310 +			echo -n "Creating rootfs.gz with lzma compression... "
   1.311 +			cat /tmp/list | cpio -o -H newc | lzma e -si -so > /rootfs.gz
   1.312 +
   1.313 +		elif [ "$COMPRESSION" = "gzip" ]; then
   1.314 +			echo -n "Creating rootfs.gz with gzip compression... "
   1.315 +			cat /tmp/list | cpio -o -H newc | gzip -9 > /rootfs.gz
   1.316 +
   1.317 +		else
   1.318 +			echo -n "Creating rootfs.gz without compression... "
   1.319 +			cat /tmp/list | cpio -o -H newc > /rootfs.gz
   1.320 +		fi
   1.321 +
   1.322 +		#get initramfs size
   1.323 +		size=`du -sh /rootfs.gz | cut -f 1`
   1.324 +
   1.325 +		#if the bootable medium is where it should be, copy across
   1.326 +
   1.327 +		if (test -e /home/boot/bzImage); then
   1.328 +			echo "Moving rootfs.gz to media. Remember to unmount for delayed writes!"
   1.329 +
   1.330 +			#move the old filesystem with the unix timestamp for reference
   1.331 +			if (test -e /home/boot/previous.gz); then
   1.332 +				mv /home/boot/previous.gz /home/boot/rootfs.gz.$(date +%s)
   1.333 +			fi
   1.334 +			
   1.335 +			mv /home/boot/rootfs.gz /home/boot/previous.gz
   1.336 +			mv /rootfs.gz /home/boot/.
   1.337 +		else
   1.338 +			echo "rootfs.gz is located in /"
   1.339 +		fi
   1.340 +
   1.341 +		echo "==============================================================================="
   1.342 +		echo "Root filesystem size: $size"
   1.343 +		echo ""
   1.344 +		;;
   1.345 +	format)
   1.346 +		# Format a partitions in ext3.
   1.347 +		check_root
   1.348 +		echo ""
   1.349 +		echo -e "\033[1mFormat a device\033[0m"
   1.350 +		echo "==============================================================================="
   1.351 +		DEVICE=$2
   1.352 +		if [ -z $DEVICE ]; then
   1.353 +			echo -e "\nPlease specify a device to format: tazusb $COMMAND /dev/name\n"
   1.354 +			exit 0
   1.355 +		fi
   1.356 +		check_for_device
   1.357 +		echo "Device : $DEVICE"
   1.358 +		mkfs_ext3
   1.359 +		echo "==============================================================================="
   1.360 +		echo "Device $label ($PARTID) is ready to use as LiveUSB and/or /home partition."
   1.361 +		echo ""
   1.362 +		;;
   1.363 +	gen-liveusb)
   1.364 +		# Generate a LiveUSB media using files from a LiveCD.
   1.365 +		check_root
   1.366 +		echo ""
   1.367 +		echo -e "\033[1mGen a LiveUSB media\033[0m"
   1.368 +		echo "==============================================================================="
   1.369 +		DEVICE=$2
   1.370 +		if [ -z $DEVICE ]; then
   1.371 +			echo -e "\No device specified. Usage: tazusb $CAMMAND /dev/name\n"
   1.372 +			exit 0
   1.373 +		fi
   1.374 +		
   1.375 +		check_for_device
   1.376 +		get_part_info
   1.377 +		unmount_target_usb
   1.378 +		install_mbr
   1.379 +		set_bootable
   1.380 +		mount_target_usb
   1.381 +		mount_cdrom
   1.382 +		copy_cdrom_files
   1.383 +		install_boot
   1.384 +		exit_or_reboot
   1.385 +		;;
   1.386 +	usage|*)
   1.387 +		# Display usage by default.
   1.388 +		usage
   1.389 +		;;
   1.390 +esac
   1.391 +
   1.392 +exit 0