#!/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 systemIntegrity="clean"
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

#======================================
# Functions
#--------------------------------------
function VMFindSystem {
	local haveSDA=0
	local haveHDA=0
	local devices=""
	waitForStorageDevice /dev/sda
	if [ ! -e /dev/sda ];then
		waitForStorageDevice /dev/hda
		if [ -e /dev/hda ];then
			haveHDA=1
			devices="/dev/hda1 /dev/hda2"
		fi
	else
		haveSDA=1
		devices="/dev/sda1 /dev/sda2"
	fi
	if [ $haveSDA = 0 ] && [ $haveHDA = 0 ];then
		systemException \
			"Couldn't find any storage device... abort" \
		"reboot"
	fi
	for rdevice in $devices;do
		imageRootDevice=$rdevice
		imageDiskDevice=`echo $imageRootDevice | tr -d [0-9]`
		if kiwiMount $imageRootDevice "/mnt" &>/dev/null;then
			umount /mnt
			break
		fi
	done
}
#======================================
# Beautify Startup
#--------------------------------------
echo "Loading KIWI VMX Boot-System..."
echo "-------------------------------"

#======================================
# 1) Mounting local file systems
#--------------------------------------
mountSystemFilesystems &>/dev/null
closeKernelConsole

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

#======================================
# 3) run udevd
#--------------------------------------
udevStart

#======================================
# 4) start boot shell
#--------------------------------------
startBlogD
startShell
errorLogStart
openKernelConsole

#======================================
# 5) Include proc/cmdline information
#--------------------------------------
includeKernelParameters
if [ ! -z $UNIONFS_CONFIG ] || [ "$COMBINED_IMAGE" = "local" ];then
	# /.../
	# if the unionfs/combined information is already in place at this
	# stage it comes from the cmdline data which means we are not
	# booting the first time
	# ----
	export LOCAL_BOOT="yes"
fi

#======================================
# 6) Including required kernel modules
#--------------------------------------
probeDevices

#======================================
# 7) Mount VM (boot)
#--------------------------------------
VMFindSystem

#======================================
# 8) Get filesystem type
#--------------------------------------
probeFileSystem $imageRootDevice
if [ $FSTYPE = "unknown" ];then
	systemException \
		"Couldn't determine filesystem type... abort" \
	"reboot"
fi

#======================================
# 9) Check filesystem
#--------------------------------------
Echo "Filesystem of VMX system is: $FSTYPE -> $imageRootDevice"
if isFSTypeReadOnly;then
	export UNIONFS_CONFIG="$imageDiskDevice"2,"$imageDiskDevice"1,aufs
fi

#======================================
# 10) Mount VM (boot)
#--------------------------------------
if ! mountSystem $imageRootDevice;then
	systemException "Failed to mount root filesystem" "reboot"
fi
validateRootTree

#======================================
# 11) Get kernel list for local boot
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	kernelList /mnt
fi

#======================================
# 11) Create system dependant files
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	setupDefaultFstab /config
	updateRootDeviceFstab /config $imageRootDevice
	if [ -z "$UNIONFS_CONFIG" ]; then
		setupBootLoader /mnt /config 0 $imageRootDevice VMX
	else
		export KIWI_INITRD_PARAMS="UNIONFS_CONFIG=\"$UNIONFS_CONFIG\""
		setupBootLoader /mnt /config 1 $imageRootDevice VMX
	fi
	setupKernelModules /config
fi

#======================================
# 12) copy system dependant files
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];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

#======================================
# 13) setup real root device
#--------------------------------------
echo 256 > /proc/sys/kernel/real-root-dev

#======================================
# 14) umount system filesystems
#--------------------------------------
umountSystemFilesystems

#======================================
# 15) copy initrd files to image
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	importBranding
fi
cp /preinit /mnt
cp /include /mnt

#======================================
# 16) kill boot shell
#--------------------------------------
killShell
killBlogD

#======================================
# 17) Activate new root
#--------------------------------------
Echo "Activating Image: [$imageRootDevice]"
reopenKernelConsole
/sbin/udevsettle --timeout=30
mount --move /dev /mnt/dev
udevKill

#======================================
# 18) call preinit phase
#--------------------------------------
Echo "Calling preinit phase..."
cd /mnt
/mnt/sbin/pivot_root . mnt >/dev/null 2>&1
if test $? != 0;then
	PIVOT=false
	cleanInitrd && mount --move . / && chroot . ./preinit
	chroot . rm -f  ./preinit
	chroot . rm -f  ./include
	chroot . rm -rf ./image
else
	PIVOT=true
	./preinit
	rm -f  ./preinit
	rm -f  ./include
	rm -rf ./image
fi

#======================================
# 19) Unmount initrd / system init
#--------------------------------------
echo " "
echo "Booting into final System..."
echo "----------------------------"
export IFS=$IFS_ORIG
mount -n -o remount,rw / 2>/dev/null
if [ $PIVOT = "true" ];then
	exec < dev/console >dev/console 2>&1
	exec umount -n -l /mnt
else
	exec < dev/console >dev/console 2>&1
	exec chroot . /sbin/init $@
fi
