diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f643826..f4771c0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -155,6 +155,14 @@ jobs: run: bash qemu/tests/init-power-signals-test.sh - name: rig init daemon order matches the panel (flare-edge#348) run: bash qemu/tests/init-daemon-order-test.sh + - name: rig bus starts when the payload carries a broker (flare-edge#1754) + # A stub broker stands in for mosquitto (the runner has none), and + # once more under busybox sh, the rig's own shell. + run: | + bash qemu/tests/init-rig-bus-test.sh + if command -v busybox >/dev/null; then + WARDEN_TEST_SH="busybox sh" bash qemu/tests/init-rig-bus-test.sh + fi - name: rig control bridge waits out a slow UI (flare-edge#1448) run: bash qemu/tests/ctl-bridge-test.sh - name: mkimage.sh SEED_DIR regression test diff --git a/qemu/README.md b/qemu/README.md index 71443fc..995a3ec 100644 --- a/qemu/README.md +++ b/qemu/README.md @@ -73,6 +73,11 @@ daemons a bench panel already runs: flare-edge `tools/rig-pull-daemons.sh chardev without `--rs485`), so `warden-modbus` polls a port with nothing on it, as on a panel with an empty header, and never the control channel. +A `mosquitto` in the payload starts the panel's device bus first (socket +`/run/warden/bus/bus.sock`, one credential file per bus user under +`/run/warden/bus/cred/`), as S10bus does before every bus client on a panel; +see `payload/README.md`. The rig broker takes any user and loads no plugin. + The VT cursor is kept off (`vt.global_cursor_default=0`): fbcon shares the virtio-gpu framebuffer with warden-ui and its blinking cursor would otherwise show up in screendumps at random (issue #18). @@ -107,6 +112,7 @@ All take the virt-fragment ``; `FLARE_EDGE=` where noted. | `test-ui-drive-rs485.sh` | - | offline, no VM: a fake `run.sh` and (for one case) a fake `socat` stand in so `ui-drive.sh --rs485-devices`'s startup fails closed instead of printing a false "== rs485 simulator: ..." over a bus nothing is serving, for each of a qemu rs.sock that never appears, a socat that never links rs.pty, and an mbsim.py that dies before opening rs.ctl; also proves `cleanup()` escalates to SIGKILL for a sim process that ignores SIGTERM | | `run-sh-args-test.sh` | - | offline, no VM: a fake `qemu-system-arm` captures run.sh's own argv and pins the ctl/rs485 pci-serial ordering contract rootfs/sbin/init's ttyS0-vs-ttyS1 alias depends on -- the ctl device always enumerates before rs485, warden.ctl only lands on the cmdline when --ctl is given, and an rs485 pci-serial device (real or null-backed) is always present so the port count never shifts | | `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) | +| `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) | | watchdog (`run.sh --watchdog`) | - | arm `/dev/watchdog`, don't pet: the VM resets ~30 s later (verified) | Scenario fine print: diff --git a/qemu/payload/README.md b/qemu/payload/README.md index 1ef464c..903723d 100644 --- a/qemu/payload/README.md +++ b/qemu/payload/README.md @@ -26,6 +26,13 @@ musl binary, so flare-edge `tools/rig-pull-daemons.sh --host ` copies release and each md5 in `PULLED.txt`), minus `warden-ui`, which the rig needs as the fbdev+evdev build. +A `mosquitto` here (the static armhf broker of flare-edge +`tools/mosquitto-build.sh armhf`) gives the guest the panel's device bus: +stage-2 init starts it on `/run/warden/bus/bus.sock` before any daemon and +writes a credential file per bus user to `/run/warden/bus/cred/` +(`warden_rig_bus` in `qemu/rootfs/etc/warden-lib.sh`; the broker log is +`/tmp/mosquitto.log`). Without it the guest has no bus, as before. + Stage-2 init starts every `warden-*` daemon it finds here, in the panel's own S-number order (ai, automation, modbus, mikrotik, asic, starlink, stratum, flared), then `warden-ui` (the UI also needs `--display diff --git a/qemu/rootfs/etc/warden-lib.sh b/qemu/rootfs/etc/warden-lib.sh index 4bbc5e4..ac617b0 100644 --- a/qemu/rootfs/etc/warden-lib.sh +++ b/qemu/rootfs/etc/warden-lib.sh @@ -148,3 +148,94 @@ _wcb_wait_reply() { _wcb_n=$((_wcb_n + 1)) done } + +# The rig's device bus (flare-edge #1754, the #1050 QEMU half). A panel runs +# one mosquitto on /run/warden/bus/bus.sock (flare-edge S10bus) and every +# daemon, the UI included, talks over it. The rig starts the same broker +# when the payload carries one (/usr/bin/mosquitto, the static armhf build of +# flare-edge tools/mosquitto-build.sh); a payload without it boots exactly as +# before, with no bus. +# +# Same paths as the panel, so the production clients need no rig switch: +# the socket at /sock/bus.sock with the /bus.sock link clients +# dial, and one credential file per bus user at /cred/, which a +# client reads at every CONNECT. What the rig leaves out, on purpose: the +# password file and ACL (the broker takes any user; the rig is a test +# instrument and every process in it runs as root anyway) and the retain gate +# plugin (a static broker loads no plugins). The credential files hold a +# fresh random secret all the same, so a client finds what it expects. +# +# WARDEN_RIG_BUS_USERS lists the bus users that get a credential file: the +# panel's /etc/warden/bus/users and bundled-users (flare-edge busgen output). +# WARDEN_RIG_BROKER, WARDEN_BUS_RUN, WARDEN_RIG_BUS_LOG and +# WARDEN_RIG_BUS_WAIT_S exist for the host test only. +warden_rig_bus() { + _wrb_broker="${WARDEN_RIG_BROKER:-/usr/bin/mosquitto}" + [ -x "$_wrb_broker" ] || return 0 + _wrb_dir="${WARDEN_BUS_RUN:-/run/warden/bus}" + _wrb_log="${WARDEN_RIG_BUS_LOG:-/tmp/mosquitto.log}" + _wrb_users="${WARDEN_RIG_BUS_USERS:-flared ui io modbus appd telemetry automation.main}" + if ! mkdir -p "$_wrb_dir/cred" "$_wrb_dir/sock"; then + echo "init: rig bus FAILED: cannot create $_wrb_dir" + return 1 + fi + chmod 0751 "$_wrb_dir" + chmod 0710 "$_wrb_dir/cred" + # The panel's broker settings (flare-edge + # overlay-flare-warden-ui/etc/warden/bus/mosquitto.conf) minus the + # password file, ACL and plugin lines (see above). `user root` keeps the + # broker from dropping to an account the rig rootfs does not have. + cat > "$_wrb_dir/mosquitto.conf" < "$_wrb_dir/cred/$_wrb_u") + chmod 0400 "$_wrb_dir/cred/$_wrb_u" + done + ln -sfn sock/bus.sock "$_wrb_dir/bus.sock" + ln -sfn sock/mosquitto.pid "$_wrb_dir/mosquitto.pid" + "$_wrb_broker" -c "$_wrb_dir/mosquitto.conf" > "$_wrb_log" 2>&1 & + _wrb_pid=$! + # Clients retry a missing socket, but a daemon started before the broker + # listens logs a refused first dial: wait for the socket (0.1 s polls, + # bounded) so the payload daemons find it on their first try. + _wrb_n=0 + _wrb_limit=$(( ${WARDEN_RIG_BUS_WAIT_S:-10} * 10 )) + while [ ! -S "$_wrb_dir/sock/bus.sock" ]; do + if ! kill -0 "$_wrb_pid" 2>/dev/null; then + echo "init: rig bus FAILED: the broker exited; log tail:" + tail -n 5 "$_wrb_log" 2>/dev/null + return 1 + fi + if [ "$_wrb_n" -ge "$_wrb_limit" ]; then + echo "init: rig bus FAILED: no socket after $((_wrb_limit / 10)) s" + return 1 + fi + sleep 0.1 + _wrb_n=$((_wrb_n + 1)) + done + echo "init: rig bus up on $_wrb_dir/bus.sock (broker pid $_wrb_pid)" +} diff --git a/qemu/rootfs/sbin/init b/qemu/rootfs/sbin/init index 6030248..c5ade96 100755 --- a/qemu/rootfs/sbin/init +++ b/qemu/rootfs/sbin/init @@ -108,6 +108,11 @@ export WARDEN_HARD_RESET=0 # Production init scripts create this volatile socket directory before their # daemons start. The compact QEMU rootfs must provide the same contract. mkdir -p /run/warden +# The device bus, when the payload carries a broker (warden_rig_bus): up +# before the daemons, as S10bus is before every bus client on the panel. A +# failed start is logged and the boot goes on: the clients keep dialing, and +# a flow that needs the bus fails on its own checks. +warden_rig_bus || true # OTA apply is opt-in per boot (run.sh --allow-apply): writing rootfs_b is # safe inside disk.img but must never be the default posture. if grep -qw warden.fwapply /proc/cmdline; then diff --git a/qemu/tests/init-rig-bus-test.sh b/qemu/tests/init-rig-bus-test.sh new file mode 100755 index 0000000..fbdb7a7 --- /dev/null +++ b/qemu/tests/init-rig-bus-test.sh @@ -0,0 +1,176 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2317 # the checks and the cleanup run through check() and trap +# Host test for the rig's device bus, warden_rig_bus in +# qemu/rootfs/etc/warden-lib.sh (flare-edge #1754, #1050). Checks: +# - a payload without a broker boots as before: nothing created, status 0 +# - with a broker: the panel's layout (sock/bus.sock and the bus.sock link, +# one 0400 credential file of 64 hex digits per bus user, each its own), +# a config with the listener and no password file, ACL or plugin +# - a broker that exits, or never listens, is reported and returns 1 +# within the wait bound +# - a user name that is not a plain file name gets no file +# - /sbin/init starts the bus before the payload daemons +# A stub broker (python3, binds the configured listener) stands in for +# mosquitto. With WARDEN_TEST_MOSQUITTO_DIR set to a directory holding +# mosquitto, mosquitto_pub and mosquitto_sub (flare-edge +# tools/build-host-mosquitto.sh), the real broker also carries one message +# between two users that log in with their credential files. +set -uo pipefail + +HERE="$(cd "$(dirname "$0")" && pwd)" +LIB="$HERE/../rootfs/etc/warden-lib.sh" +INIT="$HERE/../rootfs/sbin/init" +SH="${WARDEN_TEST_SH:-sh}" +WORK="$(mktemp -d)" || exit 1 +cleanup() { + # The brokers outlive the shell that started them, as on the rig; each + # has this test's folder in its argv (the stub that never listens writes + # its pid instead). + pkill -f -- "$WORK/" 2>/dev/null + [ -f "$STUB/mute.pid" ] && kill "$(cat "$STUB/mute.pid")" 2>/dev/null + rm -rf "$WORK" +} +trap cleanup EXIT +STUB="$WORK/stubs" +mkdir -p "$STUB" +FAIL=0 + +check() { # check