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

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

#======================================
# Functions...
#--------------------------------------
VMMountSystem () {
	for rdevice in /dev/hda1 /dev/sda1 /dev/hda /dev/sda;do
		imageRootDevice=$rdevice
		mount $imageRootDevice /mnt &>/dev/null
		if [ $? = 0 ];then
			break
		fi
	done
}
VMUmountSystem () {
	umount /mnt
}
#======================================
# Beautify Startup
#--------------------------------------
clear
echo "Loading KIWI Xen Boot-System..."
echo "-------------------------------"

#======================================
# 1) Mounting local file systems
#--------------------------------------
mount -t proc  proc    /proc
mount -t sysfs sysfs   /sys
mount -t devpts devpts /dev/pts
closeKernelConsole

#======================================
# 2) Prepare module load support 
#--------------------------------------
touch /etc/modules.conf
touch /lib/modules/*/modules.dep

#======================================
# 3) Including required kernel modules
#--------------------------------------
Echo "Including required kernel modules..."
for module in xennet xenblk reiserfs ext2 ext3 loop ipv6;do
	modprobe $module >/dev/null 2>&1
done

#======================================
# 4) Mount VM (boot)
#--------------------------------------
VMMountSystem

#======================================
# 5) Get filesystem type
#--------------------------------------
probeFileSystem $imageRootDevice

#======================================
# 6) Create system dependant files
#--------------------------------------
#======================================
# a) /etc/fstab
#--------------------------------------
mkdir -p /config/etc
echo "$imageRootDevice / $FSTYPE defaults 0 0"        > /config/etc/fstab
echo "devpts  /dev/pts   devpts mode=0620,gid=5 0 0" >> /config/etc/fstab
echo "proc    /proc   proc    defaults 0 0"          >> /config/etc/fstab
echo "sysfs   /sys    sysfs   noauto 0 0"            >> /config/etc/fstab
echo "tmpfs   /dev/shm tmpfs  defaults 0 0"          >> /config/etc/fstab

#======================================
# 7) copy system dependant files
#--------------------------------------
cd /config
find . -type d | while read d ; do  mkdir -p /mnt/$d ; done
find . -type f | while read f ; do  cp $f /mnt/$f ; done
cd /
rm -rf /config

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

#======================================
# 9) umount system filesystems
#--------------------------------------
umount /dev/pts
umount /sys
umount /proc

#======================================
# 10) copy initrd files to image
#--------------------------------------
cp /preinit /mnt
cp /include /mnt

#======================================
# 11) Activate new root
#--------------------------------------
Echo "Activating Image: [$imageRootDevice]"
cd /mnt && exec < dev/console >dev/console 2>&1
Echo "Calling preinit phase..."
reopenKernelConsole
/mnt/sbin/pivot_root . mnt >/dev/null 2>&1
if test $? != 0;then
	PIVOT=false
	cleanInitrd && mount --move . / && chroot . ./preinit
	chroot . rm ./preinit
	chroot . rm ./include
else
	PIVOT=true
	./preinit
	rm ./preinit
	rm ./include
fi
#======================================
# 12) Unmount initrd / system init
#--------------------------------------
echo " "
echo "Booting into Xen System..."
echo "--------------------------"
mount -n -o remount,rw / &>/dev/null
if [ $PIVOT = "true" ];then
	exec umount -n -l /mnt
else
	exec chroot . /sbin/init
fi
