Add boot image management with ISO extraction and serving
Upload Proxmox ISOs via API or dashboard UI, extract kernel+initrd using pure-Go iso9660 library, store on disk, and serve over HTTP for PXE booting. Dynamic kernel/initrd filenames per image replace the previous hardcoded paths. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/kdomanski/iso9660"
|
||||
)
|
||||
|
||||
type ExtractResult struct {
|
||||
KernelFilename string
|
||||
InitrdFilename string
|
||||
}
|
||||
|
||||
var kernelCandidates = []string{"linux26", "vmlinuz", "bzImage"}
|
||||
var initrdCandidates = []string{"initrd.img", "initrd", "initrd.gz"}
|
||||
|
||||
func ExtractFromISO(r io.Reader, destDir string) (*ExtractResult, error) {
|
||||
tmp, err := os.CreateTemp(filepath.Dir(destDir), "iso-upload-*.tmp")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create temp file: %w", err)
|
||||
}
|
||||
tmpPath := tmp.Name()
|
||||
defer os.Remove(tmpPath)
|
||||
|
||||
if _, err := io.Copy(tmp, r); err != nil {
|
||||
tmp.Close()
|
||||
return nil, fmt.Errorf("write ISO to temp file: %w", err)
|
||||
}
|
||||
tmp.Close()
|
||||
|
||||
f, err := os.Open(tmpPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open temp ISO: %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
img, err := iso9660.OpenImage(f)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse ISO: %w", err)
|
||||
}
|
||||
|
||||
root, err := img.RootDir()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read ISO root: %w", err)
|
||||
}
|
||||
|
||||
candidateSet := make(map[string]bool)
|
||||
for _, c := range kernelCandidates {
|
||||
candidateSet[strings.ToLower(c)] = true
|
||||
}
|
||||
for _, c := range initrdCandidates {
|
||||
candidateSet[strings.ToLower(c)] = true
|
||||
}
|
||||
|
||||
found := make(map[string]*iso9660.File)
|
||||
walkDir(root, candidateSet, found)
|
||||
|
||||
kernelFile, kernelName := matchFirst(found, kernelCandidates)
|
||||
initrdFile, initrdName := matchFirst(found, initrdCandidates)
|
||||
|
||||
if kernelFile == nil {
|
||||
return nil, fmt.Errorf("no kernel found in ISO (looked for %s)", strings.Join(kernelCandidates, ", "))
|
||||
}
|
||||
if initrdFile == nil {
|
||||
return nil, fmt.Errorf("no initrd found in ISO (looked for %s)", strings.Join(initrdCandidates, ", "))
|
||||
}
|
||||
|
||||
if err := extractFile(kernelFile, filepath.Join(destDir, kernelName)); err != nil {
|
||||
return nil, fmt.Errorf("extract kernel: %w", err)
|
||||
}
|
||||
if err := extractFile(initrdFile, filepath.Join(destDir, initrdName)); err != nil {
|
||||
return nil, fmt.Errorf("extract initrd: %w", err)
|
||||
}
|
||||
|
||||
return &ExtractResult{
|
||||
KernelFilename: kernelName,
|
||||
InitrdFilename: initrdName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func walkDir(dir *iso9660.File, candidates map[string]bool, found map[string]*iso9660.File) {
|
||||
children, err := dir.GetChildren()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, child := range children {
|
||||
name := strings.ToLower(child.Name())
|
||||
if child.IsDir() {
|
||||
walkDir(child, candidates, found)
|
||||
} else if candidates[name] {
|
||||
if _, exists := found[name]; !exists {
|
||||
found[name] = child
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func matchFirst(found map[string]*iso9660.File, candidates []string) (*iso9660.File, string) {
|
||||
for _, c := range candidates {
|
||||
lower := strings.ToLower(c)
|
||||
if f, ok := found[lower]; ok {
|
||||
return f, f.Name()
|
||||
}
|
||||
}
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func extractFile(isoFile *iso9660.File, destPath string) error {
|
||||
out, err := os.Create(destPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
reader := isoFile.Reader()
|
||||
if reader == nil {
|
||||
return fmt.Errorf("cannot read %s from ISO", isoFile.Name())
|
||||
}
|
||||
_, err = io.Copy(out, reader)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
||||
"provisioning/internal/model"
|
||||
"provisioning/internal/store"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
Store *store.Images
|
||||
ImageDir string
|
||||
}
|
||||
|
||||
type UploadParams struct {
|
||||
Name string
|
||||
Kind string
|
||||
Version string
|
||||
ISO io.Reader
|
||||
}
|
||||
|
||||
var slugRegex = regexp.MustCompile(`^[a-z0-9][a-z0-9.-]*$`)
|
||||
|
||||
func (s *Service) Upload(ctx context.Context, p UploadParams) (*model.Image, error) {
|
||||
if !slugRegex.MatchString(p.Name) {
|
||||
return nil, fmt.Errorf("invalid name %q: must be lowercase alphanumeric with hyphens/dots", p.Name)
|
||||
}
|
||||
if p.Kind == "" {
|
||||
p.Kind = "proxmox"
|
||||
}
|
||||
if p.Version == "" {
|
||||
return nil, fmt.Errorf("version is required")
|
||||
}
|
||||
|
||||
if _, err := s.Store.GetByName(ctx, p.Name); err == nil {
|
||||
return nil, fmt.Errorf("image %q already exists", p.Name)
|
||||
}
|
||||
|
||||
destDir := filepath.Join(s.ImageDir, p.Name)
|
||||
if err := os.MkdirAll(destDir, 0o755); err != nil {
|
||||
return nil, fmt.Errorf("create image dir: %w", err)
|
||||
}
|
||||
|
||||
result, err := ExtractFromISO(p.ISO, destDir)
|
||||
if err != nil {
|
||||
os.RemoveAll(destDir)
|
||||
return nil, fmt.Errorf("extract ISO: %w", err)
|
||||
}
|
||||
|
||||
kernelPath := filepath.Join(p.Name, result.KernelFilename)
|
||||
initrdPath := filepath.Join(p.Name, result.InitrdFilename)
|
||||
|
||||
id, err := s.Store.Create(ctx, model.Image{
|
||||
Name: p.Name,
|
||||
Kind: p.Kind,
|
||||
Version: p.Version,
|
||||
KernelPath: kernelPath,
|
||||
InitrdPath: initrdPath,
|
||||
})
|
||||
if err != nil {
|
||||
os.RemoveAll(destDir)
|
||||
return nil, fmt.Errorf("save image record: %w", err)
|
||||
}
|
||||
|
||||
img, err := s.Store.Get(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
|
||||
func (s *Service) Delete(ctx context.Context, id int64) error {
|
||||
img, err := s.Store.Get(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
destDir := filepath.Join(s.ImageDir, img.Name)
|
||||
os.RemoveAll(destDir)
|
||||
|
||||
return s.Store.Delete(ctx, id)
|
||||
}
|
||||
Reference in New Issue
Block a user