Skip to content

fork: share template mem-file via symlink for firecracker fan-out#214

Draft
sjmiller609 wants to merge 1 commit into
hypeship/template-as-statefrom
hypeship/fork-shared-memfile
Draft

fork: share template mem-file via symlink for firecracker fan-out#214
sjmiller609 wants to merge 1 commit into
hypeship/template-as-statefrom
hypeship/fork-shared-memfile

Conversation

@sjmiller609
Copy link
Copy Markdown
Collaborator

@sjmiller609 sjmiller609 commented May 8, 2026

Summary

  • when a Firecracker fork descends from a Standby/Template source, symlink the snapshot mem-file instead of copying it
  • firecracker mmaps the mem-file MAP_PRIVATE on restore, so all forks COW from a single backing file
  • gated to Firecracker only; other hypervisors take the unchanged copy path
  • skipped for the running-fork flow because the source restores afterward and would mutate the shared file

Stacked on

  • builds on Introduce "template" as a VM state #229 (Template as instance state). Template promotion now governs "this snapshot is safe to fan out from", and the Template state's ForkCount keeps the shared mem-file alive while any fork still references it.

Test plan

  • go test ./lib/forkvm/...CopyOptions.SkipRelPaths skips the named relpath
  • go test -run 'TestInstallForkSharedMemFile' ./lib/instances/... — symlink installed at the fork's snapshot path
  • go test -run 'TestForkFirecrackerSharesMemFile_FromStandby' ./lib/instances/... — end-to-end: standby Firecracker source → fork mem-file is a symlink, not a copy
  • go test -run 'TestForkFirecrackerRunningSourceDoesNotShareMemFile' ./lib/instances/... — running-fork carve-out: mem-file is a regular file

When a Firecracker fork descends from a Template source, skip copying the
snapshot mem-file and hardlink it to the source's instead. Firecracker
mmaps the mem-file MAP_PRIVATE on restore, so all forks COW from the same
backing inode — no per-fork copy required.

Hardlink rather than symlink: firecracker's restore path temporarily
aliases the source data dir to the fork data dir while loading the
snapshot (withSnapshotSourceDirAlias). A symlink whose target traverses
the source dir would resolve back into the fork dir during that window
and trip ELOOP; a hardlink resolves by inode so the alias has no effect
on it. Hardlinks require both paths on the same filesystem, which holds
for our standard data-dir layout.

Gated to Firecracker only because other hypervisors (cloud-hypervisor,
qemu, vz) don't share MAP_PRIVATE semantics on their snapshot layouts.
Restricted to Template sources because they are explicitly promoted as
fork-only and can never be restored — sharing the mem-file with a
non-Template source would let a later RestoreInstance mutate the file
out from under live forks.

Stacked on hypeship/template-as-state so the Template state both gates
"this snapshot is safe to fan out from" and lets fork counts be derived
at read time.
@sjmiller609 sjmiller609 force-pushed the hypeship/fork-shared-memfile branch from d46be7a to 7b799f7 Compare May 13, 2026 18:13
@sjmiller609 sjmiller609 changed the title fork: share template mem-file via symlink for fan-out forks fork: share template mem-file via symlink for firecracker fan-out May 13, 2026
@sjmiller609 sjmiller609 changed the base branch from hypeship/templates to hypeship/template-as-state May 13, 2026 18:13
@sjmiller609 sjmiller609 force-pushed the hypeship/fork-shared-memfile branch 2 times, most recently from 355ad7f to a45d471 Compare May 13, 2026 20:39
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