local-server/cmd/dkl-local-server/boot-tar.go
2022-05-31 09:52:43 +02:00

63 lines
1.0 KiB
Go

package main
import (
"archive/tar"
"bytes"
"io"
"io/ioutil"
"log"
"os"
)
func rmTempFile(f *os.File) {
f.Close()
if err := os.Remove(f.Name()); err != nil {
log.Print("failed to remove ", f.Name(), ": ", err)
}
}
func buildBootTar(out io.Writer, ctx *renderContext) (err error) {
arch := tar.NewWriter(out)
defer arch.Close()
archAdd := func(path string, ba []byte) (err error) {
err = arch.WriteHeader(&tar.Header{Name: path, Mode: 0640, Size: int64(len(ba))})
if err != nil {
return
}
_, err = arch.Write(ba)
return
}
// kernel
kernelPath, err := ctx.distFetch("kernels", ctx.Host.Kernel)
if err != nil {
return
}
kernelBytes, err := ioutil.ReadFile(kernelPath)
if err != nil {
return
}
err = archAdd("current/vmlinuz", kernelBytes)
if err != nil {
return
}
// initrd
initrd := new(bytes.Buffer)
err = buildInitrdV2(initrd, ctx)
if err != nil {
return
}
err = archAdd("current/initrd", initrd.Bytes())
if err != nil {
return
}
// done
return nil
}