Phase 4 of the daemon god-struct refactor. VM lifecycle, create-op
registry, handle cache, disk provisioning, stats polling, ports
query, and the per-VM lock set all move off *Daemon onto *VMService.
Daemon keeps thin forwarders only for FindVM / TouchVM (dispatch
surface) and is otherwise out of VM lifecycle. Lazy-init via
d.vmSvc() mirrors the earlier services so test literals like
\`&Daemon{store: db, runner: r}\` still get a functional service
without spelling one out.
Three small cleanups along the way:
* preflight helpers (validateStartPrereqs / addBaseStartPrereqs
/ addBaseStartCommandPrereqs / validateWorkDiskResizePrereqs)
move with the VM methods that call them.
* cleanupRuntime / rebuildDNS move to *VMService, with
HostNetwork primitives (findFirecrackerPID, cleanupDMSnapshot,
killVMProcess, releaseTap, waitForExit, sendCtrlAltDel)
reached through s.net instead of the hostNet() facade.
* vsockAgentBinary becomes a package-level function so both
*Daemon (doctor) and *VMService (preflight) call one entry
point instead of each owning a forwarder method.
WorkspaceService's peer deps switch from eager method values to
closures — vmSvc() constructs VMService with WorkspaceService as a
peer, so resolving d.vmSvc().FindVM at construction time recursed
through workspaceSvc() → vmSvc(). Closures defer the lookup to call
time.
Pure code motion: build + unit tests green, lint clean. No RPC
surface or lock-ordering changes.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
73 lines
2.5 KiB
Go
73 lines
2.5 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"banger/internal/model"
|
|
"banger/internal/system"
|
|
)
|
|
|
|
var vsockHostDevicePath = "/dev/vhost-vsock"
|
|
|
|
func (s *VMService) validateStartPrereqs(ctx context.Context, vm model.VMRecord, image model.Image) error {
|
|
checks := system.NewPreflight()
|
|
s.addBaseStartPrereqs(checks, image)
|
|
s.capHooks.addStartPrereqs(ctx, checks, vm, image)
|
|
return checks.Err("vm start preflight failed")
|
|
}
|
|
|
|
func (s *VMService) validateWorkDiskResizePrereqs() error {
|
|
checks := system.NewPreflight()
|
|
checks.RequireCommand("truncate", toolHint("truncate"))
|
|
checks.RequireCommand("e2fsck", `install e2fsprogs`)
|
|
checks.RequireCommand("resize2fs", `install e2fsprogs`)
|
|
return checks.Err("work disk resize preflight failed")
|
|
}
|
|
|
|
func (s *VMService) addBaseStartPrereqs(checks *system.Preflight, image model.Image) {
|
|
s.addBaseStartCommandPrereqs(checks)
|
|
checks.RequireExecutable(s.config.FirecrackerBin, "firecracker binary", `install firecracker or set "firecracker_bin"`)
|
|
if helper, err := vsockAgentBinary(s.layout); err == nil {
|
|
checks.RequireExecutable(helper, "vsock agent helper", `run 'make build' or reinstall banger`)
|
|
} else {
|
|
checks.Addf("%v", err)
|
|
}
|
|
checks.RequireFile(vsockHostDevicePath, "vsock host device", "load the vhost_vsock kernel module on the host")
|
|
checks.RequireFile(image.RootfsPath, "rootfs image", "select a valid registered image")
|
|
checks.RequireFile(image.KernelPath, "kernel image", `re-register or rebuild the image with a valid kernel`)
|
|
if strings.TrimSpace(image.InitrdPath) != "" {
|
|
checks.RequireFile(image.InitrdPath, "initrd image", `re-register or rebuild the image with a valid initrd`)
|
|
}
|
|
}
|
|
|
|
func (s *VMService) addBaseStartCommandPrereqs(checks *system.Preflight) {
|
|
for _, command := range []string{"sudo", "ip", "dmsetup", "losetup", "blockdev", "truncate", "pgrep", "chown", "chmod", "kill", "e2cp", "e2rm", "debugfs"} {
|
|
checks.RequireCommand(command, toolHint(command))
|
|
}
|
|
}
|
|
|
|
func toolHint(command string) string {
|
|
switch command {
|
|
case "ip":
|
|
return "install iproute2"
|
|
case "iptables":
|
|
return "install iptables"
|
|
case "sysctl", "losetup", "blockdev", "mount", "umount":
|
|
return "install util-linux"
|
|
case "dmsetup":
|
|
return "install device-mapper"
|
|
case "pgrep", "kill":
|
|
return "install procps"
|
|
case "chown", "chmod", "cp", "truncate":
|
|
return "install coreutils"
|
|
case "e2fsck", "resize2fs", "debugfs", "mkfs.ext4":
|
|
return "install e2fsprogs"
|
|
case "e2cp", "e2rm":
|
|
return "install e2tools"
|
|
case "sudo":
|
|
return "install sudo"
|
|
default:
|
|
return ""
|
|
}
|
|
}
|