#!/bin/sh

# (C) 2009-2012 Proxmox Server Solutions GmbH <support@proxmox.com>

export PATH=/sbin:/bin:/usr/bin:/usr/sbin

# busybox needs full paths until proc is mounted

/bin/echo "Proxmox startup"

/bin/echo "mounting proc filesystem"
/bin/mount -nt proc proc /proc

echo "mounting sys filesystem"
mount -nt sysfs sysfs /sys

KERNEL_VER=`uname -r`

parse_cmdline() {
    root=
    lvm2root=
    proxdebug=0
    for par in $(cat /proc/cmdline); do 
	case $par in
	    lvm2root=*)
		lvm2root=${par#lvm2root=}
		;;
	    root=/dev/mapper/*)
		lvm2root=${par#root=}
		;;
	    root=*)
		root=${par#root=}
		;;
	    proxdebug)
		proxdebug=1
		;;
	esac
    done;
}

myreboot() {
    echo b > /proc/sysrq-trigger
    echo "rebooting..."
    sleep 100
    exit 0
}

debugsh() {
    getty 38400 tty1 -i -l /bin/sh -n
}

echo "comandline: $(cat /proc/cmdline)"
parse_cmdline

# use mdev as firmware loader
echo /sbin/mdev >/proc/sys/kernel/hotplug

DRIVERS="msdos isofs"
for mod in $DRIVERS; do 
    modprobe -q "$mod"
done

echo "loading drivers"

for mod in $(pcimodules) ; do
    modprobe "$mod"
done

stdmod="usb-storage usbhid virtio_blk"
for mod in $stdmod; do 
    modprobe "$mod"
done

# wait for scsi scan
modprobe scsi_wait_scan

# we have no iscsi daemon, so we need to scan iscsi device manually.
# else we cant boot from iscsi hba because devices are not detected.
for i in /sys/class/scsi_host/host*; do 
    host=${i##*/}
    if [ -d $i ] && [ -f $i/scan ] && [ -d /sys/class/iscsi_host/$host ] ; then
	echo "Scanning iSCSI $host"
	echo "- - -" > $i/scan
    fi
done

if [ -n "$lvm2root" ]; then

    echo -n "Finding device mapper major and minor numbers: "

    MAJOR=$(sed -n 's/^ *\([0-9]\+\) \+misc$/\1/p' /proc/devices)
    MINOR=$(sed -n 's/^ *\([0-9]\+\) \+device-mapper$/\1/p' /proc/misc)
    if test -n "$MAJOR" -a -n "$MINOR" ; then
        mkdir -p -m 755 /dev/mapper
        mknod -m 600 /dev/mapper/control c $MAJOR $MINOR
    fi

    echo "($MAJOR,$MINOR)"

    vg=${lvm2root}
    vg=${vg#/dev/mapper/}
    if [ "$vg" = "$1" ]; then
	echo "activating all volume groups"
	lvm vgchange --ignorelockingfailure -aly
    else
	# Split volume group from logical volume.
	vg=$(echo ${vg} | sed -e 's#\(.*\)\([^-]\)-[^-].*#\1\2#')
	# Reduce padded --'s to -'s
	vg=$(echo ${vg} | sed -e 's#--#-#g')
	echo "activating volume group $vg"
	lvm vgchange -aly --ignorelockingfailure ${vg}	
    fi

    echo "trying to mount lvm root: ($lvm2root)"

    found=
    for try in 5 4 3 2 1; do
	for t in ext4 auto; do 
	    if mount -n -t $t -r $lvm2root /mnt; then 
		found=ok
		break; 
	    fi
	done
	if test -n "$found"; then
	    break;
	fi
	if test $try -gt 1; then
	    echo "testing again in 5 seconds"
	    sleep 5
	fi
    done

elif [ -n "$root" ]; then

    case $root in
	/dev/*)
	    real_root=$root
	    ;; 
	*:*)
	    dev_min=$((0x${root#*:}))
	    dev_maj=$((0x${root%:*}))
	    mknod /tmp/rootdev b $dev_maj $dev_min
	    real_root=/tmp/rootdev
	    ;;
	*)
	    dev_min=$((0x$root & 255))
	    dev_maj=$((0x$root >> 8))
	    mknod /tmp/rootdev b $dev_maj $dev_min
	    real_root=/tmp/rootdev
	    ;;
    esac

    echo "trying to mount root: $real_root ($root)"

    found=
    for try in 5 4 3 2 1; do
	for t in ext4 auto; do 
	    if mount -n -t $t -r $real_root /mnt; then 
		found=ok
		break; 
	    fi
	done
	if test -n "$found"; then
	    break;
	fi
	if test $try -gt 1; then
	    echo "testing again in 5 seconds"
	    sleep 5
	fi
    done
    
else
 
    cdrom=

    initrdisoimage="/proxmox.iso"

    if [ -f $initrdisoimage ]; then
	# this is useful for PXE boot
	echo "found proxmox cdrom image inside initrd image"
	if mount -t iso9660 -o loop,ro $initrdisoimage /mnt >/dev/null 2>&1; then
	    cdrom=$initrdisoimage
	fi
#added
elif [ -f /proxmox.iso ]; then
    echo "found proxmox cdrom ISO image"
    echo "mounting /proxmox.iso image"
    mount -t iso9660 -o loop /proxmox.iso /mnt
    echo "mounting ISO done"

    else 
	echo "searching for cdrom"
	reqid=$(cat /.pve-cd-id.txt)
	for try in 5 4 3 2 1; do
	    for i in /sys/block/hd* /sys/block/sr* /sys/block/scd* /sys/block/sd*; do 

		if [ -d $i ] && [ $(cat $i/removable) = 1 ]; then

		    path="/dev/${i##*/}"
		    echo "testing cdrom $path"
		    if mount -t iso9660 -o ro $path /mnt >/dev/null 2>&1; then
			if [ -r /mnt/.pve-cd-id.txt ] && [ "X$(cat /mnt/.pve-cd-id.txt)" = "X$reqid" ]; then
			    echo "found proxmox cdrom"
			    cdrom=$path
			    break
			fi
		    fi
		    umount /mnt
		fi
	    done
	    if test -n "$cdrom"; then
		break;
	    fi
	    if test $try -gt 1; then
		echo "testing again in 5 seconds"
		sleep 5
	    fi
	done
    fi

    if [ -z $cdrom ]; then 
	echo "no cdrom found - unable to continue (type exit or CTRL-D to reboot)"
	debugsh
	myreboot
    fi
fi

if [ $proxdebug -ne 0 ]; then 
    echo "Debugging mode (type exit or CTRL-D to continue startup)"
    debugsh
fi


umount /sys

if [ -x "/mnt/sbin/unconfigured.sh" ]; then
    # and run the installer

    chroot /mnt sbin/unconfigured.sh

    cd /

    # Send a SIGKILL to all processes, except for init.
    echo i >/proc/sysrq-trigger

    exec /sbin/ejectcd.sh

else
    # or begin normal sysvinit

    umount /proc

    exec /sbin/switch_root -c /dev/console /mnt sbin/init
fi


