#!/bin/sh

mount -o nosuid,strictatime,mode=755 -t devtmpfs devtmpfs /dev
mount -o nosuid,noexec,nodev -t sysfs sysfs /sys
mount -o nosuid,noexec,nodev -t proc proc /proc

/bin/mkdir /sysroot

#Find devices
ROOTFS=`/sbin/blkid -L rootfs`
if [ x$ROOTFS = "x" ]
then
    ROOTFS=`/sbin/blkid -t PARTLABEL=rootfs -o device`
fi
###Temporary Fix for SD card booting###
echo "Found Rootfs: " $ROOTFS
echo "Assigning to mmcblk1"
ROOTFS=/dev/mmcblk1p3
#######################################

#Check and mount devices
FIRSTBOOT=1

if [ x$ROOTFS != "x" ]
then
    /bin/mount $ROOTFS /sysroot
else
    echo "WARNING : THERE IS NO ROOTFS."
    exec /bin/sh
fi

cd /sysroot
mkdir -p ./initrd

/sbin/pivot_root . ./initrd
/bin/mount -M ./initrd/dev /dev
/bin/mount -M ./initrd/sys /sys
/bin/mount -M ./initrd/proc /proc
/bin/umount -l ./initrd
/bin/rm -r ./initrd

INIT=/sbin/init

if [ $$ = 1 ]
then
  exec chroot . $INIT $@
  #/bin/chroot . $INIT $@
fi
exec /bin/sh
