local-server/cmd/dkl-local-server/initrd.go

112 lines
1.9 KiB
Go
Raw Normal View History

2018-06-12 10:09:47 +00:00
package main
import (
2019-02-06 03:27:20 +00:00
"encoding/json"
2018-06-12 10:09:47 +00:00
"fmt"
"io"
"log"
"net/http"
"os"
2022-04-28 08:01:21 +00:00
cpio "github.com/cavaliergopher/cpio"
2018-06-12 10:09:47 +00:00
yaml "gopkg.in/yaml.v2"
)
2019-02-06 03:27:20 +00:00
func renderConfig(w http.ResponseWriter, r *http.Request, ctx *renderContext, asJson bool) (err error) {
2018-06-12 10:09:47 +00:00
log.Printf("sending config for %q", ctx.Host.Name)
_, cfg, err := ctx.Config()
if err != nil {
return err
}
2019-02-06 03:27:20 +00:00
if asJson {
err = json.NewEncoder(w).Encode(cfg)
} else {
err = yaml.NewEncoder(w).Encode(cfg)
2018-06-12 10:09:47 +00:00
}
return nil
}
func buildInitrd(out io.Writer, ctx *renderContext) error {
_, cfg, err := ctx.Config()
if err != nil {
return err
}
// send initrd basis
2018-12-10 10:59:24 +00:00
initrdPath, err := ctx.distFetch("initrd", ctx.Host.Initrd)
2018-06-12 10:09:47 +00:00
if err != nil {
return err
}
err = writeFile(out, initrdPath)
if err != nil {
return err
}
// and our extra archive
archive := cpio.NewWriter(out)
// - required dirs
for _, dir := range []string{
"boot",
"boot/current",
"boot/current/layers",
} {
archive.WriteHeader(&cpio.Header{
Name: dir,
2022-04-28 08:01:21 +00:00
Mode: cpio.FileMode(0600 | os.ModeDir),
2018-06-12 10:09:47 +00:00
})
}
// - the layers
for _, layer := range cfg.Layers {
2018-12-10 10:59:24 +00:00
layerVersion := ctx.Host.Versions[layer]
2018-06-12 10:09:47 +00:00
if layerVersion == "" {
return fmt.Errorf("layer %q not mapped to a version", layer)
}
path, err := ctx.distFetch("layers", layer, layerVersion)
if err != nil {
return err
}
stat, err := os.Stat(path)
if err != nil {
return err
}
archive.WriteHeader(&cpio.Header{
Name: "boot/current/layers/" + layer + ".fs",
Mode: 0600,
Size: stat.Size(),
})
if err = writeFile(archive, path); err != nil {
return err
}
}
// - the configuration
ba, err := yaml.Marshal(cfg)
if err != nil {
return err
}
archive.WriteHeader(&cpio.Header{
Name: "boot/config.yaml",
Mode: 0600,
Size: int64(len(ba)),
})
archive.Write(ba)
// finalize the archive
archive.Flush()
archive.Close()
return nil
}