From 579ac260ca29b4a3f4e0813695a1850c93643e5b Mon Sep 17 00:00:00 2001 From: BFE Engineering Date: Sat, 31 Oct 2026 17:05:50 -0600 Subject: [PATCH 1/3] qemu: rig agent, a framed guest shell on its own serial port (flare-edge M9 soak-lite) run.sh --agent SOCK adds a third pci-serial after ctl and rs485 (their ttyS order is unchanged) and warden.agent on the command line. Stage-2 init then runs warden_agent (rootfs/etc/warden-lib.sh) on that port with echo off: `@run ID SECS B64` runs a script and answers <>, the base64 of its output and <>; `@chunk` pieces get past the 4095-byte cooked line; a script past its deadline is killed with its process group and answers 124 (the panel BusyBox has no timeout). It is the guest channel of flare-edge tools/soak/rig-qemu.py, which needs bus reads and writes, the clock and /proc inside the gate image. Rig rootfs only: no panel image carries it. Tests: qemu/tests/agent-test.sh (sh and busybox sh, in CI) and new run-sh-args-test.sh cases (agent last, warden.agent only with the flag). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BdKQBQkJkq4zE7iPFMkAhS --- .github/workflows/ci.yml | 8 +++ qemu/README.md | 1 + qemu/rootfs/etc/warden-lib.sh | 110 +++++++++++++++++++++++++++++++ qemu/rootfs/sbin/init | 22 +++++++ qemu/run.sh | 15 ++++- qemu/tests/agent-test.sh | 117 +++++++++++++++++++++++++++++++++ qemu/tests/run-sh-args-test.sh | 30 +++++++++ 7 files changed, 302 insertions(+), 1 deletion(-) create mode 100755 qemu/tests/agent-test.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 92855a3..c884531 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -170,6 +170,14 @@ jobs: if command -v busybox >/dev/null; then WARDEN_TEST_SH="busybox sh" bash qemu/tests/ctl-bridge-test.sh fi + - name: rig agent runs framed scripts under a deadline (flare-edge M9 soak-lite) + # warden_agent against request lines on stdin; once more under + # busybox sh, the rig's own shell. + run: | + bash qemu/tests/agent-test.sh + if command -v busybox >/dev/null; then + WARDEN_TEST_SH="busybox sh" bash qemu/tests/agent-test.sh + fi - name: rig init boots through the shipped init chain (flare-edge#2210) # warden_init_chain against stub scripts and the warden.initchain # wiring in sbin/init; once more under busybox sh, the rig's own shell. diff --git a/qemu/README.md b/qemu/README.md index 47eab54..dba5dcf 100644 --- a/qemu/README.md +++ b/qemu/README.md @@ -146,6 +146,7 @@ All take the virt-fragment ``; `FLARE_EDGE=` where noted. | `console-shell-test.sh` | - | offline, no VM: `warden_console_shell` (warden.shell) runs `setsid cttyhack sh` where cttyhack exists, else `setsid -c sh` on the console tty read from sysfs (the panel BusyBox has no cttyhack), else a plain sh. CI runs it under bash's `sh` and busybox `sh` | | `ctl-bridge-test.sh` | - | offline, no VM: runs the control bridge (`warden_ctl_bridge` in rootfs/etc/warden-lib.sh) against a fake warden-ui that answers some commands late. A reply slower than the old 5 s bound still arrives (the bound is now 30 s, `WARDEN_CTL_WAIT_S`), a timeout answers `bridge: no reply from warden-ui ...` (qmp.py raises it as a rig fault), and a timed-out command's late reply is drained instead of being read as the next command's answer (flare-edge #1448). A fake `wthctl` stands in for the hook client behind `@hookdoc`/`@hook` (flare-edge #2054): the pieces join into one request whose args are used once, only plain words reach the client, and a missing client or a request with no reply answers a `bridge:` line. CI runs it under bash's `sh` and busybox `sh` | | `init-rig-bus-test.sh` | - | offline, no VM: `warden_rig_bus` in rootfs/etc/warden-lib.sh against stub brokers. No broker in the payload creates nothing; a broker that listens gets the panel's layout (the `bus.sock` link, a 0400 64-hex credential per bus user, a config without password file, ACL or plugin); a broker that exits or never listens is reported within the bound; init starts the bus before the payload daemons. `WARDEN_TEST_MOSQUITTO_DIR` adds a real mosquitto carrying one message between two users logged in by credential file (flare-edge #1754) | +| `agent-test.sh` | - | offline, no VM: the rig agent (`warden_agent` in rootfs/etc/warden-lib.sh, armed by `run.sh --agent` and `warden.agent`) answers `@run ID SECS B64` with `<>`, the base64 of the script's output and `<>`; `@chunk` pieces join into one script used once; a script past its deadline is killed with its process group and answers 124; a malformed request answers one `<>` line and runs nothing; init puts the agent's port after ctl and RS485. It is the guest shell of flare-edge `tools/soak/rig-qemu.py` (M9 soak-lite). CI runs it under bash's `sh` and busybox `sh` | | watchdog (`run.sh --watchdog`) | - | arm `/dev/watchdog`, don't pet: the VM resets ~30 s later (verified) | Scenario fine print: diff --git a/qemu/rootfs/etc/warden-lib.sh b/qemu/rootfs/etc/warden-lib.sh index 95c3f10..94adb1b 100644 --- a/qemu/rootfs/etc/warden-lib.sh +++ b/qemu/rootfs/etc/warden-lib.sh @@ -387,3 +387,113 @@ warden_init_chain() { done echo "$_wic_ran" } + +# The rig agent (flare-edge M9 soak-lite, d10 6.5): a shell inside the guest +# for a host-side rig that must read state and change it (tools/soak/rig-qemu.py: +# bus reads and writes, the clock, /proc for metrics). /sbin/init runs it on +# its own pci-serial port, only on a boot with warden.agent on the command +# line (run.sh --agent). It is part of the rig rootfs, which no panel image +# carries; the flow channels (ctl bridge, test hook) are untouched by it. +# +# One request per line on stdin, every answer framed so the host never needs +# a timeout to know a reply is complete: +# +# @ping -> <> +# @chunk ID B64 -> <> appends B64 to ID's script text +# @run ID SECS [B64] -> <>, the base64 of the script's stdout +# and stderr, <> +# anything else -> <> +# +# The script is the base64 text of the @chunk lines for ID, then the @run +# line's own B64, decoded and run by sh with stdin from /dev/null. The agent +# port is a tty in cooked mode, which cuts a line at 4095 bytes, hence the +# chunks. ID is [A-Za-z0-9_-] (1 to 32 characters), SECS 1 to 3600. A script +# still running after SECS seconds is killed with its whole process group and +# answers RC 124, as timeout(1) does; the panel BusyBox has no timeout applet. +# WARDEN_AGENT_DIR exists for the host test only. +warden_agent() { + _wa_dir="${WARDEN_AGENT_DIR:-/tmp/warden-agent}" + mkdir -p "$_wa_dir" + while IFS= read -r _wa_line; do + # A cooked tty may hand the line over with a trailing CR. + _wa_line="${_wa_line%"$(printf '\r')"}" + [ -n "$_wa_line" ] || continue + set -f + # shellcheck disable=SC2086 # split into words on purpose, globbing off + set -- $_wa_line + set +f + case "$1" in + @ping) echo "<>" ;; + @chunk) + if [ "$#" -ne 3 ] || ! _wa_id_ok "$2" || ! _wa_b64_ok "$3"; then + echo "<>" + continue + fi + printf '%s' "$3" >> "$_wa_dir/$2.b64" + echo "<>" + ;; + @run) + if [ "$#" -lt 3 ] || [ "$#" -gt 4 ] || ! _wa_id_ok "$2" || + ! _wa_secs_ok "$3" || { [ "$#" -eq 4 ] && ! _wa_b64_ok "$4"; }; then + echo "<>" + continue + fi + [ "$#" -eq 4 ] && printf '%s' "$4" >> "$_wa_dir/$2.b64" + _wa_run "$2" "$3" + ;; + *) echo "<>" ;; + esac + done +} + +_wa_id_ok() { + case "$1" in ""|*[!A-Za-z0-9_-]*) return 1 ;; esac + [ "${#1}" -le 32 ] +} + +_wa_b64_ok() { + case "$1" in ""|*[!A-Za-z0-9+/=]*) return 1 ;; esac + return 0 +} + +_wa_secs_ok() { + case "$1" in ""|*[!0-9]*) return 1 ;; esac + [ "${#1}" -le 4 ] && [ "$1" -ge 1 ] && [ "$1" -le 3600 ] +} + +# _wa_run ID SECS: decode, run under the deadline, answer, forget ID. +_wa_run() { + _wr_b64="$_wa_dir/$1.b64" + _wr_sh="$_wa_dir/$1.sh" + _wr_out="$_wa_dir/$1.out" + if [ ! -s "$_wr_b64" ] || ! base64 -d < "$_wr_b64" > "$_wr_sh" 2>/dev/null || + [ ! -s "$_wr_sh" ]; then + rm -f "$_wr_b64" "$_wr_sh" + echo "<>" + return 0 + fi + rm -f "$_wr_b64" + # setsid: the script leads its own process group, so a timeout kills + # every process it started, not only its shell. + setsid sh "$_wr_sh" < /dev/null > "$_wr_out" 2>&1 & + _wr_pid=$! + _wr_n=0 + _wr_limit=$(($2 * 10)) + _wr_late=0 + while kill -0 "$_wr_pid" 2>/dev/null; do + if [ "$_wr_n" -ge "$_wr_limit" ]; then + kill -KILL "-$_wr_pid" 2>/dev/null || kill -KILL "$_wr_pid" 2>/dev/null + _wr_late=1 + break + fi + sleep 0.1 + _wr_n=$((_wr_n + 1)) + done + wait "$_wr_pid" 2>/dev/null + _wr_rc=$? + [ "$_wr_late" = 1 ] && _wr_rc=124 + echo "<>" + [ -s "$_wr_out" ] && base64 < "$_wr_out" + echo "<>" + rm -f "$_wr_sh" "$_wr_out" +} diff --git a/qemu/rootfs/sbin/init b/qemu/rootfs/sbin/init index 8a197ac..fe3e5b7 100755 --- a/qemu/rootfs/sbin/init +++ b/qemu/rootfs/sbin/init @@ -229,6 +229,28 @@ if grep -qw warden.ctl /proc/cmdline && [ -c /dev/ttyS0 ]; then ) & fi +# Rig agent (run.sh --agent, warden.agent on the command line): a shell for a +# host-side rig that reads and changes guest state, such as the QEMU soak rig +# (flare-edge tools/soak/rig-qemu.py). Its pci-serial port comes after the +# ctl port (when there is one) and the always-present RS485 port, so it is +# ttyS2 with warden.ctl and ttyS1 without; the ctl/RS485 order above is +# unchanged. Echo is turned off so the host reads only the agent's frames. +# The protocol is warden_agent (/etc/warden-lib.sh). Rig rootfs only: no +# panel image carries it. +if grep -qw warden.agent /proc/cmdline; then + if grep -qw warden.ctl /proc/cmdline; then agent=/dev/ttyS2; else agent=/dev/ttyS1; fi + if [ -c "$agent" ]; then + echo "init: rig agent on $agent" + ( + exec 4<> "$agent" + stty -echo <&4 2>/dev/null || echo "init: rig agent: stty -echo failed" + warden_agent <&4 >&4 2>/dev/null + ) & + else + echo "init: rig agent: $agent missing (needs the virt.fragment kernel)" + fi +fi + if grep -qw warden.shell /proc/cmdline; then echo "warden.shell: interactive shell (exit powers off)" warden_console_shell diff --git a/qemu/run.sh b/qemu/run.sh index f808c86..e0c0d4a 100755 --- a/qemu/run.sh +++ b/qemu/run.sh @@ -17,6 +17,10 @@ # --ctl SOCK unix socket to warden-ui's debug channel (nav/page/hit), # bridged by init from a pci-serial port; the rig-side # twin of tools/warden-ctl over SSH on a real panel +# --agent SOCK unix socket to the rig agent (warden_agent in +# rootfs/etc/warden-lib.sh): a guest shell for host-side +# rigs such as flare-edge tools/soak/rig-qemu.py; a third +# pci-serial after ctl and rs485, so their order holds # --watchdog add i6300esb watchdog, reset on expiry (fragment kernel) # --qmp SOCK QMP unix socket (screendump, input-send-event, quit) # --display MODE off (default, -nographic) | on (gtk window) | headless @@ -38,7 +42,7 @@ QEMU_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" OUT="${OUT:-$QEMU_DIR/out}" KERNEL="" INITRD="$OUT/initramfs.cpio.gz" DISK="" NO_DISK=0 SLOT="_a" -RTC="" RS485="" CTL="" WATCHDOG=0 QMP="" DISPLAY_MODE="off" SHELL_FLAG=0 ALLOW_APPLY=0 +RTC="" RS485="" CTL="" AGENT="" WATCHDOG=0 QMP="" DISPLAY_MODE="off" SHELL_FLAG=0 ALLOW_APPLY=0 SSH_PORT=2222 HTTP_PORT=8080 API_PORT=28443 EXTRA=() EXTRA_APPEND="" @@ -53,6 +57,7 @@ while [ $# -gt 0 ]; do --rtc) RTC="${2:?}"; shift 2 ;; --rs485) RS485="${2:?}"; shift 2 ;; --ctl) CTL="${2:?}"; shift 2 ;; + --agent) AGENT="${2:?}"; shift 2 ;; --watchdog) WATCHDOG=1; shift ;; --qmp) QMP="${2:?}"; shift 2 ;; --display) DISPLAY_MODE="${2:?}"; shift 2 ;; @@ -143,6 +148,14 @@ if [ -n "$RS485" ]; then else ARGS+=( -chardev "null,id=rs485" -device "pci-serial,chardev=rs485" ) fi +# The rig agent comes LAST of the pci-serial ports, so ctl stays ttyS0 and +# rs485 stays where init looks for it; init finds the agent at ttyS1 (no +# ctl) or ttyS2 (with ctl), and only with warden.agent on the cmdline. +if [ -n "$AGENT" ]; then + APPEND="$APPEND warden.agent" + ARGS+=( -chardev "socket,id=agent,path=$AGENT,server=on,wait=off" + -device "pci-serial,chardev=agent" ) +fi [ -n "$QMP" ] && ARGS+=( -qmp "unix:$QMP,server=on,wait=off" ) case "$DISPLAY_MODE" in diff --git a/qemu/tests/agent-test.sh b/qemu/tests/agent-test.sh new file mode 100755 index 0000000..d66ee9a --- /dev/null +++ b/qemu/tests/agent-test.sh @@ -0,0 +1,117 @@ +#!/usr/bin/env bash +# Host test for the rig agent, warden_agent in qemu/rootfs/etc/warden-lib.sh +# (flare-edge M9 soak-lite). Feeds request lines on stdin, as the host rig +# does over the agent's serial port, and checks the framed answers: a script +# runs and answers its output in base64 with its exit code, chunks join into +# one script, a script past its deadline is killed with its children and +# answers 124, and malformed requests answer one <> line and run +# nothing. +set -uo pipefail + +HERE="$(cd "$(dirname "$0")" && pwd)" +LIB="$HERE/../rootfs/etc/warden-lib.sh" +# Unquoted where it runs: WARDEN_TEST_SH may be "busybox sh", the rig's shell. +SH="${WARDEN_TEST_SH:-sh}" +WORK="$(mktemp -d)" || exit 1 +trap 'rm -rf "$WORK"' EXIT +FAIL=0 +CHECKS=0 + +check() { # check