#!/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

#======================================
# Exports (Booting)
#--------------------------------------
export LOCAL_BOOT=no
export systemIntegrity=clean

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

#======================================
# Functions...
#--------------------------------------
VMFindSystem () {
	imageRootDevice="/dev/"$root
	waitForStorageDevice $imageRootDevice
	if [ ! -e $imageRootDevice ];then
		systemException \
			"Couldn't find storage device... abort" \
		"reboot"
	fi
}
#======================================
# Beautify Startup
#--------------------------------------
echo "Loading KIWI Xen Boot-System..."
echo "-------------------------------"

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

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

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

#======================================
# 4) start boot shell
#--------------------------------------
ELOG_CONSOLE=/dev/console
ELOG_EXCEPTION=$ELOG_CONSOLE
startBlogD
startShell
errorLogStart

#======================================
# 5) Including required kernel modules
#--------------------------------------
Echo "Including required kernel modules..."
for module in xennet xenblk reiserfs ext2 ext3 ext4 loop ipv6;do
	modprobe $module >/dev/null 2>&1
done

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

#======================================
# 7) Get filesystem type
#--------------------------------------
probeFileSystem $imageRootDevice
if [ "$FSTYPE" = "luks" ];then
	imageRootDevice=$(luksOpen $imageRootDevice)
	probeFileSystem $imageRootDevice
	export haveLuks=yes
fi
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) check for local boot
#--------------------------------------
if [ -e /mnt/etc/ImagePackages ];then
	export LOCAL_BOOT=yes
fi

#======================================
# 10) Create system dependant files
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	setupDefaultFstab /config
	updateRootDeviceFstab /config $imageRootDevice
fi

#======================================
# 11) copy system dependant files
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	setupConfigFiles
fi

#======================================
# 12) update system dependant files
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	setupInittab /mnt
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 [ ! -f /mnt/boot/deployed ];then
	touch /mnt/boot/deployed
	importBranding
fi
cp /preinit /mnt
cp /include /mnt

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

#======================================
# 17) Activate new root
#--------------------------------------
activateImage

#======================================
# 18) Unmount initrd / system init
#--------------------------------------
bootImage $@
