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

#======================================
# Functions...
#--------------------------------------
USBStickDevice () {
	for device in /sys/bus/usb/drivers/usb-storage/*;do
		if [ -L $device ];then
			descriptions=$device/host*/target*/*/block*
			for description in $descriptions;do
				if [ ! -d $description ];then
					continue
				fi
				isremovable=$description/removable
				storageID=`echo $description | cut -f1 -d: | xargs basename`
				devicebID=`basename $description | cut -f2 -d:`
				serial="/sys/bus/usb/devices/$storageID/serial"
				device="/dev/$devicebID"
				if [ ! -b $device ];then
					continue;
				fi
				if [ ! -f $isremovable ];then
					continue;
				fi
				if [ ! -f $serial ];then
					serial="USB Stick (unknown type)"
				else
					serial=`cat $serial`
				fi
				removable=`cat $isremovable`
				if [ $removable -eq 1 ];then
					stickFound=1
					stickRoot=$device
					stickDevice="$device"2
					stickSerial=$serial
				fi
			done
		fi
	done
}
#======================================
# Beautify Startup
#--------------------------------------
clear
echo "Loading KIWI USB-Stick 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) Load USB storage modules
#--------------------------------------
for module in usb_storage sg sd_mod;do
	modprobe $module
done
sleep 5

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

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

#======================================
# 7) 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
	Echo "Resize filesystem to full partition space..."
	if test "$FSTYPE" = "reiserfs";then
		resize_reiserfs $stickDevice
	fi
	if test "$FSTYPE" = "ext2";then
		e2fsck -y -f $stickDevice
		resize2fs -F -p $stickDevice
	fi
	if test "$FSTYPE" = "ext3";then
		e2fsck -y -f $stickDevice
		resize2fs -F -p $stickDevice
		tune2fs -j $stickDevice
	fi
fi

#======================================
# 8) Mount USB stick (system)
#--------------------------------------
mountSystem $stickDevice

#======================================
# 9) Create system dependant files
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
if test $systemIntegrity = "clean";then
	mkdir -p /config/etc
	cat > /config/etc/fstab < /dev/null
	if [ -z "$UNIONFS_CONFIG" ]; then
		echo "$stickDevice / $FSTYPE defaults 0 0"       > /config/etc/fstab
	fi
	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
fi
fi

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

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