From 645c617956f1f2cacb8c7f27bae904a3744c55dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Cluseau?= Date: Tue, 31 May 2022 09:52:43 +0200 Subject: [PATCH] migrate boot.tar to initrd-v2 --- Dockerfile | 2 +- cmd/dkl-local-server/boot-tar.go | 82 +++++++++----------------------- 2 files changed, 24 insertions(+), 60 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9e48838..2d3d34b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/cmd/dkl-local-server/boot-tar.go b/cmd/dkl-local-server/boot-tar.go index 8ab5f25..34cf6cc 100644 --- a/cmd/dkl-local-server/boot-tar.go +++ b/cmd/dkl-local-server/boot-tar.go @@ -2,11 +2,11 @@ package main import ( "archive/tar" - "fmt" + "bytes" "io" + "io/ioutil" "log" "os" - "path/filepath" ) func rmTempFile(f *os.File) { @@ -21,7 +21,7 @@ func buildBootTar(out io.Writer, ctx *renderContext) (err error) { defer arch.Close() 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 { return } @@ -29,70 +29,34 @@ func buildBootTar(out io.Writer, ctx *renderContext) (err error) { return } - // config - cfgBytes, cfg, err := ctx.Config() + // kernel + kernelPath, err := ctx.distFetch("kernels", ctx.Host.Kernel) if err != nil { - return err + return } - archAdd("config.yaml", cfgBytes) - - // add "current" elements - type distCopy struct { - Src []string - Dst string + kernelBytes, err := ioutil.ReadFile(kernelPath) + if err != nil { + return } - // kernel and initrd - copies := []distCopy{ - {Src: []string{"kernels", ctx.Host.Kernel}, Dst: "current/vmlinuz"}, - {Src: []string{"initrd", ctx.Host.Initrd}, Dst: "current/initrd"}, + err = archAdd("current/vmlinuz", kernelBytes) + if err != nil { + return + } + + // initrd + initrd := new(bytes.Buffer) + err = buildInitrdV2(initrd, ctx) + if err != nil { + return } - // layers - for _, layer := range cfg.Layers { - layerVersion := ctx.Host.Versions[layer] - 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 - } + err = archAdd("current/initrd", initrd.Bytes()) + if err != nil { + return } + // done return nil }