#!/bin/sh

ROOT="/mnt/.root"

echo "OpenCN initrd filesystem - now executing init from initramfs"

# mount temporary filesystems
mount -n -t proc     proc     /proc
mount -n -t sysfs    sysfs    /sys
mount -n -t tmpfs    tmpfs    /run
sleep 2

dev_mount=0
# Get the "rootfs" labelled partition
while [ $dev_mount -eq 0 ]
do
  ROOT_DEV=`blkid | grep "LABEL=\"rootfs\"" | sed -n 's/^\(\/dev\/.\+[[:digit:]]\): .*/\1/p'`
  echo $ROOT_DEV

  if [ ! -z $ROOT_DEV ]; then
    dev_mount=1
  else
    sleep 2
  fi
done

# mount new root
[ -d ${ROOT} ] || mkdir -p ${ROOT}
mount ${ROOT_DEV} ${ROOT}

# switch to new rootfs and exec init
cd ${ROOT}
exec switch_root . "/init" "$@"
