sandkiln-vmm is where sandkiln’s host-side behaviour lives. The daemon is a thin HTTP wrapper over it: parse a request, call into this crate, shape a response. The crate’s own AGENTS.md states the placement rule plainly. New VM-lifecycle behaviour belongs here, not in the daemon.
It is also deliberately synchronous. There is no async runtime dependency, every blocking call is plain std, and the daemon bridges into it with spawn_blocking. That one decision explains several others in this tour: why the Firecracker API client is hand-rolled, why the tunnel acceptor uses std::sync::mpsc, and why retries are simple sleep loops.
This post is the crate-level tour. Driving Firecracker covers the boot sequence in depth, the create path and rootfs covers the clone, and networking covers the bridge. Here the focus is how the modules fit together, what each owns, and how they fail. Read the system overview first.
Public API surface
core/crates/vmm/src/lib.rs is twelve lines: nine public modules and one private one.
core/crates/vmm/src/lib.rs:3-12.) The one private module, firecracker_api, is an implementation detail of vm: a caller never talks to Firecracker’s API directly, only through Vm methods. Everything else is public, and the daemon uses most of it.
Vm’s public methods are the verbs the daemon uses: boot, resume, is_jailed, call, call_with_timeout, agent_endpoint, open_pty, open_exec_stream, update_metadata, pause, snapshot, stop, and force_stop. The type has no public fields. Its private fields are an id, the Firecracker Child handle, the API socket path, the vsock socket path, and an optional jail instance directory.
Module tour
The crate is about 3,000 lines across 13 source files. The split follows the rootAGENTS.md rule: files separate when they mix independently workable concerns.
vm/boot.rs and vm/snapshot.rs are submodules, not siblings, of vm. That is a visibility decision: they need to see Vm’s private fields, and keeping them in-tree lets those fields stay private while the public surface stays a short file.
The boot path
The guest kernel command line is built bykernel_command_line() (core/crates/vmm/src/vm/boot.rs:243): the fixed base console=ttyS0 reboot=k panic=1 pci=off, DEFAULT_KERNEL_ARGS = "quiet loop.max_loop=1" (boot.rs:238), ip= when networked, then VmConfig::extra_boot_args (vm/mod.rs:88) last so operators can override. Two unit tests in kernel_args_tests (boot.rs:344) cover the ordering. The defaults cut time to first command from 413 ms to 214.5 ms; see Guest boot time.
Vm::boot allocates an id from a process-wide AtomicU64, derives a console log path from it, runs the boot, and annotates any failure with that log path. The mechanics live in boot.rs, where the direct and jailed paths converge early. Both produce a BootTarget holding the child process and the paths Firecracker must be told about, so the configuration sequence never branches on jail versus direct.
The ordered list is built up front by configuration_requests and then issued in a loop. That structure is what makes each PUT individually timeable (the debug event vm boot phase breakdown reports them) and keeps ordering constraints readable in one place. One of those constraints is called out at the call site: MMDS configuration must come after the network interface exists.
core/crates/vmm/src/vm/boot.rs:287-288.) MMDS is configured as V2, the token-gated version, because a sandbox may run untrusted code that could otherwise reach an open metadata endpoint. Setting VmConfig::metadata without VmConfig::network is an error raised while building the list, after the process has spawned, so the failure path above kills it.
Cleanup on failure is explicit. If configuration or InstanceStart fails, boot kills and waits on the child, removes both sockets, and removes the jail instance directory:
core/crates/vmm/src/vm/boot.rs:73-80.) The comment above it names the stakes: a live orphaned process is a resource leak, and a half-built chroot with hard-linked kernel and rootfs copies is an information-disclosure surface. The same discipline applies in spawn_jailed, which removes the instance directory if linking or spawning fails partway.
Retry and the “is it listening yet” race
Two races exist at every boot: the API socket is not accepting the instant the process spawns, and the guest agent is not listening the instantInstanceStart returns. The API socket and the pty and exec-stream opens use one helper:
core/crates/vmm/src/vm/mod.rs:341-359.) The API socket retry starts at 200 microseconds and caps at 5 ms; the vsock retry starts at 1 ms and caps at 20 ms, with a 5 second overall deadline. The doc comment records the history: before this helper there were two separate fixed-sleep loops, and the API-socket one measured a flat ~20.1 ms on every boot, more than half of a ~34 ms boot, because it was a quantized sleep rather than real waiting. connect_api_with_retry also retries the connect, not the existence of the socket file. The file appears at bind(), a moment before listen(), so an existence poll can win that race and then get ECONNREFUSED.
Request/response calls to the agent use a sibling, retry_while, with the same schedule but a different stop rule. It takes an attempt that returns Result<T, CallError> and gives up immediately, deadline or not, when the error says request_sent:
core/crates/vmm/src/vm/mod.rs:332.) A plain backoff loop would retry any failure, which is correct for “is the socket listening yet” and wrong for “did the guest already accept my command”. The next section explains where that flag comes from.
Talking to the guest
vsock_client holds the host side of the protocol. Firecracker bridges guest vsock to a Unix domain socket: connect to the UDS, send CONNECT <port>\n, read OK <port>\n, and the rest of the connection is bridged to that guest port.
The request/response entry point reports not just that a call failed but whether the guest could have seen the request:
core/crates/vmm/src/vsock_client.rs:30-67, encode and write lines elided.) Each call opens a fresh connection. The two closures are the design: everything up to and including writing the frame maps to request_sent: false, everything after maps to true. A write that fails part-way still counts as unsent, because the agent discards an incomplete frame without executing it. IO_TIMEOUT (three seconds) bounds connecting, the handshake and the write, because a paused VM has halted vCPUs and a guest that never answers would otherwise hang a blocking call forever. The wait for the reply is separate: response_timeout replaces the read timeout once the request is out, and None waits indefinitely.
That split fixed a real correctness bug. Previously one three-second bound covered the reply too, and Vm::call retried any failure, so a user command that stayed silent for more than three seconds came back as an error after about 6.5 seconds and had run twice. Now a retry can only follow a failure where the request never left. CallError converts to io::Error through From, so callers outside the crate keep the plain error type. The open_pty and open_exec_stream variants apply the timeout only to the handshake and then clear it, since an idle terminal is not a hung connection. See the vsock timeout that could hang a stop for the original hang story.
Three entry points sit on top, differing only in who chooses the reply bound:
Vm::call(&Request)is the control-plane call. It waits at mostCONTROL_PLANE_RESPONSE_TIMEOUT(3 seconds) for the reply, which suitssync, health checks and tunnel setup, where a paused guest never answers.Vm::call_with_timeout(&Request, Option<Duration>)takes the bound from the caller.Vm::agent_endpoint()returns anAgentEndpoint, an owned,Clonehandle holding only the VM id and the vsock socket path.AgentEndpoint::call_with_timeoutdoes the retrying call. Because the handle references noVm, a caller can copy it out of a shared map, drop the lock that guarded the map, and then make a call that may take minutes. If the VM has been stopped meanwhile, the call simply fails. The daemon uses this for user commands withexec_timeout_secs(default 600) as the bound; see the daemon tour.
Stop, force stop and teardown ownership
stop and force_stop both route through one private function that takes a boolean:
core/crates/vmm/src/vm/mod.rs:248-257, tail elided.) A bare SIGKILL loses unflushed page-cache writes to attached drives, so stop asks the guest to sync first, best-effort. force_stop skips that for a VM that cannot answer, and the daemon uses it in two places. A failed post-resume health check uses it because the guest is already known dead. Snapshotting uses it because the VM is paused: a paused guest has halted vCPUs and can never reply to a sync, so the old stop() made every snapshot sit out a doomed call before the kill. Moving to force_stop took the end-to-end snapshot request from 6.8 s to 0.56 s (measured live 2026-11-24, directional, one shared dev box). The earlier figures in the crate’s AGENTS.md for the health-check case (about 10.3 s versus 5.4 s) predate the 3 second control-plane bound and were not re-measured for this post. Both consume self, so a stopped Vm cannot be reused: ownership makes “use after stop” a compile error.
Vm has no explicit state field. Its lifecycle is carried by which methods are valid and by Firecracker’s own state:
The daemon, not this crate, decides that a snapshot is followed by a stop (a force_stop, as above). Vm::resume is an associated function that builds a brand-new Vm from a snapshot, which is why the diagram has a second entry point. It always spawns Firecracker directly: the jailer covers Vm::boot only, because a jailed snapshot bakes in in-jail paths that would need relinking. The daemon refuses to snapshot a jailed sandbox rather than half-support it, and Vm::is_jailed is how it knows.
Network, egress and the jailer
NetworkManager hands out two things per VM: a tap device from a pre-created pool and a host octet from 2..=254 of a /24. The pool exists because creating a new tap device is a TUNSETIFF ioctl that needs real root, while the daemon runs with only ambient CAP_NET_ADMIN, which covers netlink operations on existing interfaces. lease is written to leave pools consistent on every failure path:
core/crates/vmm/src/network.rs:128-132.) If there is no free tap, the host octet already popped is pushed back; if attaching fails, both go back. attach_tap brings the link up, sets the bridge as master, and turns on bridge port isolation, which blocks sandbox-to-sandbox frames at layer 2. All three are fork+exec of ip or bridge, individually timed. reserve exists for the opposite problem: reconstructing a Lease for a tap held by a snapshot reconciled from disk at startup, so a later lease cannot hand the same tap to a second sandbox. NetworkManager deliberately does not track which sandbox holds which lease; the daemon owns that mapping.
egress::apply builds one iptables chain per sandbox and loads it with a single iptables-restore --noflush call. Rule order is deny, then allow, then the mode default, so first-match-wins makes deny beat allow on overlap with no special casing:
core/crates/vmm/src/egress.rs:95-100.) The crate’s AGENTS.md records that batching took a six-CIDR policy from about 8.3 ms to about 3.1 ms. Rules match only -o <uplink> traffic, so gateway-bound DNS is structurally exempt. See iptables and per-sandbox egress policy.
The jailer module is mostly pure functions: path derivation, cgroup arithmetic, argument building, and one filesystem helper. JailerIdPool leases a distinct uid/gid per concurrent jailed VM from a fixed range, and its doc comment explains why collisions are a security bug rather than a bookkeeping one: two jailed VMs sharing a uid would let one guest’s escaped process read the other’s files. link_resource_into_jail hard-links a host file into the chroot (falling back to a copy across filesystems on EXDEV) and makes it world-readable rather than chowning it, because a hard link shares an inode with its source and a chown would silently change the source’s ownership too. Jailer boot is not yet proven on real hardware (see the jailer privilege model).
Rootfs, drives and images
rootfs::clone_file tries a FICLONE ioctl and falls back to a byte copy, and it returns which one happened:
core/crates/vmm/src/rootfs.rs:56-70.) The method is returned, never hidden, because a silent fallback is indistinguishable from “slow disk” in latency numbers, which is how an earlier benchmark was misread. The daemon records the method in a metric. The repository’s docs/benchmarks.md records the measured effect on an XFS reflink volume: cold create 187 ms to 17.9 ms, with the rootfs clone itself going from 127.9 ms to 0.08 ms. A re-measurement on 2026-11-23 (bench-report, 20 cold creates with a reflink rootfs, kernel 6.8) gave a mean total of 18.4 ms: clone 0.084 ms, network lease 3.1 ms, boot 13.4 ms. Both runs are directional figures from one shared dev box, and neither is authoritative. No partial destination file is left on error, and an existing destination is refused.
DriveStore and ImageStore follow the same pattern: one <id>.ext4 file per resource in a directory, the filesystem as the source of truth, no index to fall out of sync. Both validate ids as 1 to 64 characters of ASCII alphanumerics, hyphen and underscore, because an id becomes a filename (and for drives a Firecracker drive_id). ImageStore::register checks the ext4 superblock magic at byte 1080, copies through rootfs::clone_file, and verifies the copied size matches the source, since a near-full disk can truncate a copy silently. It cannot verify the guest agent is baked in (that needs a loop mount and root). See managed images and persistent drives.
Error model
The crate returnsstd::io::Error everywhere. There is no crate-specific error enum, and that is deliberate: every failure here originates in I/O (a socket, a process, a file, an iptables spawn), and the daemon maps io::Error into its own AppError::Internal at the boundary. Where the kind carries meaning, it is chosen deliberately: InvalidInput for a bad id or size, AlreadyExists for a duplicate drive or image, TimedOut for an API socket that never accepted, WriteZero for a truncated image copy, ConnectionRefused for a failed vsock CONNECT.
Context is attached rather than lost. Firecracker API failures carry method, path, status and body (firecracker API PUT /drives/rootfs -> 400: ...). Boot and resume failures are annotated with the guest console log path, because a kernel panic before vsock comes up is otherwise invisible; the console output goes to /tmp/sandkiln-fc-<id>.log rather than being discarded.
Teardown is best-effort (let _ = on kills and removals) so a failed cleanup step cannot mask the original error or abort the rest. A tunnel listener that cannot bind logs a warning and boot still succeeds.
Test strategy
66 tests run undercargo test -p sandkiln-vmm (re-run 2026-11-24), none needing KVM or root:
Pure logic is extracted so it can be tested without a VM (
build_jailer_args, cgroup_limits, validate_cidr, looks_like_ext4, chain_name, parse_status_code), and tests that need files use real temp directories, real mkfs.ext4 and real hard links rather than mocks. The timeout test for connect_api_with_retry asserts both the TimedOut kind and that elapsed time is at least the timeout, because a backoff bug that exits early would otherwise look like a clean fast failure.
Two pieces of the retry-safety design have no unit test in this crate, and that is a real gap: vsock_client (including the request_sent classification in call) and retry_while are not covered by any #[test]. Both are small and pure enough to test with a stub UnixListener that accepts and then stays silent, or an attempt closure returning scripted CallErrors; neither has been added yet. Their regression coverage is behavioural instead: scripts/integration-tests/25-long-running-commands.sh runs a command silent for 4 seconds through a real daemon, asserts it succeeds, and asserts a marker file shows it ran exactly once. That topic also checks that GET /sandboxes answers in under a second while another sandbox runs a command, and that a snapshot request completes in under 3 seconds.
What the unit tests cannot cover is stated in the crate’s AGENTS.md: nothing here proves a VM boots. The examples/ directory (exec_test.rs, file_test.rs) and the criterion bench benches/vm_lifecycle.rs run against real Firecracker, and the full daemon integration suite is the real bar.
How neighbours consume it
The daemon is the only production consumer. It callsVm::boot from its cold-create path and Vm::resume from its resume and fork paths, Vm::call, call_with_timeout (through an AgentEndpoint), open_pty and open_exec_stream from its exec, file, PTY and log routes, update_metadata after claiming a pre-warmed pool snapshot, and force_stop both when a post-resume health check fails and when snapshotting a paused sandbox. It builds VmConfig and ResumeConfig itself (including rootfs_override, the private clone a retaining resume or fork writes to), holds NetworkManager, DriveStore, ImageStore and JailerIdPool in its application state, and calls rootfs::clone_file and egress::apply around boot. Resource ownership follows from that: this crate creates and tears down the Firecracker process and chroot, the network manager owns the free pools, and the daemon owns the mapping from sandbox to lease and drive.
Downward, the crate depends on sandkiln-protocol for message types and framing, plus libc, serde, serde_json and tracing.
Design alternatives rejected
A full HTTP client for the Firecracker API. Rejected infirecracker_api.rs and AGENTS.md: the surface is a handful of fixed-shape JSON PUT and PATCH calls over a Unix socket, not worth hyper and an async runtime.
On-demand tap device creation. Rejected because TUNSETIFF needs root and the daemon runs with ambient CAP_NET_ADMIN only. A pre-created pool turns a privileged operation into an unprivileged lease.
A netlink library for ip, bridge and iptables. Rejected: privileged operations run via std::process::Command to match the shell scripts exactly, and the AGENTS.md warns not to switch without understanding why ambient capabilities propagate to spawned processes but may not to a library call. The per-call cost (about 1.4 ms each, 4.3 ms for the three attach calls) was measured and left alone.
Retrying every failed call. Rejected after it ran user commands twice: only failures where CallError::request_sent is false are retried.
An async runtime in this crate. Rejected: the daemon already bridges every call through spawn_blocking.
Jailed snapshot and resume. Not supported: the jailer covers boot only, and the daemon refuses the combination rather than half-supporting it.