#!/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=""
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_IDS="/config.partids"
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

#======================================
# Start logging
#--------------------------------------
errorLogStart

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

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

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

#======================================
# 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
	waitForStorageDevice $disk
	export imageDiskDevice=$(dn $disk)
fi
Echo "Found boot device: $imageDiskDevice"

#======================================
# 9) Setup device names a)...
#--------------------------------------
if [ -z "$imageDiskDevice" ];then
	systemException \
		"Couldn't find any boot device... abort" \
	"reboot"
fi
#======================================
# 9) Setup device names b)...
#--------------------------------------
if [ ! -f $PART_IDS ];then
	systemException \
		"Couldn't find partition IDs meta data... abort" \
	"reboot"
fi
importFile < $PART_IDS
export imageBootDevice=$(ddn $imageDiskDevice $kiwi_BootPart)
if searchVolumeGroup; then
	export haveLVM=yes
	export imageRootDevice=/dev/$VGROUP/$kiwi_RootPart
	if [ ! -z "$kiwi_ROPart" ];then
		export imageRODevice=/dev/$VGROUP/$kiwi_ROPart
	fi
	if [ ! -z "$kiwi_RWPart" ];then
		export imageRWDevice=/dev/$VGROUP/$kiwi_RWPart
	fi
elif [ ! -z "$kiwi_RaidDev" ];then
	export imageRootDevice=$kiwi_RaidDev
	if [ ! -z "$kiwi_ROPart" ] || [ ! -z "$kiwi_RWPart" ];then
		systemException \
			"split systems not supported in software raid mode"
		"reboot"
	fi
else
	export imageRootDevice=$(ddn $imageDiskDevice $kiwi_RootPart)
	if [ ! -z "$kiwi_ROPart" ];then
		export imageRODevice=$(ddn $imageDiskDevice $kiwi_ROPart)
	fi
	if [ ! -z "$kiwi_RWPart" ];then
		export imageRWDevice=$(ddn $imageDiskDevice $kiwi_RWPart)
	fi
fi
#======================================
# 9) Setup device names c)...
#--------------------------------------
if [ ! -z "$kiwi_ramonly" ];then
	export imageRWDevice=/dev/ram1
fi

#======================================
# 10) Get filesystem type
#--------------------------------------
probeFileSystem $imageRootDevice
if [ "$FSTYPE" = "luks" ];then
	luksOpen $imageRootDevice
	imageRootDevice=$luksDeviceOpened
	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
	setupKernelLinks
fi

#======================================
# 15) Create system dependant files
#--------------------------------------
if [ "$LOCAL_BOOT" = "no" ];then
	setupDefaultFstab /config
	updateRootDeviceFstab /config $imageRootDevice
	updateBootDeviceFstab /config $imageBootDevice
	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
if [ -f /etc/mdadm.conf ];then
	cp /etc/mdadm.conf /mnt/etc
fi
cp /preinit /mnt
cp /include /mnt

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

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

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