Refactor VM lifecycle around capabilities
Make host-integrated VM features fit a standard Go extension path instead of adding more one-off branches through vm.go. This is the enabling refactor for future work like shared mounts, not the /work feature itself. Add a daemon capability pipeline plus a structured guest-config builder, then move the existing /root work-disk mount, built-in DNS, and NAT wiring onto those hooks. Generalize Firecracker drive config at the same time so later storage features can extend machine setup without another hardcoded path. Add banger doctor on top of the shared readiness checks, update the docs to describe the new architecture, and cover the new seams with guest-config, capability, report, CLI, and full go test verification. Also verify make build and a real ./banger doctor run on the host.
This commit is contained in:
parent
9e98445fa2
commit
4930d82cb9
18 changed files with 1120 additions and 105 deletions
159
internal/guestconfig/guestconfig.go
Normal file
159
internal/guestconfig/guestconfig.go
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
package guestconfig
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MountSpec struct {
|
||||
Source string
|
||||
Target string
|
||||
FSType string
|
||||
Options []string
|
||||
Dump int
|
||||
Pass int
|
||||
}
|
||||
|
||||
func (m MountSpec) String() string {
|
||||
options := strings.Join(compactStrings(m.Options), ",")
|
||||
if options == "" {
|
||||
options = "defaults"
|
||||
}
|
||||
return strings.Join([]string{
|
||||
m.Source,
|
||||
m.Target,
|
||||
m.FSType,
|
||||
options,
|
||||
strconv.Itoa(m.Dump),
|
||||
strconv.Itoa(m.Pass),
|
||||
}, " ")
|
||||
}
|
||||
|
||||
type Builder struct {
|
||||
files map[string][]byte
|
||||
dropTargets map[string]struct{}
|
||||
mounts map[string]MountSpec
|
||||
order []string
|
||||
}
|
||||
|
||||
func NewBuilder() *Builder {
|
||||
return &Builder{
|
||||
files: make(map[string][]byte),
|
||||
dropTargets: make(map[string]struct{}),
|
||||
mounts: make(map[string]MountSpec),
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Builder) WriteFile(path string, data []byte) {
|
||||
if b.files == nil {
|
||||
b.files = make(map[string][]byte)
|
||||
}
|
||||
b.files[path] = append([]byte(nil), data...)
|
||||
}
|
||||
|
||||
func (b *Builder) DropMountTarget(target string) {
|
||||
target = strings.TrimSpace(target)
|
||||
if target == "" {
|
||||
return
|
||||
}
|
||||
if b.dropTargets == nil {
|
||||
b.dropTargets = make(map[string]struct{})
|
||||
}
|
||||
b.dropTargets[target] = struct{}{}
|
||||
}
|
||||
|
||||
func (b *Builder) AddMount(spec MountSpec) {
|
||||
spec.Source = strings.TrimSpace(spec.Source)
|
||||
spec.Target = strings.TrimSpace(spec.Target)
|
||||
spec.FSType = strings.TrimSpace(spec.FSType)
|
||||
spec.Options = compactStrings(spec.Options)
|
||||
if spec.Source == "" || spec.Target == "" || spec.FSType == "" {
|
||||
return
|
||||
}
|
||||
if b.mounts == nil {
|
||||
b.mounts = make(map[string]MountSpec)
|
||||
}
|
||||
if _, exists := b.mounts[spec.Target]; !exists {
|
||||
b.order = append(b.order, spec.Target)
|
||||
}
|
||||
b.mounts[spec.Target] = spec
|
||||
}
|
||||
|
||||
func (b *Builder) Files() map[string][]byte {
|
||||
if len(b.files) == 0 {
|
||||
return nil
|
||||
}
|
||||
keys := b.FilePaths()
|
||||
out := make(map[string][]byte, len(keys))
|
||||
for _, path := range keys {
|
||||
out[path] = append([]byte(nil), b.files[path]...)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (b *Builder) FilePaths() []string {
|
||||
keys := make([]string, 0, len(b.files))
|
||||
for path := range b.files {
|
||||
keys = append(keys, path)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
||||
|
||||
func (b *Builder) RenderFSTab(existing string) string {
|
||||
lines := strings.Split(existing, "\n")
|
||||
out := make([]string, 0, len(lines)+len(b.mounts))
|
||||
managedTargets := make(map[string]struct{}, len(b.mounts))
|
||||
for target := range b.mounts {
|
||||
managedTargets[target] = struct{}{}
|
||||
}
|
||||
for _, line := range lines {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if trimmed == "" {
|
||||
continue
|
||||
}
|
||||
target := mountTarget(trimmed)
|
||||
if target != "" {
|
||||
if _, drop := b.dropTargets[target]; drop {
|
||||
continue
|
||||
}
|
||||
if _, managed := managedTargets[target]; managed {
|
||||
continue
|
||||
}
|
||||
}
|
||||
out = append(out, line)
|
||||
}
|
||||
for _, target := range b.order {
|
||||
out = append(out, b.mounts[target].String())
|
||||
}
|
||||
return strings.Join(out, "\n") + "\n"
|
||||
}
|
||||
|
||||
func mountTarget(line string) string {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) < 2 {
|
||||
return ""
|
||||
}
|
||||
return fields[1]
|
||||
}
|
||||
|
||||
func compactStrings(values []string) []string {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]string, 0, len(values))
|
||||
seen := make(map[string]struct{}, len(values))
|
||||
for _, value := range values {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[value]; ok {
|
||||
continue
|
||||
}
|
||||
seen[value] = struct{}{}
|
||||
out = append(out, value)
|
||||
}
|
||||
return out
|
||||
}
|
||||
71
internal/guestconfig/guestconfig_test.go
Normal file
71
internal/guestconfig/guestconfig_test.go
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
package guestconfig
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuilderRenderFSTabReplacesManagedTargetsAndDropsLegacyMounts(t *testing.T) {
|
||||
builder := NewBuilder()
|
||||
builder.DropMountTarget("/home")
|
||||
builder.DropMountTarget("/var")
|
||||
builder.AddMount(MountSpec{
|
||||
Source: "/dev/vdb",
|
||||
Target: "/root",
|
||||
FSType: "ext4",
|
||||
Options: []string{"defaults"},
|
||||
Dump: 0,
|
||||
Pass: 2,
|
||||
})
|
||||
builder.AddMount(MountSpec{
|
||||
Source: "tmpfs",
|
||||
Target: "/run",
|
||||
FSType: "tmpfs",
|
||||
Options: []string{"defaults", "nodev", "nosuid", "mode=0755"},
|
||||
})
|
||||
builder.AddMount(MountSpec{
|
||||
Source: "tmpfs",
|
||||
Target: "/tmp",
|
||||
FSType: "tmpfs",
|
||||
Options: []string{"defaults", "nodev", "nosuid", "mode=1777"},
|
||||
})
|
||||
|
||||
input := strings.Join([]string{
|
||||
"/dev/vdb /home ext4 defaults 0 2",
|
||||
"/dev/vdc /var ext4 defaults 0 2",
|
||||
"/dev/vdb /root ext4 defaults 0 2",
|
||||
"tmpfs /run tmpfs defaults,nodev,nosuid,mode=0700 0 0",
|
||||
"",
|
||||
}, "\n")
|
||||
|
||||
got := builder.RenderFSTab(input)
|
||||
|
||||
if strings.Contains(got, "/home") {
|
||||
t.Fatalf("RenderFSTab() kept /home mount: %q", got)
|
||||
}
|
||||
if strings.Contains(got, "/var") {
|
||||
t.Fatalf("RenderFSTab() kept /var mount: %q", got)
|
||||
}
|
||||
if strings.Count(got, "/dev/vdb /root") != 1 {
|
||||
t.Fatalf("RenderFSTab() duplicated /root mount: %q", got)
|
||||
}
|
||||
if !strings.Contains(got, "tmpfs /run tmpfs defaults,nodev,nosuid,mode=0755 0 0") {
|
||||
t.Fatalf("RenderFSTab() missing rendered /run mount: %q", got)
|
||||
}
|
||||
if !strings.Contains(got, "tmpfs /tmp tmpfs defaults,nodev,nosuid,mode=1777 0 0") {
|
||||
t.Fatalf("RenderFSTab() missing rendered /tmp mount: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuilderFilesReturnsCopies(t *testing.T) {
|
||||
builder := NewBuilder()
|
||||
builder.WriteFile("/etc/hostname", []byte("devbox\n"))
|
||||
|
||||
files := builder.Files()
|
||||
files["/etc/hostname"][0] = 'x'
|
||||
|
||||
again := builder.Files()
|
||||
if string(again["/etc/hostname"]) != "devbox\n" {
|
||||
t.Fatalf("Files() returned aliasing data: %q", string(again["/etc/hostname"]))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue