banger/internal/imagepull/firstboot_test.go
Thales Maciel 8f4be112c2
Generic kernel + init= boot path for OCI-pulled images
Closes the full arc: banger kernel pull + image pull + vm create + vm ssh
now works end-to-end against docker.io/library/debian:bookworm with zero
manual image building.

Generic kernel:
 - New scripts/make-generic-kernel.sh builds vmlinux from upstream
   kernel.org sources using Firecracker's official minimal config
   (configs/firecracker-x86_64-6.1.config). All critical drivers
   (virtio_blk, virtio_net, ext4, vsock) compiled in — no modules,
   no initramfs needed.
 - Published as generic-6.12 in the catalog (kernels.thaloco.com).
 - catalog.json updated with the new entry.

Direct-boot init= override (vm_lifecycle.go):
 - For images without an initrd (direct-boot / OCI-pulled), banger now
   passes init=/usr/local/libexec/banger-first-boot on the kernel
   cmdline. The script runs as PID 1, mounts /proc /sys /dev /run,
   checks for systemd — if present execs it immediately; if not
   (container images), installs systemd-sysv + openssh-server via the
   guest's package manager, then execs systemd.
 - Also passes kernel-level ip= parameter via BuildBootArgsWithKernelIP
   so the kernel configures the network interface before init runs
   (container images don't ship iproute2, so the userspace bootstrap
   script can't call ip(8)).
 - Masks dev-ttyS0.device and dev-vdb.device systemd units that
   otherwise wait 90s for udev events that never fire in Firecracker
   guests started from container rootfses.

first-boot.sh rewritten as universal init wrapper:
 - Works as PID 1 (mounts essential filesystems) OR as a systemd
   oneshot (existing behavior).
 - Installs both systemd-sysv AND openssh-server (container images
   have neither).
 - Dispatch updated: debian, alpine, fedora, arch, opensuse families
   + ID_LIKE fallback. All tests updated.

Opencode capability skip for direct-boot images:
 - The opencode readiness check (WaitReady on vsock port 4096) now
   returns nil for images without an initrd, since pulled container
   images don't ship the opencode service. Without this, the VM
   would be marked as error for lacking an opinionated add-on.

Docs: README and kernel-catalog.md updated to recommend generic-6.12
as the default kernel for OCI-pulled images. AGENTS.md notes the new
build script.

Verified live:
 - banger kernel pull generic-6.12
 - banger image pull docker.io/library/debian:bookworm --kernel-ref generic-6.12
 - banger vm create --image debian-bookworm --name testbox --nat
 - banger vm ssh testbox -- "id; uname -r; systemctl is-active banger-vsock-agent"
 → uid=0(root), kernel 6.12.8, Debian bookworm, vsock-agent active,
   sshd running, SSH working.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-16 20:12:56 -03:00

140 lines
3.8 KiB
Go

package imagepull
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// runFirstBootPlan executes first-boot.sh in planning mode (RUN_PLAN=1)
// against a synthetic /etc/os-release. Returns the planned install
// command or an error.
func runFirstBootPlan(t *testing.T, osReleaseContent string) string {
t.Helper()
if _, err := exec.LookPath("sh"); err != nil {
t.Skip("sh not available")
}
dir := t.TempDir()
osRelease := filepath.Join(dir, "os-release")
if err := os.WriteFile(osRelease, []byte(osReleaseContent), 0o644); err != nil {
t.Fatal(err)
}
scriptPath := filepath.Join(dir, "banger-first-boot")
if err := os.WriteFile(scriptPath, []byte(FirstBootScript()), 0o755); err != nil {
t.Fatal(err)
}
marker := filepath.Join(dir, "first-boot-pending")
if err := os.WriteFile(marker, nil, 0o644); err != nil {
t.Fatal(err)
}
cmd := exec.Command("sh", scriptPath)
cmd.Env = append(os.Environ(),
"RUN_PLAN=1",
"OS_RELEASE_FILE="+osRelease,
"BANGER_FIRST_BOOT_MARKER="+marker,
)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("first-boot script: %v\noutput:\n%s", err, out)
}
// Planned command is printed to stdout (no [banger-first-boot] prefix);
// log output goes to stderr. CombinedOutput merges them, so pick the
// last non-log line.
lines := strings.Split(strings.TrimRight(string(out), "\n"), "\n")
for i := len(lines) - 1; i >= 0; i-- {
l := lines[i]
if strings.TrimSpace(l) == "" {
continue
}
if strings.HasPrefix(l, "[banger-first-boot]") {
continue
}
return l
}
t.Fatalf("no planned command in output:\n%s", out)
return ""
}
func TestFirstBootScriptDispatchesByDistro(t *testing.T) {
cases := []struct {
name string
osRel string
wantRe string
}{
{"debian", `ID=debian` + "\n" + `ID_LIKE=""`, "systemd-sysv openssh-server"},
{"ubuntu", `ID=ubuntu`, "systemd-sysv openssh-server"},
{"alpine", `ID=alpine`, "apk add"},
{"fedora", `ID=fedora`, "dnf install -y systemd openssh-server"},
{"arch", `ID=arch`, "pacman -Sy --noconfirm openssh"},
{"opensuse-leap", `ID="opensuse-leap"`, "zypper --non-interactive install"},
{"unknown-with-debian-like", `ID=someweirddistro` + "\n" + `ID_LIKE=debian`, "systemd-sysv openssh-server"},
{"unknown-with-rhel-like", `ID=something` + "\n" + `ID_LIKE="rhel fedora"`, "dnf install -y systemd openssh-server"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := runFirstBootPlan(t, tc.osRel)
if !strings.Contains(got, tc.wantRe) {
t.Errorf("got=%q, want contains %q", got, tc.wantRe)
}
})
}
}
func TestFirstBootScriptContainsDistroCases(t *testing.T) {
s := FirstBootScript()
for _, snippet := range []string{
"debian|ubuntu|kali|raspbian",
"apt-get",
"systemd-sysv",
"openssh-server",
"alpine)",
"apk add",
"fedora|rhel|centos|rocky|almalinux",
"dnf install",
"arch|archlinux|manjaro",
"pacman -Sy",
"opensuse*|suse",
"zypper",
`ID_LIKE`,
"RUN_PLAN",
"/usr/lib/systemd/systemd",
"mount -t proc",
} {
if !strings.Contains(s, snippet) {
t.Errorf("script missing %q", snippet)
}
}
}
func TestFirstBootScriptIsShSyntaxValid(t *testing.T) {
if _, err := exec.LookPath("sh"); err != nil {
t.Skip("sh not available")
}
dir := t.TempDir()
path := filepath.Join(dir, "first-boot")
if err := os.WriteFile(path, []byte(FirstBootScript()), 0o755); err != nil {
t.Fatal(err)
}
out, err := exec.Command("sh", "-n", path).CombinedOutput()
if err != nil {
t.Fatalf("sh -n first-boot: %v: %s", err, out)
}
}
func TestFirstBootUnitReferencesScript(t *testing.T) {
u := FirstBootUnit()
for _, want := range []string{
FirstBootScriptPath,
"ConditionPathExists=" + FirstBootMarkerPath,
"After=network-online.target",
"Before=sshd.service",
} {
if !strings.Contains(u, want) {
t.Errorf("unit missing %q", want)
}
}
}