Beat VM create wall time without changing VM semantics. Generate a work-seed ext4 sidecar during image builds and rootfs rebuilds, then clone and resize that seed for each new VM instead of rebuilding /root from scratch. Plumb the new seed artifact through config, runtime metadata, store state, runtime-bundle defaults, doctor checks, and default-image reconciliation so older images still fall back cleanly. Add a daemon TAP pool to keep idle bridge-attached devices warm, expose stage timing in lifecycle logs, add a create/SSH benchmark script plus Make target, and teach verify.sh that tap-pool-* devices are reusable capacity rather than cleanup leaks. Validated with go test ./..., make build, ./verify.sh, and make bench-create ARGS="--runs 2".
171 lines
4.7 KiB
Go
171 lines
4.7 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"banger/internal/api"
|
|
"banger/internal/model"
|
|
"banger/internal/paths"
|
|
"banger/internal/system"
|
|
)
|
|
|
|
func (d *Daemon) BuildImage(ctx context.Context, params api.ImageBuildParams) (image model.Image, err error) {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
op := d.beginOperation("image.build")
|
|
buildLogPath := ""
|
|
defer func() {
|
|
if err != nil {
|
|
err = annotateLogPath(err, buildLogPath)
|
|
op.fail(err, imageLogAttrs(image)...)
|
|
return
|
|
}
|
|
op.done(imageLogAttrs(image)...)
|
|
}()
|
|
|
|
name := params.Name
|
|
if name == "" {
|
|
name = fmt.Sprintf("image-%d", model.Now().Unix())
|
|
}
|
|
if _, err := d.FindImage(ctx, name); err == nil {
|
|
return model.Image{}, fmt.Errorf("image name already exists: %s", name)
|
|
}
|
|
baseRootfs := params.BaseRootfs
|
|
if baseRootfs == "" {
|
|
baseRootfs = d.config.DefaultBaseRootfs
|
|
}
|
|
if baseRootfs == "" {
|
|
return model.Image{}, fmt.Errorf("base rootfs is required; %s", paths.RuntimeBundleHint())
|
|
}
|
|
id, err := model.NewID()
|
|
if err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
now := model.Now()
|
|
artifactDir := filepath.Join(d.layout.ImagesDir, id)
|
|
if err := os.MkdirAll(artifactDir, 0o755); err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
buildLogDir := filepath.Join(d.layout.StateDir, "image-build")
|
|
if err := os.MkdirAll(buildLogDir, 0o755); err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
buildLogPath = filepath.Join(buildLogDir, id+".log")
|
|
logFile, err := os.OpenFile(buildLogPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
|
|
if err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
defer logFile.Close()
|
|
rootfsPath := filepath.Join(artifactDir, "rootfs.ext4")
|
|
workSeedPath := filepath.Join(artifactDir, "work-seed.ext4")
|
|
kernelPath := params.KernelPath
|
|
if kernelPath == "" {
|
|
kernelPath = d.config.DefaultKernel
|
|
}
|
|
initrdPath := params.InitrdPath
|
|
if initrdPath == "" {
|
|
initrdPath = d.config.DefaultInitrd
|
|
}
|
|
modulesDir := params.ModulesDir
|
|
if modulesDir == "" {
|
|
modulesDir = d.config.DefaultModulesDir
|
|
}
|
|
if err := d.validateImageBuildPrereqs(ctx, baseRootfs, kernelPath, initrdPath, modulesDir, params.Size); err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
spec := imageBuildSpec{
|
|
ID: id,
|
|
Name: name,
|
|
BaseRootfs: baseRootfs,
|
|
RootfsPath: rootfsPath,
|
|
BuildLog: logFile,
|
|
KernelPath: kernelPath,
|
|
InitrdPath: initrdPath,
|
|
ModulesDir: modulesDir,
|
|
PackagesPath: d.config.DefaultPackagesFile,
|
|
InstallDocker: params.Docker,
|
|
Size: params.Size,
|
|
}
|
|
op.stage("launch_builder", "build_log_path", buildLogPath, "artifact_dir", artifactDir)
|
|
if err := d.runImageBuild(ctx, spec); err != nil {
|
|
_ = logFile.Sync()
|
|
_ = os.RemoveAll(artifactDir)
|
|
return model.Image{}, err
|
|
}
|
|
if err := system.BuildWorkSeedImage(ctx, d.runner, rootfsPath, workSeedPath); err != nil {
|
|
_ = logFile.Sync()
|
|
_ = os.RemoveAll(artifactDir)
|
|
return model.Image{}, err
|
|
}
|
|
if err := writePackagesMetadata(rootfsPath, d.config.DefaultPackagesFile); err != nil {
|
|
_ = logFile.Sync()
|
|
_ = os.RemoveAll(artifactDir)
|
|
return model.Image{}, err
|
|
}
|
|
image = model.Image{
|
|
ID: id,
|
|
Name: name,
|
|
Managed: true,
|
|
ArtifactDir: artifactDir,
|
|
RootfsPath: rootfsPath,
|
|
WorkSeedPath: workSeedPath,
|
|
KernelPath: kernelPath,
|
|
InitrdPath: initrdPath,
|
|
ModulesDir: modulesDir,
|
|
PackagesPath: d.config.DefaultPackagesFile,
|
|
BuildSize: params.Size,
|
|
Docker: params.Docker,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
if err := d.store.UpsertImage(ctx, image); err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
op.stage("persisted", "build_log_path", buildLogPath)
|
|
if d.logger != nil {
|
|
d.logger.Info("image build log preserved", append(imageLogAttrs(image), "build_log_path", buildLogPath)...)
|
|
}
|
|
_ = logFile.Sync()
|
|
return image, nil
|
|
}
|
|
|
|
func writePackagesMetadata(rootfsPath, packagesPath string) error {
|
|
if rootfsPath == "" || packagesPath == "" {
|
|
return nil
|
|
}
|
|
lines, err := system.ReadNormalizedLines(packagesPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
metadataPath := rootfsPath + ".packages.sha256"
|
|
return os.WriteFile(metadataPath, []byte(packagesHash(lines)+"\n"), 0o644)
|
|
}
|
|
|
|
func (d *Daemon) DeleteImage(ctx context.Context, idOrName string) (model.Image, error) {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
|
|
image, err := d.FindImage(ctx, idOrName)
|
|
if err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
vms, err := d.store.FindVMsUsingImage(ctx, image.ID)
|
|
if err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
if len(vms) > 0 {
|
|
return model.Image{}, fmt.Errorf("image %s is still referenced by %d VM(s)", image.Name, len(vms))
|
|
}
|
|
if err := d.store.DeleteImage(ctx, image.ID); err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
if image.Managed && image.ArtifactDir != "" {
|
|
if err := os.RemoveAll(image.ArtifactDir); err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
}
|
|
return image, nil
|
|
}
|