#!/bin/busybox sh

rescue_shell() {
  echo "Something went wrong ${1}. Dropping you to a shell..."
  busybox --install -s
  exec /bin/sh
}

mount -t proc none /proc || rescue_shell "mounting /proc"

mount -t sysfs none /sys || rescue_shell "mounting /sys"

mount -t devtmpfs none /dev || rescue_shell "mounting /dev"

mdadm --assemble --scan -q || rescue_shell "assembling arrays"

REAL_ROOT="$(sed 's/.*root=\(\S*\).*/\1/' <proc/cmdline)"
mount -o ro $REAL_ROOT /mnt || rescue_shell "mounting the rootfs"

mount --move /dev /mnt/dev || rescue_shell "unmounting /dev"

mount --move /sys /mnt/sys || rescue_shell "unmounting /sys"

mount --move /proc /mnt/proc || rescue_shell "unmounting /proc"

exec busybox switch_root /mnt /sbin/init || rescue_shell "doing switch_root"