#!/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
export ARCH=`arch`

#======================================
# Exports (Configuration)...
#--------------------------------------
export systemIntegrity=clean
export LIVECD_CONFIG="/cdrom/config.isoclient"
export LIVECD="livecd"
export LOCAL_BOOT="no"

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

#======================================
# Beautify Startup
#--------------------------------------
clear
echo "Loading KIWI CD 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) Mount the CD device
#--------------------------------------
CDMount

#======================================
# 4) Load configuration
#--------------------------------------
importFile < $LIVECD_CONFIG

#======================================
# 5) Download RW part of the CD image
#--------------------------------------
IFS="," ; for i in $IMAGE;do
	field=0
	IFS=";" ; for n in $i;do
	case $field in
		0) imageDevice=$n ; field=1 ;;
		1) imageName=$n   ; field=2 ;;
		2) imageVersion=$n
	esac
	done
	imageReadOnly="$imageName"
	imageReadOnly=`echo $imageReadOnly | sed -e s@.$ARCH@@`
	imageReadOnly="$imageReadOnly-read-only.$ARCH-$imageVersion"
	imageName="/cdrom/$imageName-$imageVersion"
	imageMD5s="$imageName.md5"
	if [ ! -z "$UNIONFS_CONFIG" ];then
		continue
	fi
	while true;do
		Echo "Loading $imageName [$imageDevice]..."
		IFS=" "
		read sum1 blocks blocksize < $imageMD5s
		if ! validateSize;then
			systemException \
				"Not enough RAM available for this image" \
			"reboot"
		fi
		dd if=$imageName of=$imageDevice count=$blocks \
			bs=$blocksize >/dev/null 2>&1
		dd if=$imageDevice count=$blocks bs=$blocksize 2>/dev/null |\
			md5sum - > /etc/ireal.md5
		read sum2 dumy < /etc/ireal.md5
		if test $sum1 = $sum2;then
			Echo "Image checksum test: fine :-)"
			break
		fi
		Echo "Image checksum test failed:"
		echo
		Echo -b "1) Data corruption while loading the image:"
		Echo -b "   will give it a new try..."
		echo
		Echo -b "2) ramdisk size is too small for the image:"
		Echo -b "   try to set the kernel parameter ramdisk_size=<size>"
		echo
		Echo "Retry to load image..."
		sleep 15
	done
	rm -f /etc/ireal.md5
done
CDUmount

#======================================
# 6) Get filesystem type
#--------------------------------------
if [ -z "$UNIONFS_CONFIG" ];then
	probeFileSystem $imageDevice
fi

#======================================
# 7) Resize filesystem to full space
#--------------------------------------
if [ -z "$UNIONFS_CONFIG" ];then
	Echo "Resize filesystem to full partition space..."
	if test "$FSTYPE" = "reiserfs";then
		resize_reiserfs $imageDevice
	fi
	if test "$FSTYPE" = "ext2";then
		resize2fs -p $imageDevice
	fi
	if test "$FSTYPE" = "ext3";then
		resize2fs -p $imageDevice
		tune2fs -j $imageDevice
	fi
fi

#======================================
# 8) Mount OS image to /mnt
#--------------------------------------
if [ -z "$UNIONFS_CONFIG" ];then
	#======================================
	# 8.1) mount RW part and CD to /mnt
	#--------------------------------------
	if ! mount $imageDevice /mnt &>/dev/null;then
		systemException "Failed to mount RW root filesystem" "reboot"
	fi
	mkdir -p /mnt/$LIVECD && mount $cddev /mnt/$LIVECD &>/dev/null

	#======================================
	# 8.2) Create RO Link list
	#--------------------------------------
	cd /mnt
	if [ ! -d $LIVECD/read-only-system ];then
		Echo "Mounting compressed read only tree..."
		mkdir -p /mnt/read-only-system
		mount -t squashfs -o loop /mnt/$LIVECD/$imageReadOnly \
			/mnt/read-only-system
		if [ ! $? = 0 ];then
			systemException "Failed to mount RO root filesystem" "reboot"
		fi
		Echo "Creating live media links..."
		for dir in bin boot lib opt sbin usr;do
			ln -s read-only-system/$dir $dir
		done
	else
		Echo "Creating live media links..."
		for dir in bin boot lib opt sbin usr;do
			ln -s $LIVECD/read-only-system/$dir $dir
		done
	fi
	cd /
else
	#======================================
	# 8.1) mount CD to /mnt and unify
	#--------------------------------------
	Echo "Mounting compressed unified tree..."
	mkdir -p /mnt/$LIVECD && mount $cddev /mnt/$LIVECD &>/dev/null
	losetup /dev/loop1 /mnt/$LIVECD/$imageReadOnly
	if ! mountSystem /dev/loop1;then
		systemException "Failed to mount root filesystem" "reboot"
	fi
fi

#======================================
# 9) Create system dependant files
#--------------------------------------
mkdir -p /config/etc
echo "$imageDevice / $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

#======================================
# 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: [$imageName]"
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 Live CD System..."
echo "------------------------------"
mount -n -o remount,rw / &>/dev/null
if [ $PIVOT = "true" ];then
	exec umount -n -l /mnt
else
	exec chroot . /sbin/init
fi
