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

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

#======================================
# Update library path
#--------------------------------------
ldconfig

#======================================
# 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) Select language if not in cmdline
#--------------------------------------
selectLanguage

#======================================
# 8) Search boot device...
#--------------------------------------
Echo "Searching for boot device..."
if [ "$LOCAL_BOOT" = "no" ];then
	if ! searchBIOSBootDevice;then
		systemException "$biosBootDevice" "reboot"
	fi
	export imageDiskDevice=$biosBootDevice
else
	export imageDiskDevice=$(dn $disk)
fi
Echo "Found boot device: $imageDiskDevice"

#======================================
# 9) Setup device names...
#--------------------------------------
export imageRootDevice=$(ddn $imageDiskDevice 1)
export imageRWDevice=$(ddn $imageDiskDevice 2)
export imageRODevice=$(ddn $imageDiskDevice 1)
if [ -z "$imageDiskDevice" ];then
	systemException \
		"Couldn't find any boot device... abort" \
	"reboot"
fi
if searchVolumeGroup; then
	export haveLVM=yes
	if [ -e /dev/$VGROUP/LVComp ];then
		export imageRootDevice=/dev/$VGROUP/LVComp
	else
		export imageRootDevice=/dev/$VGROUP/LVRoot
	fi
	export imageRWDevice=/dev/$VGROUP/LVRoot
	export imageRODevice=/dev/$VGROUP/LVComp
	export imageBootDevice=$(ddn $imageDiskDevice 2)
fi

#======================================
# 10) Get filesystem type
#--------------------------------------
probeFileSystem $imageRootDevice
if [ "$FSTYPE" = "luks" ];then
	imageRootDevice=$(luksOpen $imageRootDevice)
	imageRODevice=$imageRootDevice
	probeFileSystem $imageRootDevice
	export haveLuks=yes
fi
if [ "$FSTYPE" = "unknown" ];then
	systemException \
		"Couldn't determine filesystem type... abort" \
	"reboot"
fi

#======================================
# 11) Check filesystem
#--------------------------------------
Echo "Filesystem of VMX system is: $FSTYPE -> $imageRootDevice"
if isFSTypeReadOnly;then
	setupUnionFS $imageRWDevice $imageRODevice $unionFST
fi

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

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

#======================================
# 14) setup ird/kernel links for union
#--------------------------------------
if [ "$LOCAL_BOOT" = "no" ];then
	if 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.
		# ----
		mountCalled=no
		if [ -e "$imageRWDevice" ];then
			kiwiMount $imageRWDevice "/mnt"
			mountCalled=yes
		fi
		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
		if [ "$mountCalled" = "yes" ];then
			umount /mnt
		fi
	fi
fi

#======================================
# 15) Create system dependant files
#--------------------------------------
if [ "$LOCAL_BOOT" = "no" ];then
	setupDefaultFstab /config
	updateRootDeviceFstab /config $imageRootDevice
	if [ "$haveLVM" = "yes" ];then
		updateLVMBootDeviceFstab /config $imageBootDevice
	elif [ "$haveClicFS" = "yes" ];then
		updateClicBootDeviceFstab /config $imageBootDevice
	elif [ "$haveBtrFS" = "yes" ];then
		updateBtrBootDeviceFstab /config $imageBootDevice
	elif [ "$loader" = "syslinux" ] || [ "$loader" = "extlinux" ];then
		updateSyslinuxBootDeviceFstab /config $imageBootDevice
	elif [ "$haveLuks" = "yes" ];then
		updateLuksBootDeviceFstab /config $imageBootDevice
	fi
	if [ ! -z "$UNIONFS_CONFIG" ]; then
		export KIWI_INITRD_PARAMS="UNIONFS_CONFIG=yes"
	fi
	if [ ! -z "$COMBINED_IMAGE" ]; then
		export KIWI_INITRD_PARAMS="COMBINED_IMAGE=local"
	fi
	setupBootLoader /mnt /config $(($bootid - 1)) $imageRootDevice VMX
	setupKernelModules /config
fi

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

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

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

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

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

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

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

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