Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1a28079
tools/lab: add isolated fresh environment harness
Cd1s Oct 17, 2026
99ccb18
tools/lab: record A1 verification results
Cd1s Oct 17, 2026
cbb2a51
tools/lab: keep veth pairs inside lab namespaces
Cd1s Oct 17, 2026
988b1a3
tools/lab: isolate LAN and add guest seed installation
Cd1s Oct 17, 2026
13e6873
tools/lab: add ISP slirp PPPoE hooks and live scenarios
Cd1s Oct 17, 2026
3b49179
tools/lab: record leakage and ISP milestones
Cd1s Oct 17, 2026
515ed0c
tools/lab: complete cloud-init guest handoff and fallback LAN services
Cd1s Oct 17, 2026
5615cb1
tools/lab: fix cloud-init seed YAML and document guest limits
Cd1s Oct 17, 2026
09e43f3
tools/lab: update A1 continuation report
Cd1s Oct 17, 2026
f0d4f49
tools/lab: seed Alpine router package layer for offline install
Cd1s Oct 17, 2026
ccf03a1
tools/lab: protect host resolver and namespace state
Cd1s Oct 17, 2026
93e7998
tools/lab: continue guest install with offline packages
Cd1s Oct 17, 2026
73f6393
tools/lab: add A1 handover notes
Cd1s Oct 17, 2026
abca8d9
tools/lab: run the real install.sh — signed offline apk mirror, virti…
Cd1s Oct 17, 2026
871e7d7
tools/lab: lts base, virtio agent console, PPPoE + DHCPv6-PD ISP, lab…
Cd1s Oct 17, 2026
b664685
Merge remote-tracking branch 'origin/main' into lab/fresh-env
Cd1s Oct 17, 2026
77f257d
tools/lab: no host daemon config or services, isp --gw, 03 for same-s…
Cd1s Oct 17, 2026
f93a2c8
tools/lab: README links the product fixes #147 #148
Cd1s Oct 17, 2026
facce94
tools/lab: review fixes — pinned downloads, image lock, pppd off the …
Cd1s Oct 17, 2026
78cd9a2
Merge remote-tracking branch 'origin/main' into lab/fresh-env
Cd1s Oct 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tools/lab/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
state/
release/
*.log
15 changes: 15 additions & 0 deletions tools/lab/PROGRESS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Lab progress

This file records milestones for the fresh-environment lab. Add one dated line after each
milestone and commit it with the corresponding implementation change.

- [x] 2026-10-17 image: Alpine 3.24 x86_64 cloud qcow2 downloaded on demand; per-lab qcow2 overlay/reset implemented.
- [x] 2026-10-17 topology: per-lab switch, ISP and LAN namespaces with QEMU taps; teardown removes mrlab-only objects.
- [x] 2026-10-17 host safety: per-namespace resolv.conf, private udhcpc script, host guard on /etc/resolv.conf, /etc/hosts, /etc/ppp.
- [x] 2026-10-17 real install: unmodified install.sh after boot, signed offline apk mirror, no apk stub or fallbacks (A1c).
- [x] 2026-10-17 product bug found: install.sh rolls back on every WiFi-less machine (mr-hostapd); fixed in PR #143.
- [x] 2026-10-17 lts base image (linux-virt has no PPPoE); virtio-serial agent console; PPPoE + kea DHCPv6-PD ISP.
- [x] 2026-10-17 scenarios 01–05 rewritten on scenarios/lib.sh: 01 6/6, 02 8/8, 04 8/8 ok; 03 7/9 and 05 5/6 report product issues.
- [x] 2026-10-17 merged main (#132 #134 #138 #141 #143); daemons never use host config/services (dnsmasq --conf-file=/dev/null, kea with a private /run).
- [x] 2026-10-17 found: #138 made every fresh install roll back (/run/mini-router missing); 03 reworked for #138 (modem .254 and .1). main + fix: 01 6/6, 02 8/8, 03 11/11, 04 8/8, 05 5/6.
- [x] 2026-10-17 review of PR #149: pinned SHA256 downloads, image lock + safe mounts/temp dirs, veth inside namespaces, pppd in a private /run, TERM-then-KILL by namespace, guard baseline + signals, scenario cleanup traps. 3 labs in parallel: 01 6/6, 02 8/8, 04 8/8.
181 changes: 181 additions & 0 deletions tools/lab/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# mini-router fresh environment lab

## Quick reference for test agents

Run as root on IN from the repository root. IDs 1–8 are independent labs and may run at the same
time. Every command checks the host's `/etc/resolv.conf`, `/etc/hosts` and `/etc/ppp/**` before
and after and fails loudly if anything changed.

```sh
L=tools/lab/lab.sh; ID=1
trap '$L down $ID' EXIT # always clean up

# 1. a new router: never-booted Alpine 3.24 + the real install.sh (2–3 min)
$L reset $ID # empty lab: namespaces + a fresh disk
$L isp $ID 1 dhcp # what the "ISP" offers (see ISP modes)
$L install $ID --wan-proto dhcp # ok / the install.sh error; LAN 192.168.1.1, admin labpass1
# --wan-proto pppoe|static --lan-ip 10.0.0.1/24 --no-apply (MR_APPLY=0)
# --tree /path/to/checkout test another branch (also MR_LAB_TREE=...)
# --release vX.Y.Z a published release instead of the working tree

# 2. change the ISP under the running router
$L isp $ID 1 pppoe v6=pd # PPPoE (lab/labpass) + DHCPv6-PD /60
$L isp $ID 1 dhcp --subnet 192.168.1.0/24 # a modem on the router's own LAN subnet
$L isp $ID 1 down # cable pulled

# 3. check from the LAN client (a netns behind the router's LAN port)
$L lan $ID dhcp # {"dhcp":"ok","addr":...} or exit 1
$L lan $ID check # {"ipv4","ipv6","dns","panel"}; exit 1 without IPv4+DNS
$L lan $ID exec 'curl -fsS http://example.com/ >/dev/null && ping6 -c2 2001:db8:1:1::1'

# 4. settings through the web UI API (logs in with labpass1, keeps the session cookie)
$L api $ID GET status
$L change $ID '[{"op":"set","path":"system.hostname","value":"r1"}]' # apply + confirm
$L change $ID '[{"op":"set","path":"dhcp.lease","value":"1h"}]' --no-confirm # leave it pending

# 5. inside the router, power
$L console $ID 'mr status; tail -20 /var/log/messages' # root shell, works with the network down
$L reboot $ID; $L powercut $ID # clean reboot / kill -9 QEMU and boot again

# 6. clean up
$L down $ID
ip -br link | grep -c mrlab # must print 0
```

Scenarios (TAP output, log in `/tmp/mrlab<ID>-<name>.log`): `$L run $ID 01-fresh-dhcp`,
`02-pppoe-pd`, `03-move-dhcp-samesubnet`, `04-powercut-confirm`, `05-isp-switch-live`.
Each starts with `reset` and removes its lab when it ends (passed, failed or interrupted;
`MRLAB_KEEP=1` leaves it up for inspection), so give every parallel scenario its own ID. A failed
setup step prints `Bail out!` and stops the scenario.

## Dependencies

Root on the IN build host with KVM. Install the **binaries only; do not enable their system
services** — every daemon is started by `lab.sh` inside a lab namespace with its own config, and a
host dnsmasq would listen on the host's port 53:

```sh
# masked first: the packages' postinst would otherwise start radvd / kea on the host for a moment
systemctl mask dnsmasq kea-dhcp6-server radvd
apt-get install --no-install-recommends qemu-system-x86 qemu-utils slirp4netns iproute2 nftables \
dnsmasq-base ppp pppoe radvd kea-dhcp6-server genisoimage python3 openssh-client curl jq busybox
```

`lab.sh up` refuses to start while one of those services runs (`MRLAB_ALLOW_HOST_SERVICES=1`
overrides). Kea needs only the package's `/etc/kea`
and `/var/lib/kea` directories (its AppArmor profile allows no other paths), which a private mount
namespace replaces with lab directories. Go comes from `/etc/profile.d/buildtools.sh`.

## What a lab is

```text
slirp4netns (user-mode NAT to the internet; no host route or rule)
| |
mrlab<ID>-isp1 netns mrlab<ID>-isp2 netns (second WAN, unused yet)
DHCP / PPPoE / static, DNS, radvd, kea (PD)
|
mrlab<ID>-sw netns: br-wan1, br-wan2, br-lan, QEMU taps
| eth0 (WAN)
+-----------+-----------------------+
| QEMU: Alpine 3.24, 512 MiB, 1 vCPU| console: ttyS0 -> state/mrlab<ID>/console.log
| | lab agent: virtio-serial "mrlab.agent"
+-----------+-----------------------+
| eth1 (LAN)
mrlab<ID>-lan netns: the user's computer (udhcpc, SLAAC, curl, ping6)
```

- **Guest**: the official Alpine 3.24 cloud image, booted once offline to add `linux-lts`
(`alpine-3.24-x86_64-lts.qcow2`, built automatically). `MR_LAB_KERNEL=virt` uses the cloud
image's `linux-virt` kernel instead — that kernel has no PPPoE (see "Known product findings").
`install` gives the lab a new disk over the read-only base, so every install is a first boot.
- **Install**: cloud-init only sets the root password (`labpass1`) and a lab SSH key, points apk at
the seeded mirror and starts the unmodified `install.sh` after OpenRC finished booting
(`guest/setup.sh`, `guest/install-run.sh`), with `MR_YES=1 MR_LOCAL=... MR_WAN=eth0
MR_LAN_PORTS=eth1`. Its output is on the serial console; the last line is
`MRLAB-INSTALL-RC=<n>`. Nothing in the guest is faked: no apk stub, no fallback DHCP or web server.
- **Packages**: `/root/build/mr-lab-images/apk-mirror` holds the official signed APKINDEX of
main and community plus every package `install.sh` installs (found by installing the base
image's world and install.sh's list into a scratch root; rebuilt when the list in `install.sh`
changes). The guest therefore needs no internet during the install, in every ISP mode.
- **Release**: `tools/release.sh` of the chosen tree, cached in `mr-lab-images/release-<hash>`.
- **Console**: `lab.sh console` talks to a root shell on a virtio-serial port (`guest/agent.sh`,
started at every boot from `/etc/local.d`). A serial UART was not reliable for this. Use it when
the router's network is down; `ssh` (LAN side, lab key) works when it is up.

## ISP modes

| `isp <id> <wan> ...` | ISP side | router WAN |
| --- | --- | --- |
| `dhcp [--subnet 192.168.1.0/24]` | dnsmasq DHCP + DNS on `10.<id>.<wan>.1/24`; with `--subnet 192.168.1.0/24` it also serves a modem page on 192.168.1.1 | DHCP |
| `static` | `10.<id>.<wan>.1/24` + DNS; `install --wan-proto static` uses `.2/24` | static |
| `pppoe [v6=slaac\|pd] [--badpass] [nopado] [--cgnat]` | pppoe-server, CHAP `lab`/`labpass` (`--badpass`: the server expects another password; `nopado`: no server at all; `--cgnat`: 100.64.<id>.x). After IPv6CP the ISP end is `2001:db8:<id>:<wan>::1/64`; `v6=slaac` adds RAs for that /64, `v6=pd` also kea DHCPv6 delegating /60s from `2001:db8:<id>:<wan>00::/56` | PPPoE |
| `down` | link down | — |

The ISP's own resolver files (`/etc/resolv.conf`, `/etc/ppp`) are per-namespace copies under
`/etc/netns/mrlab<ID>-*`; kea runs under the host's AppArmor profile in a private mount namespace
whose `/etc/kea`, `/run/kea`, `/run/lock/kea` and `/var/lib/kea` are lab directories. Its NAT
is an nftables table inside the ISP namespace only.

## Commands

```text
up|down|reset|status <id> lab lifecycle (down removes only mrlab<ID>* objects and state)
isp <id> <wan> <mode> [opts] ISP simulator (table above)
install <id> [opts] fresh disk + real install.sh; exit 1 with the installer's last lines
lan <id> dhcp|static [a/len [gw]]|check|exec <cmd>
console <id> <cmd> MRLAB_TIMEOUT=<s> (default 60)
ssh <id> <cmd> LAN-side SSH with the lab key (only when sshd runs)
api <id> GET|POST <action> [json] docs/api.md; MR_TOKEN=<token> uses a Bearer token instead
change <id> <patch> [--no-confirm] [--secrets json]
reboot|powercut <id>
run <id> <scenario>
mirror [tree] (re)build the apk mirror for that tree's install.sh
```

State lives in `tools/lab/state/mrlab<ID>/` (`console.log`, `agent.out`, `install/`, `isp1/`
with the dnsmasq/pppd/radvd/kea logs). Images and caches are in `/root/build/mr-lab-images/`
(about 1.5 GiB).

## Host safety

- No host bridge, route, nftables rule or sysctl is created; all addresses and daemons are inside
`mrlab<ID>-*` namespaces. slirp4netns gives the ISP namespace internet as a user-mode stack.
- Each namespace has its own `/etc/netns/<ns>/resolv.conf`; the LAN client's DHCP uses a private
`udhcpc -s` script that writes lab state only (the BusyBox default script would overwrite the
host's `/etc/resolv.conf`).
- pppd reads `/etc/netns/<isp>/ppp` (bind-mounted over `/etc/ppp` by `ip netns exec`), never the
host's `/etc/ppp` scripts. The first PPPoE run loads the host's `ppp_generic` module (via
`/dev/ppp`), which is the only host-kernel side effect.
- pppoe-server and its pppd run in a private mount namespace with their own tmpfs `/run`
(`/run/ppp0.pid`, `/run/pppd2.tdb` never reach the host); kea likewise. Lab processes are stopped
with TERM first (pppd cleans up), KILL after 3 s, and only processes inside the lab's namespaces
or ones whose name matches their pid file are signalled.
- veth pairs are created inside the lab namespaces (`ip -n <sw> link add … peer … netns <ns>`),
never in the host's.
- The host guard compares every command's before/after manifest and a baseline kept across commands
(`state/.host-guard/baseline`), so a lab daemon changing a host file between two commands stops
the next one. Ctrl-C / TERM still run it. If the change was yours: `lab.sh guard-reset`.
- Shared images are initialised under a lock (`mr-lab-images/.build.lock`), with temp directories from
`mktemp` and loop mounts that are unmounted on every exit.
- After a run: `sha256sum /etc/resolv.conf` unchanged and `ip -br link | grep -c mrlab` = 0.

### Downloads

The cloud image and the minirootfs used for the package chroot are fixed versions with pinned SHA256s
in `lab.sh` (checked against Alpine's published `.sha512` / `.sha256` when pinned); a mismatch stops
the command. apk in the chroot checks every APKINDEX signature against the minirootfs's keys and every
package against the index; the indexes copied into the mirror must be byte-identical to those it
verified, and the guest's apk verifies them again with the cloud image's keys.

Never point the lab at a real LAN or a household device.

## Known product findings

- `install.sh` rolled back on every machine without WiFi (`mr-hostapd` restarted) — fixed in #143.
- Since #138 the first apply of `install.sh` rolls back on every fresh machine: `network.sh` writes
`/run/mini-router/modem-aliases` before `mr-network` ever created the directory — PR #147.
Until it is merged run the lab with `MR_LAB_TREE=<checkout of that branch>`.
- Alpine's `linux-virt` kernel has no PPPoE: without a check `install.sh` waited 90 s and rolled
back — PR #148 (`MR_LAB_KERNEL=virt` reproduces it).
- `05-isp-switch-live` check 5: when the line moves from PPPoE to DHCP, `mr doctor` only reports
"no IPv4 address on pppoe-wan" (no "no PPPoE server answers" / "DHCP answers on this port").
23 changes: 23 additions & 0 deletions tools/lab/guest/agent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh
# Lab control channel: a root shell on the virtio-serial port "mrlab.agent" (host side:
# state/mrlab<ID>/agent.in and agent.out). Started at every boot from /etc/local.d; lab.sh console
# sends commands framed by markers. Not a tty, so the shell is non-interactive and nothing echoes.
port=
for p in /sys/class/virtio-ports/*; do
[ "$(cat "$p/name" 2> /dev/null)" = mrlab.agent ] && port=/dev/${p##*/}
done
[ -c "$port" ] || exit 0
if [ -s /run/mrlab-agent.pid ] && kill -0 "$(cat /run/mrlab-agent.pid)" 2> /dev/null; then exit 0; fi
(
export PATH=/usr/sbin:/usr/bin:/sbin:/bin HOME=/root
cd /root || exit 1
# a virtio port can be opened only once: one read-write descriptor for input and output
while :; do
exec 3<> "$port"
echo "MRLAB-AGENT-READY $(cat /proc/sys/kernel/random/boot_id)" >&3
sh <&3 >&3 2>&3
exec 3>&-
sleep 1
done
) < /dev/null > /dev/null 2>&1 &
echo $! > /run/mrlab-agent.pid
17 changes: 17 additions & 0 deletions tools/lab/guest/install-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
# Runs the real install.sh with the lab's answers once OpenRC finished the default runlevel.
# Output goes to /root/mr-lab-install.log and the first serial console; the last line is
# MRLAB-INSTALL-RC=<exit code>.
i=0
while [ "$i" -lt 600 ]; do
[ "$(cat /run/openrc/softlevel 2> /dev/null)" = default ] && [ -z "$(ls /run/openrc/starting 2> /dev/null)" ] && break
sleep 1
i=$((i + 1))
done
sleep 2
cd /root || exit 1
{
env -i PATH=/usr/sbin:/usr/bin:/sbin:/bin HOME=/root TERM=vt100 sh -c \
'set -a; . /root/mr-lab-release/request.env; set +a; exec sh /root/mr-lab-release/install.sh'
echo "MRLAB-INSTALL-RC=$?"
} < /dev/null 2>&1 | tee /root/mr-lab-install.log > /dev/ttyS0
15 changes: 15 additions & 0 deletions tools/lab/guest/prep-lts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#cloud-config
# lab.sh ensure_lts_base: one offline boot of the cloud image that adds the linux-lts kernel from the
# seeded mirror, then leaves the image as if it had never booted.
runcmd:
- |
mkdir -p /media/prep && mount -t iso9660 -o ro /dev/vdb /media/prep
cp /etc/apk/repositories /etc/apk/repositories.orig
printf '%s\n' /media/prep/apk-mirror/v3.24/main /media/prep/apk-mirror/v3.24/community > /etc/apk/repositories
apk add linux-lts linux-firmware-none && echo MRLAB-PREP-OK > /dev/ttyS0
mv /etc/apk/repositories.orig /etc/apk/repositories
umount /media/prep
cloud-init clean --logs
rm -f /etc/ssh/ssh_host_*
sync
poweroff
25 changes: 25 additions & 0 deletions tools/lab/guest/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh
# First boot only (cloud-init runcmd): install the lab channel, point apk at the seeded mirror and
# start install.sh in the background so that it runs after the boot finished, like a user would.
set -u
SEED=/media/mrlab
mkdir -p "$SEED"
mountpoint -q "$SEED" || mount -t iso9660 -o ro /dev/vdb "$SEED"
install -D -m 0755 "$SEED/lab/agent.sh" /usr/local/sbin/mrlab-agent
install -D -m 0755 "$SEED/lab/install-run.sh" /usr/local/sbin/mrlab-install
mkdir -p /etc/local.d
printf '#!/bin/sh\nexec /usr/local/sbin/mrlab-agent\n' > /etc/local.d/mrlab-agent.start
chmod 0755 /etc/local.d/mrlab-agent.start
rc-update add local default > /dev/null 2>&1
install -D -m 0600 "$SEED/lab/id_lab.pub" /root/.ssh/authorized_keys
/usr/local/sbin/mrlab-agent
# The ISP simulator has no internet path in every mode (PPPoE, ISP down), so the guest installs from
# a local copy of the Alpine repositories: official signed APKINDEX files plus the needed packages.
rm -rf /var/lib/mrlab-mirror /root/mr-lab-release
cp -a "$SEED/apk-mirror" /var/lib/mrlab-mirror
printf '%s\n' /var/lib/mrlab-mirror/v3.24/main /var/lib/mrlab-mirror/v3.24/community > /etc/apk/repositories
cp -a "$SEED/release" /root/mr-lab-release
umount "$SEED" 2> /dev/null
grep -q '^MRLAB_RUN=0' /root/mr-lab-release/request.env && exit 0
setsid /usr/local/sbin/mrlab-install < /dev/null > /dev/null 2>&1 &
exit 0
Loading
Loading