Phase 3: banger kernel import bridges make-*-kernel.sh output
`banger kernel import <name> --from <dir>` copies a staged kernel
bundle into the local catalog. <dir> is the output of
`make void-kernel` or `make alpine-kernel` (build/manual/void-kernel/
or build/manual/alpine-kernel/).
kernelcat.DiscoverPaths locates artifacts under <dir>:
1. Prefers metadata.json (written by make-void-kernel.sh).
2. Falls back to globbing: boot/vmlinux-* or vmlinuz-* (Alpine
fallback), boot/initramfs-*, lib/modules/<latest>.
The daemon's KernelImport copies kernel + optional initrd via
system.CopyFilePreferClone and modules via system.CopyDirContents
(no-sudo mode — catalog lives under ~/.local/state), computes SHA256
over the kernel, and writes the manifest via kernelcat.WriteLocal.
While wiring this up, fixed a latent bug in system.CopyDirContents:
filepath.Join(sourceDir, ".") silently drops the trailing dot, so
`cp -a source source/contents target/` was copying the whole source
directory (including its basename) instead of just its contents.
Replaced the join with a manual "/." suffix. imagemgr.StageBootArtifacts
(the only existing caller) silently benefits.
scripts/register-void-image.sh and scripts/register-alpine-image.sh
are rewritten to use `banger kernel import … && banger image register
--kernel-ref …` instead of the find-and-pass-paths dance. Preserves
the same user-facing commands and env vars.
Tests cover: metadata.json preference, glob fallback, Alpine vmlinuz
fallback, kernel-missing error, round-trip copy into the catalog, and
the --from required flag.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
48e3a938cf
commit
7192ba24ae
11 changed files with 542 additions and 80 deletions
|
|
@ -543,6 +543,13 @@ func (d *Daemon) dispatch(ctx context.Context, req rpc.Request) rpc.Response {
|
|||
}
|
||||
err = d.KernelDelete(ctx, params.Name)
|
||||
return marshalResultOrError(api.Empty{}, err)
|
||||
case "kernel.import":
|
||||
params, err := rpc.DecodeParams[api.KernelImportParams](req)
|
||||
if err != nil {
|
||||
return rpc.NewError("bad_request", err.Error())
|
||||
}
|
||||
entry, err := d.KernelImport(ctx, params)
|
||||
return marshalResultOrError(api.KernelShowResult{Entry: entry}, err)
|
||||
default:
|
||||
return rpc.NewError("unknown_method", req.Method)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,12 +2,16 @@ package daemon
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"banger/internal/api"
|
||||
"banger/internal/kernelcat"
|
||||
"banger/internal/system"
|
||||
)
|
||||
|
||||
func (d *Daemon) KernelList(_ context.Context) (api.KernelListResult, error) {
|
||||
|
|
@ -37,6 +41,97 @@ func (d *Daemon) KernelDelete(_ context.Context, name string) error {
|
|||
return kernelcat.DeleteLocal(d.layout.KernelsDir, name)
|
||||
}
|
||||
|
||||
// KernelImport copies the kernel / initrd / modules artifacts produced by
|
||||
// scripts/make-*-kernel.sh (under params.FromDir) into the local catalog
|
||||
// under params.Name and writes the manifest. It is the primary bridge from
|
||||
// "I built a kernel with the helper scripts" to "banger kernel list shows
|
||||
// it and image register --kernel-ref works."
|
||||
func (d *Daemon) KernelImport(ctx context.Context, params api.KernelImportParams) (api.KernelEntry, error) {
|
||||
name := strings.TrimSpace(params.Name)
|
||||
if err := kernelcat.ValidateName(name); err != nil {
|
||||
return api.KernelEntry{}, err
|
||||
}
|
||||
fromDir := strings.TrimSpace(params.FromDir)
|
||||
if fromDir == "" {
|
||||
return api.KernelEntry{}, errors.New("--from <dir> is required")
|
||||
}
|
||||
|
||||
discovered, err := kernelcat.DiscoverPaths(fromDir)
|
||||
if err != nil {
|
||||
return api.KernelEntry{}, fmt.Errorf("discover artifacts under %s: %w", fromDir, err)
|
||||
}
|
||||
|
||||
targetDir := kernelcat.EntryDir(d.layout.KernelsDir, name)
|
||||
// Overwrite-by-default: clear any prior entry so a re-import is clean.
|
||||
if err := kernelcat.DeleteLocal(d.layout.KernelsDir, name); err != nil {
|
||||
return api.KernelEntry{}, fmt.Errorf("clear prior catalog entry %q: %w", name, err)
|
||||
}
|
||||
if err := os.MkdirAll(targetDir, 0o755); err != nil {
|
||||
return api.KernelEntry{}, err
|
||||
}
|
||||
|
||||
kernelTarget := filepath.Join(targetDir, "vmlinux")
|
||||
if err := system.CopyFilePreferClone(discovered.KernelPath, kernelTarget); err != nil {
|
||||
return api.KernelEntry{}, fmt.Errorf("copy kernel: %w", err)
|
||||
}
|
||||
if discovered.InitrdPath != "" {
|
||||
initrdTarget := filepath.Join(targetDir, "initrd.img")
|
||||
if err := system.CopyFilePreferClone(discovered.InitrdPath, initrdTarget); err != nil {
|
||||
return api.KernelEntry{}, fmt.Errorf("copy initrd: %w", err)
|
||||
}
|
||||
}
|
||||
if discovered.ModulesDir != "" {
|
||||
modulesTarget := filepath.Join(targetDir, "modules")
|
||||
if err := os.MkdirAll(modulesTarget, 0o755); err != nil {
|
||||
return api.KernelEntry{}, err
|
||||
}
|
||||
if err := system.CopyDirContents(ctx, d.runner, discovered.ModulesDir, modulesTarget, false); err != nil {
|
||||
return api.KernelEntry{}, fmt.Errorf("copy modules: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
sum, err := kernelcat.SumFile(kernelTarget)
|
||||
if err != nil {
|
||||
return api.KernelEntry{}, fmt.Errorf("sha256 kernel: %w", err)
|
||||
}
|
||||
|
||||
entry := kernelcat.Entry{
|
||||
Name: name,
|
||||
Distro: strings.TrimSpace(params.Distro),
|
||||
Arch: strings.TrimSpace(params.Arch),
|
||||
KernelVersion: inferKernelVersion(discovered.KernelPath, discovered.ModulesDir),
|
||||
SHA256: sum,
|
||||
Source: "import:" + fromDir,
|
||||
ImportedAt: time.Now().UTC(),
|
||||
}
|
||||
if err := kernelcat.WriteLocal(d.layout.KernelsDir, entry); err != nil {
|
||||
return api.KernelEntry{}, fmt.Errorf("write manifest: %w", err)
|
||||
}
|
||||
stored, err := kernelcat.ReadLocal(d.layout.KernelsDir, name)
|
||||
if err != nil {
|
||||
return api.KernelEntry{}, err
|
||||
}
|
||||
return kernelEntryToAPI(stored), nil
|
||||
}
|
||||
|
||||
// inferKernelVersion makes a best-effort guess at the kernel version from
|
||||
// the source filename (e.g. "vmlinux-6.12.79_1") or falls back to the
|
||||
// modules directory basename. Returns "" if nothing looks useful.
|
||||
func inferKernelVersion(kernelPath, modulesDir string) string {
|
||||
if modulesDir != "" {
|
||||
if base := filepath.Base(modulesDir); base != "." && base != string(filepath.Separator) {
|
||||
return base
|
||||
}
|
||||
}
|
||||
base := filepath.Base(kernelPath)
|
||||
for _, prefix := range []string{"vmlinux-", "vmlinuz-"} {
|
||||
if strings.HasPrefix(base, prefix) {
|
||||
return strings.TrimPrefix(base, prefix)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func kernelEntryToAPI(entry kernelcat.Entry) api.KernelEntry {
|
||||
importedAt := ""
|
||||
if !entry.ImportedAt.IsZero() {
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import (
|
|||
"banger/internal/kernelcat"
|
||||
"banger/internal/paths"
|
||||
"banger/internal/rpc"
|
||||
"banger/internal/system"
|
||||
)
|
||||
|
||||
func seedKernelEntry(t *testing.T, kernelsDir, name string) {
|
||||
|
|
@ -149,6 +150,72 @@ func TestRegisterImageRejectsKernelRefAndPath(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestKernelImportCopiesArtifactsAndWritesManifest(t *testing.T) {
|
||||
src := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(src, "boot"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(src, "boot", "vmlinux-6.12.79_1"), []byte("kernel-bytes"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(src, "boot", "initramfs-6.12.79_1"), []byte("initrd-bytes"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
modulesSource := filepath.Join(src, "lib", "modules", "6.12.79_1")
|
||||
if err := os.MkdirAll(modulesSource, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(modulesSource, "modules.dep"), []byte(""), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
kernelsDir := t.TempDir()
|
||||
d := &Daemon{
|
||||
layout: paths.Layout{KernelsDir: kernelsDir},
|
||||
runner: system.NewRunner(),
|
||||
}
|
||||
|
||||
entry, err := d.KernelImport(context.Background(), api.KernelImportParams{
|
||||
Name: "void-6.12",
|
||||
FromDir: src,
|
||||
Distro: "void",
|
||||
Arch: "x86_64",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("KernelImport: %v", err)
|
||||
}
|
||||
if entry.Name != "void-6.12" || entry.Distro != "void" || entry.Arch != "x86_64" {
|
||||
t.Fatalf("entry metadata = %+v", entry)
|
||||
}
|
||||
if entry.KernelVersion != "6.12.79_1" {
|
||||
t.Errorf("KernelVersion = %q, want 6.12.79_1 (from modules dir)", entry.KernelVersion)
|
||||
}
|
||||
if entry.SHA256 == "" {
|
||||
t.Errorf("SHA256 not populated")
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(kernelsDir, "void-6.12", "vmlinux")); err != nil {
|
||||
t.Errorf("kernel not copied: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(kernelsDir, "void-6.12", "initrd.img")); err != nil {
|
||||
t.Errorf("initrd not copied: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(kernelsDir, "void-6.12", "modules", "modules.dep")); err != nil {
|
||||
t.Errorf("modules not copied: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKernelImportRejectsMissingFromDir(t *testing.T) {
|
||||
d := &Daemon{
|
||||
layout: paths.Layout{KernelsDir: t.TempDir()},
|
||||
runner: system.NewRunner(),
|
||||
}
|
||||
_, err := d.KernelImport(context.Background(), api.KernelImportParams{Name: "x"})
|
||||
if err == nil || !strings.Contains(err.Error(), "--from") {
|
||||
t.Fatalf("KernelImport without --from: err=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterImageMissingKernelRef(t *testing.T) {
|
||||
rootfs := filepath.Join(t.TempDir(), "rootfs.ext4")
|
||||
if err := os.WriteFile(rootfs, []byte("rootfs"), 0o644); err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue