#!/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 DEBUG=0

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

#======================================
# Functions...
#--------------------------------------
VMFindSystem () {
	local haveSDA=0
	local haveHDA=0
	local devices=""
	waitForStorageDevice /dev/sda1
	if [ ! -e /dev/sda1 ];then
		waitForStorageDevice /dev/hda1
		if [ -e /dev/hda1 ];then
			haveHDA=1
			devices="/dev/hda1 /dev/hda"
		fi
	else
		haveSDA=1
		devices="/dev/sda1 /dev/sda"
	fi
	if [ $haveSDA = 0 ] && [ $haveHDA = 0 ];then
		systemException \
			"Couldn't find any storage device... abort" \
		"reboot"
	fi
	for rdevice in $devices;do
		imageRootDevice=$rdevice
		if kiwiMount $imageRootDevice "/mnt" &>/dev/null;then
			umount /mnt
			break
		fi
	done
}
#======================================
# Beautify Startup
#--------------------------------------
echo "Loading KIWI Xen 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) Including required kernel modules
#--------------------------------------
Echo "Including required kernel modules..."
includeKernelParameters
for module in xennet xenblk reiserfs ext2 ext3 loop ipv6;do
	modprobe $module >/dev/null 2>&1
done

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

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

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

#======================================
# 9) Create system dependant files
#--------------------------------------
setupDefaultFstab /config
updateRootDeviceFstab /config $imageRootDevice

#======================================
# 10) copy system dependant files
#--------------------------------------
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

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

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

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

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

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

#======================================
# 16) 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

#======================================
# 17) Unmount initrd / system init
#--------------------------------------
echo " "
echo "Booting into Xen System..."
echo "--------------------------"
export IFS=$IFS_ORIG
mount -n -o remount,rw / &>/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
