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
168
internal/kernelcat/import.go
Normal file
168
internal/kernelcat/import.go
Normal file
|
|
@ -0,0 +1,168 @@
|
|||
package kernelcat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// DiscoveredArtifacts is what DiscoverPaths returns: absolute paths to a
|
||||
// kernel, an optional initrd, and an optional modules directory located
|
||||
// under the staged output of make-*-kernel.sh (or an equivalent layout).
|
||||
type DiscoveredArtifacts struct {
|
||||
KernelPath string
|
||||
InitrdPath string
|
||||
ModulesDir string
|
||||
}
|
||||
|
||||
// metadataFile is the JSON dropped by scripts/make-void-kernel.sh alongside
|
||||
// its staged output. We read it when present to avoid guessing at filenames.
|
||||
type metadataFile struct {
|
||||
KernelPath string `json:"kernel_path"`
|
||||
InitrdPath string `json:"initrd_path"`
|
||||
ModulesDir string `json:"modules_dir"`
|
||||
}
|
||||
|
||||
// DiscoverPaths locates kernel / initrd / modules artifacts under fromDir.
|
||||
// It prefers a metadata.json emitted by make-*-kernel.sh; otherwise it
|
||||
// falls back to globbing boot/vmlinux-*, boot/vmlinuz-* (for Alpine),
|
||||
// boot/initramfs-*, and the newest subdir under lib/modules/.
|
||||
func DiscoverPaths(fromDir string) (DiscoveredArtifacts, error) {
|
||||
info, err := os.Stat(fromDir)
|
||||
if err != nil {
|
||||
return DiscoveredArtifacts{}, err
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return DiscoveredArtifacts{}, fmt.Errorf("%s is not a directory", fromDir)
|
||||
}
|
||||
|
||||
if paths, ok, err := discoverFromMetadata(fromDir); err != nil {
|
||||
return DiscoveredArtifacts{}, err
|
||||
} else if ok {
|
||||
return paths, nil
|
||||
}
|
||||
|
||||
bootDir := filepath.Join(fromDir, "boot")
|
||||
kernel, err := latestMatch(bootDir, []string{"vmlinux-*", "vmlinuz-*"})
|
||||
if err != nil {
|
||||
return DiscoveredArtifacts{}, fmt.Errorf("locate kernel under %s: %w", bootDir, err)
|
||||
}
|
||||
initrd, err := latestMatch(bootDir, []string{"initramfs-*"})
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return DiscoveredArtifacts{}, fmt.Errorf("locate initrd under %s: %w", bootDir, err)
|
||||
}
|
||||
modules, err := latestSubdir(filepath.Join(fromDir, "lib", "modules"))
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return DiscoveredArtifacts{}, fmt.Errorf("locate modules under %s: %w", fromDir, err)
|
||||
}
|
||||
return DiscoveredArtifacts{
|
||||
KernelPath: kernel,
|
||||
InitrdPath: initrd,
|
||||
ModulesDir: modules,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func discoverFromMetadata(fromDir string) (DiscoveredArtifacts, bool, error) {
|
||||
data, err := os.ReadFile(filepath.Join(fromDir, "metadata.json"))
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return DiscoveredArtifacts{}, false, nil
|
||||
}
|
||||
return DiscoveredArtifacts{}, false, err
|
||||
}
|
||||
var meta metadataFile
|
||||
if err := json.Unmarshal(data, &meta); err != nil {
|
||||
return DiscoveredArtifacts{}, false, fmt.Errorf("parse metadata.json in %s: %w", fromDir, err)
|
||||
}
|
||||
kernel := absoluteOrAnchored(fromDir, meta.KernelPath)
|
||||
if kernel == "" {
|
||||
return DiscoveredArtifacts{}, false, nil
|
||||
}
|
||||
if _, err := os.Stat(kernel); err != nil {
|
||||
return DiscoveredArtifacts{}, false, fmt.Errorf("metadata.json references missing kernel %s: %w", kernel, err)
|
||||
}
|
||||
out := DiscoveredArtifacts{KernelPath: kernel}
|
||||
if meta.InitrdPath != "" {
|
||||
candidate := absoluteOrAnchored(fromDir, meta.InitrdPath)
|
||||
if _, err := os.Stat(candidate); err == nil {
|
||||
out.InitrdPath = candidate
|
||||
}
|
||||
}
|
||||
if meta.ModulesDir != "" {
|
||||
candidate := absoluteOrAnchored(fromDir, meta.ModulesDir)
|
||||
if info, err := os.Stat(candidate); err == nil && info.IsDir() {
|
||||
out.ModulesDir = candidate
|
||||
}
|
||||
}
|
||||
return out, true, nil
|
||||
}
|
||||
|
||||
// absoluteOrAnchored returns path as-is if absolute; otherwise joins it to
|
||||
// anchor. Empty input returns "".
|
||||
func absoluteOrAnchored(anchor, path string) string {
|
||||
path = filepath.Clean(path)
|
||||
if path == "" || path == "." {
|
||||
return ""
|
||||
}
|
||||
if filepath.IsAbs(path) {
|
||||
return path
|
||||
}
|
||||
return filepath.Join(anchor, path)
|
||||
}
|
||||
|
||||
// latestMatch returns the lexicographically latest file in dir matching any
|
||||
// of patterns (filename globs, not full paths). Returns os.ErrNotExist if no
|
||||
// match.
|
||||
func latestMatch(dir string, patterns []string) (string, error) {
|
||||
if _, err := os.Stat(dir); err != nil {
|
||||
return "", err
|
||||
}
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var matches []string
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
for _, pattern := range patterns {
|
||||
ok, _ := filepath.Match(pattern, entry.Name())
|
||||
if ok {
|
||||
matches = append(matches, entry.Name())
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(matches) == 0 {
|
||||
return "", os.ErrNotExist
|
||||
}
|
||||
sort.Strings(matches)
|
||||
return filepath.Join(dir, matches[len(matches)-1]), nil
|
||||
}
|
||||
|
||||
// latestSubdir returns the lexicographically latest subdirectory of root.
|
||||
// Returns os.ErrNotExist if root is missing or has no subdirs.
|
||||
func latestSubdir(root string) (string, error) {
|
||||
if _, err := os.Stat(root); err != nil {
|
||||
return "", err
|
||||
}
|
||||
entries, err := os.ReadDir(root)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var dirs []string
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
dirs = append(dirs, entry.Name())
|
||||
}
|
||||
}
|
||||
if len(dirs) == 0 {
|
||||
return "", os.ErrNotExist
|
||||
}
|
||||
sort.Strings(dirs)
|
||||
return filepath.Join(root, dirs[len(dirs)-1]), nil
|
||||
}
|
||||
133
internal/kernelcat/import_test.go
Normal file
133
internal/kernelcat/import_test.go
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
package kernelcat
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func writeFile(t *testing.T, path string, data string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(data), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverPathsPrefersMetadataJSON(t *testing.T) {
|
||||
t.Parallel()
|
||||
dir := t.TempDir()
|
||||
writeFile(t, filepath.Join(dir, "boot", "vmlinux-custom"), "ignored")
|
||||
writeFile(t, filepath.Join(dir, "boot", "initramfs-custom"), "ignored")
|
||||
writeFile(t, filepath.Join(dir, "boot", "vmlinux-pick-me"), "kernel")
|
||||
writeFile(t, filepath.Join(dir, "boot", "initramfs-pick-me"), "initrd")
|
||||
if err := os.MkdirAll(filepath.Join(dir, "lib", "modules", "6.12.79_1"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
metadata := `{
|
||||
"kernel_path": "boot/vmlinux-pick-me",
|
||||
"initrd_path": "boot/initramfs-pick-me",
|
||||
"modules_dir": "lib/modules/6.12.79_1"
|
||||
}`
|
||||
writeFile(t, filepath.Join(dir, "metadata.json"), metadata)
|
||||
|
||||
got, err := DiscoverPaths(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("DiscoverPaths: %v", err)
|
||||
}
|
||||
if got.KernelPath != filepath.Join(dir, "boot", "vmlinux-pick-me") {
|
||||
t.Errorf("KernelPath = %q", got.KernelPath)
|
||||
}
|
||||
if got.InitrdPath != filepath.Join(dir, "boot", "initramfs-pick-me") {
|
||||
t.Errorf("InitrdPath = %q", got.InitrdPath)
|
||||
}
|
||||
if got.ModulesDir != filepath.Join(dir, "lib", "modules", "6.12.79_1") {
|
||||
t.Errorf("ModulesDir = %q", got.ModulesDir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverPathsFallsBackToGlobbing(t *testing.T) {
|
||||
t.Parallel()
|
||||
dir := t.TempDir()
|
||||
writeFile(t, filepath.Join(dir, "boot", "vmlinux-6.12.0"), "k")
|
||||
writeFile(t, filepath.Join(dir, "boot", "vmlinux-6.12.1"), "newer")
|
||||
writeFile(t, filepath.Join(dir, "boot", "initramfs-6.12.1"), "i")
|
||||
if err := os.MkdirAll(filepath.Join(dir, "lib", "modules", "6.12.0"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(dir, "lib", "modules", "6.12.1"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got, err := DiscoverPaths(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("DiscoverPaths: %v", err)
|
||||
}
|
||||
if got.KernelPath != filepath.Join(dir, "boot", "vmlinux-6.12.1") {
|
||||
t.Errorf("KernelPath = %q, want latest", got.KernelPath)
|
||||
}
|
||||
if got.InitrdPath != filepath.Join(dir, "boot", "initramfs-6.12.1") {
|
||||
t.Errorf("InitrdPath = %q", got.InitrdPath)
|
||||
}
|
||||
if got.ModulesDir != filepath.Join(dir, "lib", "modules", "6.12.1") {
|
||||
t.Errorf("ModulesDir = %q, want latest subdir", got.ModulesDir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverPathsAlpineVmlinuzFallback(t *testing.T) {
|
||||
t.Parallel()
|
||||
dir := t.TempDir()
|
||||
// Alpine older layouts may only ship vmlinuz-virt.
|
||||
writeFile(t, filepath.Join(dir, "boot", "vmlinuz-virt"), "k")
|
||||
writeFile(t, filepath.Join(dir, "boot", "initramfs-virt"), "i")
|
||||
|
||||
got, err := DiscoverPaths(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("DiscoverPaths: %v", err)
|
||||
}
|
||||
if got.KernelPath != filepath.Join(dir, "boot", "vmlinuz-virt") {
|
||||
t.Errorf("KernelPath = %q, want vmlinuz-virt fallback", got.KernelPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverPathsMissingKernelIsError(t *testing.T) {
|
||||
t.Parallel()
|
||||
dir := t.TempDir()
|
||||
// boot/ exists but contains no kernel
|
||||
if err := os.MkdirAll(filepath.Join(dir, "boot"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := DiscoverPaths(dir)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when no kernel present")
|
||||
}
|
||||
if !errors.Is(err, os.ErrNotExist) && !containsErr(err, "locate kernel") {
|
||||
t.Fatalf("error shape: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverPathsNotADirectory(t *testing.T) {
|
||||
t.Parallel()
|
||||
path := filepath.Join(t.TempDir(), "file")
|
||||
writeFile(t, path, "")
|
||||
_, err := DiscoverPaths(path)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when fromDir is a file")
|
||||
}
|
||||
}
|
||||
|
||||
func containsErr(err error, substr string) bool {
|
||||
return err != nil && (err.Error() == substr || len(err.Error()) >= len(substr) && errContains(err.Error(), substr))
|
||||
}
|
||||
|
||||
func errContains(s, substr string) bool {
|
||||
for i := 0; i+len(substr) <= len(s); i++ {
|
||||
if s[i:i+len(substr)] == substr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue