Skip to content

feat(host): resume stopped Firecracker sandboxes where they were - #431

Merged
kalaspuff merged 28 commits into
mainfrom
fctl/802-firecracker-snapshots
Nov 7, 2026
Merged

kalaspuff merged 28 commits into
mainfrom
fctl/802-firecracker-snapshots

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Stopping a Firecracker sandbox shut its VM down, so a message that resumed the session started Claude again. Now a stop saves the VM's state, and the next start resumes it with its programs, Claude among them, running where they were:

  • Saving. The host has the VM write out its disk (sync), pauses it, and has Firecracker save its state and memory in the sandbox's saved/ (PUT /snapshot/create), then punches out the memory file's zero pages, so it takes only what the VM used: a 512 MiB VM took 51 MiB. It saves nothing where the disk lacks room for the memory above min_free_disk_gb, for a VM paused because the disk is nearly full, for an image builder (feat(host): build projects' images with BuildKit in Firecracker VMs #432), which boots afresh for each build, or for a sandbox that is being removed: a session that destroys a Firecracker sandbox now removes it without stopping it first (other drivers still stop first, as Apple's container sometimes needs). One host saves one VM at a time, so that two saves can't both count on the same free disk.
  • Resuming. A start loads the state into a new Firecracker (PUT /snapshot/load), checks the init answers, and links the network again: passt is new, and the VM's network bridge now takes the host's new link (fix(host): give Firecracker VMs a network on Ubuntu, and boot them in CI #429). The state is deleted once loaded, since the VM's disk moves past it. A state that another Firecracker release saved, that doesn't load, or whose VM the session now wants with other CPUs or memory, is deleted too, and the sandbox boots from its disk as before, so a host that updates Firecracker loses only the running programs. A running VM never keeps a saved state.
  • Disk. While the disk is below min_free_disk_gb, the sweep deletes the saved states of stopped sandboxes before it empties caches or removes sandboxes, and measures the disk again before it does more. A saved state otherwise lasts as long as its sandbox (keep_stopped_hours). firecracker.snapshots = false in host.toml turns saving off, and lets the states kept before go at the next sweep or start rather than resume them.

I verified it with a new ignored test, stopped_sandboxes_resume_where_they_were, in Firecracker nested in a QEMU VM on aarch64: a program started before the stop still runs after the resume, a file in the VM's /run (a tmpfs) is still there, DNS works again, and with snapshots off the same stop and start boots from the disk instead. The resume took 0.6 s there, and the save 21 s under nested emulation. Unit tests cover the snapshot request against the API stand-in, the hole punching, deleting states that can't resume, and deleting states for room on the disk. CI's Firecracker job runs the new test on x86_64.

Worth confirming

  • Decision: saving is on by default. A stopped sandbox's saved state takes as much disk as the memory its VM used, until the sandbox starts again, expires or the disk runs low.
  • Decision: a VM that runs through the jailer (feat(host): run Firecracker VMs through the jailer on root hosts #434, on hosts that run as root) saves no state yet and boots from its disk, as before: Firecracker would write the state in the jail's chroot, which goes as the VM stops. Saving and resuming jailed VMs is Save and resume the state of jailed Firecracker VMs #437.
  • Decision: when a state can't resume, the sandbox boots from its disk and the session carries on, with a line in its log, rather than failing.
  • Assumption: Claude and the tools it runs cope with being frozen for hours and coming back with every network connection reset, as they do after a laptop sleeps.
  • Follow-up: if feat(host): share package caches with Firecracker sandboxes #430 merges first, a resumed VM also needs its package caches served and mounted again (its 9p connection is reset with the rest); whichever of the two merges second gets that.

Refs #345

fctl session


Note

Medium risk. It changes how Firecracker sandboxes stop and start, saving the VM's memory to disk and resuming from it, and it adds a low-disk sweep that deletes saved states. A bug could lose a running agent's state or fill a host's disk.

What it does. When a Firecracker sandbox stops, the host now saves the VM's memory and state, and a later message resumes the session with Claude running where it was instead of booting again. Saved states are dropped first when the disk runs low, when the VM's requested size changes, or when snapshots is turned off in host.toml; saves go one at a time under a host-wide lock.

Disk use. Each saved state can be as large as the VM's memory; hosts can turn it off with snapshots = false, and hosts that use the jailer keep none yet.

fctl review's first look at 6fac9b2 · 9 files, +889 −19 · Review agent settings

fctl-sh Bot added 17 commits November 6, 2026 18:11
The firecracker driver's test that boots a VM only ran by hand, on
aarch64 under QEMU. GitHub's x86_64 runners have KVM: a CI job builds a
static fctl-agent and runs the driver's ignored tests there, with
Ubuntu's passt and its AppArmor profile, and the kernel hosts pull,
which the test now takes unless FCTL_TEST_FIRECRACKER_KERNEL names one.

Refs #345
… lacks

Ubuntu 24.04's passt (0.0~git20240220) refuses --hostname, which came
later, so no Firecracker VM got a network there. The VM names itself, so
passt's DHCP needs no hostname.
…Docker does

Ubuntu's /etc/resolv.conf names only systemd-resolved's 127.0.0.53, and
the passt Ubuntu 24.04 has forwards no DNS to a resolver on the host's
loopback with --no-map-gw, so VMs resolved nothing there. Where the
host's resolv.conf names only loopback resolvers, the host passes passt
the servers systemd-resolved asks, as Docker does for its containers.
…t reach

systemd-resolved lists a link-local IPv6 server with its interface
(fe80::1%eth0), which --dns can't carry: without it, passt finds a
server itself rather than get one it can't reach.
…resolved's stub

The --dns passt took doesn't set where its forwarder sends queries in
Ubuntu 24.04's passt, so VMs still resolved nothing there. As Docker
gives its containers, the VM now asks the host's nameservers itself
(fctl.dns= on its kernel's command line), but for those on the host's
loopback, and where only those are left, the servers systemd-resolved
asks. With none, it asks passt's forwarder, as before.
…u's passt takes

On Ubuntu 24.04 no Firecracker VM got a network: passt's AppArmor
profile there refuses to accept the connection Firecracker makes for the
VM (accept4 fails with EACCES, and nothing is logged), so the VM's frames
waited in passt's backlog. CI's x86_64 runners showed it.

Now the VM's init listens on its vsock port 1025, and the host, once the
VM is up, connects to it and hands passt the connection with --fd, which
passt reads without accepting anything. passt exits when the VM closes
the connection, as it stops, rather than linger until the next start,
and the host links a running VM again when its passt is gone.

The test's diagnostics that found it (passt under strace, its trace and
capture) are gone; on a failure it still says what passt, the VM's
console and its network say.
…makes the file

passt makes its pid's file as it reads its options and writes the pid
only once it has set itself up, so a passt that failed in between left
the VM without a network while the host took it as linked. The host now
waits for the pid; the test's stand-in passt makes the file first, as
passt does, and one that fails then gets no link.
Stopping a sandbox shut its VM down, so a resume started Claude again.
Now, where the disk has room for its memory, the host pauses the VM,
saves its state (Firecracker's and the VM's memory, its zero pages given
back to the disk) beside its disk, and a start loads it into a new
Firecracker, the VM's programs running on, then links its network again.
A state another Firecracker release saved, or one that doesn't load, is
let go and the VM boots from its disk, which the host wrote out before
saving. While the disk is low, the saved states of stopped sandboxes go
first. firecracker.snapshots = false turns it off.
Every driver's remove stops its sandbox, and stopping a Firecracker VM
first would save its state only for the removal to delete it. Docs say
what saved states are and cost.
@fctl-sh
fctl-sh Bot requested a review from kalaspuff as a code owner November 6, 2026 19:52
@fctl-review

This comment has been minimized.

Comment thread crates/fctl-host/src/daemon.rs Resolved
Comment thread crates/fctl-host/src/session.rs Resolved
fctl-sh Bot added 3 commits November 6, 2026 20:30
… as root in CI

passt opens its pid file as it starts, as root but for its capabilities
on a host that runs as root, then becomes nobody: it can write only in a
directory root owns, and, under Ubuntu's AppArmor profile, only under
/tmp or home. Its pid now goes beside the VM's socket directory, in the
host's user's directory in /tmp, which both allow, and CI boots a
sandbox as root too.
… free what saves leave

- A sandbox asked for another size than its VM had boots from its disk,
  rather than resume at the old size.
- Saves run one at a time, so that sandboxes stopping together don't
  each find room for their memory and write it all at once.
- What a save that failed or stopped halfway left (saved.new) goes, with
  the saved states, and the sweep counts it.
- After letting saved states go for room, the sweep measures the disk
  again before it removes any sandbox.
- A sandbox that goes is stopped first again with every driver but
  Firecracker (Driver::finish): Apple's container sometimes needs its
  stop's second try.
@fctl-review

This comment has been minimized.

Comment thread crates/fctl-host/src/daemon.rs Resolved
fctl-sh Bot added 4 commits November 6, 2026 20:38
… make first

Ubuntu's passt of 2024 drops to nobody before it opens its pid's file, and later ones open it as root without capabilities: on a host that runs as root, only /tmp lets both write. The name has a random part, recorded in the VM's directory.
… told

A failed link now forgets where passt's pid went, so the test reads the path from the stand-in, which says where it was told to write.
…ge-caches-builds' into fctl/802-firecracker-snapshots
@fctl-review

This comment has been minimized.

@fctl-sh

fctl-sh Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

On the review's finding (a stale passt pid file in /tmp trusted when the record is gone): it's real, and it comes from #429, this pull request's base, so I fixed it there, in 2530fcf: new_passt_pid now removes the file at the path it is about to use whatever the record says, and link_network waits for the pid of the passt it just started rather than any pid. links_the_vms_network_once_it_is_up now leaves an earlier pid at that path before the failing link, and fails without the fix. This pull request's CI runs on its merge with #429's branch, so it includes the fix.

Base automatically changed from fctl/802-firecracker-hosts-package-caches-builds to main November 6, 2026 23:32
kalaspuff pushed a commit that referenced this pull request Nov 6, 2026
## Summary

A host that runs as root now runs each Firecracker VM through
Firecracker's jailer, so that a VM escape lands as a user of its own in
a chroot rather than as the host's user, who can read the runner's
token. Each VM gets a uid and gid of its own (from 1,900,000,000 up,
picked under a lock so that no other running VM's jail has it), a chroot
under `firecracker/jail/`, and a cgroup of its own (`fctl/<VM key>`, at
most 256 processes):

- **What the jail holds.** Hard links of the VM's disks (the images'
filesystems readable by all) and of its kernel and initramfs (copies
where they're private or on another filesystem, and links resolved
first). Its writable disk is lent to the jail's user while it runs,
noted beside the chroot where that user can't change the note, and given
back to root as the VM stops, so a later jail with the same uid owns
nothing of the host's; jails that a stopped or crashed host left go
before the next VM starts.
- **How the host reaches it.** The jail's user owns the chroot, so the
host never goes through it: once Firecracker has made its API and vsock
sockets there, the host opens each without following links, checks it's
a socket, and hard-links that inode into a directory of root's beside
the chroot, which the VM's socket path leads to. A jail can't send the
host to another VM's sockets.
- **What it needs.** The jailer comes with the pinned Firecracker, or
sits beside the one `host.toml` names (as `jailer` or as the release
names it). It needs cgroup v2 and a data directory on a filesystem that
allows device files; without either, or without a jailer, the driver
reports itself not ready with what to change. `jailer = false` in
`[firecracker]` turns it off, and hosts that don't run as root are
unchanged.

I verified it in Firecracker nested in a QEMU VM on aarch64:
`boots_a_sandbox` as root passes with both Ubuntu 24.04's passt and
Debian 13's, checks that Firecracker runs as a jail's user, and that the
sandbox's disk and the images' filesystems are root's again once their
VMs stop. CI's root step runs it jailed on x86_64, and runs the
stale-jail test as root, where it lends and takes back a disk. Unit
tests cover the jail's users, the jailer's arguments and where it's
found, the chroot's links and copies, linking only the jail's own
sockets, and removing stale jails. The docs (security, architecture,
`host.toml`, ADR 0070's consequences) say what a jailed VM can reach.

- #429

## Worth confirming

- **Decision:** the jailer is on by default wherever the host runs as
root; only `jailer = false` turns it off, and a root host without cgroup
v2 must set it to run VMs at all.
- **Decision:** the jails' users are bare uids and gids from
1,900,000,000 up, with no entries in `/etc/passwd`, chosen far from the
ranges people, system users and user namespaces take.
- **Decision:** passt stays outside the jail, as `nobody` (passt drops
to it itself when started as root), rather than as the jail's user.
- **Assumption:** a host runs as root by root installing the host
service (its systemd user unit in root's manager, with lingering);
nothing else here changes for that.
- **Follow-up:** package caches (#430) and snapshots (#431) don't know
about jails yet: a jailed VM's Firecracker can't connect to a cache
server's socket that root made, and a snapshot written inside the chroot
goes with it when the VM stops. Whichever of these merges after the
other needs that integration (or turns caches or snapshots off for
jailed VMs).

Refs #345

[fctl session](https://app.fctl.sh/s/ses_01M6S0DK83842CC62AW3X74BR4)

<!-- FCTL_REVIEW_SUMMARY -->
---

> [!WARNING]
> **High risk.** It changes how every Firecracker VM starts on hosts
that run as root, on by default, and it chowns disks and creates uids,
chroots, links and cgroups on the host, so a mistake could stop VMs from
booting or weaken the isolation it sets out to add.
>
> **What it does.** On hosts that run as root, each Firecracker VM now
starts through Firecracker's jailer as its own uid and gid in a chroot
and cgroup, so that a VM escape no longer lands as the host's user. The
jailer comes with the pinned Firecracker or from beside a configured
one, and `jailer = false` in `[firecracker]` turns it off. The host
links the VM's sockets, after checking them, into a root-owned directory
beside the chroot that the VM's socket directory leads to, hands
writable disks back when a jail ends, picks uids under a lock, and
removes stale jails. Since the last review, the stale-jail test also
lends a disk to a jail uid when run as root, and CI runs it as root.
>
> **On by default.** The jailer is on for every host running as root
unless `jailer = false`; it needs cgroup v2 and a data directory not
mounted `nodev`, and package caches and snapshots don't yet work with
jailed VMs.
>
> <sub>[fctl
review](https://app.fctl.sh/s/ses_01M6SDFTHBA4P30V9TM3Q6ZCD2)'s first
look at
[`6dca881`](6dca881)
· 9 files, +856 −36 · [Review agent
settings](https://app.fctl.sh/p/fctl?tab=reviews)</sub>

<!-- /FCTL_REVIEW_SUMMARY -->

---------

Co-authored-by: fctl-sh[bot] <343971779+fctl-sh[bot]@users.noreply.github.com>
kalaspuff
kalaspuff previously approved these changes Nov 6, 2026
…ate for jailed VMs

#429, this branch's base, was squash-merged into main with #434, the jailer: the tree is main's with this pull request's own changes applied again. A jailed Firecracker would write its state in its chroot, which goes as the VM stops, and a restore would need the jail made again around it, so a jailed VM stops without saving and boots from its disk, as before; a state saved before the host ran as root goes. The snapshot test runs without the jailer, and the root boot test checks that a jailed stop saves nothing.
@fctl-review

This comment has been minimized.

fctl-sh Bot added 2 commits November 6, 2026 23:52
…ate when an image builder stops

Main took #432 (builds) and #433 (images over their bases). Both pull requests' tests are kept. An image builder stops after each build and boots afresh for the next, so it saves no state: the build test checks that.
… snapshots off

Only stop looked at snapshots: a host that turned them off resumed the states of sandboxes stopped before, and kept their memory on disk until then. A start now lets such a state go without trying it, and the sweep lets the states of stopped sandboxes go whenever snapshots are off.
@fctl-review

This comment has been minimized.

Comment thread docs/cli.md Resolved
@kalaspuff
kalaspuff merged commit 62d24e0 into main Nov 7, 2026
12 checks passed
@kalaspuff
kalaspuff deleted the fctl/802-firecracker-snapshots branch November 7, 2026 00:06
fctl-sh Bot added a commit that referenced this pull request Nov 7, 2026
With snapshots (#431), a stopped sandbox resumes with its cache mount on a connection whose server stopped with the VM, so its package managers got 'Connection reset by peer'. A resume now serves the caches again and the VM mounts them in place of the old mount, as a running VM whose server is gone gets. docs/cli.md says that Firecracker keeps its caches in the same directory as Apple's container.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant