mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
Linux's 9p client loses data with Lima's default cache=mmap for a writable share: a stat() during an extending write() takes the host's stale size and writeback drops the data, so files reach the host empty or cut to 4096 bytes even after fsync (Claude Code's Edit sets it off); and on devvm's Ubuntu 26.04 guest kernel, a lock after a write makes the next read return zeros, which breaks SQLite in any journal mode and Gradle's daemon. macOS's vz VMs use virtiofs and aren't affected. - New VMs on a Linux host record cache mode none in a new mount section of vm.json, and their project folder is made and started with it, as is the folder devvm clone off mounts. VMs whose state has no mode (0.1.0's, and every VM made before this) keep Lima's mmap untouched, with no limactl edit, and each launch warns (mount_cache_unchosen) until devvm mount-cache none. - devvm mount-cache [none], with --json: none is the only mode devvm sets, so any other value is a usage error (mount_cache_invalid); a stopped VM gets it in its next start's one edit, a running one after the restart devvm offers. macOS refuses (backend_lacks), and so does a Linux VM whose folder isn't a 9p mount (mount_cache_no_choice). No -g or config.toml key: every new VM gets none. - The start that changes the mode mounts the folder again in the guest, as cloud-init rewrites fstab only after systemd has mounted it, through the logged runner with a minute's limit (output pipes closed a second after), then checks it with mountpoint and findmnt, reading 4 KiB at most and only the last line: a folder left unmounted stops the VM and fails the start before its shell (mount_cache_unmounted), and so does a check that gets no answer (mount_cache_unchecked); one still mounted with the old mode is warned of (mount_cache_remount_failed), any mode but none or mmap named unknown. An interrupt meanwhile, on any tier, leaves a cleanup that stops the VM, the one stop then. A launch into a running VM asks the guest, in words, whether its folder is a mount point, and refuses one that isn't (project_unmounted) or whose check fails or gives no answer (project_unchecked). A stored mmap, from a build before none alone, reads as none chosen, and devvm clone off records none for its new mount; any other mode is refused naming vm.json. - backend: FeatureMountCache (native on Linux, none on macOS, built on project-mount), Want.ProjectCache, Instance.ProjectCache, CacheModes. - lima: mount rewrites keep only the sshfs and 9p settings devvm knows where they differ from Lima's defaults, and drop the rest for Lima to fill in, so lima.yaml never freezes a default; same() compares the cache where devvm sets one. Build VMs' writable output folders (tools, --from) get none. - tools: an EROFS tool image that ends before its superblock does, or is shorter than its superblock says, as a build under mmap could leave one, counts as not built, so the next build makes it again; a build's short output is refused. The fakes' tool images are a page (backendtest.ToolImage). - The limactl stub lists QEMU's vmType, fills in 9p cache defaults on Linux, and answers the remount's check. - Docs: backends/lima (the project folder's cache, when it changes, programs that map files for writing, SQLite), using/limits, using/clone, commands, internals/lima, internals/backends, the design note (both mmap bugs, why none alone, the remount, tool images, the e2e findings), the CLI reference and the cli skill. - Tests: TestProjectCache, TestMountRoundTrip, TestStartSetsProjectCacheOnce (the remount's outcomes), TestCutShort, TestBuiltCutShort, TestGuestMountOptions, TestFilledMounts, TestUpCacheMode, TestWarnMountCache, TestCacheAtNextStart, TestSwitchCache, TestSetMountCache, TestCheckStateCache, the mount_cache CLI test, clone_switch and the 0.1.0 fixture on Linux, and e2e's projectMountCache (findmnt, a stat-while-write loop, SQLite's journals, a shared writable mmap refused, the opt-in switch of an unchosen VM, what mmap does reported, macOS unchanged). Co-Authored-By: Claude Opus 5.5 <[email protected]>
217 lines
7.5 KiB
Go
217 lines
7.5 KiB
Go
package vm
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"errors"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/devvmhq/devvm/internal/backend"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/shell"
|
|
"github.com/devvmhq/devvm/internal/state"
|
|
)
|
|
|
|
// mountInVM is a VM's mount section: the cache mode of its project
|
|
// folder's mount (backend.CacheModes), which a new VM gets as it's made,
|
|
// and devvm mount-cache sets. "" for a VM whose mode wasn't chosen, which
|
|
// keeps its backend's own: one made before devvm chose it, whose mount
|
|
// its starts leave as it is, warning at each launch where the backend's
|
|
// mode can lose writes (warnMountCache).
|
|
type mountInVM struct {
|
|
Cache string `json:"cache,omitempty"`
|
|
}
|
|
|
|
// cacheOf is the cache mode chosen for st's VM, "" for none. A stored
|
|
// mmap, which a build of devvm before it settled on none alone could
|
|
// write, counts as none chosen, so the VM warns and devvm mount-cache none
|
|
// still switches it.
|
|
func cacheOf(st *state.VM) string {
|
|
c := sectionOf[mountInVM](st, mountSection).Cache
|
|
if c == backend.CacheMmap {
|
|
return ""
|
|
}
|
|
return c
|
|
}
|
|
|
|
// CheckCacheMode refuses a cache mode devvm doesn't set
|
|
// (backend.CacheModes), as devvm mount-cache or a VM's state gives it.
|
|
func CheckCacheMode(v string) error {
|
|
if !slices.Contains(backend.CacheModes(), v) {
|
|
return msg.MountCacheInvalid(strconv.Quote(v))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// readMount is st's mount section, refused when its cache mode isn't one,
|
|
// naming the VM's state file; a stored mmap is read as none chosen
|
|
// (cacheOf).
|
|
func readMount(st *state.VM) (mountInVM, error) {
|
|
m, err := state.Section[mountInVM](st, mountSection)
|
|
if err != nil {
|
|
return m, err
|
|
}
|
|
if m.Cache != "" && m.Cache != backend.CacheMmap {
|
|
if err := CheckCacheMode(m.Cache); err != nil {
|
|
mm, _ := errors.AsType[msg.M](err)
|
|
return m, &state.CorruptError{Path: st.File(), Reason: mountSection + ".cache: ", Msg: mm}
|
|
}
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// newCache is the cache mode a new VM on b gets, or one whose folder
|
|
// devvm clone off mounts anew: none, where b has the choice; "" where it
|
|
// has none to make.
|
|
func (e *Env) newCache(b backend.Backend) string {
|
|
if !e.Registry.Allowed(b, backend.FeatureMountCache) {
|
|
return ""
|
|
}
|
|
return backend.CacheNone
|
|
}
|
|
|
|
// warnMountCache warns at each launch of a VM, as in has it, whose
|
|
// project folder's mount keeps a cache mode no one chose that can lose
|
|
// writes (its backend's own, such as Lima's mmap on a Linux host), until
|
|
// devvm mount-cache none chooses one. A --clone VM mounts no folder.
|
|
func (e *Env) warnMountCache(st *state.VM, in backend.Instance) {
|
|
if isClone(st) || cacheOf(st) != "" || in.ProjectCache == "" || in.ProjectCache == backend.CacheNone {
|
|
return
|
|
}
|
|
e.Warn(msg.MountCacheUnchosen(in.ProjectCache, st.Name()))
|
|
}
|
|
|
|
// checkMounted refuses a launch into t's VM, running, whose project folder
|
|
// isn't a mount point in the guest, where its mount has a cache mode (a
|
|
// Linux host's 9p): one a remount or the guest left unmounted, where
|
|
// what's written goes to the VM's own disk and vanishes once the folder
|
|
// is mounted again. It costs one command over the VM's ssh, which answers
|
|
// in words rather than mountpoint's exit code (util-linux's 32, busybox's
|
|
// 1): a check that fails to run, or gives no answer, refuses too, as
|
|
// project_unchecked. Its connection carries agent, the agent the VM runs
|
|
// with, as devvm's every connection to it does.
|
|
func (s starter) checkMounted(ctx context.Context, t *target, agent string) error {
|
|
if isClone(t.st) || t.in.ProjectCache == "" || t.in.Project == "" {
|
|
return nil
|
|
}
|
|
out := &smallOutput{}
|
|
script := "if mountpoint -q " + shell.Quote(t.in.Project) + "; then echo " + mountedWord + "; else echo " + unmountedWord + "; fi"
|
|
err := t.b.Exec(ctx, t.f.Name, []string{"sh", "-c", script}, backend.ExecOpts{Agent: agent, Stdout: out})
|
|
switch answer := strings.TrimSpace(out.String()); {
|
|
case ctx.Err() != nil:
|
|
return ctx.Err()
|
|
case err == nil && answer == mountedWord:
|
|
return nil
|
|
case err == nil && answer == unmountedWord:
|
|
return msg.ProjectUnmounted(t.f.Name)
|
|
case err == nil:
|
|
err = errors.New("the guest gave no answer")
|
|
}
|
|
return msg.ProjectUnchecked(t.f.Name).Because(err)
|
|
}
|
|
|
|
// The words checkMounted's guest command answers with.
|
|
const (
|
|
mountedWord = "devvm-mounted"
|
|
unmountedWord = "devvm-not-mounted"
|
|
)
|
|
|
|
// smallOutput keeps the first 64 bytes a guest command writes: enough for
|
|
// a word, and no more of what a VM could write.
|
|
type smallOutput struct{ buf []byte }
|
|
|
|
func (o *smallOutput) Write(p []byte) (int, error) {
|
|
o.buf = append(o.buf, p[:min(len(p), 64-len(o.buf))]...)
|
|
return len(p), nil
|
|
}
|
|
|
|
func (o *smallOutput) String() string { return string(o.buf) }
|
|
|
|
// MountCacheShown is a VM's cache mode as devvm mount-cache shows it.
|
|
type MountCacheShown struct {
|
|
// Mode is the mode its project folder's mount has; a --clone VM's is
|
|
// the one it gets once it mounts the folder.
|
|
Mode string
|
|
// Chosen: the mode was chosen, as the VM was made or with devvm
|
|
// mount-cache, rather than kept from its backend.
|
|
Chosen bool
|
|
// Pending is the mode its next start gives it; "" for none.
|
|
Pending string
|
|
}
|
|
|
|
// MountCache returns how the folder's VM caches its project folder's
|
|
// mount, saying what changes at its next start, and warning as each
|
|
// launch does of a mode no one chose (warnMountCache).
|
|
func (e Settings) MountCache(ctx context.Context) (MountCacheShown, error) {
|
|
t, err := e.finder.target(ctx, need{has: hasMade, noVM: msg.NoVMMountCache})
|
|
if err != nil {
|
|
return MountCacheShown{}, err
|
|
}
|
|
vm, st := t.f.Name, t.st
|
|
if err := e.cacheable(t); err != nil {
|
|
return MountCacheShown{}, err
|
|
}
|
|
chosen := cacheOf(st)
|
|
s := MountCacheShown{Mode: t.in.ProjectCache, Chosen: chosen != ""}
|
|
switch {
|
|
case isClone(st):
|
|
s.Mode = cmp.Or(chosen, backend.CacheNone)
|
|
e.Note(msg.MountCacheClone(s.Mode, vm))
|
|
case chosen != "" && chosen != t.in.ProjectCache:
|
|
s.Pending = chosen
|
|
e.Note(msg.MountCachePending(chosen, vm))
|
|
default:
|
|
e.warnMountCache(st, t.in)
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
// cacheable refuses t's VM a cache mode its backend has no choice of
|
|
// (Require), or that its project folder's mount has none of, as its
|
|
// instance says: a mode saved for it would never apply. A --clone VM
|
|
// mounts no folder yet, and keeps one for when it does.
|
|
func (e Settings) cacheable(t *target) error {
|
|
if err := e.Registry.Require(t.b, t.f.Name, backend.FeatureMountCache); err != nil {
|
|
return err
|
|
}
|
|
if !isClone(t.st) && t.in.ProjectCache == "" {
|
|
return msg.MountCacheNoChoice(t.f.Name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetMountCache chooses how the folder's VM caches its project folder's
|
|
// mount, none being the one mode devvm sets: from its next start when
|
|
// it's stopped; when it runs, after a restart the user agrees to
|
|
// (restartOr), else from its next start. A --clone VM keeps it for when
|
|
// it mounts the folder (devvm clone off).
|
|
func (e Settings) SetMountCache(ctx context.Context, mode string) error {
|
|
if err := CheckCacheMode(mode); err != nil {
|
|
return err
|
|
}
|
|
t, err := e.finder.target(ctx, need{has: hasMade, noVM: msg.NoVMMountCache})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
vm, st, in := t.f.Name, t.st, t.in
|
|
if err := e.cacheable(t); err != nil {
|
|
return err
|
|
}
|
|
if err := update(ctx, st, mountSection, func(m *mountInVM) error { m.Cache = mode; return nil }); err != nil {
|
|
return err
|
|
}
|
|
switch {
|
|
case isClone(st):
|
|
e.Info(msg.MountCacheClone(mode, vm))
|
|
case in.ProjectCache == mode:
|
|
e.Info(msg.VMMountCache(mode, vm))
|
|
case in.Status == backend.StatusRunning:
|
|
return e.restartOr(ctx, vm, e.Warn, msg.NextStart(1, vm))
|
|
default:
|
|
e.Info(msg.MountCachePending(mode, vm))
|
|
}
|
|
return nil
|
|
}
|