Skip to content

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

Draft
sjmiller609 wants to merge 1 commit intohypeship/templatesfrom
hypeship/fork-shared-memfile
Draft

fork: share template mem-file via symlink for fan-out forks#214
sjmiller609 wants to merge 1 commit intohypeship/templatesfrom
hypeship/fork-shared-memfile

Conversation

@sjmiller609
Copy link
Copy Markdown
Collaborator

Stacked on: #213 (templates) — review/merge that first.

Summary

  • Forks descended from a template now skip copying the snapshot mem-file. Instead forkInstanceFromStoppedOrStandby symlinks <dst>/snapshots/snapshot-latest/memory to the template's canonical mem-file path.
  • Adds forkvm.CopyOptions{SkipRelPaths} so the directory copy walks past the mem-file without touching it.
  • Bumps the template's ForkCount before releasing cleanup so a partial fork rolls back the refcount.
  • Adds ForkInstanceRequest.TemplateID and a pre-lock template→source resolution step so the right instance lock is held.

Why

The template's memory file 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 the SkipRelPaths option)
  • go test ./lib/instances/... -run "TestForkInstance|TestValidateFork|TestForkRequest"
  • Manual: promote a Standby template, fork, confirm <fork>/snapshots/snapshot-latest/memory is a symlink to the template's mem-file
  • Manual: delete the template instance with live forks (must refuse via ErrInUse)

🤖 Generated with Claude Code

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.
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