#!/bin/busybox sh
# /init of the Vetro initramfs (M3): mounts the virtual file systems, prints the
# boot marker, runs the autotest and opens a shell on the console.
# Parameters from the kernel command line:
#   vetro.noautotest  skips the autotest;
#   vetro.poweroff    powers off the machine (PSCI) after the autotest, no shell;
#   vetro.kselftest   runs the kselftests (/etc/kselftest.sh, kselftest
#                     initramfs) and powers off the machine.
# With a virtio-vsock device vetro-files starts, the file manager daemon
# (M8, ADR 0020): under QEMU (no vsock) it does not start and the log is unchanged.
# Console messages stay in Italian: they are compared with the reference log
# guest/kernel/reference/qemu-boot.log.
/bin/busybox --install -s
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev
mount -t tmpfs tmpfs /tmp
echo "VETRO-BOOT-OK: /init avviato ($(uname -r))"
# virtio-vsock has device id 19 (0x0013).
if grep -qx 0x0013 /sys/bus/virtio/devices/*/device 2>/dev/null; then
  vetro-files </dev/null &
fi
cmdline=$(cat /proc/cmdline)
case " $cmdline " in
  *" vetro.noautotest "*) ;;
  *) sh /etc/autotest.sh ;;
esac
case " $cmdline " in
  *" vetro.kselftest "*)
    echo "VETRO-KSELFTEST-INIZIO"
    sh /etc/kselftest.sh
    echo "VETRO-KSELFTEST-FINE"
    poweroff -f ;;
esac
case " $cmdline " in
  *" vetro.poweroff "*) poweroff -f ;;
esac
# Interactive shell with a controlling terminal on the real console
# (/dev/console cannot become a controlling terminal). Alpine's BusyBox
# has no cttyhack: the PL011 is opened directly. If it exits, another one
# is opened.
tty=/dev/ttyAMA0
[ -c "$tty" ] || tty=/dev/console
while true; do
  setsid sh -c "exec sh -l <>$tty >&0 2>&1"
done
