tazlito rev 101

Add tazlito merge
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Aug 08 10:07:24 2009 +0200 (2009-08-08)
parents 9b31f28a4eb0
children 329dcaa0bd9b
files tazlito
line diff
     1.1 --- a/tazlito	Fri Aug 07 11:57:01 2009 +0200
     1.2 +++ b/tazlito	Sat Aug 08 10:07:24 2009 +0200
     1.3 @@ -88,6 +88,7 @@
     1.4    gen-distro      Generate a Live distro and ISO from a list of packages.
     1.5    clean-distro    Remove all files generated by gen-distro.
     1.6    check-distro    Help to check if distro is ready to release.
     1.7 +  merge           Merge multiple rootfs into one iso.
     1.8    burn-iso        Burn ISO image to a cdrom using Wodim.\n"
     1.9  }
    1.10  
    1.11 @@ -102,6 +103,7 @@
    1.12  		echo -en "\\033[1;31mFailed"
    1.13  	fi
    1.14  	echo -e "\\033[0;39m ]"
    1.15 +	return $CHECK
    1.16  }
    1.17  
    1.18  yesorno()
    1.19 @@ -226,6 +228,19 @@
    1.20  	fi
    1.21  }
    1.22  
    1.23 +create_iso()
    1.24 +{
    1.25 +	genisoimage -R -o $1 -b boot/isolinux/isolinux.bin \
    1.26 + 		-c boot/isolinux/boot.cat -no-emul-boot -boot-load-size 4 \
    1.27 +		-V "$VOLUM_NAME" -p "$PREPARED" -input-charset iso8859-1 \
    1.28 +		-boot-info-table $2
    1.29 +	if [ -x /usr/bin/isohybrid ]; then
    1.30 +		echo -n "Create hybrid ISO..."
    1.31 +		/usr/bin/isohybrid $1 2> /dev/null
    1.32 +		status
    1.33 +	fi
    1.34 +}
    1.35 +
    1.36  # Generate a new ISO image using isolinux.
    1.37  gen_livecd_isolinux()
    1.38  {
    1.39 @@ -251,15 +266,7 @@
    1.40  	echo ""
    1.41  	echo -e "\033[1mGenerating ISO image\033[0m"
    1.42  	echo "================================================================================"
    1.43 -	genisoimage -R -o $ISO_NAME.iso -b boot/isolinux/isolinux.bin \
    1.44 -		-c boot/isolinux/boot.cat -no-emul-boot -boot-load-size 4 \
    1.45 -		-V "$VOLUM_NAME" -p "$PREPARED" -input-charset iso8859-1 \
    1.46 -		-boot-info-table $ROOTCD
    1.47 -	if [ -x /usr/bin/isohybrid ]; then
    1.48 -		echo -n "Create hybrid ISO..."
    1.49 -		/usr/bin/isohybrid $ISO_NAME.iso 2> /dev/null
    1.50 -		status
    1.51 -	fi
    1.52 +	create_iso $ISO_NAME.iso $ROOTCD
    1.53  	echo -n "Creating the ISO md5sum..."
    1.54  	md5sum $ISO_NAME.iso > $ISO_NAME.md5
    1.55  	status
    1.56 @@ -268,6 +275,22 @@
    1.57  	genisohooks final
    1.58  }
    1.59  
    1.60 +# Pack rootfs
    1.61 +pack_rootfs()
    1.62 +{
    1.63 +	( cd $1 ; find . -print | cpio -o -H newc ) | \
    1.64 +	if [ "$COMPRESSION" = "none" ]; then
    1.65 +		echo -n "Generating uncompressed initramfs... "
    1.66 +		cat > $2
    1.67 +	elif [ -x /usr/bin/lzma -a "$COMPRESSION" != "gzip" ]; then
    1.68 +		echo -n "Generating lzma'ed initramfs... "
    1.69 +		lzma e -si -so -d24 > $2
    1.70 +	else
    1.71 +		echo -n "Generating gziped initramfs... "
    1.72 +		gzip -9 > $2
    1.73 +	fi
    1.74 +}
    1.75 +
    1.76  # Generate a new initramfs from the root filesystem.
    1.77  gen_initramfs()
    1.78  {
    1.79 @@ -296,16 +319,7 @@
    1.80          )
    1.81  
    1.82  	# Use lzma if installed
    1.83 -	if [ "$COMPRESSION" = "none" ]; then
    1.84 -		echo -n "Generating uncompressed initramfs... "
    1.85 -		find . -print | cpio -o -H newc > $DISTRO/$INITRAMFS
    1.86 -	elif [ -x /usr/bin/lzma -a "$COMPRESSION" != "gzip" ]; then
    1.87 -		echo -n "Generating lzma'ed initramfs... "
    1.88 -		find . -print | cpio -o -H newc | lzma e -si -so -d24 > $DISTRO/$INITRAMFS
    1.89 -	else
    1.90 -		echo -n "Generating gziped initramfs... "
    1.91 -		find . -print | cpio -o -H newc | gzip -9 > $DISTRO/$INITRAMFS
    1.92 -	fi
    1.93 +	pack_rootfs . $DISTRO/$INITRAMFS
    1.94  	cd $DISTRO
    1.95  	mv $INITRAMFS $ROOTCD/boot
    1.96  }
    1.97 @@ -367,6 +381,51 @@
    1.98  EOF
    1.99  }
   1.100  
   1.101 +# extract rootfs.gz somewhere
   1.102 +extract_rootfs()
   1.103 +{
   1.104 +	(zcat $1 || unlzma -c $1 || cat $1) 2>/dev/null | \
   1.105 +		(cd $2; cpio -idm > /dev/null)
   1.106 +}
   1.107 +
   1.108 +# Remove duplicate files
   1.109 +mergefs()
   1.110 +{
   1.111 +	# merge symlinks files and devices
   1.112 +	( cd $1; find ) | while read file; do
   1.113 +		if [ -L $1/$file ]; then
   1.114 +			[ -L $2/$file ] &&
   1.115 +			[ "$(readlink $1/$file)" == "$(readlink $2/$file)" ] &&
   1.116 +			rm -f $2/$file
   1.117 +		elif [ -f $1/$file ]; then
   1.118 +			[ -f $2/$file ] &&
   1.119 +			cmp $1/$file $2/$file > /dev/null 2>&1 && rm -f $2/$file
   1.120 +			[ -f $2/$file ] && 
   1.121 +			[ "$(basename $file)" == "volatile.cpio.gz" ] &&
   1.122 +			[ "$(dirname $(dirname $file))" == \
   1.123 +			  "./var/lib/tazpkg/installed" ] && rm -f $2/$file
   1.124 +		elif [ -b $1/$file ]; then
   1.125 +			[ -b $2/$file ] && rm -f $2/$file
   1.126 +		elif [ -c $1/$file ]; then
   1.127 +			[ -c $2/$file ] && rm -f $2/$file
   1.128 +		fi
   1.129 +	done
   1.130 +	
   1.131 +	# cleanup directories
   1.132 +	( cd $1; find ) | while read file; do
   1.133 +		if [ -d $1/$file ]; then
   1.134 +			[ -d $2/$file ] && rmdir $2/$file 2> /dev/null
   1.135 +		fi
   1.136 +	done
   1.137 +	true
   1.138 +}
   1.139 +
   1.140 +cleanup_merge()
   1.141 +{
   1.142 +	rm -rf $TMP_DIR
   1.143 +	exit 1
   1.144 +}
   1.145 +
   1.146  ####################
   1.147  # Tazlito commands #
   1.148  ####################
   1.149 @@ -532,9 +591,7 @@
   1.150  		# Extract initramfs.
   1.151  		cd $TARGET/rootfs
   1.152  		echo -n "Extracting the rootfs... "
   1.153 -		( zcat ../rootcd/boot/rootfs.gz 2>/dev/null || \
   1.154 -		  lzma d ../rootcd/boot/rootfs.?z -so 2>/dev/null || \
   1.155 -		  cat ../rootcd/boot/rootfs.gz ) | cpio -id
   1.156 +		extract_rootfs ../rootcd/boot/rootfs.gz $TARGET/rootfs
   1.157  		# unpack /usr
   1.158  		for i in etc/tazlito/*.extract; do
   1.159  			[ -f "$i" ] && . $i ../rootcd
   1.160 @@ -1043,6 +1100,144 @@
   1.161  		fi
   1.162  		echo ""
   1.163  		;;
   1.164 +	merge)
   1.165 +		# Merge multiple rootfs into one iso.
   1.166 +		#
   1.167 +		if [ -z "$2" ]; then
   1.168 +			cat << EOT
   1.169 +Usage: tazlito merge size1 iso size2 rootfs2 [sizeN rootfsN]...
   1.170 +
   1.171 +Merge multiple rootfs into one iso. Rootfs are like russian doll
   1.172 +i.e: rootfsN is a subset of rootfsN-1
   1.173 +rootfs1 is found in iso, sizeN is the RAM size need to launch rootfsN.
   1.174 +The boot loader will select the rootfs according to the RAM size detected.
   1.175 +
   1.176 +Example: 
   1.177 +$ tazlito merge 160M slitaz-core.iso 96M rootfs-justx.gz 32M rootfs-base.gz
   1.178 +
   1.179 +Will start slitaz-core with 160M+ RAM, slitaz-justX with 96M-160M RAM,
   1.180 +slitaz-base with 32M-96M RAM and display an error message if RAM < 32M.
   1.181 +EOT
   1.182 +			exit 2
   1.183 +		fi
   1.184 +
   1.185 +		shift	# skip merge
   1.186 +		append="append $(( (${1%M} - 3) * 1024 )) slitaz1"
   1.187 +		shift	# skip size1
   1.188 +		mkdir -p $TMP_DIR/mnt $TMP_DIR/rootfs1
   1.189 +
   1.190 +		ISO=$1.merged
   1.191 +		# Extract filesystems
   1.192 +		echo -n "Mount $1"
   1.193 +		mount -o loop,ro $1 $TMP_DIR/mnt 2> /dev/null
   1.194 +		status || cleanup_merge
   1.195 +		cp -a $TMP_DIR/mnt $TMP_DIR/iso
   1.196 +		rm -f $TMP_DIR/iso/boot/bzImage
   1.197 +		ln $TMP_DIR/iso/boot/vmlinuz* $TMP_DIR/iso/boot/bzImage
   1.198 +		umount -d $TMP_DIR/mnt
   1.199 +		if [ -f $TMP_DIR/iso/boot/rootfs1.gz ]; then
   1.200 +			echo "$1 is already a merged iso. Abort."
   1.201 +			cleanup_merge
   1.202 +		fi
   1.203 +		if [ ! -f $TMP_DIR/iso/boot/isolinux/ifmem.c32 ]; then
   1.204 +			if [ ! -f /boot/isolinux/ifmem.c32 ]; then
   1.205 +				cat <<EOT
   1.206 +No file /boot/isolinux/ifmem.c32
   1.207 +Please install syslinux package !
   1.208 +EOT
   1.209 +				rm -rf $TMP_DIR
   1.210 +				exit 1
   1.211 +			fi
   1.212 +			cp /boot/isolinux/ifmem.c32 $TMP_DIR/iso/boot/isolinux
   1.213 +		fi
   1.214 +		
   1.215 +		echo -n "Extract iso/rootfs.gz"
   1.216 +		extract_rootfs $TMP_DIR/iso/boot/rootfs.gz $TMP_DIR/rootfs1 &&
   1.217 +		[ -d $TMP_DIR/rootfs1/etc ]
   1.218 +		status || cleanup_merge
   1.219 +		n=1
   1.220 +		while [ -n "$2" ]; do
   1.221 +			shift	# skip rootfs N-1
   1.222 +			p=$n
   1.223 +			n=$(($n + 1))
   1.224 +			append="$append $(( (${1%M} - 3) * 1024 )) slitaz$n"
   1.225 +			shift	# skip size N
   1.226 +			mkdir -p $TMP_DIR/rootfs$n
   1.227 +			echo -n "Extract $1"
   1.228 +			extract_rootfs $1 $TMP_DIR/rootfs$n &&
   1.229 +			[ -d $TMP_DIR/rootfs$n/etc ]
   1.230 +			status || cleanup_merge
   1.231 +			echo -n "Merge rootfs$n into rootfs$p"
   1.232 +			mergefs $TMP_DIR/rootfs$n $TMP_DIR/rootfs$p
   1.233 +			status
   1.234 +			echo "Create rootfs$p.gz"
   1.235 +			pack_rootfs $TMP_DIR/rootfs$p $TMP_DIR/iso/boot/rootfs$p.gz
   1.236 +			status
   1.237 +		done
   1.238 +		echo "Create rootfs$n.gz"
   1.239 +		pack_rootfs $TMP_DIR/rootfs$n $TMP_DIR/iso/boot/rootfs$n.gz
   1.240 +		status
   1.241 +		rm -f $TMP_DIR/iso/boot/rootfs.gz
   1.242 +	
   1.243 +		echo -n "Update boot files"
   1.244 +		grep -l 'include common' $TMP_DIR/iso/boot/isolinux/*.cfg | \
   1.245 +		while read file ; do
   1.246 +			awk -v n=$n '{
   1.247 +if (/label/) label=$0;
   1.248 +else if (/kernel/) kernel=$0;
   1.249 +else if (/append/) {
   1.250 +	i=index($0,"rootfs.gz");
   1.251 +	append=substr($0,i+9);
   1.252 +}
   1.253 +else if (/include/) {
   1.254 +	for (i = 1; i <= n; i++) {
   1.255 +		print label i
   1.256 +		print kernel;
   1.257 +		initrd="initrd=/boot/rootfs" n ".gz"
   1.258 +		for (j = n - 1; j >= i; j--) {
   1.259 +			initrd=initrd ",/boot/rootfs" j ".gz";
   1.260 +		}
   1.261 +		printf "\tappend %s%s\n",initrd,append;
   1.262 +		print "";
   1.263 +	}
   1.264 +	print;
   1.265 +}
   1.266 +else print;
   1.267 +}' < $file > $file.$$
   1.268 +			mv -f $file.$$ $file
   1.269 +		done
   1.270 +		cat >> $TMP_DIR/iso/boot/isolinux/common.cfg <<EOT
   1.271 +label slitaz
   1.272 +	kernel /boot/isolinux/ifmem.c32
   1.273 +	$append noram
   1.274 +
   1.275 +label noram
   1.276 +	config noram.cfg
   1.277 +
   1.278 +EOT
   1.279 +		cat >> $TMP_DIR/iso/boot/isolinux/noram.cfg <<EOT
   1.280 +display isolinux.msg
   1.281 +say Not enough RAM to boot slitaz.
   1.282 +default reboot
   1.283 +label reboot
   1.284 +	com32 reboot.c32
   1.285 +
   1.286 +implicit 0
   1.287 +prompt 1
   1.288 +timeout 80
   1.289 +F1 help.txt
   1.290 +F2 options.txt
   1.291 +F3 isolinux.msg
   1.292 +F4 display.txt
   1.293 +F5 enhelp.txt
   1.294 +F6 enopts.txt
   1.295 +EOT
   1.296 +		status
   1.297 +		echo "Generate $ISO"
   1.298 +		create_iso $ISO $TMP_DIR/iso
   1.299 +		rm -rf $TMP_DIR
   1.300 +		;;
   1.301 +
   1.302  	usage|*)
   1.303  		# Clear and print usage also for all unknown commands.
   1.304  		#