Compare commits
2 Commits
dacfc8c6ce
...
78947747be
Author | SHA1 | Date | |
---|---|---|---|
|
78947747be | ||
|
645c617956 |
@ -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
|
||||||
|
@ -2,11 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
"archive/tar"
|
||||||
"fmt"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
|
"novit.tech/direktil/local-server/pkg/utf16"
|
||||||
)
|
)
|
||||||
|
|
||||||
func rmTempFile(f *os.File) {
|
func rmTempFile(f *os.File) {
|
||||||
@ -21,7 +23,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 +31,95 @@ 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"},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// layers
|
// initrd
|
||||||
for _, layer := range cfg.Layers {
|
initrd := new(bytes.Buffer)
|
||||||
layerVersion := ctx.Host.Versions[layer]
|
err = buildInitrdV2(initrd, ctx)
|
||||||
if layerVersion == "" {
|
if err != nil {
|
||||||
return fmt.Errorf("layer %q not mapped to a version", layer)
|
return
|
||||||
}
|
|
||||||
|
|
||||||
copies = append(copies,
|
|
||||||
distCopy{
|
|
||||||
Src: []string{"layers", layer, layerVersion},
|
|
||||||
Dst: filepath.Join("current", "layers", layer+".fs"),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, copy := range copies {
|
err = archAdd("current/initrd", initrd.Bytes())
|
||||||
outPath, err := ctx.distFetch(copy.Src...)
|
if err != nil {
|
||||||
if err != nil {
|
return
|
||||||
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
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildBootEFITar(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
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
prefix = "EFI/dkl/"
|
||||||
|
efiPrefix = "\\EFI\\dkl\\"
|
||||||
|
)
|
||||||
|
|
||||||
|
// boot.csv
|
||||||
|
// -> annoyingly it's UTF-16...
|
||||||
|
bootCsvBytes := utf16.FromUTF8([]byte("" +
|
||||||
|
"current_kernel.efi,dkl current,initrd=" + efiPrefix + "current_initrd.img,Direktil current\n" +
|
||||||
|
"previous_kernel.efi,dkl previous,initrd=" + efiPrefix + "previous_initrd.img,Direktil previous\n"))
|
||||||
|
|
||||||
|
err = archAdd(prefix+"BOOT.CSV", []byte(bootCsvBytes))
|
||||||
|
if err != nil {
|
||||||
|
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(prefix+"current_kernel.efi", kernelBytes)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// initrd
|
||||||
|
initrd := new(bytes.Buffer)
|
||||||
|
err = buildInitrdV2(initrd, ctx)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = archAdd(prefix+"current_initrd.img", initrd.Bytes())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// done
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -56,6 +56,9 @@ func (ws *wsHost) register(rws *restful.WebService, alterRB func(*restful.RouteB
|
|||||||
b("boot.tar").
|
b("boot.tar").
|
||||||
Produces(mime.TAR).
|
Produces(mime.TAR).
|
||||||
Doc("Get the " + ws.hostDoc + "'s /boot archive (ie: for metal upgrades)"),
|
Doc("Get the " + ws.hostDoc + "'s /boot archive (ie: for metal upgrades)"),
|
||||||
|
b("boot-efi.tar").
|
||||||
|
Produces(mime.TAR).
|
||||||
|
Doc("Get the " + ws.hostDoc + "'s /boot archive (ie: for metal upgrades)"),
|
||||||
|
|
||||||
// read-only ISO support
|
// read-only ISO support
|
||||||
b("boot.iso").
|
b("boot.iso").
|
||||||
@ -168,6 +171,8 @@ func renderHost(w http.ResponseWriter, r *http.Request, what string, host *local
|
|||||||
|
|
||||||
case "boot.tar":
|
case "boot.tar":
|
||||||
err = renderCtx(w, r, ctx, what, buildBootTar)
|
err = renderCtx(w, r, ctx, what, buildBootTar)
|
||||||
|
case "boot-efi.tar":
|
||||||
|
err = renderCtx(w, r, ctx, what, buildBootEFITar)
|
||||||
|
|
||||||
case "boot.img":
|
case "boot.img":
|
||||||
err = renderCtx(w, r, ctx, what, buildBootImg)
|
err = renderCtx(w, r, ctx, what, buildBootImg)
|
||||||
|
29
pkg/utf16/utf16.go
Normal file
29
pkg/utf16/utf16.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package utf16
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
|
"unicode/utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
func FromUTF8(data []byte) (res []byte) {
|
||||||
|
endian := binary.LittleEndian
|
||||||
|
|
||||||
|
res = make([]byte, (len(data)+1)*2)
|
||||||
|
|
||||||
|
res = res[0:2]
|
||||||
|
endian.PutUint16(res, 0xfeff)
|
||||||
|
|
||||||
|
for len(data) > 0 {
|
||||||
|
r, size := utf8.DecodeRune(data)
|
||||||
|
if r > 65535 {
|
||||||
|
panic(fmt.Errorf("r=0x%x > 0xffff", r))
|
||||||
|
}
|
||||||
|
|
||||||
|
slen := len(res)
|
||||||
|
res = res[0 : slen+2]
|
||||||
|
endian.PutUint16(res[slen:], uint16(r))
|
||||||
|
data = data[size:]
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user