migrate boot.tar to initrd-v2

This commit is contained in:
Mikaël Cluseau 2022-05-31 09:52:43 +02:00
parent dacfc8c6ce
commit 645c617956
2 changed files with 24 additions and 60 deletions

View File

@ -1,5 +1,5 @@
# ------------------------------------------------------------------------ # ------------------------------------------------------------------------
from mcluseau/golang-builder:1.18.0 as build from mcluseau/golang-builder:1.18.2 as build
# ------------------------------------------------------------------------ # ------------------------------------------------------------------------
from debian:stretch from debian:stretch

View File

@ -2,11 +2,11 @@ package main
import ( import (
"archive/tar" "archive/tar"
"fmt" "bytes"
"io" "io"
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath"
) )
func rmTempFile(f *os.File) { func rmTempFile(f *os.File) {
@ -21,7 +21,7 @@ func buildBootTar(out io.Writer, ctx *renderContext) (err error) {
defer arch.Close() defer arch.Close()
archAdd := func(path string, ba []byte) (err error) { archAdd := func(path string, ba []byte) (err error) {
err = arch.WriteHeader(&tar.Header{Name: path, Size: int64(len(ba))}) err = arch.WriteHeader(&tar.Header{Name: path, Mode: 0640, Size: int64(len(ba))})
if err != nil { if err != nil {
return return
} }
@ -29,70 +29,34 @@ func buildBootTar(out io.Writer, ctx *renderContext) (err error) {
return return
} }
// config // kernel
cfgBytes, cfg, err := ctx.Config() kernelPath, err := ctx.distFetch("kernels", ctx.Host.Kernel)
if err != nil { if err != nil {
return err return
} }
archAdd("config.yaml", cfgBytes) kernelBytes, err := ioutil.ReadFile(kernelPath)
if err != nil {
// add "current" elements return
type distCopy struct {
Src []string
Dst string
} }
// kernel and initrd err = archAdd("current/vmlinuz", kernelBytes)
copies := []distCopy{ if err != nil {
{Src: []string{"kernels", ctx.Host.Kernel}, Dst: "current/vmlinuz"}, return
{Src: []string{"initrd", ctx.Host.Initrd}, Dst: "current/initrd"}, }
// initrd
initrd := new(bytes.Buffer)
err = buildInitrdV2(initrd, ctx)
if err != nil {
return
} }
// layers err = archAdd("current/initrd", initrd.Bytes())
for _, layer := range cfg.Layers { if err != nil {
layerVersion := ctx.Host.Versions[layer] return
if layerVersion == "" {
return fmt.Errorf("layer %q not mapped to a version", layer)
}
copies = append(copies,
distCopy{
Src: []string{"layers", layer, layerVersion},
Dst: filepath.Join("current", "layers", layer+".fs"),
})
}
for _, copy := range copies {
outPath, err := ctx.distFetch(copy.Src...)
if err != nil {
return err
}
f, err := os.Open(outPath)
if err != nil {
return err
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return err
}
if err = arch.WriteHeader(&tar.Header{
Name: copy.Dst,
Size: stat.Size(),
}); err != nil {
return err
}
_, err = io.Copy(arch, f)
if err != nil {
return err
}
} }
// done
return nil return nil
} }