Skip to content

feat: run OCI containers as lab devices - #45

Open
Frando wants to merge 1 commit into
feat/userns-rootfrom
feat/containers
Open

Frando wants to merge 1 commit into
feat/userns-rootfrom
feat/containers

Conversation

@Frando

@Frando Frando commented Jul 29, 2026 •

Copy link
Copy Markdown
Member

Description

Integration tests written against patchbay often need an auxiliary service next to the code under test: an ACME test server like Pebble, a database, a real DNS server. These are usually easier to run as a container than to embed. This lets patchbay run one as an ordinary lab node.

A container is a device. Lab::add_container(name, image) returns a builder that mirrors the device builder for wiring (uplink, iface, default_via, mtu) and adds container configuration on top. It creates the device namespace and veth uplink like any device, then runs the image joined to that namespace, so the service is reachable from other lab devices at the container's lab IP.

let pebble = lab
    .add_container("pebble", "ghcr.io/letsencrypt/pebble:latest")
    .uplink(net.id())
    .env("PEBBLE_VA_NOSLEEP", "1")
    .volume_ro(config_dir.join("pebble.json"), "/test/config/pebble.json")
    .args(["-config", "/test/config/pebble.json"])
    .ready_tcp(14000)
    .build()
    .await?;

// Reachable from any lab device.
let directory = format!("https://{}:14000/dir", pebble.ip().unwrap());

The returned Container dereferences to Device and adds exec, logs, stop, write_file/read_file and copy_to/copy_from. Volumes come in read-write, read-only and tmpfs forms. The container is removed when the last handle drops.

Runtime and requirements

Containers run with podman, which must be on PATH. podman is daemonless, so podman run, forked from the device's namespace worker, joins that namespace with --network=host. Docker cannot do this rootlessly: its daemon runs in a separate namespace.

The host needs the standard rootless-podman setup: newuidmap/newgidmap on PATH and an /etc/subuid and /etc/subgid entry for the user. Without it, build() fails with an error that names these requirements.

User namespaces

Builds on #94: init_userns() keeps the caller's uid and maps the subordinate range (0 to the first subordinate id, the rest to itself).

podman runs in a user namespace nested in patchbay's, laid out like podman's own rootless one: root is the host user, and 1.. are the subordinate ids in host order. Every podman call joins it as root, with _CONTAINERS_USERNS_CONFIGURED, _CONTAINERS_ROOTLESS_UID and _CONTAINERS_ROOTLESS_GID set, which is the state after podman's own rootless re-exec. So podman uses the user's normal rootless storage and images, while the patchbay process keeps its uid for the session D-Bus or PipeWire portals.

Two details follow from the nesting:

  • Device workers bind-mount over /proc/<pid>/net. The nested namespace would lock that mount, and the kernel then refuses the container's proc mount. The podman child detaches it before it joins.
  • Container root has no capabilities in the lab's network namespaces, so a container cannot change routes or firewall rules. build() sets ip_unprivileged_port_start and ping_group_range in the device so services can still bind low ports and ping.

podman cannot set up this namespace itself: its rootless re-exec calls newuidmap, whose setuid bit has no effect inside patchbay's namespace. A supported flag for "run in an already-configured user namespace" is the open request podman-container-tools/podman#7774.

Cleanup

Containers carry a patchbay.owner=<pid>:<start time> label. The first build() in a process removes containers whose owner is gone, so a SIGKILL or Ctrl-C does not leak them for good. A failed podman run removes its container.

Notes

The API borrows its shape from testcontainers-rs (bind mounts with an access mode, tmpfs, copy in and out) but stays smaller: ready_tcp covers the common readiness case.

A container gets a copy of the device's /etc/resolv.conf at start, so the lab DNS server works if it exists by then. Device::set_host entries do not reach the container.

Testing

  • Unit tests: run arguments, mounts, name sanitizing, id-map parsing, the nested map layout, ping_group_range, and orphan detection.
  • Lab tests (skipped without podman): nginx reachable from another device with root-owned image files, a read-only bind mount, and file transfer.
  • No containers are left running after the suite, and files podman writes to the user's storage stay owned by the user.

🤖 Generated with Claude Code

Integration tests often need an auxiliary service next to the code under
test: an ACME test server like Pebble, a database, a real DNS server.
`Lab::add_container(name, image)` runs one as a lab device. The builder
mirrors the device builder for wiring and adds env, args, volumes, tmpfs,
run_arg, ready_tcp and runtime. The returned `Container` derefs to
`Device` and adds exec, logs, stop, write_file/read_file and
copy_to/copy_from. It is removed when the last handle drops.

Containers run with podman, forked from the device's namespace worker,
so `--network=host` joins the device's network namespace. Docker cannot
do this rootlessly, because its daemon runs in a separate namespace.

podman runs in a user namespace nested in patchbay's, laid out like its
own rootless one: root is the host user, 1.. the subordinate ids in host
order. Every podman call joins it as root with the
_CONTAINERS_USERNS_CONFIGURED/ROOTLESS_UID/ROOTLESS_GID variables set,
the state after podman's own rootless re-exec. podman therefore uses the
user's normal rootless storage and images, while patchbay keeps the
user's uid. podman cannot build this namespace itself: its re-exec calls
newuidmap, whose setuid bit has no effect inside patchbay's namespace.

The podman child detaches the device worker's /proc/<pid>/net bind
mount before it joins the nested namespace. That namespace would lock
the mount, and the kernel then refuses the container's proc mount.
Container root has no capabilities in the lab's network namespaces, so
build() sets ip_unprivileged_port_start and ping_group_range in the
device.

Containers carry a patchbay.owner=<pid>:<start time> label. The first
build() in a process removes containers whose owner is gone, so a
SIGKILL or Ctrl-C does not leak them for good. A failed `podman run`
removes its container.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Frando
Frando changed the base branch from main to feat/userns-root November 23, 2026 15:38

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant