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

#======================================
# Functions
#--------------------------------------
VMMountSystem () {
	for rdevice in /dev/hda1 /dev/sda1;do
		imageRootDevice=$rdevice
		mount $imageRootDevice /mnt &>/dev/null
		if [ $? = 0 ];then
			break
		fi
	done
}
VMUmountSystem () {
	umount /mnt
}
#======================================
# Beautify Startup
#--------------------------------------
clear
echo "Loading KIWI VMX 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
#--------------------------------------
probeDevices

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

#======================================
# 5) Get filesystem type
#--------------------------------------
probeFileSystem $imageRootDevice
kernelList /mnt

#======================================
# 6) Umount VM system
#--------------------------------------
VMUmountSystem

#======================================
# 7) Resize filesystem to full space
#--------------------------------------
Echo "Resize filesystem to full partition space..."
if test "$FSTYPE" = "reiserfs";then
	resize_reiserfs $imageRootDevice
	INITRD_MODULES="$INITRD_MODULES reiserfs"
fi
if test "$FSTYPE" = "ext2";then
	e2fsck -y -f $imageRootDevice
	resize2fs -F -p $imageRootDevice
	INITRD_MODULES="$INITRD_MODULES ext2"
fi
if test "$FSTYPE" = "ext3";then
	e2fsck -y -f $imageRootDevice
	resize2fs -F -p $imageRootDevice
	tune2fs -j $imageRootDevice
	INITRD_MODULES="$INITRD_MODULES ext3"
fi

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

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

#======================================
# b) /boot/grub/menu.lst
#--------------------------------------
console=""
mkdir -p /config/boot/grub
echo "timeout 10" > /config/boot/grub/menu.lst
IFS="," ; for i in $KERNEL_LIST;do
	if test ! -z "$i";then
		kernel=`echo $i | cut -f1 -d:`
		initrd=`echo $i | cut -f2 -d:`
		menu=/config/boot/grub/menu.lst
		echo "title $kernel" >> $menu
		if [ $kernel = "vmlinuz-xen" ];then
			echo " root (hd0,0)"                   >> $menu
			echo " kernel /boot/xen.gz"            >> $menu
			echo -n " module /boot/$kernel"        >> $menu
			echo " root=$imageRootDevice $console" >> $menu
			echo " module /boot/$initrd"           >> $menu
		else
			echo -n " kernel (hd0,0)/boot/$kernel" >> $menu
			echo " root=$imageRootDevice $console" >> $menu
			echo " initrd (hd0,0)/boot/$initrd"    >> $menu
		fi
	fi
done

#======================================
# c) /etc/grub.conf
#--------------------------------------
mkdir -p /config/etc
gconf=/config/etc/grub.conf
echo -en "root (hd0,1)\ninstall"         > $gconf
echo -n " --stage2=/boot/grub/stage2"   >> $gconf
echo -n " /boot/grub/stage1 d (hd0)"    >> $gconf
echo -n " /boot/grub/stage2 0x8000"     >> $gconf
echo " (hd0,1)/boot/grub/menu.lst"      >> $gconf
echo "quit"                             >> $gconf

#======================================
# d) /etc/sysconfig/kernel
#--------------------------------------
mkdir -p /config/etc/sysconfig
syskernel=/config/etc/sysconfig/kernel
echo "INITRD_MODULES=\"$INITRD_MODULES\""       > $syskernel
echo "DOMU_INITRD_MODULES=\"$DOMURD_MODULES\"" >> $syskernel

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

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

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

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

#======================================
# 14) 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
#======================================
# 15) Unmount initrd / system init
#--------------------------------------
echo " "
echo "Booting into final System..."
echo "----------------------------"
mount -n -o remount,rw / 2>/dev/null
if [ $PIVOT = "true" ];then
	exec umount -n -l /mnt
else
	exec chroot . /sbin/init
fi
