fork: share template mem-file via symlink for fan-out forks#214
Draft
sjmiller609 wants to merge 1 commit intohypeship/templatesfrom
Draft
fork: share template mem-file via symlink for fan-out forks#214sjmiller609 wants to merge 1 commit intohypeship/templatesfrom
sjmiller609 wants to merge 1 commit intohypeship/templatesfrom
Conversation
When ForkInstanceRequest.TemplateID is set, the fork resolves the source
instance from the templates registry, skips the per-fork mem-file copy,
and installs a symlink to the template's snapshot mem-file instead.
firecracker mmaps the symlinked mem-file MAP_PRIVATE during restore, so
many concurrent forks COW from the same backing file rather than each
holding a private copy.
Also wires:
- templateGuard on Start/Restore so a template parent is never resumed
while live forks share its mem-file.
- Refcount lifecycle: bump on fork creation, decrement on fork delete.
- Delete safety: deleting a template instance refuses while ForkCount>0
via templates.ErrInUse.
- forkvm.CopyOptions.SkipRelPaths so callers can opt out of specific
files in the source dir without breaking the existing copy semantics.
This was referenced May 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on: #213 (templates) — review/merge that first.
Summary
forkInstanceFromStoppedOrStandbysymlinks<dst>/snapshots/snapshot-latest/memoryto the template's canonical mem-file path.forkvm.CopyOptions{SkipRelPaths}so the directory copy walks past the mem-file without touching it.ForkCountbefore releasing cleanup so a partial fork rolls back the refcount.ForkInstanceRequest.TemplateIDand a pre-lock template→source resolution step so the right instance lock is held.Why
The template's
memoryfile is read-only and ~equal to guest RAM; copying it per fork burns most of fan-out's wall-clock budget. Sharing via symlink is the cheapest legal MAP_PRIVATE-equivalent today and the building block PR 5's UFFD server replaces with a real shared mapping later.Test plan
go test ./lib/forkvm/...(covers theSkipRelPathsoption)go test ./lib/instances/... -run "TestForkInstance|TestValidateFork|TestForkRequest"<fork>/snapshots/snapshot-latest/memoryis a symlink to the template's mem-fileErrInUse)🤖 Generated with Claude Code