#!/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"
export stickFound=0

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

#======================================
# Beautify Startup
#--------------------------------------
echo "Loading KIWI USB-Stick 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
includeKernelParameters

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

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

#======================================
# 6) Setup device names...
#--------------------------------------
USBStickDevice
if [ $stickFound = 0 ];then
	systemException \
		"No USB stick found... abort" \
	"reboot"
fi
Echo "Searching for kiwiVG volume group..."
export stickRWDevice=$(ddn $stickRoot 2)
export stickRODevice=$(ddn $stickRoot 1)
if searchVolumeGroup; then
	export haveLVM=yes
	if [ -e /dev/kiwiVG/LVComp ];then
		export stickDevice=/dev/kiwiVG/LVComp
	else
		export stickDevice=/dev/kiwiVG/LVRoot
	fi
	export stickRWDevice=/dev/kiwiVG/LVRoot
	export stickRODevice=/dev/kiwiVG/LVComp
	export stickBootDevice=$(ddn $stickRoot 2)
fi

#======================================
# 7) Probe filesystem of stick system
#--------------------------------------
probeFileSystem $stickDevice
if [ "$FSTYPE" = "luks" ];then
	stickDevice=$(luksOpen $stickDevice)
	stickRODevice=$stickDevice
	probeFileSystem $stickDevice
	export haveLuks=yes
fi
if [ $FSTYPE = "unknown" ];then
	systemException \
		"Couldn't determine filesystem type... abort" \
	"reboot"
fi

#======================================
# 8) Setup union on compressed root
#--------------------------------------
Echo "Filesystem of stick system is: $FSTYPE -> $stickDevice"
if isFSTypeReadOnly;then
	setupUnionFS $stickRWDevice $stickRODevice $unionFST
fi

#======================================
# 9) Mount USB stick (system)
#--------------------------------------
if ! mountSystem $stickDevice;then
	systemException "Failed to mount root filesystem" "reboot"
fi
validateRootTree

#======================================
# 10) check for local boot
#--------------------------------------
if [ -e /mnt/etc/ImagePackages ];then
	export LOCAL_BOOT=yes
fi

#======================================
# 11) setup /lvmboot
#--------------------------------------
if [ "$LOCAL_BOOT" = "no" ];then
	if [ "$haveLVM" = "yes" ];then
		#======================================
		# LVM use second partition as boot
		#--------------------------------------
		mkdir /mnt/lvmboot
		mount $stickBootDevice /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 $stickRoot 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 $stickRoot $i`
			if [ "$pType" = "6" ];then
				imageFatDevice=$stickRoot$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 $stickRoot 3)
		if [ ! -e $imageBootDevice ];then
			imageBootDevice=$(ddn $stickRoot 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

#======================================
# 12) Create system dependant files
#--------------------------------------
if [ "$LOCAL_BOOT" = "no" ];then
	setupDefaultFstab /config
	updateRootDeviceFstab /config $stickDevice
	if [ "$haveLVM" = "yes" ];then
		updateLVMBootDeviceFstab /config $stickBootDevice
	elif [ "$haveDMSquash" = "yes" ];then
		updateDMBootDeviceFstab /config $imageBootDevice
	elif [ "$loader" = "syslinux" ];then
		updateSyslinuxBootDeviceFstab /config $imageFatDevice
	elif [ "$haveLuks" = "yes" ];then
		updateLuksBootDeviceFstab /config $imageBootDevice
	fi
fi

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

#======================================
# 14) update system dependant files
#--------------------------------------
if [ "$LOCAL_BOOT" = "no" ];then
	setupInittab /mnt
fi

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

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

#======================================
# 17) copy initrd files to image
#--------------------------------------
if [ ! -f /mnt/boot/deployed ];then
	touch /mnt/boot/deployed
	importBranding
fi
cp /preinit /mnt
cp /include /mnt

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

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

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