All pastes #482947 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-05-11 14:13 UTC
rendered paste body
<image name="ltsp-suse-10.2">
	<description type="system">
		<author>CyberOrg</author>
		<contact>cyberorg@cyberorg.info</contact>
		<specification>LTSP Test System</specification>
	</description>
	<preferences>
		<type>ext3</type>
		<!--<type>vmx:ext3:vmxboot/suse-10.3</type>-->
		<version>0.0.1</version>
		<size unit="M">2700</size>
		<packagemanager>smart</packagemanager>
		<rpm-check-signatures>False</rpm-check-signatures>
		<rpm-force>True</rpm-force>
	</preferences>
	<users group="users">
		<user name="linux" pwd="$2a$10$90AjhfXU2YIwTRrIftBauecvWXVuaNZ6JLM2IpWi0svu2kO16le9e" home="/home/linux"/>
	</users>
	<repository type="rpm-md">
		<!--<source path="/image/CDs/full-i386"/>-->
		<!--<source path="/mounts/machcd2/consistent/full-i386"/>-->
		<source path="ftp://ftp-1.gwdg.de/pub/opensuse/repositories/home%3A/cyberorg/openSUSE_10.2"/>
	</repository>
	<repository type="yast2">
		<!--<source path="/image/CDs/full-i386"/>-->
		<!--<source path="/mounts/machcd2/consistent/full-i386"/>-->
		<source path="/home/instsrc/10_2/CD1"/>
	</repository>
	<packages type="image" patternType="onlyRequired">
		<package name="subversion"/>
      		<package name="915resolution"/>
      		<package name="kbd"/>
      		<package name="insserv"/>
      		<package name="aaa_base"/>
      		<package name="aaa_skel"/>
      		<package name="dhcpcd"/>
      		<package name="hwinfo"/>
      		<package name="binutils"/>
      		<package name="mkinitrd"/>
		<package name="fuse"/>
      		<package name="module-init-tools"/>
      		<package name="netcfg"/>
      		<package name="openSUSE-release"/>
      		<package name="openssh"/>
      		<package name="procps"/>
      		<package name="pwdutils"/>
      		<package name="rpm"/>
      		<package name="smart"/>
      		<package name="suse-build-key"/>
      		<package name="syslog-ng"/>
      		<package name="sysconfig"/>
      		<package name="x11-tools"/>
      		<package name="x11-input-wacom"/>
     		<package name="x11-input-synaptics"/>
      		<package name="xorg-x11"/>
      		<package name="xorg-x11-Xvnc"/>
      		<package name="xorg-x11-libX11-ccache"/>
      		<package name="xorg-x11-server"/>
      		<package name="gtk2"/>
      		<package name="gtk2-engines"/>
      		<package name="nbd"/>
      		<package name="python-gnome"/>
      		<package name="psmisc"/>
      		<package name="tar"/>
      		<package name="ldm"/>
      		<package name="ltsp-client"/>
      		<package name="ltspfs"/>
      		<package name="ltspfs-server"/>
		<ignore  name="smtp_daemon"/>
	</packages>
    <packages type="boot">
        <package name="filesystem"/> 
        <package name="glibc-locale"/>
		<package name="kernel-default"/>
        <package name="devs"/>
    </packages>
</image>


#!/bin/bash
#================
# FILE          : linuxrc
#----------------
# PROJECT       : OpenSuSE KIWI Image System
# COPYRIGHT     : (c) 2006 SUSE LINUX Products GmbH. All rights reserved
#               :
# AUTHOR        : Marcus Schaefer <ms@suse.de>
#               :
# BELONGS TO    : Operating System images
#               :
# DESCRIPTION   : This file is changed to become the real
#               : linuxrc script which is used to prepare the
#               : operating system for the main image
#               :
#               :
# STATUS        : BETA
#----------------
#======================================
# Exports (General)
#--------------------------------------
export PATH="/sbin:/bin:/usr/sbin:/usr/bin"
export IFS_ORIG=$IFS
export ARCH=`arch`
export DEBUG=0

#======================================
# Exports (Booting)
#--------------------------------------
export DOMURD_MODULES="xennet xenblk"
export INITRD_MODULES="reiserfs"
export LOCAL_BOOT="no"
export KERNEL_LIST

#======================================
# Exports (Alias)
#--------------------------------------
export SYSALIAS="undefined"
export NAME=0

#======================================
# Exports (Partitioning)
#--------------------------------------
export PART_FILE="/etc/partition.table"
export PART_MOUNT
export PART_DEV
export PART_COUNT=0
export PART_NUMBER=0
export PART_NEED_EXTENDED=0
export PART_NEED_FILL=0
export NO_FILE_SYSTEM=0

#======================================
# Exports (Status)
#--------------------------------------
export SYSTEM_INTEGRITY
export SYSTEM_MD5STATUS

#======================================
# Functions
#--------------------------------------
. /include

#======================================
# Beautify Startup
#--------------------------------------
clear
echo "Loading KIWI Boot-System..."
echo "---------------------------"

#======================================
# 1) Mounting local file systems
#--------------------------------------
mount -t proc  proc    /proc
mount -t sysfs sysfs   /sys
mount -t devpts devpts /dev/pts
closeKernelConsole

#======================================
# 2) Prepare module load support 
#--------------------------------------
touch /etc/modules.conf
touch /lib/modules/*/modules.dep

#======================================
# 3) Include proc/cmdline information
#--------------------------------------
includeKernelParameters
if [ ! -z $IMAGE ];then
	# /.../
	# if the image information is already in place at this stage
	# it comes from the cmdline data which means we are not booting
	# from the network but want to boot the local system
	# ----
	LOCAL_BOOT="yes"
fi

#======================================
# 4) Obtain/load network module
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	loaded=0
	probeNetworkCard
	IFS=":"
	for i in $networkModule;do
		if [ ! -z $i ];then
			modprobe $i 2>/dev/null
			if test $? = 0;then
				loaded=1
			fi
		fi
	done
	IFS=$IFS_ORIG
	if test $loaded = 0;then
		systemException \
			"Network module: Failed to load network module !" \
		"reboot"
	fi
fi

#======================================
# 5) Setup network interface and DNS
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	setupNetwork
fi

#======================================
# 6) get TFTP Server IP/name
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	checkTFTP
	if [ -z $TSERVER ];then
		TSERVER=tftp.$DOMAIN
	fi
	Echo "Checking TFTP Server name: $TSERVER"
	ip=`host $TSERVER | cut -f4 -d' '`
	if test $ip = "found:" -o $ip = "out;";then
		Echo "TFTP Server: $TSERVER not found"
		Echo "Using: $DHCPSIADDR from dhcpcd-info"
		TSERVER=$DHCPSIADDR
	fi
fi

#======================================
# 7) Load configuration
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	CONFIG=/etc/config.netclient
	while true;do
		result=`atftp -g \
			-r KIWI/config.$DHCPCHADDR -l $CONFIG $TSERVER 2>&1 | head -n 1`
		status1=`echo $result | cut -f2 -d" "`
		status2=`echo $result | cut -f6,7 -d" "`
		if test "$status1" = "unknown";then
			Echo -n "TFTP Server unknown, enter IP: " && read TSERVER
		elif test "$status2" = "<Access violation>";then
			Echo -n "TFTP access violation, enter IP: " && read TSERVER
		else
			break
		fi
		if test -z $TSERVER;then
			TSERVER=$DHCPSIADDR
		fi
	done
	if test $? != 0 -o ! -s $CONFIG;then
		#======================================
		# Register new network client
		#--------------------------------------
		Echo "Registering new network client..."
		if test $NAME -ne 0;then
		if test $NAME -eq -1;then
			Echo -n "Enter Alias Name for this system: " && \
			read SYSALIAS
		else
			Echo -n "Enter Alias Name [timeout in $NAME sec]: " && \
			SYSALIAS=`/sbin/timed $NAME`
		fi
		fi
		hwinfo --all --log=hwinfo.$DHCPCHADDR >/dev/null
		echo "NCNAME=$SYSALIAS"   >> hwtype.$DHCPCHADDR
		echo "ARCHITECTURE=$ARCH" >> hwtype.$DHCPCHADDR
		atftp -p -l hwtype.$DHCPCHADDR \
			-r upload/hwtype.$DHCPCHADDR $TSERVER >/dev/null 2>&1
		atftp -p -l hwinfo.$DHCPCHADDR \
			-r upload/hwinfo.$DHCPCHADDR $TSERVER >/dev/null 2>&1
		echo
		Echo "Registered as: $DHCPCHADDR"
		Echo "Waiting for configuration..."
		sleep 60

		#======================================
		# Try to get config again
		#--------------------------------------
		while test ! -s $CONFIG;do
			Echo "Lookup network client config file again..."
			dhcpcd -n
			atftp -g -r KIWI/config.$DHCPCHADDR \
				-l $CONFIG $TSERVER \
			>/dev/null 2>&1
			test -s $CONFIG || {
				Echo "Couldn't get image configuration"
				Echo "sleeping [60 sec]..."
				sleep 60
			}
		done
	fi
	importFile < $CONFIG
fi

#======================================
# (8) Load Device modules
#--------------------------------------
probeDevices

export systemIntegrity="unknown"
#======================================
# 9) Is this a diskful station
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
if [ ! -z "$PART" ] && [ -z "$NFSROOT" ];then
	#======================================
	# Check for installed system
	#--------------------------------------
	count=0
	IFS="," ; for i in $IMAGE;do
	case $count in
	0) {
		field=0
		IFS=";" ; for n in $i;do
		case $field in
			0) imageDevice=$n ; field=1 ;;
			1) imageName=$n   ; field=2 ;;
			2) imageVersion=$n; field=3
		esac
		done
		count=1
		updateNeeded initialize
		if linuxPartition $DISK;then
			mountSystem $imageDevice
			updateNeeded
			umountSystem
			systemIntegrity=`getSystemIntegrity 1`
			if [ $systemIntegrity = "fine" ];then
				Echo "Base system is up to date, activating disk system..."
			fi
		else
			systemIntegrity="clean"
		fi
	}
	;;
	*)
		# handle other images here...
	;;
	esac
	done
	#======================================
	# Create partition table if needed
	#--------------------------------------
	if test $systemIntegrity = "clean";then
		Echo "Creating partition table..."
		partitionCount
		createSwap
		createPartition
		writePartitionTable $DISK
		IFS=":" ; for i in $PART_DEV;do
		if test ! -z "$i";then
			createFileSystem $i
		fi
		done
		mkswap ${DISK}1 >/dev/null 2>&1
	fi
fi
fi

#======================================
# 10) Download network client image
#--------------------------------------
if [ -z "$NFSROOT" ];then
	count=0
	IFS="," ; for i in $IMAGE;do
		imageZipped="uncompressed"
		count=$(($count + 1))
		field=0
		IFS=";" ; for n in $i;do
		case $field in
			0) imageDevice=$n ; field=1 ;;
			1) imageName=$n   ; field=2 ;;
			2) imageVersion=$n; field=3 ;;
			3) imageServer=$n ; field=4 ;;
			4) imageBlkSize=$n; field=5 ;;
			5) imageZipped=$n ;
		esac
		done
		if [ $count = 1 ];then
			imageRootDevice=$imageDevice
			imageRootName=$imageName
		fi
		if [ $count = 2 ];then
			imageNextRootDevice=$imageDevice
		fi
		if [ $LOCAL_BOOT = "yes" ];then
			continue
		fi
		if test `getSystemIntegrity $count` = "fine";then
			continue
		fi
		imageName="image/$imageName-$imageVersion"
		imageMD5s="$imageName.md5"
		[ -z "$imageServer" ]  && imageServer=$TSERVER
		[ -z "$imageBlkSize" ] && imageBlkSize=8192
		while true;do
			# /.../
			# get image md5sum to be able to check for the size
			# requirements if we are loading into RAM
			# ---
			loadStatus=`atftp -g \
				-r $imageMD5s -l /etc/image.md5 $imageServer 2>&1`
			if ! loadOK "$loadStatus";then
				systemException \
				"Failed to load image MD5: $imageName -> no such image MD5" \
				"reboot"
			fi
			IFS=" "
			read sum1 blocks blocksize < /etc/image.md5
			if test -z $DISK;then
				if ! validateRAM;then
					systemException \
						"Not enough RAM available for this image" \
					"reboot"
				fi
			else
				if ! validateSize;then
					systemException \
						"Not enough space available for this image" \
					"reboot"
				fi
			fi
			validateBlockSize
			# /.../
			# now load the image into the imageDevice
			# ---
			multicast="-m"
			if test "$imageZipped" = "compressed"; then
				Echo "Compressed image found: Disable multicast download"
				multicast=""
				test ! -p /dev/compressed_image && mkfifo /dev/compressed_image
				cat /dev/compressed_image | gzip -d > $imageDevice 2>/dev/null &
				imageDevice_orig=$imageDevice
				imageName_orig=$imageName
				imageDevice="/dev/compressed_image"
				imageName="$imageName.gz"
			fi
			Echo "Loading $imageName [$imageDevice BS:$imageBlkSize Byte]..."
			loadStatus=`atftp $multicast --option "blksize $imageBlkSize" -g \
				-r $imageName -l $imageDevice $imageServer 2>&1`
			if ! loadOK "$loadStatus";then
				systemException \
					"Failed to load image: $imageName -> no such image" \
				"reboot"
			fi
			if test "$imageZipped" = "compressed"; then
				imageDevice=$imageDevice_orig
				imageName=$imageName_orig
			fi
			# /.../
			# check the md5sum of the downloaded data records
			# ----
			Echo "Download complete, checking data..."
			dd if=$imageDevice count=$blocks bs=$blocksize 2>/dev/null |\
				md5sum - > /etc/ireal.md5
			read sum2 dumy < /etc/ireal.md5
			if test $sum1 = $sum2;then
				Echo "Image checksum test: fine :-)"
				break
			fi
			Echo "Image checksum test failed:"
			Echo "Possible reasons:"
			echo 
			Echo -b "1) Physical ethernet connection lost:"
			Echo -b "   please check cable"
			echo
			Echo -b "2) Data corruption while loading the image:"
			Echo -b "   will give it a new try..."
			echo
			Echo -b "3) wrong checksum file created for the image:"
			Echo -b "   check with the md5sum command if the image on the"
			Echo -b "   TFTP server provides the same md5 sum as included"
			Echo -b "   within the appropriate .md5 file for this image"
			echo
			if test -z $DISK;then
			Echo -b "4) ramdisk size is too small for the image:"
			Echo -b "   check the ramdisk_size parameter of the PXE"
			Echo -b "   configuration file on the TFTP server"
			else
			Echo -b "4) partition size is too small for the image:"
			Echo -b "   check the PART line in the image config file on the"
			Echo -b "   TFTP server"
			fi
			echo
			Echo "Retry to load image..."
			sleep 15
		done
		echo $imageDevice | grep -q ram
		haveDataInRam=$?
		if test ! -z $DISK && test $haveDataInRam != 0;then
			mountSystem $imageRootDevice
			readFileSystem /mnt/image/config.xml
			umountSystem
			if test "$FSTYPE" = "reiserfs";then
				Echo "Resize Reiser filesystem to full partition space..."
				resize_reiserfs $imageDevice
				INITRD_MODULES="$INITRD_MODULES reiserfs"
			fi
			if test "$FSTYPE" = "ext2";then
				Echo "Checking EXT2 filesystem..."
				e2fsck -f $imageDevice -y
				Echo "Resize EXT2 filesystem to full partition space..."
				resize2fs -F -p $imageDevice
				Echo "Checking EXT2 filesystem..."
				e2fsck -f $imageDevice -y
				INITRD_MODULES="$INITRD_MODULES ext2"
			fi
			if test "$FSTYPE" = "ext3";then
				Echo "Checking EXT3 filesystem..."
				e2fsck -f $imageDevice -y
				Echo "Resize EXT3 filesystem to full partition space..."
				resize2fs -F -p $imageDevice
				Echo "Adding EXT3 journal..."
				tune2fs -j $imageDevice
				Echo "Checking EXT3 filesystem..."
				e2fsck -f $imageDevice -y
				INITRD_MODULES="$INITRD_MODULES ext3"
			fi
		fi
		if test ! -z $DISK && test $haveDataInRam = 0;then
			Echo "Including RAM device data to disk..."
			RELOAD_CONFIG="yes"
			mkdir -p /config
			mountSystem
			rm -rf /mnt/lost+found
			cp -a /mnt/* /config && rm -f /mnt/*
			umountSystem
		fi
		rm -f /etc/ireal.md5
		rm -f /etc/image.md5
	done
else
	IFS="," ; for i in $NFSROOT;do
		field=0
		IFS=";" ; for n in $i;do
		case $field in
			0) nfsRootServer=$n ; field=1 ;;
			1) nfsRootDevice=$n ; field=2
		esac
		done
		Echo "Mounting NFS root system: $nfsRootServer:$nfsRootDevice..."
		imageRootDevice="-o nolock,rw $nfsRootServer:$nfsRootDevice"
		imageRootName="NFSRoot-System"
		systemIntegrity="clean"
	done
fi

#======================================
# 11) Check for RELOAD_CONFIG
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	if test ! -z $DISK;then
	if test ! -z $RELOAD_CONFIG;then
		systemIntegrity_save=$systemIntegrity
		systemIntegrity="clean"
	fi
	fi
fi

#======================================
# 12) Mount OS image to /mnt
#--------------------------------------
if ! mountSystem;then
	systemException "Failed to mount root filesystem" "reboot"
fi

#======================================
# 13) Import fixed configuration files
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	if test $systemIntegrity = "clean";then
		mkdir -p /config
		#======================================
		# Get files from CONF value
		#--------------------------------------
		IFS="," ; for i in $CONF;do
			field=0
			IFS=";" ; for n in $i;do
			case $field in
				0) configSource=$n ; field=1 ;;
				1) configDest=$n   ; field=2 ;;
				2) configServer=$n ; field=3 ;;
				3) configBlkSize=$n;
			esac
			done
			Echo "Import configuration file: $configDest"
			dirs=`dirname  $configDest`
			mkdir -p /config/$dirs
			atftp --option "blksize $configBlkSize" -g \
				-r $configSource -l /config/$configDest $configServer \
			>/dev/null 2>&1
		done
		#======================================
		# Check for KIWI_INITRD
		#--------------------------------------
		if [ ! -z $KIWI_INITRD ];then
			Echo "Import KIWI initrd file: $KIWI_INITRD"
			rm -f /mnt/boot/initrd* && mkdir -p /mnt/boot
			atftp -g -r $KIWI_INITRD -l /mnt/boot/initrd \
				$TSERVER \
			>/dev/null 2>&1
			KIWI_INITRD_PARAMS="IMAGE=\"$IMAGE\""
			if [ ! -z $COMBINED_IMAGE ]; then
				SPLIT="COMBINED_IMAGE=$COMBINED_IMAGE"
				KIWI_INITRD_PARAMS="$KIWI_INITRD_PARAMS $SPLIT"
			fi
			if [ ! -z "$UNIONFS_CONFIG" ]; then
				UNIONED="UNIONFS_CONFIG=$UNIONFS_CONFIG"
				KIWI_INITRD_PARAMS="$KIWI_INITRD_PARAMS $UNIONED"
			fi
		fi
	fi
fi

#======================================
# 14) send DHCP_RELEASE, reset cache
#--------------------------------------
if [ $LOCAL_BOOT = "no" ] && [ -z "$NFSROOT" ];then
	if test $systemIntegrity = "clean";then
		dhcpcd -k
	fi
fi

#======================================
# 15) Check if this is a split image
#--------------------------------------
if [ ! -z "$COMBINED_IMAGE" ] && [ -z "$NFSROOT" ]; then
	READ_ONLY_SYSTEM=system
	mkdir -p /mnt/$READ_ONLY_SYSTEM && \
		mount $imageNextRootDevice /mnt/$READ_ONLY_SYSTEM
	cd /mnt
	for dir in bin boot lib opt sbin usr;do
		ln -s $READ_ONLY_SYSTEM/$dir $dir
	done
	cd /
fi

#======================================
# 16) check filesystem and kernels
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	if test $systemIntegrity = "clean";then
		readFileSystem /mnt/image/config.xml
		kernelList /mnt
	fi
fi

#======================================
# 17) make initrd available on unionfs
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	if test ! -z "$UNIONFS_CONFIG" && test $systemIntegrity = "clean";then
		# /.../
		# have to do this so that /boot gets copied to the
		# ext2 partition, which is necessary for grub to find
		# the stuff in there (it can't read squashfs)
		# ----
		cp -a /mnt/boot /mnt/boot2
		rm -rf /mnt/boot
		mv /mnt/boot2 /mnt/boot
	fi
fi

#======================================
# 18) Create system dependant files
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
if test $systemIntegrity = "clean";then
	mkdir -p /config/etc
	cat > /config/etc/fstab < /dev/null
	if [ -z "$UNIONFS_CONFIG" ]; then
		echo "$imageRootDevice / $FSTYPE defaults 0 0"    > /config/etc/fstab
	fi
	echo "devpts  /dev/pts   devpts mode=0620,gid=5 0 0" >> /config/etc/fstab
	echo "proc    /proc   proc    defaults 0 0"          >> /config/etc/fstab
	echo "sysfs   /sys    sysfs   noauto 0 0"            >> /config/etc/fstab
	echo "tmpfs   /dev/shm tmpfs  defaults 0 0"          >> /config/etc/fstab
	if test ! -z $DISK;then
		# a) /etc/fstab...
		# ----------------
		echo "${DISK}1 swap swap pri=42 0 0" >> /config/etc/fstab
		index=0
		IFS=":" ; for i in $PART_MOUNT;do
		if test ! -z "$i";then
			count=0
			IFS=":" ; for n in $PART_DEV;do
				device=$n
				if test $count -eq $index;then
					break
				fi
				count=`expr $count + 1`
			done
			index=`expr $index + 1`
			if test ! $i = "/" && test ! $i = "x";then
				probeFileSystem $device
				echo "$device $i $FSTYPE defaults 1 1" >> /config/etc/fstab
			fi
		fi
		done
		# b) /boot/grub/menu.lst...
		# -------------------------
		console=""
		mkdir -p /config/boot/grub
		echo "timeout 10" > /config/boot/grub/menu.lst
		IFS="," ; for i in $KERNEL_LIST;do
		if test ! -z "$i";then
			kernel=`echo $i | cut -f1 -d:`
			initrd=`echo $i | cut -f2 -d:`
			menu=/config/boot/grub/menu.lst
			echo "title $kernel" >> $menu
			if [ $kernel = "vmlinuz-xen" ];then
				echo " root (hd0,1)"                   >> $menu
				echo " kernel /boot/xen.gz"            >> $menu
				echo -n " module /boot/$kernel"        >> $menu
				echo -n " root=${DISK}2 $console"      >> $menu
				echo " $KIWI_INITRD_PARAMS"            >> $menu
				echo " module /boot/$initrd"           >> $menu
			else
				echo -n " kernel (hd0,1)/boot/$kernel" >> $menu
				echo -n " root=${DISK}2 $console"      >> $menu
				echo " $KIWI_INITRD_PARAMS"            >> $menu
				echo " initrd (hd0,1)/boot/$initrd"    >> $menu
			fi
		fi
		done
		# c) /etc/grub.conf...
		# -------------------------
		mkdir -p /config/etc
		gconf=/config/etc/grub.conf
		echo -en "root (hd0,1)\ninstall"         > $gconf
		echo -n " --stage2=/boot/grub/stage2"   >> $gconf
		echo -n " /boot/grub/stage1 d (hd0)"    >> $gconf
		echo -n " /boot/grub/stage2 0x8000"     >> $gconf
		echo " (hd0,1)/boot/grub/menu.lst"      >> $gconf
		echo "quit"                             >> $gconf
		# d) /etc/sysconfig/kernel...
		# ---------------------------
		mkdir -p /config/etc/sysconfig
		syskernel=/config/etc/sysconfig/kernel
		echo "INITRD_MODULES=\"$INITRD_MODULES\""       > $syskernel
		echo "DOMU_INITRD_MODULES=\"$DOMURD_MODULES\"" >> $syskernel
	fi
fi
fi

#======================================
# 19) copy system dependant files
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	if test $systemIntegrity = "clean";then
		cd /config
		find . -type d | while read d ; do  mkdir -p /mnt/$d ; done
		find . -type f | while read f ; do  cp $f /mnt/$f ; done
		cd /
		rm -rf /config
	fi
fi

echo 256 > /proc/sys/kernel/real-root-dev
#======================================
# 20) umount system filesystems
#--------------------------------------
umount /dev/pts
umount /sys
umount /proc

#======================================
# 21) copy initrd files to image
#--------------------------------------
cp /preinit /mnt
cp /include /mnt

#======================================
# 22) Activate new root
#--------------------------------------
Echo "Activating Image: [$imageRootName]"
cd /mnt && exec < dev/console >dev/console 2>&1
Echo "Calling preinit phase..."
/mnt/sbin/pivot_root . mnt >/dev/null 2>&1
if test $? != 0;then
	PIVOT=false
	mount --move . / && chroot . ./preinit
	chroot . rm ./preinit
	chroot . rm ./include
else
	PIVOT=true
	./preinit
	rm ./preinit
	rm ./include
fi
#======================================
# 23) reboot system if specified
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	if test ! -z $REBOOT_IMAGE;then
		Echo "Rebooting System to activate new kernel..."
		/sbin/restart >/dev/null 2>&1
	fi
fi
#======================================
# 24) Unmount initrd / system init
#--------------------------------------
echo " "
echo "Booting into final System..."
echo "----------------------------"
reopenKernelConsole
mount -n -o remount,rw / 2>/dev/null
if [ $PIVOT = "true" ];then
	exec umount -n -l /mnt
else
	exec chroot . /sbin/init
fi



#!/bin/bash
#================
# FILE          : preinit
#----------------
# PROJECT       : OpenSuSE KIWI Image System
# COPYRIGHT     : (c) 2006 SUSE LINUX Products GmbH. All rights reserved
#               :
# AUTHOR        : Marcus Schaefer <ms@suse.de>
#               :
# BELONGS TO    : Operating System images
#               :
# DESCRIPTION   : This file is called after the image root
#               : has changed by the linuxrc script
#               :
# STATUS        : BETA
#----------------
#======================================
# Functions
#--------------------------------------
. /include

#======================================
# 1) check for LOCAL_BOOT
#--------------------------------------
if [ $LOCAL_BOOT = "yes" ];then
	exit 0
fi

#======================================
# 2) create initrd on diskful
#--------------------------------------
if test -z $KIWI_INITRD;then
if test ! -z $DISK;then
	if test $systemIntegrity = "clean";then
		if test -L /boot/boot;then
			mount -t proc proc /proc
			mount -t sysfs sysfs /sys
			mkinitrd 
			umount /sys
			umount /proc
		else
			Echo "Image doesn't provide kernel system map"
			Echo "Can't create initrd"
		fi
	fi
fi
fi

#======================================
# 3) Install boot loader on diskful
#--------------------------------------
if test ! -z $DISK;then
if test $systemIntegrity = "clean";then
	if test -f "/usr/sbin/grub";then
		Echo "Installing boot loader..."
		/usr/sbin/grub --batch --no-floppy < /etc/grub.conf >/dev/null 2>&1
		echo "alias eth0 $networkModule" >> /etc/modules.conf
	else
		Echo "Image doesn't provide grub support"
		Echo "Can't install boot loader"
	fi
fi
fi

#======================================
# 4) Reset systemIntegrity
#--------------------------------------
if test ! -z $DISK;then
if test ! -z $RELOAD_CONFIG;then
	systemIntegrity=$systemIntegrity_save
fi
fi

#======================================
# 5) check for valid mount points 
#--------------------------------------
if test ! -z $DISK;then
	IFS=":" ; for i in $PART_MOUNT;do
	if test ! -z "$i";then
	if test ! -f "$i";then
		mkdir -p $i
	fi
	fi
	done
fi

#======================================
# 6) Update /etc/ImageVersion files
#--------------------------------------
if test ! -z $DISK;then
	count=0
	IFS="," ; for i in $IMAGE;do
		count=$(($count + 1))
		field=0
		IFS=";" ; for n in $i;do
		case $field in
			0) field=1 ;;
			1) imageName=$n   ; field=2 ;;
			2) imageVersion=$n; field=3 ;;
			3) imageServer=$n ; field=4 ;;
			4) imageBlkSize=$n
		esac
		done
		atversion="$imageName-$imageVersion"
		versionFile="/etc/ImageVersion-$atversion"
		md5sum=`getSystemMD5Status $count`
		if test `getSystemIntegrity $count` = "clean";then
			echo "$atversion $md5sum" > $versionFile
		fi
	done
fi

#======================================
# 7) setup network for nfs boot
#--------------------------------------
if test ! -z $NFSROOT;then
	mount -o nolock -t proc  proc    /proc
	mount -o nolock -t sysfs sysfs   /sys
	mount -o nolock -t devpts devpts /dev/pts
	/etc/init.d/portmap start
fi



#!/bin/bash
#================
# FILE          : linuxrc
#----------------
# PROJECT       : OpenSuSE KIWI Image System
# COPYRIGHT     : (c) 2006 SUSE LINUX Products GmbH. All rights reserved
#               :
# AUTHOR        : Marcus Schaefer <ms@suse.de>
#               :
# BELONGS TO    : Operating System images
#               :
# DESCRIPTION   : This file is changed to become the real
#               : linuxrc script which is used to prepare the
#               : operating system for the main image
#               :
#               :
# STATUS        : BETA
#----------------
#======================================
# Exports (General)
#--------------------------------------
export PATH="/sbin:/bin:/usr/sbin:/usr/bin"
export IFS_ORIG=$IFS
export ARCH=`arch`
export DEBUG=0

#======================================
# Exports (Booting)
#--------------------------------------
export DOMURD_MODULES="xennet xenblk"
export INITRD_MODULES="reiserfs"
export LOCAL_BOOT="no"
export KERNEL_LIST

#======================================
# Exports (Alias)
#--------------------------------------
export SYSALIAS="undefined"
export NAME=0

#======================================
# Exports (Partitioning)
#--------------------------------------
export PART_FILE="/etc/partition.table"
export PART_MOUNT
export PART_DEV
export PART_COUNT=0
export PART_NUMBER=0
export PART_NEED_EXTENDED=0
export PART_NEED_FILL=0
export NO_FILE_SYSTEM=0

#======================================
# Exports (Status)
#--------------------------------------
export SYSTEM_INTEGRITY
export SYSTEM_MD5STATUS

#======================================
# Functions
#--------------------------------------
. /include

#======================================
# Beautify Startup
#--------------------------------------
clear
echo "Loading KIWI Boot-System..."
echo "---------------------------"

#======================================
# 1) Mounting local file systems
#--------------------------------------
mount -t proc  proc    /proc
mount -t sysfs sysfs   /sys
mount -t devpts devpts /dev/pts
closeKernelConsole

#======================================
# 2) Prepare module load support 
#--------------------------------------
touch /etc/modules.conf
touch /lib/modules/*/modules.dep

#======================================
# 3) Include proc/cmdline information
#--------------------------------------
includeKernelParameters
if [ ! -z $IMAGE ];then
	# /.../
	# if the image information is already in place at this stage
	# it comes from the cmdline data which means we are not booting
	# from the network but want to boot the local system
	# ----
	LOCAL_BOOT="yes"
fi

#======================================
# 4) Obtain/load network module
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	loaded=0
	probeNetworkCard
	IFS=":"
	for i in $networkModule;do
		if [ ! -z $i ];then
			modprobe $i 2>/dev/null
			if test $? = 0;then
				loaded=1
			fi
		fi
	done
	IFS=$IFS_ORIG
	if test $loaded = 0;then
		systemException \
			"Network module: Failed to load network module !" \
		"reboot"
	fi
fi

#======================================
# 5) Setup network interface and DNS
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	setupNetwork
fi

#======================================
# 6) get TFTP Server IP/name
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	checkTFTP
	if [ -z $TSERVER ];then
		TSERVER=tftp.$DOMAIN
	fi
	Echo "Checking TFTP Server name: $TSERVER"
	ip=`host $TSERVER | cut -f4 -d' '`
	if test $ip = "found:" -o $ip = "out;";then
		Echo "TFTP Server: $TSERVER not found"
		Echo "Using: $DHCPSIADDR from dhcpcd-info"
		TSERVER=$DHCPSIADDR
	fi
fi

#======================================
# 7) Load configuration
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	CONFIG=/etc/config.netclient
	while true;do
		result=`atftp -g \
			-r KIWI/config.$DHCPCHADDR -l $CONFIG $TSERVER 2>&1 | head -n 1`
		status1=`echo $result | cut -f2 -d" "`
		status2=`echo $result | cut -f6,7 -d" "`
		if test "$status1" = "unknown";then
			Echo -n "TFTP Server unknown, enter IP: " && read TSERVER
		elif test "$status2" = "<Access violation>";then
			Echo -n "TFTP access violation, enter IP: " && read TSERVER
		else
			break
		fi
		if test -z $TSERVER;then
			TSERVER=$DHCPSIADDR
		fi
	done
	if test $? != 0 -o ! -s $CONFIG;then
		#======================================
		# Register new network client
		#--------------------------------------
		Echo "Registering new network client..."
		if test $NAME -ne 0;then
		if test $NAME -eq -1;then
			Echo -n "Enter Alias Name for this system: " && \
			read SYSALIAS
		else
			Echo -n "Enter Alias Name [timeout in $NAME sec]: " && \
			SYSALIAS=`/sbin/timed $NAME`
		fi
		fi
		hwinfo --all --log=hwinfo.$DHCPCHADDR >/dev/null
		echo "NCNAME=$SYSALIAS"   >> hwtype.$DHCPCHADDR
		echo "ARCHITECTURE=$ARCH" >> hwtype.$DHCPCHADDR
		atftp -p -l hwtype.$DHCPCHADDR \
			-r upload/hwtype.$DHCPCHADDR $TSERVER >/dev/null 2>&1
		atftp -p -l hwinfo.$DHCPCHADDR \
			-r upload/hwinfo.$DHCPCHADDR $TSERVER >/dev/null 2>&1
		echo
		Echo "Registered as: $DHCPCHADDR"
		Echo "Waiting for configuration..."
		sleep 60

		#======================================
		# Try to get config again
		#--------------------------------------
		while test ! -s $CONFIG;do
			Echo "Lookup network client config file again..."
			dhcpcd -n
			atftp -g -r KIWI/config.$DHCPCHADDR \
				-l $CONFIG $TSERVER \
			>/dev/null 2>&1
			test -s $CONFIG || {
				Echo "Couldn't get image configuration"
				Echo "sleeping [60 sec]..."
				sleep 60
			}
		done
	fi
	importFile < $CONFIG
fi

#======================================
# (8) Load Device modules
#--------------------------------------
probeDevices

export systemIntegrity="unknown"
#======================================
# 9) Is this a diskful station
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
if [ ! -z "$PART" ] && [ -z "$NFSROOT" ];then
	#======================================
	# Check for installed system
	#--------------------------------------
	count=0
	IFS="," ; for i in $IMAGE;do
	case $count in
	0) {
		field=0
		IFS=";" ; for n in $i;do
		case $field in
			0) imageDevice=$n ; field=1 ;;
			1) imageName=$n   ; field=2 ;;
			2) imageVersion=$n; field=3
		esac
		done
		count=1
		updateNeeded initialize
		if linuxPartition $DISK;then
			mountSystem $imageDevice
			updateNeeded
			umountSystem
			systemIntegrity=`getSystemIntegrity 1`
			if [ $systemIntegrity = "fine" ];then
				Echo "Base system is up to date, activating disk system..."
			fi
		else
			systemIntegrity="clean"
		fi
	}
	;;
	*)
		# handle other images here...
	;;
	esac
	done
	#======================================
	# Create partition table if needed
	#--------------------------------------
	if test $systemIntegrity = "clean";then
		Echo "Creating partition table..."
		partitionCount
		createSwap
		createPartition
		writePartitionTable $DISK
		IFS=":" ; for i in $PART_DEV;do
		if test ! -z "$i";then
			createFileSystem $i
		fi
		done
		mkswap ${DISK}1 >/dev/null 2>&1
	fi
fi
fi

#======================================
# 10) Download network client image
#--------------------------------------
if [ -z "$NFSROOT" ];then
	count=0
	IFS="," ; for i in $IMAGE;do
		imageZipped="uncompressed"
		count=$(($count + 1))
		field=0
		IFS=";" ; for n in $i;do
		case $field in
			0) imageDevice=$n ; field=1 ;;
			1) imageName=$n   ; field=2 ;;
			2) imageVersion=$n; field=3 ;;
			3) imageServer=$n ; field=4 ;;
			4) imageBlkSize=$n; field=5 ;;
			5) imageZipped=$n ;
		esac
		done
		if [ $count = 1 ];then
			imageRootDevice=$imageDevice
			imageRootName=$imageName
		fi
		if [ $count = 2 ];then
			imageNextRootDevice=$imageDevice
		fi
		if [ $LOCAL_BOOT = "yes" ];then
			continue
		fi
		if test `getSystemIntegrity $count` = "fine";then
			continue
		fi
		imageName="image/$imageName-$imageVersion"
		imageMD5s="$imageName.md5"
		[ -z "$imageServer" ]  && imageServer=$TSERVER
		[ -z "$imageBlkSize" ] && imageBlkSize=8192
		while true;do
			# /.../
			# get image md5sum to be able to check for the size
			# requirements if we are loading into RAM
			# ---
			loadStatus=`atftp -g \
				-r $imageMD5s -l /etc/image.md5 $imageServer 2>&1`
			if ! loadOK "$loadStatus";then
				systemException \
				"Failed to load image MD5: $imageName -> no such image MD5" \
				"reboot"
			fi
			IFS=" "
			read sum1 blocks blocksize < /etc/image.md5
			if test -z $DISK;then
				if ! validateRAM;then
					systemException \
						"Not enough RAM available for this image" \
					"reboot"
				fi
			else
				if ! validateSize;then
					systemException \
						"Not enough space available for this image" \
					"reboot"
				fi
			fi
			validateBlockSize
			# /.../
			# now load the image into the imageDevice
			# ---
			multicast="-m"
			if test "$imageZipped" = "compressed"; then
				Echo "Compressed image found: Disable multicast download"
				multicast=""
				test ! -p /dev/compressed_image && mkfifo /dev/compressed_image
				cat /dev/compressed_image | gzip -d > $imageDevice 2>/dev/null &
				imageDevice_orig=$imageDevice
				imageName_orig=$imageName
				imageDevice="/dev/compressed_image"
				imageName="$imageName.gz"
			fi
			Echo "Loading $imageName [$imageDevice BS:$imageBlkSize Byte]..."
			loadStatus=`atftp $multicast --option "blksize $imageBlkSize" -g \
				-r $imageName -l $imageDevice $imageServer 2>&1`
			if ! loadOK "$loadStatus";then
				systemException \
					"Failed to load image: $imageName -> no such image" \
				"reboot"
			fi
			if test "$imageZipped" = "compressed"; then
				imageDevice=$imageDevice_orig
				imageName=$imageName_orig
			fi
			# /.../
			# check the md5sum of the downloaded data records
			# ----
			Echo "Download complete, checking data..."
			dd if=$imageDevice count=$blocks bs=$blocksize 2>/dev/null |\
				md5sum - > /etc/ireal.md5
			read sum2 dumy < /etc/ireal.md5
			if test $sum1 = $sum2;then
				Echo "Image checksum test: fine :-)"
				break
			fi
			Echo "Image checksum test failed:"
			Echo "Possible reasons:"
			echo 
			Echo -b "1) Physical ethernet connection lost:"
			Echo -b "   please check cable"
			echo
			Echo -b "2) Data corruption while loading the image:"
			Echo -b "   will give it a new try..."
			echo
			Echo -b "3) wrong checksum file created for the image:"
			Echo -b "   check with the md5sum command if the image on the"
			Echo -b "   TFTP server provides the same md5 sum as included"
			Echo -b "   within the appropriate .md5 file for this image"
			echo
			if test -z $DISK;then
			Echo -b "4) ramdisk size is too small for the image:"
			Echo -b "   check the ramdisk_size parameter of the PXE"
			Echo -b "   configuration file on the TFTP server"
			else
			Echo -b "4) partition size is too small for the image:"
			Echo -b "   check the PART line in the image config file on the"
			Echo -b "   TFTP server"
			fi
			echo
			Echo "Retry to load image..."
			sleep 15
		done
		echo $imageDevice | grep -q ram
		haveDataInRam=$?
		if test ! -z $DISK && test $haveDataInRam != 0;then
			mountSystem $imageRootDevice
			readFileSystem /mnt/image/config.xml
			umountSystem
			if test "$FSTYPE" = "reiserfs";then
				Echo "Resize Reiser filesystem to full partition space..."
				resize_reiserfs $imageDevice
				INITRD_MODULES="$INITRD_MODULES reiserfs"
			fi
			if test "$FSTYPE" = "ext2";then
				Echo "Checking EXT2 filesystem..."
				e2fsck -f $imageDevice -y
				Echo "Resize EXT2 filesystem to full partition space..."
				resize2fs -F -p $imageDevice
				Echo "Checking EXT2 filesystem..."
				e2fsck -f $imageDevice -y
				INITRD_MODULES="$INITRD_MODULES ext2"
			fi
			if test "$FSTYPE" = "ext3";then
				Echo "Checking EXT3 filesystem..."
				e2fsck -f $imageDevice -y
				Echo "Resize EXT3 filesystem to full partition space..."
				resize2fs -F -p $imageDevice
				Echo "Adding EXT3 journal..."
				tune2fs -j $imageDevice
				Echo "Checking EXT3 filesystem..."
				e2fsck -f $imageDevice -y
				INITRD_MODULES="$INITRD_MODULES ext3"
			fi
		fi
		if test ! -z $DISK && test $haveDataInRam = 0;then
			Echo "Including RAM device data to disk..."
			RELOAD_CONFIG="yes"
			mkdir -p /config
			mountSystem
			rm -rf /mnt/lost+found
			cp -a /mnt/* /config && rm -f /mnt/*
			umountSystem
		fi
		rm -f /etc/ireal.md5
		rm -f /etc/image.md5
	done
else
	IFS="," ; for i in $NFSROOT;do
		field=0
		IFS=";" ; for n in $i;do
		case $field in
			0) nfsRootServer=$n ; field=1 ;;
			1) nfsRootDevice=$n ; field=2
		esac
		done
		Echo "Mounting NFS root system: $nfsRootServer:$nfsRootDevice..."
		imageRootDevice="-o nolock,rw $nfsRootServer:$nfsRootDevice"
		imageRootName="NFSRoot-System"
		systemIntegrity="clean"
	done
fi

#======================================
# 11) Check for RELOAD_CONFIG
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	if test ! -z $DISK;then
	if test ! -z $RELOAD_CONFIG;then
		systemIntegrity_save=$systemIntegrity
		systemIntegrity="clean"
	fi
	fi
fi

#======================================
# 12) Mount OS image to /mnt
#--------------------------------------
if ! mountSystem;then
	systemException "Failed to mount root filesystem" "reboot"
fi

#======================================
# 13) Import fixed configuration files
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	if test $systemIntegrity = "clean";then
		mkdir -p /config
		#======================================
		# Get files from CONF value
		#--------------------------------------
		IFS="," ; for i in $CONF;do
			field=0
			IFS=";" ; for n in $i;do
			case $field in
				0) configSource=$n ; field=1 ;;
				1) configDest=$n   ; field=2 ;;
				2) configServer=$n ; field=3 ;;
				3) configBlkSize=$n;
			esac
			done
			Echo "Import configuration file: $configDest"
			dirs=`dirname  $configDest`
			mkdir -p /config/$dirs
			atftp --option "blksize $configBlkSize" -g \
				-r $configSource -l /config/$configDest $configServer \
			>/dev/null 2>&1
		done
		#======================================
		# Check for KIWI_INITRD
		#--------------------------------------
		if [ ! -z $KIWI_INITRD ];then
			Echo "Import KIWI initrd file: $KIWI_INITRD"
			rm -f /mnt/boot/initrd* && mkdir -p /mnt/boot
			atftp -g -r $KIWI_INITRD -l /mnt/boot/initrd \
				$TSERVER \
			>/dev/null 2>&1
			KIWI_INITRD_PARAMS="IMAGE=\"$IMAGE\""
			if [ ! -z $COMBINED_IMAGE ]; then
				SPLIT="COMBINED_IMAGE=$COMBINED_IMAGE"
				KIWI_INITRD_PARAMS="$KIWI_INITRD_PARAMS $SPLIT"
			fi
			if [ ! -z "$UNIONFS_CONFIG" ]; then
				UNIONED="UNIONFS_CONFIG=$UNIONFS_CONFIG"
				KIWI_INITRD_PARAMS="$KIWI_INITRD_PARAMS $UNIONED"
			fi
		fi
	fi
fi

#======================================
# 14) send DHCP_RELEASE, reset cache
#--------------------------------------
if [ $LOCAL_BOOT = "no" ] && [ -z "$NFSROOT" ];then
	if test $systemIntegrity = "clean";then
		dhcpcd -k
	fi
fi

#======================================
# 15) Check if this is a split image
#--------------------------------------
if [ ! -z "$COMBINED_IMAGE" ] && [ -z "$NFSROOT" ]; then
	READ_ONLY_SYSTEM=system
	mkdir -p /mnt/$READ_ONLY_SYSTEM && \
		mount $imageNextRootDevice /mnt/$READ_ONLY_SYSTEM
	cd /mnt
	for dir in bin boot lib opt sbin usr;do
		ln -s $READ_ONLY_SYSTEM/$dir $dir
	done
	cd /
fi

#======================================
# 16) check filesystem and kernels
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	if test $systemIntegrity = "clean";then
		readFileSystem /mnt/image/config.xml
		kernelList /mnt
	fi
fi

#======================================
# 17) make initrd available on unionfs
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	if test ! -z "$UNIONFS_CONFIG" && test $systemIntegrity = "clean";then
		# /.../
		# have to do this so that /boot gets copied to the
		# ext2 partition, which is necessary for grub to find
		# the stuff in there (it can't read squashfs)
		# ----
		cp -a /mnt/boot /mnt/boot2
		rm -rf /mnt/boot
		mv /mnt/boot2 /mnt/boot
	fi
fi

#======================================
# 18) Create system dependant files
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
if test $systemIntegrity = "clean";then
	mkdir -p /config/etc
	cat > /config/etc/fstab < /dev/null
	if [ -z "$UNIONFS_CONFIG" ]; then
		echo "$imageRootDevice / $FSTYPE defaults 0 0"    > /config/etc/fstab
	fi
	echo "devpts  /dev/pts   devpts mode=0620,gid=5 0 0" >> /config/etc/fstab
	echo "proc    /proc   proc    defaults 0 0"          >> /config/etc/fstab
	echo "sysfs   /sys    sysfs   noauto 0 0"            >> /config/etc/fstab
	echo "tmpfs   /dev/shm tmpfs  defaults 0 0"          >> /config/etc/fstab
	if test ! -z $DISK;then
		# a) /etc/fstab...
		# ----------------
		echo "${DISK}1 swap swap pri=42 0 0" >> /config/etc/fstab
		index=0
		IFS=":" ; for i in $PART_MOUNT;do
		if test ! -z "$i";then
			count=0
			IFS=":" ; for n in $PART_DEV;do
				device=$n
				if test $count -eq $index;then
					break
				fi
				count=`expr $count + 1`
			done
			index=`expr $index + 1`
			if test ! $i = "/" && test ! $i = "x";then
				probeFileSystem $device
				echo "$device $i $FSTYPE defaults 1 1" >> /config/etc/fstab
			fi
		fi
		done
		# b) /boot/grub/menu.lst...
		# -------------------------
		console=""
		mkdir -p /config/boot/grub
		echo "timeout 10" > /config/boot/grub/menu.lst
		IFS="," ; for i in $KERNEL_LIST;do
		if test ! -z "$i";then
			kernel=`echo $i | cut -f1 -d:`
			initrd=`echo $i | cut -f2 -d:`
			menu=/config/boot/grub/menu.lst
			echo "title $kernel" >> $menu
			if [ $kernel = "vmlinuz-xen" ];then
				echo " root (hd0,1)"                   >> $menu
				echo " kernel /boot/xen.gz"            >> $menu
				echo -n " module /boot/$kernel"        >> $menu
				echo -n " root=${DISK}2 $console"      >> $menu
				echo " $KIWI_INITRD_PARAMS"            >> $menu
				echo " module /boot/$initrd"           >> $menu
			else
				echo -n " kernel (hd0,1)/boot/$kernel" >> $menu
				echo -n " root=${DISK}2 $console"      >> $menu
				echo " $KIWI_INITRD_PARAMS"            >> $menu
				echo " initrd (hd0,1)/boot/$initrd"    >> $menu
			fi
		fi
		done
		# c) /etc/grub.conf...
		# -------------------------
		mkdir -p /config/etc
		gconf=/config/etc/grub.conf
		echo -en "root (hd0,1)\ninstall"         > $gconf
		echo -n " --stage2=/boot/grub/stage2"   >> $gconf
		echo -n " /boot/grub/stage1 d (hd0)"    >> $gconf
		echo -n " /boot/grub/stage2 0x8000"     >> $gconf
		echo " (hd0,1)/boot/grub/menu.lst"      >> $gconf
		echo "quit"                             >> $gconf
		# d) /etc/sysconfig/kernel...
		# ---------------------------
		mkdir -p /config/etc/sysconfig
		syskernel=/config/etc/sysconfig/kernel
		echo "INITRD_MODULES=\"$INITRD_MODULES\""       > $syskernel
		echo "DOMU_INITRD_MODULES=\"$DOMURD_MODULES\"" >> $syskernel
	fi
fi
fi

#======================================
# 19) copy system dependant files
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	if test $systemIntegrity = "clean";then
		cd /config
		find . -type d | while read d ; do  mkdir -p /mnt/$d ; done
		find . -type f | while read f ; do  cp $f /mnt/$f ; done
		cd /
		rm -rf /config
	fi
fi

echo 256 > /proc/sys/kernel/real-root-dev
#======================================
# 20) umount system filesystems
#--------------------------------------
umount /dev/pts
umount /sys
umount /proc

#======================================
# 21) copy initrd files to image
#--------------------------------------
cp /preinit /mnt
cp /include /mnt

#======================================
# 22) Activate new root
#--------------------------------------
Echo "Activating Image: [$imageRootName]"
cd /mnt && exec < dev/console >dev/console 2>&1
Echo "Calling preinit phase..."
/mnt/sbin/pivot_root . mnt >/dev/null 2>&1
if test $? != 0;then
	PIVOT=false
	mount --move . / && chroot . ./preinit
	chroot . rm ./preinit
	chroot . rm ./include
else
	PIVOT=true
	./preinit
	rm ./preinit
	rm ./include
fi
#======================================
# 23) reboot system if specified
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	if test ! -z $REBOOT_IMAGE;then
		Echo "Rebooting System to activate new kernel..."
		/sbin/restart >/dev/null 2>&1
	fi
fi
#======================================
# 24) Unmount initrd / system init
#--------------------------------------
echo " "
echo "Booting into final System..."
echo "----------------------------"
reopenKernelConsole
mount -n -o remount,rw / 2>/dev/null
if [ $PIVOT = "true" ];then
	exec umount -n -l /mnt
else
	exec chroot . /sbin/init
fi


#!/bin/bash
#================
# FILE          : KIWILinuxRC.sh
#----------------
# PROJECT       : OpenSUSE Build-Service
# COPYRIGHT     : (c) 2006 SUSE LINUX Products GmbH, Germany
#               :
# AUTHOR        : Marcus Schaefer <ms@suse.de>
#               :
# BELONGS TO    : Operating System images
#               :
# DESCRIPTION   : This module contains common used functions
#               : for the suse linuxrc and preinit boot image
#               : files
#               : 
#               :
# STATUS        : Development
#----------------
#======================================
# Debug
#--------------------------------------
function Debug {
	# /.../
	# print message if variable DEBUG is set to 1
	# -----
	if test "$DEBUG" = 1;then
		echo "+++++> $1"
	fi
}
#======================================
# Echo
#--------------------------------------
function Echo {
	# /.../
	# print a message to the controling terminal
	# ----
	if test "$1" = "-n";then
		echo $1 "-----> $2"
	elif test "$1" = "-b";then
		echo "       $2"
	else
		echo "-----> $1"
	fi
}
#======================================
# WaitKey
#--------------------------------------
function WaitKey {
	# /.../
	# if DEBUG is set wait for any key to continue
	# ----
	if test "$DEBUG" = 1;then
		Echo -n "Press any key to continue..."
		read
	fi
}
#======================================
# closeKernelConsole
#--------------------------------------
function closeKernelConsole {
	# /.../
	# close the kernel console, messages from the kernel
	# will not be displayed on the controling terminal
	# if DEBUG is set the kernel console remains open
	# ----
	if test "$DEBUG" = 0;then
		/usr/sbin/klogconsole -l 1
	fi
}
#======================================
# reopenKernelConsole
#--------------------------------------
function reopenKernelConsole {
	# /.../
	# reopen kernel console to be able to see kernel messages
	# while the system is booting
	# ----
	/usr/sbin/klogconsole -l 7
}
#======================================
# importFile
#--------------------------------------
function importFile {
	# /.../
	# import the config.<MAC> style format. the function
	# will export each entry of the file as variable into
	# the current shell environment
	# ----
	IFS="
	"
	while read line;do
	IFS="="
	count=0
	for item in $line;do
		if test $count = 0 ; then
			key=$item ; count=1 ; continue
		fi
		if test $count = 1 ; then
			item=`echo $item | tr -d \'`
			Debug "$key=$item"
			eval export $key\=\"$item\"
			count=0
		fi
	done
	done
}
#======================================
# systemException
#--------------------------------------
function systemException {
	# /.../
	# print a message to the controling terminal followed
	# by an action. Possible actions are reboot, wait
	# and opening a shell
	# ----
	Echo "$1"
	case "$2" in
	"reboot")
		Echo "rebootException: reboot in 60 sec..."; sleep 60
		/sbin/restart >/dev/null 2>&1
	;;
	"wait")
		Echo "waitException: waiting for ever..."
		while true;do sleep 100;done
	;;
	"shell")
		Echo "shellException: providing shell..."
		/bin/sh
	;;
	*)
		Echo "unknownException..."
	;;
	esac
}
#======================================
# probeFileSystem
#--------------------------------------
function probeFileSystem {
	# /.../
	# probe for the filesystem type. The function will
	# read the first 128 kB of the given device and check
	# the filesystem header data to detect the type of the
	# filesystem
	# ----
	FSTYPE=unknown
	dd if=$1 of=/tmp/filesystem-$$ bs=128k count=1 >/dev/null 2>&1
	data=$(file /tmp/filesystem-$$) && rm -f /tmp/filesystem-$$
	case $data in
		*ext3*)     FSTYPE=ext3 ;;
		*ext2*)     FSTYPE=ext2 ;;
		*ReiserFS*) FSTYPE=reiserfs ;;
		*cramfs*)   FSTYPE=cramfs ;;
		*Squashfs*) FSTYPE=squashfs ;;
		*)
			FSTYPE=unknown
		;;
	esac
}
#======================================
# readFileSystem
#--------------------------------------
function readFileSystem {
	# /.../
	# read the image type information from the image itself
	# and return the appropriate filesystem type.
	# ----
	imageConfig=$1
	if [ ! -f $imageConfig ];then
		FSTYPE=auto
		return
	fi
	data=`cat $imageConfig | grep "<type>" | cut -f2 -d ">" | cut -f1 -d "<"`
	case $data in
		*ext3*)     FSTYPE=ext3 ;;
		*ext2*)     FSTYPE=ext2 ;;
		*reiserfs*) FSTYPE=reiserfs ;;
		*cramfs*)   FSTYPE=cramfs ;;
		*squashfs*) FSTYPE=squashfs ;;
		*)
			FSTYPE=auto
		;;
	esac
}
#======================================
# getSystemIntegrity
#--------------------------------------
function getSystemIntegrity {
	# /.../
	# check the variable SYSTEM_INTEGRITY which contains
	# information about the status of all image portions
	# per partition. If a number is given as parameter only
	# the information from the image assigned to this partition
	# is returned
	# ----
	if [ -z "$SYSTEM_INTEGRITY" ];then
		echo "clean"
	else
		echo $SYSTEM_INTEGRITY | cut -f$1 -d:
	fi
}
#======================================
# getSystemMD5Status
#--------------------------------------
function getSystemMD5Status {
	# /.../
	# return the md5 status of the given image number.
	# the function works similar to getSystemIntegrity
	# ----
	echo $SYSTEM_MD5STATUS | cut -f$1 -d:
}
#======================================
# probeDeviceAlias
#--------------------------------------
function probeDeviceAlias {
	# /.../
	# create the modalias information file from all
	# registered devices of the kernel
	# ----
	modalias=/tmp/modalias
	cat > $modalias < /dev/null
	for i in `find /sys -name modalias 2>/dev/null`;do
		alias=`cat $i | grep pci:`
		if [ ! -z "$alias" ];then
			echo $alias >> $modalias
		fi
	done
	cat $modalias | sort | uniq > $modalias.new
	mv $modalias.new $modalias
}
#======================================
# probeDeviceInfo
#--------------------------------------
function probeDeviceInfo {
	# /.../
	# create the modinfo information file from all
	# installed kernel drivers
	# ----
	modinfo=/tmp/modinfo
	cat > $modinfo < /dev/null
	for file in `find /lib/modules/*/kernel/drivers -type f`;do
		/sbin/modinfo -F alias $file |\
			sed -e s@*@.*@g -e s@.@$file%\&@ \
		>> $modinfo
	done
	cat $modinfo | sort | uniq > $modinfo.new
	mv $modinfo.new $modinfo
}
#======================================
# probeDevices
#--------------------------------------
function probeDevices {
	# /.../
	# check the modalias with the modinfo file and load
	# all matching kernel modules into the kernel
	# ----
	Echo "Including required kernel modules..."
	IFS=$IFS_ORIG
	probeDeviceInfo
	probeDeviceAlias
	IFS="%"; while read file info in;do
		grep -q $info $modalias >/dev/null 2>&1
		if [ $? = 0 ];then
			module=`basename $file`
			module=`echo $module | sed -e s@.ko@@`
			INITRD_MODULES="$INITRD_MODULES $module"
			modprobe $module >/dev/null 2>&1
		fi
	done < $modinfo
	IFS=$IFS_ORIG
	# bad fix for ata probe, load order is required
	Echo "Waiting for devices to become ready..."
	lsmod | grep -q ata_piix
	if [ $? = 0 ];then
		rmmod ata_piix
		sleep 5
		modprobe ata_piix
	fi
}
#======================================
# CDDevice
#--------------------------------------
function CDDevice {
	# /.../
	# detect CD/DVD device. The function use the information
	# from /proc/sys/dev/cdrom/info to activate the drive
	# ----
	for module in usb-storage sr_mod cdrom ide-generic ide-cd BusLogic;do
		/sbin/modprobe $module
	done
	info="/proc/sys/dev/cdrom/info"
	cddevs=`cat $info | grep "drive name:" | cut -f2 -d: | tr "\t" ":"`
	cddevs=`echo $cddevs | cut -f3- -d:`
	cddevs=`echo $cddevs | tr -d [:space:]`
	IFS=":"; for i in $cddevs;do
		if [ -b "/dev/$i" ];then
			test -z $cddev && cddev=/dev/$i || cddev=$cddev:/dev/$i
		fi
	done
	IFS=$IFS_ORIG
	if [ -z $cddev ]; then
		systemException \
			"Failed to detect CD drive !" \
		"reboot"
	fi
}
#======================================
# CDMount
#--------------------------------------
function CDMount {
	# /.../
	# search all CD/DVD drives and use the one we can find
	# the CD configuration on
	# ----
	CDDevice
	mkdir -p /cdrom
	IFS=":"; for i in $cddev;do
		mount $i /cdrom &>/dev/null
		if [ -f $LIVECD_CONFIG ];then
			cddev=$i; return
		fi
		umount $i &>/dev/null
	done
	systemException \
		"Couldn't find CD image configuration file" \
	"reboot"
}
#======================================
# CDUmount
#--------------------------------------
function CDUmount {
	# /.../
	# umount the CD device
	# ----
	umount $cddev
}
#======================================
# probeNetworkCard
#--------------------------------------
function probeNetworkCard {
	# /.../
	# use hwinfo to probe for all network devices. The
	# function will check for the driver which is needed
	# to support the card and returns the information in
	# the networkModule variable
	# ----
	hwnet=/usr/sbin/hwinfo
	hwstr="Driver Activation Cmd:"
	IFS="
	"
	for i in `$hwnet --netcard | grep "$hwstr" | cut -f2 -d:`;do
		hwmod=`echo $i | tr -d \" | cut -f3 -d" "`
		hwcmd="$hwcmd:$hwmod"
	done
	if [ ! -z "$hwcmd" ];then
		networkModule=$hwcmd
	fi
	IFS=$IFS_ORIG
}
#======================================
# setupNetwork
#--------------------------------------
function setupNetwork {
	# /.../
	# setup the eth0 network interface using a dhcp
	# request. On success the dhcp info file is imported
	# into the current shell environment and the nameserver
	# information is written to /etc/resolv.conf
	# ----
	dhcpcd eth0 >/dev/null 2>&1
	if test $? != 0;then
		systemException \
			"Failed to setup DHCP network interface !" \
		"reboot"
	fi
	ifconfig lo 127.0.0.1 netmask 255.0.0.0 up
	for i in 1 2 3 4 5 6 7 8 9 0;do
		[ -s /var/lib/dhcpcd/dhcpcd-eth0.info ] && break
		sleep 5
	done
	importFile < /var/lib/dhcpcd/dhcpcd-eth0.info
	echo "search $DOMAIN" > /etc/resolv.conf
	IFS="," ; for i in $DNS;do
		echo "nameserver $i" >> /etc/resolv.conf
	done
}
#======================================
# updateNeeded
#--------------------------------------
function updateNeeded {
	# /.../
	# check the contents of the IMAGE key and compare the
	# image version file as well as the md5 sum of the installed
	# and the available image on the tftp server
	# ----
	SYSTEM_INTEGRITY=""
	SYSTEM_MD5STATUS=""
	count=0
	IFS="," ; for i in $IMAGE;do
		field=0
		IFS=";" ; for n in $i;do
		case $field in
			0) field=1 ;;
			1) imageName=$n   ; field=2 ;;
			2) imageVersion=$n; field=3 ;;
			3) imageServer=$n ; field=4 ;;
			4) imageBlkSize=$n
		esac
		done
		atversion="$imageName-$imageVersion"
		versionFile="/mnt/etc/ImageVersion-$atversion"
		IFS=" "
		if [ -f "$versionFile" ];then
			read installed sum2 < $versionFile
		fi
		imageMD5s="image/$imageName-$imageVersion.md5"
		[ -z "$imageServer" ]  && imageServer=$TSERVER
		[ -z "$imageBlkSize" ] && imageBlkSize=8192
		atftp -g -r $imageMD5s -l /etc/image.md5 $imageServer >/dev/null 2>&1
		read sum1 blocks blocksize < /etc/image.md5
		if [ ! -z "$sum1" ];then
			SYSTEM_MD5STATUS="$SYSTEM_MD5STATUS:$sum1"
		else
			SYSTEM_MD5STATUS="$SYSTEM_MD5STATUS:none"
		fi
		if [ ! -z "$1" ];then
			continue
		fi
		if test "$count" = 1;then
		if test "$SYSTEM_INTEGRITY" = ":clean";then
			Echo "Main OS image update needed"
			Echo -b "Forcing download for multi image session"
			RELOAD_IMAGE="yes"
		fi
		fi
		count=$(($count + 1))
		Echo "Cecking update status for image: $imageName"
		if test ! -z $RELOAD_IMAGE;then
			Echo -b "Update forced via RELOAD_IMAGE..."
			Echo -b "Update status: Clean"
			SYSTEM_INTEGRITY="$SYSTEM_INTEGRITY:clean"
			continue
		fi
		if test ! -f $versionFile;then
			Echo -b "Update forced: /etc/ImageVersion-$atversion not found"
			Echo -b "Update status: Clean"
			SYSTEM_INTEGRITY="$SYSTEM_INTEGRITY:clean"
			continue
		fi
		Echo -b "Current: $atversion Installed: $installed"
		if test "$atversion" = "$installed";then
			if test "$sum1" = "$sum2";then
				Echo -b "Update status: Fine"
				SYSTEM_INTEGRITY="$SYSTEM_INTEGRITY:fine"
				continue
			fi
			Echo -b "Image Update for image [ $imageName ] needed"
			Echo -b "Image version equals but md5 checksum failed"
			Echo -b "This means the contents of the new image differ"
		else
			Echo -b "Image Update for image [ $imageName ] needed"
			Echo -b "Name and/or image version differ"
		fi
		Echo -b "Update status: Clean"
		SYSTEM_INTEGRITY="$SYSTEM_INTEGRITY:clean"
	done
	SYSTEM_INTEGRITY=`echo $SYSTEM_INTEGRITY | cut -f2- -d:`
	SYSTEM_MD5STATUS=`echo $SYSTEM_MD5STATUS | cut -f2- -d:`
}
#======================================
# cleanSweep
#--------------------------------------
function cleanSweep {
	# /.../
	# zero out a the given disk device
	# ----
	diskDevice=$1
	dd if=/dev/zero of=$diskDevice bs=32M >/dev/null 2>&1
}
#======================================
# createFileSystem
#--------------------------------------
function createFileSystem {
	# /.../
	# create a filesystem on the specified partition
	# if the partition is of type LVM a volume group
	# is created
	# ----
	diskPartition=$1
	diskID=`echo $diskPartition | sed -e s@[^0-9]@@g`
	diskPD=`echo $diskPartition | sed -e s@[0-9]@@g`
	diskPartitionType=`sfdisk -c $diskPD $diskID 2>/dev/null`
	if test "$diskPartitionType" = "8e";then
		Echo "Creating Volume group [systemvg]"
		pvcreate $diskPartition >/dev/null 2>&1
		vgcreate systemvg $diskPartition >/dev/null 2>&1
	else
		# .../
		# There is no need to create a filesystem on the partition
		# because the image itself contains the filesystem
		# ----
		# mke2fs $diskPartition >/dev/null 2>&1
		# if test $? != 0;then
		#   systemException \
		#       "Failed to create filesystem on: $diskPartition !" \
		#   "reboot"
		# fi
		:
	fi
}
#======================================
# partitionCount
#--------------------------------------
function partitionCount {
	# /.../
	# calculate the number of partitions to create. If the
	# number is more than 4 an extended partition needs to be
	# created.
	IFS="," ; for i in $PART;do
		PART_NUMBER=`expr $PART_NUMBER + 1`
	done
	if [ $PART_NUMBER -gt 4 ];then
		PART_NEED_EXTENDED=1
	fi
	PART_NUMBER=`expr $PART_NUMBER + 1`
	PART_NEED_FILL=`expr $PART_NUMBER / 8`
	PART_NEED_FILL=`expr 8 - \( $PART_NUMBER - $PART_NEED_FILL \* 8 \)`
}
#======================================
# fillPartition
#--------------------------------------
function fillPartition {
	# /.../
	# in case of an extended partition the number of input lines
	# must be a multiple of 4, so this function will fill the input
	# with empty lines to make sfdisk happy
	# ----
	while test $PART_NEED_FILL -gt 0;do
		echo >> $PART_FILE
		PART_NEED_FILL=`expr $PART_NEED_FILL - 1`
	done
}
#======================================
# createSwap
#--------------------------------------
function createSwap {
	# /.../
	# create the sfdisk input line for setting up the
	# swap space
	# ----
	IFS="," ; for i in $PART;do
		field=0
		IFS=";" ; for n in $i;do
		case $field in
			0) partSize=$n   ; field=1 ;;
			1) partID=$n     ; field=2 ;;
			2) partMount=$n;
		esac
		done
		if test $partID = "82" -o $partID = "S";then
			echo "0,$partSize,$partID,-" > $PART_FILE
			PART_COUNT=`expr $PART_COUNT + 1`
			return
		fi
	done
}
#======================================
# createPartition
#--------------------------------------
function createPartition {
	# /.../
	# create the sfdisk input lines for setting up the
	# partition table except the swap space
	# ----
	devices=1
	IFS="," ; for i in $PART;do
		field=0
		IFS=";" ; for n in $i;do
		case $field in
			0) partSize=$n   ; field=1 ;;
			1) partID=$n     ; field=2 ;;
			2) partMount=$n;
		esac
		done
		if test $partID = "82" -o $partID = "S";then
			continue
		fi
		if test $partSize = "x";then
			partSize=""
		fi
		if [ $PART_COUNT -eq 1 ];then
			echo ",$partSize,$partID,*" >> $PART_FILE
		else
			echo ",$partSize,$partID,-" >> $PART_FILE
		fi
		PART_COUNT=`expr $PART_COUNT + 1`
		if [ $PART_NEED_EXTENDED -eq 1 ];then
		if [ $PART_COUNT -eq 3 ];then
			echo ",,E" >> $PART_FILE
			NO_FILE_SYSTEM=1
		fi
		fi
		devices=`expr $devices + 1`
		if test -z "$PART_MOUNT";then
			PART_MOUNT="$partMount"
			PART_DEV="$DISK$devices"
		else
			PART_MOUNT="$PART_MOUNT:$partMount"
			if [ $NO_FILE_SYSTEM -eq 2 ];then
				devices=`expr $devices + 1`
				NO_FILE_SYSTEM=0
			fi
			PART_DEV="$PART_DEV:$DISK$devices"
		fi
		if [ $NO_FILE_SYSTEM -eq 1 ];then
			NO_FILE_SYSTEM=2
		fi
	done
	if [ $PART_NEED_EXTENDED -eq 1 ];then
		fillPartition
	fi
}
#======================================
# writePartitionTable
#--------------------------------------
function writePartitionTable {
	# /.../
	# write the partition table using PART_FILE as
	# input for sfdisk
	# ----
	diskDevice=$1
	sfdisk -uM --force $diskDevice < $PART_FILE >/dev/null 2>&1
	if test $? != 0;then
		systemException \
			"Failed to create partition table on: $diskDevice !" \
			"reboot"
	fi

	verifyOutput=`sfdisk -V $diskDevice 2>&1`
	if test $? != 0;then
		systemException \
			"Failed to verify partition table on $diskDevice: $verifyOutput" \
			"reboot"
	fi
	
	rm -f $PART_FILE
}
#======================================
# linuxPartition
#--------------------------------------
function linuxPartition {
	# /.../
	# check for a linux partition on partition number 2
	# using the given disk device. On success return 0
	# ----
	diskDevice=$1
	diskPartitionType=`sfdisk -c $diskDevice 2 2>/dev/null`
	if test "$diskPartitionType" = "83";then
		return 0
	fi
	return 1
}
#======================================
# kernelList
#--------------------------------------
function kernelList {
	# /.../
	# check for all installed kernels whether there are valid
	# links to the initrd and kernel files. The function will
	# save the valid linknames in the variable KERNEL_LIST
	# ----
	prefix=$1
	KERNEL_LIST=""
	kcount=0
	for i in $prefix/lib/modules/*;do
		if [ ! -d $i ];then
			continue
		fi
		name=${i##*/}
		if [ ! -f $prefix/boot/vmlinux-$name.gz ];then
			continue
		fi
		KERNEL_PAIR=""
		for n in $prefix/boot/*;do
			if [ ! -L $n ];then
				continue
			fi
			real=`readlink $n`
			if [ $real = vmlinuz-$name ];then
				kernel=${n##*/}
				kcount=$((kcount+1))
			fi
			if [ $real = initrd-$name ];then
				initrd=${n##*/}
			fi
			KERNEL_PAIR=$kernel:$initrd
		done
		if [ $kcount = 1 ];then
			KERNEL_LIST=$KERNEL_PAIR
		elif [ $kcount -gt 1 ];then
			KERNEL_LIST=$KERNEL_LIST,$KERNEL_PAIR
		fi
	done
}
#======================================
# validateSize
#--------------------------------------
function validateSize {
	# /.../
	# check if the image fits into the requested partition.
	# An information about the sizes is printed out
	# ----
	haveBytes=`sfdisk -s $imageDevice`
	haveBytes=`expr $haveBytes \* 1024`
	haveMByte=`expr $haveBytes / 1048576`
	needBytes=`expr $blocks \* $blocksize`
	needMByte=`expr $needBytes / 1048576`
	Echo "Have size: $imageDevice -> $haveBytes Bytes [ $haveMByte MB ]"
	Echo "Need size: $needBytes Bytes [ $needMByte MB ]"
	if test $haveBytes -gt $needBytes;then
		return 0
	fi
	return 1
}
#======================================
# validateBlockSize
#--------------------------------------
function validateBlockSize {
	# /.../
	# check the block size value. atftp limits to a maximum of
	# 32768 blocks, so the block size must be checked according
	# to the size of the image
	# ----
	isize=`expr $blocks \* $blocksize`
	isize=`expr $isize / 32768`
	if [ $isize -gt $imageBlkSize ];then
		imageBlkSize=`expr $isize + 1024`
	fi
}
#======================================
# loadOK
#--------------------------------------
function loadOK {
	# /.../
	# check the output of the atftp command, unfortunately
	# there is no useful return code to check so we have to
	# check the output of the command
	# ----
    echo $1 | grep -q "File not found"
    if [ $? = 0 ];then
        return 1
    fi
    echo $1 | grep -q "aborting"
    if [ $? = 0 ];then
        return 1
    fi
    return 0
}
#======================================
# validateRAM
#--------------------------------------
function validateRAM {
	# /.../
	# check if the image fits into the ramdisk.
	# An information about the sizes is printed out
	# ----
	needRAM=`expr $blocks \* $blocksize`
	needRAM=`expr $needRAM / 1024`
	needRAM=`expr $needRAM + 128`
	needMByte=`expr $needRAM / 1024`
	hasRAM=`cat /proc/meminfo | grep MemFree | cut -f2 -d:`
	hasRAM=`echo $hasRAM | cut -f1 -d" "`
	hasMByte=`expr $hasRAM / 1024`
	Echo "Have size: $imageDevice -> $hasRAM KBytes [ $hasMByte MB ]"
	Echo "Need size: $needRAM KBytes [ $needMByte MB ]"
	if test $hasRAM -gt $needRAM;then
		return 0
	fi
	return 1
}
#======================================
# includeKernelParameters
#--------------------------------------
function includeKernelParameters {
	# /.../
	# include the parameters from /proc/cmdline into
	# the current shell environment
	# ----
	IFS=$IFS_ORIG
	for i in `cat /proc/cmdline`;do
		kernelKey=`echo $i | cut -f1 -d=`
		kernelVal=`echo $i | cut -f2 -d=`
		eval $kernelKey=$kernelVal
	done
}
#======================================
# checkTFTP
#--------------------------------------
function checkTFTP {
	# /.../
	# check the kernel commandline parameter kiwitftp.
	# If it exists its contents will be used as tftp
	# server address stored in the TSERVER variabe
	# ----
	if [ ! -z $kiwitftp ];then
		Echo "Found TFTP server in kernel cmdline"
		TSERVER=$kiwitftp
	fi
}

#======================================
# mountSystem
#--------------------------------------
function mountSystem () {
	retval=0
	OLDIFS=$IFS
	IFS=$IFS_ORIG
	mountDevice=$imageRootDevice
	if test ! -z $1;then
		mountDevice=$1
	fi
	if test ! -z $UNIONFS_CONFIG;then
		mkdir -p /ro_branch
		mkdir -p /rw_branch
		mkdir -p /xino
		rwDevice=`echo $UNIONFS_CONFIG | cut -d , -f 1`
		roDevice=`echo $UNIONFS_CONFIG | cut -d , -f 2`
		unionFST=`echo $UNIONFS_CONFIG | cut -d , -f 3`
		if test $LOCAL_BOOT = "no" && test $systemIntegrity = "clean";then
			Echo "Creating EXT2 filesystem for write extend..."
			if ! mke2fs $rwDevice >/dev/null 2>&1;then
				systemException \
					"Failed to create ext2 filesystem" \
				"reboot"
			fi
		fi
		Echo "Checking EXT2 write extend..."
		e2fsck -y -f $rwDevice
		if ! mount $rwDevice /rw_branch >/dev/null 2>&1;then
			retval=1
		fi
		if ! mount -t squashfs $roDevice /ro_branch >/dev/null 2>&1;then
			if ! mount $roDevice /ro_branch >/dev/null 2>&1;then
				retval=1
			fi
		fi
		if [ $unionFST = "aufs" ];then
			mount -t tmpfs tmpfs /xino >/dev/null 2>&1 || retval=1
			mount -t aufs \
				-o dirs=/rw_branch=rw:/ro_branch=ro,xino=/xino/.aufs.xino \
				none /mnt \
			>/dev/null 2>&1 || retval=1
		else
			mount -t unionfs \
				-o dirs=/rw_branch=rw:/ro_branch=ro \
				none /mnt
			>/dev/null 2>&1 || retval=1
		fi
		usleep 500000
	else
		mount $mountDevice /mnt >/dev/null 2>&1
		retval=$?
	fi
	IFS=$OLDIFS
	return $retval
}

#======================================
# umountSystem
#--------------------------------------
function umountSystem () {
	umount /mnt >/dev/null 2>&1
	if test ! -z "$UNIONFS_CONFIG";then
		umount /ro_branch >/dev/null 2>&1
		umount /rw_branch >/dev/null 2>&1
		umount /xino >/dev/null 2>&1
	fi
}

#======================================
# cleanDirectory
#--------------------------------------
function cleanDirectory () {
	directory=$1
	shift 1
	save=$@

	tmpdir=`mktemp -d`
	for saveItem in $save;do
		mv $directory/$saveItem $tmpdir >/dev/null 2>&1
	done
	rm -rf $directory/*
	mv $tmpdir/* $directory
	rm -rf $tmpdir
}