cookutils rev 523 slitaz-tank

Add libcookiso.sh. This is to replace tazlito by making it into functions for cookiso.
author Christopher Rogers <slaxemulator@gmail.com>
date Fri Aug 24 22:00:29 2012 +0000 (2012-08-24)
parents b9412d979c1e
children 66caa5a9e7c5
files lib/libcookiso.sh
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lib/libcookiso.sh	Fri Aug 24 22:00:29 2012 +0000
     1.3 @@ -0,0 +1,1271 @@
     1.4 +#!/bin/sh
     1.5 +
     1.6 +# libcookiso functions
     1.7 +
     1.8 +. /usr/lib/slitaz/libcook.sh
     1.9 +
    1.10 +TOP_DIR="$(pwd)"
    1.11 +TMP_DIR=/tmp/cookiso-$$-$RANDOM
    1.12 +TMP_MNT=/media/cookiso-$$-$RANDOM
    1.13 +INITRAMFS=rootfs.gz
    1.14 +[ -f "/etc/slitaz/cookiso.conf" ] && CONFIG_FILE="/etc/slitaz/cookiso.conf"
    1.15 +[ -f "$TOP_DIR/cookiso.conf" ] && CONFIG_FILE="$TOP_DIR/cookiso.conf"
    1.16 +DEFAULT_MIRROR="$MIRROR_URL/packages/$SLITAZ_VERSION/"
    1.17 +
    1.18 +log=/var/log/tazlito.log
    1.19 +if check_root; then
    1.20 +	newline > $log
    1.21 +fi
    1.22 +
    1.23 +if [ ! "$CONFIG_FILE" = "" ] ; then
    1.24 +	. $CONFIG_FILE
    1.25 +else
    1.26 +	if [ "$COMMAND" = "gen-config" ] ; then
    1.27 +		continue
    1.28 +	else
    1.29 +		echo "Unable to find any configuration file. Please read the docs"
    1.30 +		echo "or run '`basename $0` gen-config' to get an empty config file."
    1.31 +		exit 0
    1.32 +	fi
    1.33 +fi
    1.34 +
    1.35 +# While Tazpkg is not used the default mirror url file does not exist
    1.36 +# and user can't recharge the list of flavors.
    1.37 +if test $(id -u) = 0 ; then
    1.38 +	if [ ! -f "$MIRROR" ]; then
    1.39 +		echo "$DEFAULT_MIRROR" > $MIRROR
    1.40 +	fi
    1.41 +fi
    1.42 +
    1.43 +# Set the rootfs and rootcd path with $DISTRO
    1.44 +# configuration variable.
    1.45 +ROOTFS=$DISTRO/rootfs
    1.46 +ROOTCD=$DISTRO/rootcd
    1.47 +
    1.48 +yesorno()
    1.49 +{
    1.50 +	echo -n "$1"
    1.51 +	case "$DEFAULT_ANSWER" in
    1.52 +	Y|y) answer="y";;
    1.53 +	N|n) answer="n";;
    1.54 +	*) read answer;;
    1.55 +	esac
    1.56 +}
    1.57 +
    1.58 +field()
    1.59 +{
    1.60 +	grep "^$1" "$2" | sed 's/.*: \([0-9KMG\.]*\).*/\1/'
    1.61 +}
    1.62 +
    1.63 +todomsg()
    1.64 +{
    1.65 +	echo -e "\\033[70G[ \\033[1;31mTODO\\033[0;39m ]"
    1.66 +}
    1.67 +
    1.68 +# Download a file from this mirror
    1.69 +download_from()
    1.70 +{
    1.71 +	local i
    1.72 +	local mirrors
    1.73 +	mirrors="$1"
    1.74 +	shift
    1.75 +	for i in $mirrors; do
    1.76 +		case "$i" in
    1.77 +		http://*|ftp://*) wget -c $i$@ && break;;
    1.78 +		*) cp $i/$1 . && break;;
    1.79 +		esac
    1.80 +	done
    1.81 +}
    1.82 +
    1.83 +# Download a file trying all mirrors
    1.84 +download()
    1.85 +{
    1.86 +	local i
    1.87 +	for i in $(cat $MIRROR $DB/undigest/*/mirror 2> /dev/null); do
    1.88 +		download_from "$i" "$@" && break
    1.89 +	done
    1.90 +}
    1.91 +
    1.92 +# Execute hooks provided by some packages
    1.93 +genisohooks()
    1.94 +{
    1.95 +	local here=`pwd`
    1.96 +	for i in $(ls $ROOTFS/etc/tazlito/*.$1 2> /dev/null); do
    1.97 +		cd $ROOTFS
    1.98 +		. $i $ROOTCD
    1.99 +	done
   1.100 +	cd $here
   1.101 +}
   1.102 +
   1.103 +cleanup()
   1.104 +{
   1.105 +	if [ -d $TMP_MNT ]; then
   1.106 +		umount $TMP_MNT
   1.107 +		rmdir $TMP_MNT
   1.108 +		rm -f /boot
   1.109 +	fi
   1.110 +}
   1.111 +
   1.112 +# Echo the package name if the tazpkg is already installed
   1.113 +installed_package_name()
   1.114 +{
   1.115 +	local tazpkg
   1.116 +	local package
   1.117 +	local VERSION
   1.118 +	local EXTRAVERSION
   1.119 +	tazpkg=$1
   1.120 +	# Try to find package name and version to be able
   1.121 +	# to repack it from installation
   1.122 +	# A dash (-) can exist in name *and* in version
   1.123 +	package=${tazpkg%-*}
   1.124 +	i=$package
   1.125 +	while true; do
   1.126 +		VERSION=""
   1.127 +		eval $(grep -s ^VERSION= $INSTALLED/$i/receipt)
   1.128 +		EXTRAVERSION=""
   1.129 +		eval $(grep -s ^EXTRAVERSION= $INSTALLED/$i/receipt)
   1.130 +		if [ "$i-$VERSION$EXTRAVERSION" = "$tazpkg" ]; then
   1.131 +			echo $i
   1.132 +			break
   1.133 +		fi
   1.134 +		case "$i" in
   1.135 +		*-*);;
   1.136 +		*) break;;
   1.137 +		esac
   1.138 +		i=${i%-*}
   1.139 +	done
   1.140 +}
   1.141 +
   1.142 +
   1.143 +# Check for the rootfs tree.
   1.144 +check_rootfs()
   1.145 +{
   1.146 +	if [ ! -d "$ROOTFS/etc" ] ; then
   1.147 +		echo -e "\nUnable to find a distro rootfs...\n"
   1.148 +		exit 0
   1.149 +	fi
   1.150 +}
   1.151 +
   1.152 +# Check for the boot dir into the root CD tree.
   1.153 +verify_rootcd()
   1.154 +{
   1.155 +	if [ ! -d "$ROOTCD/boot" ] ; then
   1.156 +		echo -e "\nUnable to find the rootcd boot directory...\n"
   1.157 +		exit 0
   1.158 +	fi
   1.159 +}
   1.160 +
   1.161 +create_iso()
   1.162 +{
   1.163 +	cd $2
   1.164 +	echo -n "Computing $SUM..."
   1.165 +	find * -type f ! -name $CHECKSUM -exec $CHECKSUM {} \; > $CHECKSUM
   1.166 +	sed -i  -e '/  boot\/isolinux\/isolinux.bin$/d' \
   1.167 +		-e '/  boot\/isolinux\/boot.cat$/d' $CHECKSUM
   1.168 +	status
   1.169 +	cd - > /dev/null
   1.170 +	newline
   1.171 +	echo -e "\033[1mGenerating ISO image\033[0m"
   1.172 +	separator
   1.173 +	echo "Generating $1"
   1.174 +	if [ $(ls $2/boot/vmlinuz* $2/boot/bzImage | wc -l) -eq 2 ]; then
   1.175 +		if cmp $2/boot/vmlinuz* $2/boot/bzImage > /dev/null; then
   1.176 +			rm -f $2/boot/bzImage
   1.177 +			ln $2/boot/vmlinuz* $2/boot/bzImage
   1.178 +		fi
   1.179 +	fi
   1.180 +	genisoimage -R -o $1 -b boot/isolinux/isolinux.bin \
   1.181 + 		-c boot/isolinux/boot.cat -no-emul-boot -boot-load-size 4 \
   1.182 +		-V "$VOLUM_NAME" -p "$PREPARED" -input-charset iso8859-1 \
   1.183 +		-boot-info-table $2
   1.184 +	if [ -x /usr/bin/isohybrid ]; then
   1.185 +		echo -n "Creating hybrid ISO..."
   1.186 +		/usr/bin/isohybrid $1 -entry 2 2> /dev/null
   1.187 +		status
   1.188 +	fi
   1.189 +	if [ -s /etc/tazlito/info ]; then
   1.190 +		if [ $(stat -c %s /etc/tazlito/info) -lt $(( 31*1024 )) ]; then
   1.191 +			echo -n "Storing ISO info..."
   1.192 +			dd if=/etc/tazlito/info bs=1k seek=1 of=$1 \
   1.193 +				conv=notrunc 2> /dev/null
   1.194 +			status
   1.195 +		fi
   1.196 +	fi
   1.197 +}
   1.198 +
   1.199 +# Generate a new ISO image using isolinux.
   1.200 +gen_livecd_isolinux()
   1.201 +{
   1.202 +	# Some packages may want to alter iso
   1.203 +	genisohooks iso
   1.204 +	if [ ! -f "$ROOTCD/boot/isolinux/isolinux.bin" ]; then
   1.205 +		echo -e "\nUnable to find isolinux binary.\n"
   1.206 +		cleanup
   1.207 +		exit 0
   1.208 +	fi
   1.209 +	# Set date for boot msg.
   1.210 +	if grep -q 'XXXXXXXX' $ROOTCD/boot/isolinux/isolinux.*g; then
   1.211 +		DATE=`date +%Y%m%d`
   1.212 +		echo -n "Setting build date to: $DATE..."
   1.213 +		sed -i "s/XXXXXXXX/$DATE/" $ROOTCD/boot/isolinux/isolinux.*g
   1.214 +		status
   1.215 +	fi
   1.216 +	cd $DISTRO
   1.217 +	create_iso $ISO_NAME.iso $ROOTCD
   1.218 +	echo -n "Creating the ISO $CHECKSUM..."
   1.219 +	$CHECKSUM $ISO_NAME.iso > $ISO_NAME.$SUM
   1.220 +	status
   1.221 +	separator
   1.222 +	# Some packages may want to alter final iso
   1.223 +	genisohooks final
   1.224 +}
   1.225 +
   1.226 +lzma_history_bits()
   1.227 +{
   1.228 +	#
   1.229 +	# This generates an ISO which boots with Qemu but gives
   1.230 +	# rootfs errors in frugal or liveUSB mode.
   1.231 +	#
   1.232 +	#local n
   1.233 +	#local sz
   1.234 +	#n=20	# 1Mb
   1.235 +	#sz=$(du -sk $1 | cut -f1)
   1.236 +	#while [ $sz -gt 1024 -a $n -lt 28 ]; do
   1.237 +		#n=$(( $n + 1 ))
   1.238 +		#sz=$(( $sz / 2 ))
   1.239 +	#done
   1.240 +	#echo $n
   1.241 +	echo 24
   1.242 +}
   1.243 +
   1.244 +lzma_switches()
   1.245 +{
   1.246 +	local proc=$(grep -s '^processor' < /proc/cpuinfo | wc -l)
   1.247 +	echo "-d$(lzma_history_bits $1) -mt${proc:-1}"
   1.248 +}
   1.249 +lzma_set_size()
   1.250 +{
   1.251 +	# Update size field for lzma'd file packed using -si switch
   1.252 +	local n
   1.253 +	local i
   1.254 +	return # Need to fix kernel code ?
   1.255 +	n=$(unlzma -c $1 | wc -c)
   1.256 +	for i in $(seq 1 8); do
   1.257 +		printf '\\\\x%02X' $(($n & 255))
   1.258 +		n=$(($n >> 8))
   1.259 +	done | xargs echo -en | dd of=$1 conv=notrunc bs=1 seek=5 2> /dev/null
   1.260 +}
   1.261 +
   1.262 +# Pack rootfs
   1.263 +pack_rootfs()
   1.264 +{
   1.265 +	( cd $1 ; find . -print | cpio -o -H newc ) | \
   1.266 +	if [ "$COMPRESSION" = "none" ]; then
   1.267 +		echo "Generating uncompressed initramfs... "
   1.268 +		cat > $2
   1.269 +	elif [ -x /usr/bin/lzma -a "$COMPRESSION" != "gzip" ]; then
   1.270 +		echo -n "Generating lzma'ed initramfs... "
   1.271 +		lzma e -si -so $(lzma_switches $1) > $2
   1.272 +		lzma_set_size $2
   1.273 +	else
   1.274 +		echo "Generating gziped initramfs... "
   1.275 +		gzip -9 > $2
   1.276 +	fi
   1.277 +	echo 1 > /tmp/rootfs
   1.278 +}
   1.279 +
   1.280 +# Compression functions for writeiso.
   1.281 +write_initramfs()
   1.282 +{
   1.283 +	if [ "$COMPRESSION" = "lzma" ]; then
   1.284 +		echo -n "Creating $INITRAMFS with lzma compression... "
   1.285 +		cat /tmp/list | cpio -o -H newc | lzma e -si -so > /$INITRAMFS
   1.286 +		lzma_set_size /$INITRAMFS
   1.287 +	elif [ "$COMPRESSION" = "gzip" ]; then
   1.288 +		echo "Creating $INITRAMFS with gzip compression... "
   1.289 +		cat /tmp/list | cpio -o -H newc | gzip -9 > /$INITRAMFS
   1.290 +	else
   1.291 +		echo "Creating $INITRAMFS without compression... "
   1.292 +		cat /tmp/list | cpio -o -H newc > /$INITRAMFS
   1.293 +	fi
   1.294 +	echo 1 > /tmp/rootfs
   1.295 +}
   1.296 +
   1.297 +# Deduplicate files (MUST be on the same filesystem).
   1.298 +deduplicate()
   1.299 +{
   1.300 +	find "$@" -type f -size +0c -exec stat -c '%s-%a-%u-%g %i %h %n' {} \; | \
   1.301 +	   sort | ( save=0; old_attr=""; old_inode=""; old_link=""; old_file=""
   1.302 +	   while read attr inode link file; do
   1.303 +	   	   [ -L "$file" ] && continue
   1.304 +		   if [ "$attr" = "$old_attr" -a "$inode" != "$old_inode" ]; then
   1.305 +			   if cmp "$file" "$old_file" >/dev/null 2>&1 ; then
   1.306 +				   rm -f "$file"
   1.307 +				   ln "$old_file" "$file"
   1.308 +				   inode="$old_inode"
   1.309 +				   [ "$link" = "1" ] && save="$(expr $save + ${attr%%-*})"
   1.310 +			   fi
   1.311 +		   fi
   1.312 +		   old_attr="$attr" ; old_inode="$inode" ; old_file="$file"
   1.313 +	   done
   1.314 +	   echo "$save bytes saved in duplicate files."
   1.315 +	)
   1.316 +}
   1.317 +
   1.318 +# Generate a new initramfs from the root filesystem.
   1.319 +gen_initramfs()
   1.320 +{
   1.321 +	# Just in case CTRL+c
   1.322 +	rm -f $DISTRO/gen
   1.323 +
   1.324 +	# Some packages may want to alter rootfs
   1.325 +	genisohooks rootfs
   1.326 +	cd $1
   1.327 +
   1.328 +	# Link duplicate files
   1.329 +	deduplicate .
   1.330 +
   1.331 +	# Use lzma if installed. Display rootfs size in realtime.
   1.332 +	rm -f /tmp/rootfs
   1.333 +	pack_rootfs . $DISTRO/$(basename $1).gz &
   1.334 +	sleep 2
   1.335 +	echo -en "\nFilesystem size:"
   1.336 +	while [ ! -f /tmp/rootfs ]
   1.337 +	do
   1.338 +		sleep 1
   1.339 +		echo -en "\\033[18G`du -sh $DISTRO/$(basename $1).gz | awk '{print $1}'`    "
   1.340 +	done
   1.341 +	echo -e "\n"
   1.342 +	cd $DISTRO
   1.343 +	mv $(basename $1).gz $ROOTCD/boot
   1.344 +}
   1.345 +
   1.346 +distro_sizes()
   1.347 +{
   1.348 +	if [ "$time" ]; then
   1.349 +		time=$(($(date +%s) - $time))
   1.350 +		sec=$time
   1.351 +		div=$(( ($time + 30) / 60))
   1.352 +		[ "$div" != 0 ] && min="~ ${div}m"
   1.353 +		echo "Build time      : ${sec}s $min"
   1.354 +	fi
   1.355 +	cat << EOT
   1.356 +Build date      : $(date +%Y%m%d)
   1.357 +Packages        : $(ls -1 $ROOTFS*$INSTALLED/*/receipt | wc -l)
   1.358 +Rootfs size     : $(du -csh $ROOTFS*/ | awk '{ s=$1 } END { print s }')
   1.359 +Initramfs size  : $(du -csh $ROOTCD/boot/rootfs*.gz | awk '{ s=$1 } END { print s }')
   1.360 +ISO image size  : $(du -sh $ISO_NAME.iso | awk '{ print $1 }')
   1.361 +================================================================================
   1.362 +Image is ready: $ISO_NAME.iso
   1.363 +
   1.364 +EOT
   1.365 +}
   1.366 +
   1.367 +# Print ISO and rootfs size.
   1.368 +distro_stats()
   1.369 +{
   1.370 +	newline
   1.371 +	echo -e "\033[1mDistro statistics\033[0m ($DISTRO)"
   1.372 +	separator
   1.373 +	distro_sizes
   1.374 +}
   1.375 +
   1.376 +# tazlito gen-distro
   1.377 +gen_distro()
   1.378 +{
   1.379 +	check_root
   1.380 +	time=$(date +%s)
   1.381 +
   1.382 +	# Check if a package list was specified on cmdline.
   1.383 +	DISTRO_LIST="distro-packages.list"
   1.384 +	LIST_NAME="$DISTRO_LIST"
   1.385 +	unset CDROM
   1.386 +	while [ -n "$1" ]; do
   1.387 +		case "$1" in
   1.388 +		--iso=*)
   1.389 +			CDROM="-o loop ${2#--iso=}"
   1.390 +			;;
   1.391 +		--cdrom)
   1.392 +			CDROM="/dev/cdrom"
   1.393 +			;;
   1.394 +		--force)
   1.395 +			DELETE_ROOTFS="true"
   1.396 +			;;
   1.397 +		*)	if [ ! -f "$1" ] ; then
   1.398 +				echo -e "\nUnable to find the specified packages list."
   1.399 +				echo -e "List name : $1\n"
   1.400 +				exit 1
   1.401 +			fi
   1.402 +			LIST_NAME=$1
   1.403 +			;;
   1.404 +		esac
   1.405 +		shift
   1.406 +	done
   1.407 +
   1.408 +	if [ -d $ROOTFS ] ; then
   1.409 +		# Delete $ROOTFS if --force is set on command line
   1.410 +		if [ ! -z $DELETE_ROOTFS ]; then
   1.411 +			rm -rf $ROOTFS
   1.412 +			unset $DELETE_ROOTFS
   1.413 +		else
   1.414 +			echo -e "\nA rootfs exists in : $DISTRO"
   1.415 +			echo -e "Please clean the distro tree or change directory path.\n"
   1.416 +			exit 0
   1.417 +		fi
   1.418 +	fi
   1.419 +	if [ ! -f "$LIST_NAME" -a -d $INSTALLED ] ; then
   1.420 +		# Build list with installed packages
   1.421 +		for i in $(ls $INSTALLED); do
   1.422 +			eval $(grep ^VERSION= $INSTALLED/$i/receipt))
   1.423 +			EXTRAVERSION=""
   1.424 +			eval $(grep ^EXTRAVERSION= $INSTALLED/$i/receipt)
   1.425 +			echo "$i-$VERSION$EXTRAVERSION" >> $LIST_NAME
   1.426 +		done
   1.427 +	fi
   1.428 +	# Exit if no list name.
   1.429 +	if [ ! -f "$LIST_NAME" ]; then
   1.430 +		echo -e "\nNo packages list found or specified. Please read the docs.\n"
   1.431 +		exit 0
   1.432 +	fi
   1.433 +	# Start generation.
   1.434 +	newline
   1.435 +	echo -e "\033[1mGenerating a distro\033[0m"
   1.436 +	separator
   1.437 +	# Misc checks
   1.438 +	[ -n "$PACKAGES_REPOSITORY" ] || PACKAGES_REPOSITORY="."
   1.439 +	[ -d $PACKAGES_REPOSITORY ] || mkdir -p $PACKAGES_REPOSITORY
   1.440 +	# Get the list of packages using cat for a file list.
   1.441 +	LIST=$(cat $LIST_NAME)
   1.442 +	# Verify if all packages in list are present in $PACKAGES_REPOSITORY.
   1.443 +	unset REPACK DOWNLOAD
   1.444 +	for pkg in $LIST
   1.445 +	do
   1.446 +		[ "$pkg" = "" ] && continue
   1.447 +		pkg=${pkg%.tazpkg}
   1.448 +		[ -f $PACKAGES_REPOSITORY/$pkg.tazpkg ] && continue
   1.449 +		PACKAGE=$(installed_package_name $pkg)
   1.450 +		[ -n "$PACKAGE" -a "$REPACK" = "y" ] && continue
   1.451 +		[ -z "$PACKAGE" -a -n "$DOWNLOAD" ] && continue
   1.452 +		echo -e "\nUnable to find $pkg in the repository."
   1.453 +		echo -e "Path : $PACKAGES_REPOSITORY\n"
   1.454 +		if [ -n "$PACKAGE" -a -z "$REPACK" ]; then
   1.455 +			yesorno "Repack packages from rootfs (y/N) ? "
   1.456 +			REPACK="$answer"
   1.457 +			[ "$answer" = "y" ] || REPACK="n"
   1.458 +			[ "$DOWNLOAD" = "y" ] && break
   1.459 +		fi
   1.460 +		if [ -f $MIRROR -a -z "$DOWNLOAD" ]; then
   1.461 +			yesorno "Download packages from mirror (Y/n) ? "
   1.462 +			DOWNLOAD="$answer"
   1.463 +			if [ "$answer" = "n" ]; then
   1.464 +				[ -z "$PACKAGE" ] && exit 1
   1.465 +			else
   1.466 +				DOWNLOAD="y"
   1.467 +				[ -n "$REPACK" ] && break
   1.468 +			fi
   1.469 +		fi
   1.470 +		[ "$REPACK" = "n" -a "$DOWNLOAD" = "n" ] && exit 1
   1.471 +	done
   1.472 +
   1.473 +	# Mount cdrom to be able to repack boot-loader packages
   1.474 +	if [ ! -e /boot -a -n "$CDROM" ]; then
   1.475 +		mkdir $TMP_MNT
   1.476 +		if mount -r $CDROM $TMP_MNT 2> /dev/null; then
   1.477 +			ln -s $TMP_MNT/boot /
   1.478 +			if [ ! -d "$ADDFILES/rootcd" ] ; then
   1.479 +				mkdir -p $ADDFILES/rootcd
   1.480 +				for i in $(ls $TMP_MNT); do
   1.481 +					[ "$i" = "boot" ] && continue
   1.482 +					cp -a $TMP_MNT/$i $ADDFILES/rootcd
   1.483 +				done
   1.484 +			fi
   1.485 +		else
   1.486 +			rmdir $TMP_MNT
   1.487 +		fi
   1.488 +	fi
   1.489 +
   1.490 +	# Root fs stuff.
   1.491 +	echo "Preparing the rootfs directory..."
   1.492 +	mkdir -p $ROOTFS
   1.493 +	for pkg in $LIST
   1.494 +	do
   1.495 +		[ "$pkg" = "" ] && continue
   1.496 +		# First copy and extract the package in tmp dir.
   1.497 +		pkg=${pkg%.tazpkg}
   1.498 +		PACKAGE=$(installed_package_name $pkg)
   1.499 +		mkdir -p $TMP_DIR
   1.500 +		if [ ! -f $PACKAGES_REPOSITORY/$pkg.tazpkg ]; then
   1.501 +			# Look for package in cache
   1.502 +			if [ -f $CACHE_DIR/$pkg.tazpkg ]; then
   1.503 +				ln -s $CACHE_DIR/$pkg.tazpkg $PACKAGES_REPOSITORY
   1.504 +			# Look for package in running distribution
   1.505 +			elif [ -n "$PACKAGE" -a "$REPACK" = "y" ]; then
   1.506 +				tazpkg repack $PACKAGE && \
   1.507 +				  mv $pkg.tazpkg $PACKAGES_REPOSITORY
   1.508 +			fi
   1.509 +		fi
   1.510 +		if [ ! -f $PACKAGES_REPOSITORY/$pkg.tazpkg ]; then
   1.511 +			# Get package from mirror
   1.512 +			[ "$DOWNLOAD" = "y" ] && \
   1.513 +			download $pkg.tazpkg && \
   1.514 +			mv $pkg.tazpkg $PACKAGES_REPOSITORY
   1.515 +		fi
   1.516 +		if [ ! -f $PACKAGES_REPOSITORY/$pkg.tazpkg ]; then
   1.517 +			echo "Missing package $pkg."
   1.518 +			cleanup
   1.519 +			exit 1
   1.520 +		fi
   1.521 +	done
   1.522 +	if [ -f non-free.list ]; then
   1.523 +		echo "Preparing non-free packages..."
   1.524 +		cp non-free.list $ROOTFS/etc/tazlito/non-free.list
   1.525 +		for pkg in $(cat non-free.list); do
   1.526 +			if [ ! -d $INSTALLED/$pkg ]; then
   1.527 +				if [ ! -d $INSTALLED/get-$pkg ]; then
   1.528 +					tazpkg get-install get-$pkg
   1.529 +				fi
   1.530 +				get-$pkg
   1.531 +			fi
   1.532 +			tazpkg repack $pkg
   1.533 +			pkg=$(ls $pkg*.tazpkg)
   1.534 +			grep -q "^$pkg$" $LIST_NAME || \
   1.535 +				echo $pkg >>$LIST_NAME
   1.536 +			mv $pkg $PACKAGES_REPOSITORY
   1.537 +			done
   1.538 +	fi
   1.539 +	cp $LIST_NAME $DISTRO/$DISTRO_LIST
   1.540 +	sed 's/\(.*\)/\1.tazpkg/' < $DISTRO/$DISTRO_LIST > $DISTRO/list-packages
   1.541 +	cd $PACKAGES_REPOSITORY
   1.542 +	for pkg in $(cat $DISTRO/list-packages)
   1.543 +	do
   1.544 +		echo -n "Installing package: $pkg"
   1.545 +		yes y | tazpkg install $pkg --root=$ROOTFS 2>&1 >> $log || exit 1
   1.546 +		status
   1.547 +	done
   1.548 +	rm -f $ROOTFS/$DB/packages.*
   1.549 +	cd $DISTRO
   1.550 +	cp $DISTRO_LIST $ROOTFS/etc/tazlito
   1.551 +	# Copy all files from $ADDFILES/rootfs to the rootfs.
   1.552 +	if [ -d "$ADDFILES/rootfs" ] ; then
   1.553 +		echo -n "Copying addfiles content to the rootfs... "
   1.554 +		cp -a $ADDFILES/rootfs/* $ROOTFS
   1.555 +		status
   1.556 +	fi
   1.557 +	echo -n "Root filesystem is generated..." && status
   1.558 +	# Root CD part.
   1.559 +	echo -n "Preparing the rootcd directory..."
   1.560 +	mkdir -p $ROOTCD
   1.561 +	status
   1.562 +	# Move the boot dir with the Linux kernel from rootfs.
   1.563 +	# The boot dir goes directly on the CD.
   1.564 +	if [ -d "$ROOTFS/boot" ] ; then
   1.565 +		echo -n "Moving the boot directory..."
   1.566 +		mv $ROOTFS/boot $ROOTCD
   1.567 +		cd $ROOTCD/boot
   1.568 +		ln vmlinuz-* bzImage
   1.569 +		status
   1.570 +	fi
   1.571 +	cd $DISTRO
   1.572 +	# Copy all files from $ADDFILES/rootcd to the rootcd.
   1.573 +	if [ -d "$ADDFILES/rootcd" ] ; then
   1.574 +		echo -n "Copying addfiles content to the rootcd... "
   1.575 +		cp -a $ADDFILES/rootcd/* $ROOTCD
   1.576 +		status
   1.577 +	fi
   1.578 +	# Execute the distro script used to perform tasks in the rootfs
   1.579 +	# before compression. Give rootfs path in arg
   1.580 +	[ -z $DISTRO_SCRIPT ] && DISTRO_SCRIPT=$TOP_DIR/distro.sh
   1.581 +	if [ -x $DISTRO_SCRIPT ]; then
   1.582 +		echo "Executing distro script..."
   1.583 +		sh $DISTRO_SCRIPT $DISTRO
   1.584 +	fi
   1.585 +	if [ -s /etc/tazlito/rootfs.list ]; then
   1.586 +		FLAVOR_LIST="$(awk '{ for (i = 2; i <= NF; i+=2) \
   1.587 +		  printf("%s ",$i) }' < /etc/tazlito/rootfs.list)"
   1.588 +		sed -i "s/ *//;s/)/), flavors $FLAVOR_LIST/" \
   1.589 +		  $ROOTCD/boot/isolinux/isolinux.msg 2> /dev/null
   1.590 +		[ -f $ROOTCD/boot/isolinux/ifmem.c32 ] ||
   1.591 +		cp /boot/isolinux/ifmem.c32 $ROOTCD/boot/isolinux
   1.592 +		n=0
   1.593 +		last=$ROOTFS
   1.594 +		while read flavor; do
   1.595 +			n=$(($n+1))
   1.596 +			echo "Building $flavor rootfs..."
   1.597 +			if [ -d $flavors/$flavor ]; then
   1.598 +				cp -a $flavors
   1.599 +			[ -s $TOP_DIR/$flavor.flavor ] &&
   1.600 +				cp $TOP_DIR/$flavor.flavor .
   1.601 +			[ -s $flavor.flavor ] || download $flavor.flavor
   1.602 +			zcat $flavor.flavor | cpio -i \
   1.603 +				$flavor.pkglist $flavor.rootfs
   1.604 +			sed 's/.*/&.tazpkg/' < $flavor.pkglist \
   1.605 +				> $DISTRO/list-packages0$n
   1.606 +			mkdir ${ROOTFS}0$n
   1.607 +			cd $PACKAGES_REPOSITORY
   1.608 +			yes y | tazpkg install-list \
   1.609 +				$DISTRO/list-packages0$n --root=${ROOTFS}0$n
   1.610 +			rm -rf ${ROOTFS}0$n/boot ${ROOTFS}0$n/$DB/packages.*
   1.611 +			status
   1.612 +			cd $DISTRO
   1.613 +			if [ -s $flavor.rootfs ]; then
   1.614 +				echo "Adding $flavor rootfs extra files..."
   1.615 +				zcat $flavor.rootfs | \
   1.616 +				( cd ${ROOTFS}0$n ; cpio -idmu )
   1.617 +			fi
   1.618 +			mv $flavor.pkglist ${ROOTFS}0$n/etc/tazlito/$DISTRO_LIST
   1.619 +			rm -f $flavor.flavor install-list
   1.620 +			mergefs ${ROOTFS}0$n $last
   1.621 +			last=${ROOTFS}0$n
   1.622 +		done <<EOT
   1.623 +$(awk '{ for (i = 4; i <= NF; i+=2) print $i; }' < /etc/tazlito/rootfs.list)
   1.624 +EOT
   1.625 +		i=$(($n+1))
   1.626 +		while [ $n -gt 0 ]; do
   1.627 +			mv ${ROOTFS}0$n ${ROOTFS}$i
   1.628 +			echo "Compression ${ROOTFS}0$n ($(du -hs ${ROOTFS}$i | awk '{ print $1 }')) ..."
   1.629 +			gen_initramfs ${ROOTFS}$i
   1.630 +			n=$(($n-1))
   1.631 +			i=$(($i-1))
   1.632 +		done
   1.633 +		mv $ROOTFS ${ROOTFS}$i
   1.634 +		gen_initramfs ${ROOTFS}$i
   1.635 +		update_bootconfig $ROOTCD/boot/isolinux \
   1.636 +			"$(cat /etc/tazlito/rootfs.list)"
   1.637 +	else
   1.638 +		# Initramfs and ISO image stuff.
   1.639 +		gen_initramfs $ROOTFS
   1.640 +	fi
   1.641 +	gen_livecd_isolinux
   1.642 +	distro_stats
   1.643 +	cleanup
   1.644 +}
   1.645 +
   1.646 +# tazlito clean-distro
   1.647 +clean_distro()
   1.648 +{
   1.649 +	# Remove old distro tree.
   1.650 +	#
   1.651 +	check_root
   1.652 +	newline
   1.653 +	boldify "Cleaning : $DISTRO"
   1.654 +	separator
   1.655 +	if [ -d "$DISTRO" ] ; then
   1.656 +		if [ -d "$ROOTFS" ] ; then
   1.657 +			echo -n "Removing the rootfs..."
   1.658 +			rm -f $DISTRO/$INITRAMFS
   1.659 +			rm -rf $ROOTFS
   1.660 +			status
   1.661 +		fi
   1.662 +		if [ -d "$ROOTCD" ] ; then
   1.663 +			echo -n "Removing the rootcd..."
   1.664 +			rm -rf $ROOTCD
   1.665 +			status
   1.666 +		fi
   1.667 +		echo -n "Removing eventual ISO image..."
   1.668 +		rm -f $DISTRO/$ISO_NAME.iso
   1.669 +		rm -f $DISTRO/$ISO_NAME.$SUM
   1.670 +		status
   1.671 +	fi
   1.672 +	separator
   1.673 +	newline
   1.674 +}
   1.675 +
   1.676 +# tazlito pack-flavor
   1.677 +pack_flavor()
   1.678 +{
   1.679 +	# Create a flavor from $FLAVORS_REPOSITORY.
   1.680 +	FLAVOR=${1%.flavor}
   1.681 +	if [ -s $FLAVORS_REPOSITORY/$FLAVOR/receipt ]; then
   1.682 +		mkdir $TMP_DIR
   1.683 +		echo -n "Creating flavor $FLAVOR..."
   1.684 +		[ -s $LOCALSTATE/packages.list ] || tazpkg recharge
   1.685 +		if [ -s $FLAVORS_REPOSITORY/$FLAVOR/mirrors ]; then
   1.686 +			cp $FLAVORS_REPOSITORY/$FLAVOR/mirrors \
   1.687 +				$TMP_DIR/$FLAVOR.mirrors
   1.688 +			for i in $(cat $TMP_DIR/$FLAVOR.mirrors); do
   1.689 +				wget -O - $i/packages.list >> $TMP_DIR/packages.list
   1.690 +			done
   1.691 +		fi
   1.692 +		#add distro;sh if exist
   1.693 +		if [ -s $FLAVORS_REPOSITORY/$FLAVOR/distro.sh ]; then
   1.694 +			cp $FLAVORS_REPOSITORY/$FLAVOR/distro.sh $TMP_DIR/$FLAVOR-distro.sh
   1.695 +		fi
   1.696 +		[ -s $FLAVORS_REPOSITORY/$FLAVOR/packages.list ] &&
   1.697 +		get_pkglist $FLAVOR > $TMP_DIR/$FLAVOR.pkglist
   1.698 +		if grep -q ^ROOTFS_SELECTION \
   1.699 +			$FLAVORS_REPOSITORY/$FLAVOR/receipt; then
   1.700 +			. $FLAVORS_REPOSITORY/$FLAVOR/receipt
   1.701 +			set -- $ROOTFS_SELECTION
   1.702 +			[ -n "$FRUGAL_RAM" ] || FRUGAL_RAM=$1
   1.703 +			[ -f $FLAVORS_REPOSITORY/$2/packages.list ] ||
   1.704 +			extract_flavor $2
   1.705 +			get_pkglist $2 > $TMP_DIR/$FLAVOR.pkglist
   1.706 +			for i in rootcd rootfs; do
   1.707 +				mkdir $TMP_DIR/$i
   1.708 +				# Copy extra files from the first flavor
   1.709 +				[ -d $FLAVORS_REPOSITORY/$2/$i ] &&
   1.710 +				cp -a $FLAVORS_REPOSITORY/$2/$i $TMP_DIR
   1.711 +				# Overload extra files by meta flavor
   1.712 +				[ -d $FLAVORS_REPOSITORY/$FLAVOR/$i ] &&
   1.713 +				cp -a $FLAVORS_REPOSITORY/$FLAVOR/$i $TMP_DIR
   1.714 +				[ -n "$(ls $TMP_DIR/$i)" ] &&
   1.715 +				( cd $TMP_DIR/$i ; find . | cpio -o -H newc 2> /dev/null ) | \
   1.716 +				gzip -9 >$TMP_DIR/$FLAVOR.$i
   1.717 +				rm -rf $TMP_DIR/$i
   1.718 +			done
   1.719 +		else
   1.720 +			for i in rootcd rootfs; do
   1.721 +				[ -d $FLAVORS_REPOSITORY/$FLAVOR/$i ] || \
   1.722 +					continue
   1.723 +				( cd $FLAVORS_REPOSITORY/$FLAVOR/$i ; \
   1.724 +				find . | cpio -o -H newc 2> /dev/null ) | \
   1.725 +				gzip -9 >$TMP_DIR/$FLAVOR.$i
   1.726 +			done
   1.727 +		fi
   1.728 +		if [ -s $TMP_DIR/$FLAVOR.rootfs ]; then
   1.729 +			packed_size=$(($packed_size \
   1.730 +				+ $(cat $TMP_DIR/$FLAVOR.rootfs | wc -c ) / 100 ))
   1.731 +			unpacked_size=$(($unpacked_size \
   1.732 +				+ $(zcat $TMP_DIR/$FLAVOR.rootfs | wc -c ) / 100 ))
   1.733 +		fi
   1.734 +		# Estimate lzma
   1.735 +		packed_size=$(($packed_size * 2 / 3))
   1.736 +		iso_size=$(( $packed_size + 26000 ))
   1.737 +		if [ -s $TMP_DIR/$FLAVOR.rootcd ]; then
   1.738 +			iso_size=$(($iso_size \
   1.739 +				+ $(zcat $TMP_DIR/$FLAVOR.rootcd | wc -c ) / 100 ))
   1.740 +		fi
   1.741 +		VERSION=""
   1.742 +		MAINTAINER=""
   1.743 +		ROOTFS_SELECTION=""
   1.744 +		ROOTFS_SIZE="$(cent2human $unpacked_size) (estimated)"
   1.745 +		INITRAMFS_SIZE="$(cent2human $packed_size) (estimated)"
   1.746 +		ISO_SIZE="$(cent2human $iso_size) (estimated)"
   1.747 +		. $FLAVORS_REPOSITORY/$FLAVOR/receipt
   1.748 +		cat > $TMP_DIR/$FLAVOR.desc <<EOT
   1.749 +Flavor          : $FLAVOR
   1.750 +Description     : $SHORT_DESC
   1.751 +EOT
   1.752 +		[ -n "$VERSION" ] && cat >> $TMP_DIR/$FLAVOR.desc <<EOT
   1.753 +Version         : $VERSION
   1.754 +EOT
   1.755 +		[ -n "$MAINTAINER" ] && cat >> $TMP_DIR/$FLAVOR.desc <<EOT
   1.756 +Maintainer      : $MAINTAINER
   1.757 +EOT
   1.758 +		[ -n "$FRUGAL_RAM" ] && cat >> $TMP_DIR/$FLAVOR.desc <<EOT
   1.759 +LiveCD RAM size : $FRUGAL_RAM
   1.760 +EOT
   1.761 +		[ -n "$ROOTFS_SELECTION" ] && cat >> $TMP_DIR/$FLAVOR.desc <<EOT
   1.762 +Rootfs list     : $ROOTFS_SELECTION
   1.763 +EOT
   1.764 +		cat >> $TMP_DIR/$FLAVOR.desc <<EOT
   1.765 +Build date      : $(date +%Y%m%d\ \at\ \%H:%M:%S)
   1.766 +Packages        : $(grep -v ^# $TMP_DIR/$FLAVOR.pkglist | wc -l)
   1.767 +Rootfs size     : $ROOTFS_SIZE
   1.768 +Initramfs size  : $INITRAMFS_SIZE
   1.769 +ISO image size  : $ISO_SIZE
   1.770 +================================================================================
   1.771 +
   1.772 +EOT
   1.773 +		rm -f $TMP_DIR/packages.list
   1.774 +		( cd $TMP_DIR ; ls | cpio -o -H newc 2> /dev/null) | \
   1.775 +			gzip -9 > $FLAVOR.flavor
   1.776 +		status
   1.777 +		rm -Rf $TMP_DIR
   1.778 +	else
   1.779 +		echo "No $FLAVOR flavor in $FLAVORS_REPOSITORY."
   1.780 +	fi
   1.781 +}
   1.782 +
   1.783 +# tazlito extract-flavor
   1.784 +extract_flavor()
   1.785 +{
   1.786 +	# Extract a flavor into $FLAVORS_REPOSITORY.
   1.787 +	FLAVOR=${1%.flavor}
   1.788 +	if [ -f $FLAVOR.flavor ] || download $FLAVOR.flavor; then
   1.789 +		mkdir $TMP_DIR
   1.790 +		zcat $FLAVOR.flavor | ( cd $TMP_DIR; cpio -i >/dev/null )
   1.791 +		echo -n "Extracting $FLAVOR..."
   1.792 +		rm -rf $FLAVORS_REPOSITORY/$FLAVOR 2> /dev/null
   1.793 +		mkdir -p $FLAVORS_REPOSITORY/$FLAVOR
   1.794 +		echo "FLAVOR=\"$FLAVOR\"" > $FLAVORS_REPOSITORY/$FLAVOR/receipt
   1.795 +		grep ^Description $TMP_DIR/$FLAVOR.desc | \
   1.796 +			sed 's/.*: \(.*\)$/SHORT_DESC="\1"/' >> \
   1.797 +			$FLAVORS_REPOSITORY/$FLAVOR/receipt
   1.798 +		grep ^Version $TMP_DIR/$FLAVOR.desc | \
   1.799 +			sed 's/.*: \(.*\)$/VERSION="\1"/' >> \
   1.800 +			$FLAVORS_REPOSITORY/$FLAVOR/receipt
   1.801 +		grep ^Maintainer $TMP_DIR/$FLAVOR.desc | \
   1.802 +			sed 's/.*: \(.*\)$/MAINTAINER="\1"/' >> \
   1.803 +			$FLAVORS_REPOSITORY/$FLAVOR/receipt
   1.804 +		grep -q '^Rootfs list' $TMP_DIR/$FLAVOR.desc && \
   1.805 +		grep '^Rootfs list' $TMP_DIR/$FLAVOR.desc | \
   1.806 +			sed 's/.*: \(.*\)$/ROOTFS_SELECTION="\1"/' >> \
   1.807 +			$FLAVORS_REPOSITORY/$FLAVOR/receipt
   1.808 +		grep '^Rootfs size' $TMP_DIR/$FLAVOR.desc | \
   1.809 +			sed 's/.*: \(.*\)$/ROOTFS_SIZE="\1"/' >> \
   1.810 +			$FLAVORS_REPOSITORY/$FLAVOR/receipt
   1.811 +		grep ^Initramfs $TMP_DIR/$FLAVOR.desc | \
   1.812 +			sed 's/.*: \(.*\)$/INITRAMFS_SIZE="\1"/' >> \
   1.813 +			$FLAVORS_REPOSITORY/$FLAVOR/receipt
   1.814 +		grep ^ISO $TMP_DIR/$FLAVOR.desc | \
   1.815 +			sed 's/.*: \(.*\)$/ISO_SIZE="\1"/' >> \
   1.816 +			$FLAVORS_REPOSITORY/$FLAVOR/receipt
   1.817 +		for i in rootcd rootfs; do
   1.818 +			[ -f $TMP_DIR/$FLAVOR.$i ] || continue
   1.819 +			mkdir $FLAVORS_REPOSITORY/$FLAVOR/$i
   1.820 +			zcat $TMP_DIR/$FLAVOR.$i | \
   1.821 +				(cd $FLAVORS_REPOSITORY/$FLAVOR/$i; \
   1.822 +			cpio -idm > /dev/null)
   1.823 +		done
   1.824 +		[ -s $TMP_DIR/$FLAVOR.mirrors ] &&
   1.825 +			cp $TMP_DIR/$FLAVOR.mirrors \
   1.826 +				$FLAVORS_REPOSITORY/$FLAVOR/mirrors
   1.827 +		[ -s $LOCALSTATE/packages.list ] || tazpkg recharge
   1.828 +		while read org; do
   1.829 +			i=0
   1.830 +			pkg=$org
   1.831 +			while ! grep -q ^$pkg$ $LOCALSTATE/packages.txt; do
   1.832 +				pkg=${pkg%-*}
   1.833 +				i=$(($i + 1))
   1.834 +				[ $i -gt 5 ] && break;
   1.835 +			done
   1.836 +			echo $pkg
   1.837 +		done <  $TMP_DIR/$FLAVOR.pkglist \
   1.838 +			> $FLAVORS_REPOSITORY/$FLAVOR/packages.list
   1.839 +		status
   1.840 +		rm -Rf $TMP_DIR
   1.841 +	fi
   1.842 +}
   1.843 +
   1.844 +# tazlito show-flavor
   1.845 +show_flavor()
   1.846 +{
   1.847 +	# Show flavor description.
   1.848 +	FLAVOR=${1%.flavor}
   1.849 +	if [ ! -f "$FLAVOR.flavor" ]; then
   1.850 +		echo "File $FLAVOR.flavor not found."
   1.851 +		exit 1
   1.852 +	fi
   1.853 +	mkdir $TMP_DIR
   1.854 +	zcat $FLAVOR.flavor | ( cd $TMP_DIR; cpio -i > /dev/null)
   1.855 +	if [ "$2" = "--brief" ]; then
   1.856 +		if [ "$3" != "--noheader" ]; then
   1.857 +			echo "Name              ISO   Rootfs  Description"
   1.858 +			separator
   1.859 +		fi
   1.860 +		printf "%-16.16s %6.6s %6.6s %s\n" "$FLAVOR" \
   1.861 +			"$(field ISO $TMP_DIR/$FLAVOR.desc)" \
   1.862 +			"$(field 'Rootfs size' $TMP_DIR/$FLAVOR.desc)" \
   1.863 +			"$(grep ^Description $TMP_DIR/$FLAVOR.desc | cut -d: -f2)"
   1.864 +	else
   1.865 +		separator
   1.866 +		cat $TMP_DIR/$FLAVOR.desc
   1.867 +	fi
   1.868 +	rm -Rf $TMP_DIR
   1.869 +}
   1.870 +
   1.871 +# tazlito list-flavors
   1.872 +list_flavors()
   1.873 +{
   1.874 +	# Show available flavors.
   1.875 +	if [ ! -s /tmp/flavors.list -o "$2" == "--recharge" ]; then
   1.876 +		download flavors.list -O - > /tmp/flavors.list
   1.877 +	fi
   1.878 +	newline
   1.879 +	echo -e "\033[1mList of flavors\033[0m"
   1.880 +	separator
   1.881 +	cat /tmp/flavors.list
   1.882 +	newline
   1.883 +}
   1.884 +
   1.885 +# tazlito extract-distro
   1.886 +extract_distro()
   1.887 +{
   1.888 +	# Extract an ISO image to a directory and rebuild the LiveCD tree.
   1.889 +	#
   1.890 +	check_root
   1.891 +	ISO_IMAGE=$1
   1.892 +	if [ -z "$ISO_IMAGE" ] ; then
   1.893 +		echo -e "\nPlease specify the path to the ISO image."
   1.894 +		echo -e "Example : `basename $0` image.iso /path/target\n"
   1.895 +		exit 0
   1.896 +	fi
   1.897 +	# Set the distro path by checking for $3 on cmdline.
   1.898 +	if [ -n "$2" ] ; then
   1.899 +		TARGET=$2
   1.900 +	else
   1.901 +		TARGET=$DISTRO
   1.902 +	fi
   1.903 +	# Exit if existing distro is found.
   1.904 +	if [ -d "$TARGET/rootfs" ] ; then
   1.905 +		echo -e "\nA rootfs exists in : $TARGET"
   1.906 +		echo -e "Please clean the distro tree or change directory path.\n"
   1.907 +		exit 0
   1.908 +	fi
   1.909 +	newline
   1.910 +	echo -e "\033[1mExtracting :\033[0m `basename $ISO_IMAGE`"
   1.911 +	separator
   1.912 +	# Start to mount the ISO.
   1.913 +	newline
   1.914 +	echo "Mounting ISO image..."
   1.915 +	mkdir -p $TMP_DIR
   1.916 +	# Get ISO file size.
   1.917 +	isosize=$(du -sh $ISO_IMAGE | cut -f1)
   1.918 +	mount -o loop $ISO_IMAGE $TMP_DIR
   1.919 +	sleep 2
   1.920 +	# Prepare target dir, copy the kernel and the rootfs.
   1.921 +	mkdir -p $TARGET/rootfs
   1.922 +	mkdir -p $TARGET/rootcd/boot
   1.923 +	echo -n "Copying the Linux kernel..."
   1.924 +	if cp $TMP_DIR/boot/vmlinuz* $TARGET/rootcd/boot 2> /dev/null; then
   1.925 +		ln $TARGET/rootcd/boot/vmlinuz* $TARGET/rootcd/boot/bzImage
   1.926 +	else
   1.927 +		cp $TMP_DIR/boot/bzImage $TARGET/rootcd/boot
   1.928 +	fi
   1.929 +	status
   1.930 +	echo -n "Copying isolinux files..."
   1.931 +	cp -a $TMP_DIR/boot/isolinux $TARGET/rootcd/boot
   1.932 +	for i in $(ls $TMP_DIR); do
   1.933 +		[ "$i" = "boot" ] && continue
   1.934 +		cp -a $TMP_DIR/$i $TARGET/rootcd
   1.935 +	done
   1.936 +	status
   1.937 +	if [ -d $TMP_DIR/boot/syslinux ]; then
   1.938 +		echo -n "Copying syslinux files..."
   1.939 +		cp -a $TMP_DIR/boot/syslinux $TARGET/rootcd/boot
   1.940 +		status
   1.941 +	fi
   1.942 +	if [ -d $TMP_DIR/boot/extlinux ]; then
   1.943 +		echo -n "Copying extlinux files..."
   1.944 +		cp -a $TMP_DIR/boot/extlinux $TARGET/rootcd/boot
   1.945 +		status
   1.946 +	fi
   1.947 +	if [ -d $TMP_DIR/boot/grub ]; then
   1.948 +		echo -n "Copying GRUB files..."
   1.949 +		cp -a $TMP_DIR/boot/grub $TARGET/rootcd/boot
   1.950 +		status
   1.951 +	fi
   1.952 +	echo -n "Copying the rootfs..."
   1.953 +	cp $TMP_DIR/boot/$INITRAMFS $TARGET/rootcd/boot
   1.954 +	status
   1.955 +	# Extract initramfs.
   1.956 +	cd $TARGET/rootfs
   1.957 +	echo -n "Extracting the rootfs... "
   1.958 +	extract_rootfs $TARGET/rootfs/rootcd/boot/$INITRAMFS $TARGET/rootfs
   1.959 +	# unpack /usr
   1.960 +	for i in etc/tazlito/*.extract; do
   1.961 +		[ -f "$i" ] && . $i ../rootcd
   1.962 +	done
   1.963 +	# Umount and remove temp directory and cd to $TARGET to get stats.
   1.964 +	umount $TMP_DIR && rm -rf $TMP_DIR
   1.965 +	cd ..
   1.966 +	newline
   1.967 +	separator
   1.968 +	echo "Extracted       : `basename $ISO_IMAGE` ($isosize)"
   1.969 +	echo "Distro tree     : `pwd`"
   1.970 +	echo "Rootfs size     : `du -sh rootfs`"
   1.971 +	echo "Rootcd size     : `du -sh rootcd`"
   1.972 +	separator
   1.973 +	newline
   1.974 +}
   1.975 +
   1.976 +# tazlito update-flavor
   1.977 +update_flavor()
   1.978 +{
   1.979 +	# Update package list to the latest versions available.
   1.980 +	FLAVOR=${1%.flavor}
   1.981 +	if [ -f $FLAVOR.flavor ] || download $FLAVOR.flavor; then
   1.982 +		mkdir $TMP_DIR
   1.983 +		zcat $FLAVOR.flavor | ( cd $TMP_DIR; cpio -i >/dev/null )
   1.984 +		echo -n "Updating $FLAVOR package list..."
   1.985 +		[ -s $LOCALSTATE/packages.list ] || tazpkg recharge
   1.986 +		packed_size=0; unpacked_size=0
   1.987 +		while read org; do
   1.988 +			i=0
   1.989 +			pkg=$org
   1.990 +			while ! grep -q ^$pkg$ $LOCALSTATE/packages.txt; do
   1.991 +				pkg=${pkg%-*}
   1.992 +				i=$(($i + 1))
   1.993 +				[ $i -gt 5 ] && break;
   1.994 +			done
   1.995 +			set -- $(get_size $pkg)
   1.996 +			packed_size=$(( $packed_size + $1 ))
   1.997 +			unpacked_size=$(( $unpacked_size + $2 ))
   1.998 +			for i in $(grep ^$pkg $LOCALSTATE/packages.list); do
   1.999 +				echo $i
  1.1000 +				break
  1.1001 +			done
  1.1002 +		done <  $TMP_DIR/$FLAVOR.pkglist \
  1.1003 +			> $TMP_DIR/$FLAVOR.pkglist.$$
  1.1004 +		mv -f $TMP_DIR/$FLAVOR.pkglist.$$ $TMP_DIR/$FLAVOR.pkglist
  1.1005 +		if [ -s $TMP_DIR/$FLAVOR.rootfs ]; then
  1.1006 +			packed_size=$(($packed_size \
  1.1007 +				+ $(cat $TMP_DIR/$FLAVOR.rootfs | wc -c ) / 100 ))
  1.1008 +			unpacked_size=$(($unpacked_size \
  1.1009 +				+ $(zcat $TMP_DIR/$FLAVOR.rootfs | wc -c ) / 100 ))
  1.1010 +		fi
  1.1011 +		# Estimate lzma
  1.1012 +		packed_size=$(($packed_size * 2 / 3))
  1.1013 +		iso_size=$(( $packed_size + 26000 ))
  1.1014 +		if [ -s $TMP_DIR/$FLAVOR.rootcd ]; then
  1.1015 +			iso_size=$(($iso_size \
  1.1016 +				+ $(zcat $TMP_DIR/$FLAVOR.rootcd | wc -c ) / 100 ))
  1.1017 +		fi
  1.1018 +		sed -i -e '/Image is ready/d' \
  1.1019 +			-e "s/Rootfs size\( *:\) \(.*\)/Rootfs size\1 $(cent2human $unpacked_size)  (estimated)/" \
  1.1020 +			-e "s/Initramfs size\( *:\) \(.*\)/Initramfs size\1 $(cent2human $packed_size)  (estimated)/" \
  1.1021 +			-e "s/ISO image size\( *:\) \(.*\)/ISO image size\1 $(cent2human $iso_size)  (estimated)/" \
  1.1022 +			-e "s/date\( *:\) \(.*\)/date\1 $(date +%Y%m%d\ \at\ \%H:%M:%S)/" \
  1.1023 +			$TMP_DIR/$FLAVOR.desc
  1.1024 +		( cd $TMP_DIR ; ls | cpio -o -H newc ) | gzip -9 > \
  1.1025 +			$FLAVOR.flavor
  1.1026 +		status
  1.1027 +		rm -Rf $TMP_DIR
  1.1028 +	fi
  1.1029 +}
  1.1030 +
  1.1031 +# tazlito check-distro
  1.1032 +check_distro()
  1.1033 +{
  1.1034 +	# Check for a few LiveCD needed files not installed by packages.
  1.1035 +	#
  1.1036 +	check_rootfs
  1.1037 +	newline
  1.1038 +	echo -e "\033[1mChecking distro :\033[0m $ROOTFS"
  1.1039 +	separator
  1.1040 +	# SliTaz release info.
  1.1041 +	if [ ! -f "$ROOTFS/etc/slitaz-release" ]; then
  1.1042 +		echo "Missing release info : /etc/slitaz-release"
  1.1043 +	else
  1.1044 +		release=$(cat $ROOTFS/etc/slitaz-release)
  1.1045 +		echo -n "Release      : $release"
  1.1046 +		status
  1.1047 +	fi
  1.1048 +	# Tazpkg mirror.
  1.1049 +	if [ ! -f "$ROOTFS$LOCALSTATE/mirror" ]; then
  1.1050 +		echo -n "Mirror URL   : Missing $LOCALSTATE/mirror"
  1.1051 +		todomsg
  1.1052 +	else
  1.1053 +		echo -n "Mirror configuration exists..."
  1.1054 +		status
  1.1055 +	fi
  1.1056 +	# Isolinux msg
  1.1057 +	if grep -q "cooking-XXXXXXXX" /$ROOTCD/boot/isolinux/isolinux.*g; then
  1.1058 +		echo -n "Isolinux msg : Missing cooking date XXXXXXXX (ex `date +%Y%m%d`)"
  1.1059 +		todomsg
  1.1060 +	else
  1.1061 +		echo -n "Isolinux message seems good..."
  1.1062 +		status
  1.1063 +	fi
  1.1064 +	separator
  1.1065 +	newline
  1.1066 +}
  1.1067 +
  1.1068 +# tazlito writeiso
  1.1069 +writeiso()
  1.1070 +{
  1.1071 +	# Writefs to ISO image including /home unlike gen-distro we dont use
  1.1072 +	# packages to generate a rootfs, we build a compressed rootfs with all
  1.1073 +	# the current filesystem similar to 'tazusb writefs'.
  1.1074 +	#
  1.1075 +	DISTRO="/home/slitaz/$SLITAZ_VERSION/distro"
  1.1076 +	ROOTCD="$DISTRO/rootcd"
  1.1077 +	if [ -z $1 ]; then
  1.1078 +		COMPRESSION=none
  1.1079 +	else
  1.1080 +		COMPRESSION=$1
  1.1081 +	fi
  1.1082 +	if [ -z $2 ]; then
  1.1083 +		ISO_NAME="slitaz"
  1.1084 +	else
  1.1085 +		ISO_NAME="$2"
  1.1086 +	fi
  1.1087 +	check_root
  1.1088 +	# Start info
  1.1089 +	newline
  1.1090 +	echo -e "\033[1mWrite filesystem to ISO\033[0m
  1.1091 +===============================================================================
  1.1092 +The command writeiso will write the current filesystem into a suitable cpio
  1.1093 +archive ($INITRAMFS) and generate a bootable ISO image (slitaz.iso).
  1.1094 +
  1.1095 +Archive compression: $COMPRESSION"
  1.1096 +	newline
  1.1097 +
  1.1098 +	# Save some space
  1.1099 +	rm /var/cache/tazpkg/* -r -f
  1.1100 +	[ -d $DISTRO ] && rm -rf $DISTRO
  1.1101 +
  1.1102 +	# Optionally remove sound card selection and screen resolution.
  1.1103 +	echo "Do you wish to remove the sound card and screen configs ? "
  1.1104 +	echo -n "Press ENTER to keep or answer (No|yes|exit): "
  1.1105 +	read anser
  1.1106 +	case $anser in
  1.1107 +		e|E|"exit"|Exit)
  1.1108 +			exit 0 ;;
  1.1109 +		y|Y|yes|Yes)
  1.1110 +			echo -n "Removing current sound card and screen configurations..."
  1.1111 +			rm -f /var/lib/sound-card-driver
  1.1112 +			rm -f /etc/asound.state
  1.1113 +			rm -f /etc/X11/screen.conf
  1.1114 +			rm -f /etc/X11/xorg.conf ;;
  1.1115 +		*)
  1.1116 +			echo -n "Keeping current sound card and screen configurations..." ;;
  1.1117 +	esac
  1.1118 +	status
  1.1119 +
  1.1120 +	cd /
  1.1121 +	# Create list of files including default user files since it is defined in /etc/passwd
  1.1122 +	# and some new users might have been added.
  1.1123 +	find bin etc init sbin var dev lib root usr home >/tmp/list
  1.1124 +
  1.1125 +	for dir in proc sys tmp mnt media media/cdrom media/flash media/usbdisk
  1.1126 +	do
  1.1127 +		echo $dir >>/tmp/list
  1.1128 +	done
  1.1129 +
  1.1130 +	# Generate initramfs with specified compression and display rootfs
  1.1131 +	# size in realtime.
  1.1132 +	rm -f /tmp/rootfs
  1.1133 +	write_initramfs &
  1.1134 +	sleep 2
  1.1135 +	cd - > /dev/null
  1.1136 +	echo -en "\nFilesystem size:"
  1.1137 +	while [ ! -f /tmp/rootfs ]
  1.1138 +	do
  1.1139 +		sleep 1
  1.1140 +		echo -en "\\033[18G`du -sh /$INITRAMFS | awk '{print $1}'`    "
  1.1141 +	done
  1.1142 +	echo -e "\n"
  1.1143 +
  1.1144 +	# Move freshly generated rootfs to the cdrom.
  1.1145 +	mkdir -p $ROOTCD/boot
  1.1146 +	mv -f /$INITRAMFS $ROOTCD/boot
  1.1147 +
  1.1148 +	# Now we need the kernel and isolinux files.
  1.1149 +	if  mount /dev/cdrom /media/cdrom 2>/dev/null; then
  1.1150 +		cp /media/cdrom/boot/bzImage $ROOTCD/boot
  1.1151 +		cp -a /media/cdrom/boot/isolinux $ROOTCD/boot
  1.1152 +		unmeta_boot $ROOTCD
  1.1153 +		umount /media/cdrom
  1.1154 +	elif  mount |grep /media/cdrom; then
  1.1155 +		cp /media/cdrom/boot/bzImage $ROOTCD/boot
  1.1156 +		cp -a /media/cdrom/boot/isolinux $ROOTCD/boot
  1.1157 +		unmeta_boot $ROOTCD
  1.1158 +		umount /media/cdrom;
  1.1159 +	else
  1.1160 +		echo -e "
  1.1161 +When SliTaz is running in RAM the kernel and bootloader files are kept
  1.1162 +on the cdrom. Please insert a LiveCD or loop mount the slitaz.iso to
  1.1163 +/media/cdrom to let Tazlito copy the files.\n"
  1.1164 +		echo -en "----\nENTER to continue..."; read i
  1.1165 +		exit 1
  1.1166 +	fi
  1.1167 +
  1.1168 +	# Generate the iso image.
  1.1169 +	cd $DISTRO
  1.1170 +	echo "Generating ISO image..."
  1.1171 +	genisoimage -R -o $ISO_NAME.iso -b boot/isolinux/isolinux.bin \
  1.1172 +	-c boot/isolinux/boot.cat -no-emul-boot -boot-load-size 4 \
  1.1173 +	-V "SliTaz" -input-charset iso8859-1 -boot-info-table $ROOTCD
  1.1174 +	if [ -x /usr/bin/isohybrid ]; then
  1.1175 +		echo -n "Creating hybrid ISO..."
  1.1176 +		/usr/bin/isohybrid $ISO_NAME.iso -entry 2 2> /dev/null
  1.1177 +		status
  1.1178 +	fi
  1.1179 +	echo -n "Creating the ISO $CHECKSUM..."
  1.1180 +	$CHECKSUM $ISO_NAME.iso > $ISO_NAME.$SUM
  1.1181 +	status
  1.1182 +
  1.1183 +	echo "==============================================================================="
  1.1184 +	echo "ISO image: `du -sh $DISTRO/$ISO_NAME.iso`"
  1.1185 +	newline
  1.1186 +	echo -n "Exit or burn ISO to cdrom (Exit|burn)? "; read anser
  1.1187 +	case $anser in
  1.1188 +		burn)
  1.1189 +			eject
  1.1190 +			echo -n "Please insert a blank cdrom and press ENTER..."
  1.1191 +			read i && sleep 2
  1.1192 +			tazlito burn-iso $DISTRO/$ISO_NAME.iso
  1.1193 +			echo -en "----\nENTER to continue..."; read i ;;
  1.1194 +		*)
  1.1195 +			exit 0 ;;
  1.1196 +	esac
  1.1197 +}
  1.1198 +
  1.1199 +# tazlito repack
  1.1200 +repack()
  1.1201 +{
  1.1202 +	# Repack an iso with maximum lzma compression ratio.
  1.1203 +	#
  1.1204 +
  1.1205 +	ISO=$1
  1.1206 +
  1.1207 +	mkdir -p $TMP_DIR/mnt
  1.1208 +	# Extract filesystems
  1.1209 +	echo -n "Mounting $ISO"
  1.1210 +	mount -o loop,ro $ISO $TMP_DIR/mnt 2> /dev/null
  1.1211 +	status || cleanup_merge
  1.1212 +	cp -a $TMP_DIR/mnt $TMP_DIR/iso
  1.1213 +	umount -d $TMP_DIR/mnt
  1.1214 +
  1.1215 +	for i in $TMP_DIR/iso/boot/rootfs* ; do
  1.1216 +		echo -n "Repacking $(basename $i)"
  1.1217 +		(zcat $i 2> /dev/null || unlzma -c $i || cat $i) \
  1.1218 +			2>/dev/null > $TMP_DIR/rootfs
  1.1219 +		lzma e $TMP_DIR/rootfs $i \
  1.1220 +			 $(lzma_switches $TMP_DIR/rootfs)
  1.1221 +		status
  1.1222 +	done
  1.1223 +
  1.1224 +		create_iso $ISO $TMP_DIR/iso
  1.1225 +		rm -rf $TMP_DIR
  1.1226 +}
  1.1227 +
  1.1228 +frugal_install()
  1.1229 +{
  1.1230 +	ISO_IMAGE="$1"
  1.1231 +	newline
  1.1232 +	mkdir -p /boot/frugal
  1.1233 +	if [ -f "$ISO_IMAGE" ]; then
  1.1234 +		echo -n "Using ISO image: $ISO_IMAGE"
  1.1235 +		mkdir -p /tmp/iso && mount -o loop $ISO_IMAGE /tmp/iso
  1.1236 +		status
  1.1237 +		echo -n "Installing the Kernel and rootfs..."
  1.1238 +		cp -a /tmp/iso/boot/bzImage /boot/frugal
  1.1239 +		if [ -f $DISTRO/rootcd/boot/rootfs1.gz ]; then
  1.1240 +			cd /tmp/iso/boot
  1.1241 +			cat $(ls -r rootfs*.gz) > /boot/frugal/$INITRAMFS
  1.1242 +		else
  1.1243 +			cp -a /tmp/iso/boot/$INITRAMFS /boot/frugal
  1.1244 +		fi
  1.1245 +		umount /tmp/iso
  1.1246 +		status
  1.1247 +	else
  1.1248 +		echo -n "Using distro: $DISTRO"
  1.1249 +		cd $DISTRO && status
  1.1250 +		echo -n "Installing the Kernel and rootfs..."
  1.1251 +		cp -a $DISTRO/rootcd/boot/bzImage /boot/frugal
  1.1252 +		if [ -f $DISTRO/rootcd/boot/rootfs1.gz ]; then
  1.1253 +			cd $DISTRO/rootcd/boot
  1.1254 +			cat $(ls -r rootfs*.gz) > /boot/frugal/$INITRAMFS
  1.1255 +		else
  1.1256 +			cp -a $DISTRO/rootcd/boot/$INITRAMFS /boot/frugal
  1.1257 +		fi
  1.1258 +		status
  1.1259 +	fi
  1.1260 +	# Grub entry
  1.1261 +	if ! grep -q "^kernel /boot/frugal/bzImage" /boot/grub/menu.lst; then
  1.1262 +		echo -n "Configuring GRUB menu list..."
  1.1263 +		cat >> /boot/grub/menu.lst << EOT
  1.1264 +title SliTaz GNU/Linux (frugal)
  1.1265 +root (hd0,0)
  1.1266 +kernel /boot/frugal/bzImage root=/dev/null
  1.1267 +initrd /boot/frugal/rootfs.gz
  1.1268 +EOT
  1.1269 +	else
  1.1270 +		echo -n "GRUB menu list is up-to-date..."
  1.1271 +	fi
  1.1272 +	status
  1.1273 +	newline
  1.1274 +}