Compare commits
No commits in common. "78947747be774e76d33ae3e6ba10b3ba87acfe3e" and "dacfc8c6ce1abeef1b438f69be189efa2e0a1b75" have entirely different histories.
78947747be
...
dacfc8c6ce
@ -1,5 +1,5 @@
|
|||||||
# ------------------------------------------------------------------------
|
# ------------------------------------------------------------------------
|
||||||
from mcluseau/golang-builder:1.18.2 as build
|
from mcluseau/golang-builder:1.18.0 as build
|
||||||
|
|
||||||
# ------------------------------------------------------------------------
|
# ------------------------------------------------------------------------
|
||||||
from debian:stretch
|
from debian:stretch
|
||||||
|
@ -2,13 +2,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
"archive/tar"
|
||||||
"bytes"
|
"fmt"
|
||||||
"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) {
|
||||||
@ -23,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, Mode: 0640, Size: int64(len(ba))})
|
err = arch.WriteHeader(&tar.Header{Name: path, Size: int64(len(ba))})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -31,95 +29,70 @@ func buildBootTar(out io.Writer, ctx *renderContext) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// kernel
|
// config
|
||||||
kernelPath, err := ctx.distFetch("kernels", ctx.Host.Kernel)
|
cfgBytes, cfg, err := ctx.Config()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
kernelBytes, err := ioutil.ReadFile(kernelPath)
|
archAdd("config.yaml", cfgBytes)
|
||||||
if err != nil {
|
|
||||||
return
|
// add "current" elements
|
||||||
|
type distCopy struct {
|
||||||
|
Src []string
|
||||||
|
Dst string
|
||||||
}
|
}
|
||||||
|
|
||||||
err = archAdd("current/vmlinuz", kernelBytes)
|
// kernel and initrd
|
||||||
if err != nil {
|
copies := []distCopy{
|
||||||
return
|
{Src: []string{"kernels", ctx.Host.Kernel}, Dst: "current/vmlinuz"},
|
||||||
|
{Src: []string{"initrd", ctx.Host.Initrd}, Dst: "current/initrd"},
|
||||||
}
|
}
|
||||||
|
|
||||||
// initrd
|
// layers
|
||||||
initrd := new(bytes.Buffer)
|
for _, layer := range cfg.Layers {
|
||||||
err = buildInitrdV2(initrd, ctx)
|
layerVersion := ctx.Host.Versions[layer]
|
||||||
if err != nil {
|
if layerVersion == "" {
|
||||||
return
|
return fmt.Errorf("layer %q not mapped to a version", layer)
|
||||||
}
|
|
||||||
|
|
||||||
err = archAdd("current/initrd", initrd.Bytes())
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
copies = append(copies,
|
||||||
|
distCopy{
|
||||||
|
Src: []string{"layers", layer, layerVersion},
|
||||||
|
Dst: filepath.Join("current", "layers", layer+".fs"),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
for _, copy := range copies {
|
||||||
prefix = "EFI/dkl/"
|
outPath, err := ctx.distFetch(copy.Src...)
|
||||||
efiPrefix = "\\EFI\\dkl\\"
|
if err != nil {
|
||||||
)
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// boot.csv
|
f, err := os.Open(outPath)
|
||||||
// -> annoyingly it's UTF-16...
|
if err != nil {
|
||||||
bootCsvBytes := utf16.FromUTF8([]byte("" +
|
return err
|
||||||
"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))
|
defer f.Close()
|
||||||
if err != nil {
|
|
||||||
return
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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,9 +56,6 @@ 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").
|
||||||
@ -171,8 +168,6 @@ 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)
|
||||||
|
@ -1,29 +0,0 @@
|
|||||||
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