#!/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
#--------------------------------------
clear
echo "Loading KIWI USB-Stick Boot-System..."
echo "-------------------------------------"

#======================================
# 1) Mounting local file systems
#--------------------------------------
mountSystemFilesystems >/dev/null 2>&1
closeKernelConsole

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

#======================================
# 3) run udevd
#--------------------------------------
udevStart

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

#======================================
# 5) Load USB storage modules
#--------------------------------------
for module in ehci-hcd uhci-hcd usb_storage sg sd_mod;do
	modprobe $module &>/dev/null
done
Echo "Waiting for USB devices to settle..."
sleep 10

#======================================
# 6) Search USB stick device
#--------------------------------------
USBStickDevice
if [ $stickFound = 0 ];then
	systemException \
		"No USB stick found... abort" \
	"reboot"
fi

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

#======================================
# 8) Resize filesystem to full space
#--------------------------------------
Echo "Filesystem of stick system is: $FSTYPE -> $stickDevice"
if test "$FSTYPE" = "squashfs";then
	export UNIONFS_CONFIG="$stickRoot"3,"$stickRoot"2,aufs
else
	if test "$FSTYPE" = "reiserfs";then
		Echo "Resize Reiser filesystem to full partition space..."
		resize_reiserfs $stickDevice
	fi
	if test "$FSTYPE" = "ext2";then
		Echo "Resize EXT2 filesystem to full partition space..."
		resize2fs -f -F -p $stickDevice
		Echo "Checking EXT2 filesystem..."
		e2fsck -y -f $stickDevice
	fi
	if test "$FSTYPE" = "ext3";then
		Echo "Resize EXT3 filesystem to full partition space..."
		resize2fs -f -F -p $stickDevice
		Echo "Checking EXT3 filesystem..."
		e2fsck -y -f $stickDevice
	fi
fi

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

#======================================
# 10) Create system dependant files
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	if test $systemIntegrity = "clean";then
		setupDefaultFstab /config
		updateRootDeviceFstab /config $stickDevice
	fi
fi

#======================================
# 11) copy system dependant files
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
	if test $systemIntegrity = "clean";then
		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
	fi
fi

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

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

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

#======================================
# 15) Activate new root
#--------------------------------------
Echo "Activating Image: [$stickSerial on -> $stickDevice]"
/sbin/udevsettle --timeout=30
mount --move /dev /mnt/dev
udevKill
Echo "Calling preinit phase..."
cd /mnt && exec < dev/console >dev/console 2>&1
reopenKernelConsole
/mnt/sbin/pivot_root . mnt >/dev/null 2>&1
if test $? != 0;then
	PIVOT=false
	cleanInitrd && mount --move . / && chroot . ./preinit
	chroot . rm -f  ./preinit
	chroot . rm -f  ./include
	chroot . rm -rf ./image
else
	PIVOT=true
	./preinit
	rm -f  ./preinit
	rm -f  ./include
	rm -rf ./image
fi
#======================================
# 16) Unmount initrd / system init
#--------------------------------------
echo " "
echo "Booting into USB-Stick System..."
echo "--------------------------------"
export IFS=$IFS_ORIG
mount -n -o remount,rw / &>/dev/null
if [ $PIVOT = "true" ];then
	exec umount -n -l /mnt
else
	exec chroot . /sbin/init $@
fi
