Skip to content

Architecture ​

This page explains how cairn is built: the crates, the processes it starts, which of them hold privilege, how the code is layered, what is written to disk, and what one cairn deploy does. Read concepts first if the words app, machine and network are new. For the threat model see security.

The idea in one picture ​

cairn turns an OCI image into a root disk, boots it as a Firecracker microVM, and puts the VM on a private network. Everything runs as you. Two small programs are privileged, and each does one job.

                          you
                           |
                      cairn (CLI)  ------------------------------+
                           |                                     |
          starts (direct or via systemd --user)                  | talks to
                           v                                     v
                cairn __supervise  (one per machine)      cairn-netd (one per network)
                  |      |      |                            |         |
      pipes/vsock |      |      | cairn-helper (setuid,      |         | starts
      events      |      |      |   CAP_NET_ADMIN)           |         v
                  |      |      +--> veth, nft, reservations |    cairn-egress
                  |      |                  ^                |    (DNS + TLS/HTTP gate,
                  |      | cairn-jail (setuid root)          |     no capabilities)
                  |      v                                   |
                  |   jailer --> firecracker  (inside the network's namespace)
                  |                  |
                  |                  | virtio: disk, vsock, tap
                  v                  v
             events <-------- guest: cairn-init (PID 1) --> your workload

The crates ​

The workspace has 16 crates. Each seam between them is a privilege boundary or a trust boundary, not a matter of taste. The list below follows the header of Cargo.toml.

CrateWhat it is
cairnThe CLI and the library behind it. Unprivileged. Owns desired state and orchestration. Never writes to stdout from library code.
cairn-netdThe per-network agent. Unprivileged: it is root only inside the user and network namespace it creates for itself.
cairn-egressA network's egress gate: a DNS filter and a TLS/HTTP relay. Drops every capability before it serves.
cairn-helperThe privileged network program: setuid to its own account with CAP_NET_ADMIN and CAP_NET_BIND_SERVICE. Small on purpose.
cairn-jailThe setuid-root launcher. Builds a jail and becomes the Firecracker jailer.
cairn-initThe guest's PID 1. A static musl binary that cairn embeds in itself.
cairn-nsThe only crate that calls the kernel through unsafe: ten one-operation wrappers (namespaces, ioctls, fd duplication, one libc call), plus one Ioctl trait implementation, each behind its own #[expect(unsafe_code)].
cairn-protoThe vocabulary the privileged and unprivileged programs agree on: names, addresses, requests, refusals, installed paths.
cairn-guest-protoThe guest protocol (events, exec, checks, signals, users, volumes). Only cairn and cairn-init link it, so the privileged programs do not.
cairn-secure-fileThe one loader for root-owned files: open once, O_NOFOLLOW, check the descriptor, bounded read.
cairn-confThe parsers and loaders for /etc/cairn/net.conf and /etc/cairn/vmm.conf. Shared by the two privileged programs and cairn admin check-config.
cairn-accessThe cairn group gate and the maintenance barrier.
cairn-extractThe tar extractor and the confined tree handle for untrusted image content.
cairn-syncThe wire and file transactions of the cairn run sync. With the host feature, the host's sync engine.
cairn-durableCrash-safe file writes. std only.
cairn-genDev-only generator for the systemd units and cron file (make gen). Never installed.

Two rules shape the dependency graph. unsafe is forbidden everywhere (Cargo.toml sets unsafe_code = "forbid") except in cairn-ns and in cairn-init's sys.rs, which do not inherit the workspace lints. And the privileged programs keep short dependency lists: cairn-jail links no clap, no tokio and no libc.

The processes ​

ProcessRuns asStarted byLives
cairnyouyour shellone command
cairn __superviseyoucairn (direct launcher) or systemd --useras long as its VMM
cairn-netd serveyou (root only inside its own namespace)cairn (any command that brings the network up) or the network's systemd user unit, cairn-net-<network>.service, which runs a watcher that brings it upas long as the network is up
cairn-egressyou, no capabilitiescairn-netdwith the network, if it has an egress list
cairn-helperits setuid account, with two capabilitiescairn or the supervisor, per requestone request
cairn-jailroot (setuid)the supervisor, per bootuntil it execs the jailer
jailer, then firecrackeryou, in a chrootcairn-jailas long as the machine
cairn-initroot in the guestthe guest kernelas long as the guest

The supervisor. One cairn __supervise runs per machine. It is the only thing that starts or stops a VMM. It receives the guest's events, decides what to report, applies the restart policy and writes the machine's run record. It detaches with setsid. A machine can also run under a systemd user unit; the supervisor is the same program in both cases. Source: machine/supervisor/.

The network agent. A network is one network namespace, kept alive by one cairn-netd. The agent creates a user and network namespace for itself, so its root means nothing on the host. It holds every machine's tap as an open file descriptor and holds the host ports you publish as reservation sockets. The namespace lives exactly as long as the process. A veth pair joins the namespace to the host; cairn-helper owns the host end. Source: cairn-netd, net/ in cairn.

Firecracker. Firecracker is started inside the network's namespace. The supervisor opens the namespace of the network (it receives a descriptor for it from cairn-netd) and hands it to cairn-jail as file descriptor 3. cairn-jail checks that it is a network namespace that is not the host's and that you own, joins it with setns, then checks the request, builds a root-owned jail (a tmpfs root with your machine directory mounted at /vm), and executes the pinned Firecracker jailer. The jailer executes Firecracker as you. The pid the supervisor spawned is the VMM's pid at the end.

The guest. The guest kernel runs cairn-init as PID 1. It mounts the disks, reads the machine's config drive, starts your workload, reaps orphans, runs health checks, serves cairn exec and cairn console over vsock, and reports events to the host. Source: cairn-init/src.

Privilege boundaries ​

Only two programs run with more than your own rights, and neither is a verb of the other.

  • cairn-jail is setuid root. The Firecracker jailer needs CAP_SYS_ADMIN, CAP_MKNOD, CAP_CHOWN, CAP_SETUID, CAP_SETGID and CAP_SYS_CHROOT, and that set is root. So the program is as small as it can be: one verb per argument, a request that names no uid, path or program, a fixed environment, and exactly one place that spawns a program (checked by make audit-jail).
  • cairn-helper is setuid to a dedicated cairn-helper account and carries CAP_NET_ADMIN and CAP_NET_BIND_SERVICE as file capabilities. It creates cv-* links, writes one nftables table (table inet cairn), creates reservation sockets for published ports and deletes its own conntrack entries. The list of verbs is closed.
  • The cairn group gate (cairn-access). Both privileged programs refuse a caller who is not root and not a member of the cairn group. The group is read from /etc/group and /etc/passwd on every call, so usermod -aG cairn alice takes effect at once and gpasswd -d alice cairn revokes at once. A primary-group-only membership is refused with a message that names the fix.
  • The maintenance barrier, also in cairn-access, is a root-owned mark and lock. While it is set, no new machine or network can be created, so an administrator can remove or upgrade the package on a quiet host. Cleanup verbs still work.
  • Root-owned policy files. /etc/cairn/net.conf (read by the helper) and /etc/cairn/vmm.conf (read by the jail) go through cairn-secure-file: opened once without following links, checked on the descriptor, and refused unless root owns them and no one else can write.

security says what this buys you.

The machine/ layering ​

Everything about one machine lives in crates/cairn/src/machine/. The modules form a strict ladder. A module may use its own layer, any lower layer and the base, never a higher one. A test in machine/layering.rs enforces this.

LayerModulesWhat it is
baseerror, timing, clock, live, proc, apiLeaves: the error type, every lifecycle deadline, host time, process identity and liveness, the process table and cgroup freezer, and the client for Firecracker's api.sock.
1recordsThe on-disk records and their stores: ids, store, spec, run, deploy, lock, restart, health, vmjson.
2state, readout, statusReading what is true now from the records and two processes: a machine's state (with the start gate), its health, and the status document. No API call and no systemd query.
3supervisorThe per-VM supervisor and what only it uses: owners, the event channel, the launcher, boot files, tracking, logs.
4commandsThe user-facing operations: start, pause, resume, stop, destroy, cairn status, cairn ps.

The library rules that go with it: no function blocks without a timeout, every error has a sentence, and nothing in the library writes to stdout. The CLI renders values.

On-disk records ​

State is split between your home (CAIRN_HOME, ~/.cairn by default) and two root-owned trees. See configuration for the full layout.

apps/<app>/
  app.json            current machine, network, published ports, hold
  deploy.json         only while a deploy (or its cleanup) is unfinished
  records/<id>.spec.json   the machine's spec: the only input to a boot
  records/<id>.run.json    what the supervisor did, written only by it
  records/<id>.control.json  a pause or resume request for the supervisor
  records/<id>.json        pid, start time, boot id: who `stop` may signal
  machines/<id>/      the machine directory: only what the VMM needs
                      (rootfs.ext4, config.ext4, vm.json, console.log,
                       firecracker.log, volume links, v.sock, api.sock)
networks/<name>.toml  desired state of a network
run/net/<name>/       the agent's directory: agent.sock, pending.json, agent.log
kernels/              pinned and custom kernels
/var/lib/cairn/net    the helper's ledger (root-owned area)
/var/lib/cairn/vmm    the jail's trees

The key rule: the machine directory belongs to the VMM. It is /vm inside the jail and writable by Firecracker, so nothing that decides what stop signals or what the next boot is built from lives there. Records sit outside it, mode 0700/0600. config.ext4 and vm.json are rebuilt from the spec before every boot and never read back. Records read from the machine directory (such as the console log) are treated as untrusted: O_NOFOLLOW, regular files only, size-bounded.

Stop, kill and cleanup identify processes by pid, start time and boot id, re-checked through a pidfd before any signal. They never act on an event from the guest.

What one cairn deploy does ​

The deploy is deploy/attempt.rs: a sequence of steps, each recorded in apps/<app>/deploy.json before its effects. Its VM and network steps go through two traits (Vm, Net), and the settlement's through an Effects seam, so the failure paths are tested without a VMM.

 1  app lock; settle a leftover deploy.json (below); refusals and preflight
 2  deploy.json <- building; get the image; join the network; build the machine
    directory: root disk, config drive, volumes, spec
 3  start fence: deploy.json <- stopping; stop the previous machine, confirm its exit
 4  deploy.json <- starting; app.json <- current = new machine (one write)
    release the fence
 5  launch it: its supervisor checks the fence and that it is current, attaches it to the
    network (address, taps, the app's published ports), then the VMM boots;
    wait for "[cairn] v1 started" -- readiness: TCP ports accept and checks pass --
 6  deploy.json <- accepted; start at boot; delete the previous machine; clean the build;
    remove deploy.json

A failure in building or stopping deletes the new machine, which never ran. A failure in starting rolls back under the start fence: it decides (for an app with volumes) whether the previous release may run on the data, writes rolling-back with that decision, makes the previous machine current again in one app.json write (with a hold, when the decision is to hold), stops and deletes the new machine, and then starts the previous machine again if this deploy stopped it. Once accepted is written the deploy succeeds; anything its cleanup could not confirm stays in deploy.json and is reported.

Settling (deploy/settle.rs) is the same rollback and cleanup, run from the record by whichever of cairn deploy, start, stop or destroy finds a deploy.json whose process is gone. It runs under the app lock, each step skips an effect already made (so a settlement killed half-way is simply run again), and it never launches a machine. See lifecycle for what you see.

Status (machine/state.rs) reads the records first (app.json, deploy.json, the spec, the record, the run file), then whether the run file's supervisor and VMM are still the processes it names, then the VMM's /proc state letter and its cgroup's freezer, and derives one state from a fixed table (lifecycle). It takes no lock and calls neither Firecracker nor systemd. A pause is in that table because cairn pause goes through the supervisor: the command writes records/<id>.control.json and signals the supervisor (SIGUSR1, through a pidfd checked against the recorded identity), and the supervisor pauses the VMM, confirms it, and records paused and its answer in the run file.

Where the pieces come in: the image is unpacked by cairn-extract into a tree and turned into an ext4 disk; the network step talks to cairn-netd and, through it, cairn-helper; the spawn step starts a supervisor, which calls cairn-jail; the guest's events come back over the vsock event channel.

The event channel and exec ​

The guest talks to the supervisor over a vsock port that Firecracker exposes as a Unix socket in the machine directory. Anything in the guest can connect, so the supervisor treats it as untrusted input: only the first connection of a boot counts, lines are at most 4096 bytes, and there is a cap on lifecycle events per boot. Events decide the reported state and restarts, never stopping or cleanup.

cairn exec and cairn console use a second vsock port served by an agent inside cairn-init. The guest accepts only connections whose peer is the host (CID 2). The protocol is in cairn-guest-proto.

The cairn run sync ​

cairn run creates an ordinary jailed app and keeps your working folder and the guest's copy in step. Both halves live in cairn-sync.

  • Wire and transactions (always built): wire is the framed, strictly sequenced codec; path opens the folder as a descriptor and resolves names with openat2 (RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS); journal and txn do replace, create and delete as journaled state machines with replay. The static cairn-init links only this part, and it is the side that runs as someone else, so a confused host cannot make it write outside the folder.
  • The host engine (feature host, enabled only by cairn): reconcile is the three-way file decision table; mass holds suspicious mass deletions; engine does one tick (observe both sides, decide every path, apply in dependency order); local writes the host folder only through txn; remote speaks the wire to the guest helper; state keeps sync/state.json, the baseline.
  • The guest side is cairn-init/src/sync.rs.

The session supervisor that wires it together is cairn/src/run/supervise.rs. See run.

Crash safety ​

Records are written by cairn-durable: write a temporary file in the same directory, fsync it, rename it into place, fsync the directory. A crash leaves the old file or the whole new one, never a torn one. The run file's health renewals are the one exception: atomic and fdatasynced, without the directory sync.

The same ideas run through the rest:

  • deploy.json and the start fence make a failed or killed deploy settleable: every step is recorded before its effects, and the rollback decides under the fence.
  • The sync journals each file operation before applying it and replays on recovery.
  • The jail marks a live tree with a root-owned marker (<boot id> <host pid> <start time>), so a stale tree is recognised after a crash and a reused pid is not mistaken for it.
  • The helper keeps a ledger, and its reconcile verb repairs the host after a SIGKILLed network agent.