package daemon import ( "context" "os" "path/filepath" "strings" "banger/internal/model" "banger/internal/paths" "banger/internal/system" ) func (d *Daemon) validateStartPrereqs(ctx context.Context, vm model.VMRecord, image model.Image) error { checks := system.NewPreflight() hint := paths.RuntimeBundleHint() for _, command := range []string{"sudo", "ip", "dmsetup", "losetup", "blockdev", "truncate", "pgrep", "ps", "chown", "chmod", "kill", "e2cp", "e2rm", "debugfs"} { checks.RequireCommand(command, toolHint(command)) } checks.RequireExecutable(d.config.FirecrackerBin, "firecracker binary", hint) checks.RequireExecutable(d.config.MapDNSBin, "mapdns binary", `install mapdns or set "mapdns_bin" / BANGER_MAPDNS_BIN`) checks.RequireFile(image.RootfsPath, "rootfs image", "select a valid image or rebuild the runtime bundle") checks.RequireFile(image.KernelPath, "kernel image", `set "default_kernel" or refresh the runtime bundle`) if strings.TrimSpace(image.InitrdPath) != "" { checks.RequireFile(image.InitrdPath, "initrd image", `set "default_initrd" or refresh the runtime bundle`) } if !exists(vm.Runtime.WorkDiskPath) { for _, command := range []string{"mkfs.ext4", "mount", "umount", "cp"} { checks.RequireCommand(command, toolHint(command)) } } if vm.Spec.NATEnabled { d.addNATPrereqs(ctx, checks) } if dataFile := strings.TrimSpace(d.config.MapDNSDataFile); dataFile != "" { parent := filepath.Dir(dataFile) if parent != "." && parent != "" { if _, err := os.Stat(parent); err != nil && !os.IsNotExist(err) { checks.Addf("mapdns data directory %s is not accessible (%v)", parent, err) } } } return checks.Err("vm start preflight failed") } func (d *Daemon) validateImageBuildPrereqs(ctx context.Context, baseRootfs, kernelPath, initrdPath, modulesDir string) error { checks := system.NewPreflight() hint := paths.RuntimeBundleHint() for _, command := range []string{"bash", "sudo", "ip", "curl", "ssh", "jq", "sha256sum", "e2fsck", "resize2fs"} { checks.RequireCommand(command, toolHint(command)) } checks.RequireExecutable(d.config.CustomizeScript, "customize.sh helper", hint) checks.RequireExecutable(d.config.MapDNSBin, "mapdns binary", `install mapdns or set "mapdns_bin" / BANGER_MAPDNS_BIN`) checks.RequireFile(baseRootfs, "base rootfs image", `pass --base-rootfs or set "default_base_rootfs"`) checks.RequireFile(kernelPath, "kernel image", `pass --kernel or set "default_kernel"`) if strings.TrimSpace(initrdPath) != "" { checks.RequireFile(initrdPath, "initrd image", `pass --initrd or set "default_initrd"`) } if strings.TrimSpace(modulesDir) != "" { checks.RequireDir(modulesDir, "modules directory", `pass --modules or set "default_modules_dir"`) } return checks.Err("image build preflight failed") } func (d *Daemon) 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 (d *Daemon) addNATPrereqs(ctx context.Context, checks *system.Preflight) { checks.RequireCommand("iptables", toolHint("iptables")) checks.RequireCommand("sysctl", toolHint("sysctl")) out, err := d.runner.Run(ctx, "ip", "route", "show", "default") if err != nil { checks.Addf("failed to inspect the default route for NAT: %v", err) return } if _, err := parseDefaultUplink(string(out)); err != nil { checks.Addf("failed to detect the uplink interface for NAT: %v", err) } } 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", "ps", "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 "curl": return "install curl" case "jq": return "install jq" case "sha256sum": return "install coreutils" case "mapdns": return `install mapdns or set "mapdns_bin" / BANGER_MAPDNS_BIN` case "ssh": return "install openssh-client" case "bash": return "install bash" case "sudo": return "install sudo" default: return "" } }