#!/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) 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 $root)
fi
Echo "Found boot device: $imageDiskDevice"

#======================================
# 8) 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
Echo "Searching for kiwiVG volume group..."
if searchVolumeGroup; then
	export haveLVM=yes
	if [ -e /dev/kiwiVG/LVComp ];then
		export imageRootDevice=/dev/kiwiVG/LVComp
	else
		export imageRootDevice=/dev/kiwiVG/LVRoot
	fi
	export imageRWDevice=/dev/kiwiVG/LVRoot
	export imageRODevice=/dev/kiwiVG/LVComp
	export imageBootDevice=$(ddn $imageDiskDevice 2)
fi

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

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

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

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

#======================================
# 13) setup boot partition 
#--------------------------------------
if [ "$LOCAL_BOOT" = "no" ];then
	if [ "$haveLVM" = "yes" ];then
		#======================================
		# LVM use second partition as boot
		#--------------------------------------
		mkdir /mnt/lvmboot
		mount $imageBootDevice /mnt/lvmboot
		if [ -z "$UNIONFS_CONFIG" ];then
			cp -a /mnt/boot/* /mnt/lvmboot/boot
		fi
		rm -rf /mnt/boot
		( cd /mnt && ln -s lvmboot/boot boot )
	elif [ "$haveDMSquash" = "yes" ];then
		#======================================
		# DM use third partition as boot
		#--------------------------------------
		mkdir /mnt/dmboot
		export imageBootDevice=$(ddn $imageDiskDevice 3)
		mount $imageBootDevice /mnt/dmboot
		cp -a  /mnt/boot /mnt/dmboot
		rm -rf /mnt/boot
		( cd /mnt && ln -s dmboot/boot boot )
	elif [ "$loader" = "syslinux" ];then
		#======================================
		# Syslinux search FAT and use as boot
		#--------------------------------------
		for i in 1 2 3;do
			pType=`partitionID $imageDiskDevice $i`
			if [ "$pType" = "6" ];then
				imageFatDevice=$imageDiskDevice$i
				break
			fi 
		done
		if [ -z "$imageFatDevice" ];then
			systemException "Can't find FAT partition" "reboot"
		fi
		mkdir /mnt/syslboot
		mount $imageFatDevice /mnt/syslboot
		if [ -z "$UNIONFS_CONFIG" ];then
			cp -a /mnt/boot/* /mnt/syslboot/boot
		fi
		rm -rf /mnt/boot
		( cd /mnt && ln -s syslboot/boot boot )
	elif [ "$haveLuks" = "yes" ];then
		#======================================
		# LUKS use second or third part as boot
		#--------------------------------------
		mkdir /mnt/luksboot
		export imageBootDevice=$(ddn $imageDiskDevice 3)
		if [ ! -e $imageBootDevice ];then
			imageBootDevice=$(ddn $imageDiskDevice 2)
		fi
		mount $imageBootDevice /mnt/luksboot
		if [ -z "$UNIONFS_CONFIG" ];then
			cp -a /mnt/boot/* /mnt/luksboot/boot
		fi
		rm -rf /mnt/boot
		( cd /mnt && ln -s luksboot/boot boot )
	fi
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 [ "$haveLVM" = "yes" ];then
			pushd /mnt/lvmboot/boot >/dev/null
		elif [ "$haveDMSquash" = "yes" ];then
			pushd /mnt/dmboot/boot >/dev/null
		elif [ "$haveLuks" = "yes" ];then
			pushd /mnt/luksboot/boot >/dev/null
		else
			kiwiMount $imageRWDevice "/mnt"
			pushd /mnt/boot >/dev/null
			mountCalled=yes
		fi
		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 [ "$haveDMSquash" = "yes" ];then
		updateDMBootDeviceFstab /config $imageBootDevice
	elif [ "$loader" = "syslinux" ];then
		updateSyslinuxBootDeviceFstab /config $imageFatDevice
	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
	bootid=0
	if [ ! -z "$UNIONFS_CONFIG" ] || [ ! -z "$COMBINED_IMAGE" ]; then
		bootid=1
	fi
	if \
		[ "$loader" = "syslinux" ]  ||\
		[ "$haveDMSquash" = "yes" ] ||\
		[ "$haveLuks" = "yes" ]
	then
		bootid=$(($bootid + 1))
	fi
	setupBootLoader /mnt /config $bootid $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 $@
