#!/bin/busybox sh
# kurono alpine guest initramfs: load the virtio disk + ext4 drivers, mount the
# persistent root read-only and hand over to openrc. openrc's fsck + root
# services check it and remount it read-write. (satoru)
/bin/busybox --install -s /bin
mount -t proc proc /proc; mount -t sysfs sys /sys; mount -t devtmpfs dev /dev
ROOT=/dev/vda; RFS=ext4; SHELL_ONLY=0; INIT=/sbin/init
for a in $(cat /proc/cmdline); do
  case $a in
    kroot=*) ROOT=${a#kroot=};;
    krootfstype=*) RFS=${a#krootfstype=};;
    kshell=1) SHELL_ONLY=1;;
    kinit=*) INIT=${a#kinit=};;
  esac
done
echo "kurono: alpine initramfs on $(uname -r)"
for m in $(cat /mods/order.txt); do insmod /mods/$m 2>/dev/null || echo "kurono: insmod $m failed"; done
i=0; while [ ! -b $ROOT ] && [ $i -lt 100 ]; do sleep 0.1; i=$((i+1)); done
if [ $SHELL_ONLY = 1 ]; then exec /bin/sh; fi
if [ -b $ROOT ] && mount -t $RFS -o ro $ROOT /newroot; then
  if [ -x /newroot$INIT ] || [ -L /newroot$INIT ]; then
    mount --move /dev /newroot/dev 2>/dev/null
    umount /proc /sys 2>/dev/null
    exec switch_root /newroot $INIT
  fi
  echo "kurono: $INIT missing on $ROOT"
  umount /newroot
fi
echo "kurono: could not mount the root disk $ROOT ($RFS). block devices:"
cat /proc/partitions
echo "kurono: dropping to a rescue shell"
exec /bin/sh
