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

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

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

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

#======================================
# 7) Mount VM (boot)
#--------------------------------------
Echo "Searching for boot device..."
export imageDiskDevice=`searchBIOSBootDevice`
export imageRootDevice=$imageDiskDevice"1"
if [ -z "$imageDiskDevice" ];then
	systemException \
		"Couldn't find any boot device... abort" \
	"reboot"
fi

#======================================
# 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
	setupUnionFS "$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

#======================================
# 12) setup ird/kernel links for union
#--------------------------------------
if [ $LOCAL_BOOT = "no" ] && isFSTypeReadOnly;then
	# /.../
	# we are using a special root setup with aufs. In this case
	# we can't use the SuSE Linux initrd but must stick to the
	# kiwi boot system.
	# ----
	kiwiMount "$imageDiskDevice"2 "/mnt"
	pushd /mnt/boot >/dev/null
	IFS="," ; for i in $KERNEL_LIST;do
		if test -z "$i";then
			continue
		fi
		kernel=`echo $i | cut -f1 -d:`
		initrd=`echo $i | cut -f2 -d:`
		rm -f $initrd && ln -s initrd.vmx $initrd
		rm -f $kernel && ln -s linux.vmx  $kernel
		break
	done
	IFS=$IFS_ORIG
	popd >/dev/null
	umount /mnt
fi

#======================================
# 13) 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=yes"
		setupBootLoader /mnt /config 1 $imageRootDevice VMX
	fi
	setupKernelModules /config
fi

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

#======================================
# 15) update system dependant files
#--------------------------------------
setupInittab /mnt

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

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

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

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

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

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