#!/bin/busybox sh
# shellcheck shell=dash
# WardenOS QEMU device sim: initramfs /init (PID 1).
#
# Phase-1 duty: prove the kernel booted on -M virt; print the sentinel the
# smoke test greps for, then power off (PSCI SYSTEM_OFF, so qemu exits).
# `warden.shell` on the kernel cmdline drops to an interactive shell instead.

/bin/busybox mount -t devtmpfs devtmpfs /dev 2>/dev/null

# The cpio archive carries no device nodes (it is built unprivileged, no
# mknod); reopen stdio on the real console now that devtmpfs is mounted.
exec </dev/console >/dev/console 2>&1

/bin/busybox --install -s /bin
mount -t proc proc /proc
mount -t sysfs sysfs /sys

echo "WARDEN-QEMU-BOOT-OK"

# Kernel contract (rebuild M0): every symbol of build/required-symbols.txt (staged
# as /etc/required-symbols.txt by mkinitramfs.sh) is in the running kernel's
# /proc/config.gz, the cgroup v2 controllers the platform limits with are there,
# and pressure stall information is on. boot-smoke.sh greps these markers.
if [ -r /etc/required-symbols.txt ]; then
    if [ -e /proc/config.gz ] && zcat /proc/config.gz > /config.run 2>/dev/null; then
        miss=""; nsym=0
        while read -r line; do
            case "$line" in
                CONFIG_*=*)
                    nsym=$((nsym + 1))
                    grep -qxF "$line" /config.run || miss="$miss $line" ;;
                "# CONFIG_"*" is not set")
                    sym="${line#\# }"; sym="${sym% is not set}"; nsym=$((nsym + 1))
                    grep -q "^$sym=" /config.run && miss="$miss ${sym}(set)" ;;
            esac
        done < /etc/required-symbols.txt
        if [ -z "$miss" ]; then echo "WARDEN-QEMU-SYMBOLS-OK $nsym"
        else echo "WARDEN-QEMU-SYMBOLS-MISSING$miss"; fi
    else
        echo "WARDEN-QEMU-SYMBOLS-MISSING /proc/config.gz"
    fi
    mkdir -p /sys/fs/cgroup
    mount -t cgroup2 none /sys/fs/cgroup 2>/dev/null
    echo "WARDEN-QEMU-CGROUP-CONTROLLERS $(cat /sys/fs/cgroup/cgroup.controllers 2>/dev/null)"
    psi_missing=""
    for f in cpu memory io; do [ -r "/proc/pressure/$f" ] || psi_missing="$psi_missing $f"; done
    if [ -z "$psi_missing" ]; then echo "WARDEN-QEMU-PSI-OK"; else echo "WARDEN-QEMU-PSI-MISSING$psi_missing"; fi
fi

# warden.run=<path>: run a script that mkinitramfs.sh staged from EXTRA_DIR, then
# power off. Used by the M0 limit tests (tools/m0/limits-test.sh in flare-edge).
read -r CMDLINE < /proc/cmdline
for w in $CMDLINE; do
    case "$w" in warden.run=*) RUN_SCRIPT="${w#warden.run=}" ;; esac
done
if [ -n "${RUN_SCRIPT:-}" ]; then
    if [ -x "$RUN_SCRIPT" ]; then
        echo "WARDEN-QEMU-RUN-BEGIN $RUN_SCRIPT"
        "$RUN_SCRIPT"
        echo "WARDEN-QEMU-RUN-END rc=$?"
    else
        echo "WARDEN-QEMU-RUN-MISSING $RUN_SCRIPT"
    fi
    poweroff -f
fi

# With a virtio disk attached, hand over to the stage-1 rc (by-name symlinks,
# slot select, switch_root). It only returns on failure; then fall through to
# the diskless shell/poweroff behavior below.
if [ -b /dev/vda ]; then
    # shellcheck source=qemu/rootfs/etc/rc disable=SC1091
    . /etc/rc
fi

if grep -qw warden.shell /proc/cmdline; then
    echo "warden.shell: interactive shell (exit to power off)"
    setsid cttyhack sh
fi

poweroff -f
