boot.img: simplified generation

This commit is contained in:
Mikaël Cluseau
2019-02-06 14:27:20 +11:00
parent d79683b130
commit c185bc1f7d
7 changed files with 48 additions and 179 deletions

View File

@ -27,7 +27,6 @@ func loadSrc() {
if err != nil {
log.Fatal("failed to load config from dir: ", err)
}
}
func main() {
@ -70,6 +69,11 @@ func main() {
}
ips = append(ips, host.IPs...)
if ctx.Group.Versions["modules"] == "" {
// default modules' version to kernel's version
ctx.Group.Versions["modules"] = ctx.Group.Kernel
}
dst.Hosts = append(dst.Hosts, &localconfig.Host{
Name: host.Name,
MACs: macs,

View File

@ -22,18 +22,6 @@ func buildBootImg(out io.Writer, ctx *renderContext) (err error) {
}
defer rmTempFile(bootImg)
// 2MB + 2GB + 2MB + 34 sectors
bootImg.Truncate(2<<30 + 4<<20 + 34*512)
// partition
err = run("sgdisk",
"--new=0:4096:+2G", "--typecode=0:EF00", "-c", "0:boot",
"--new=0:0:+2M", "--typecode=0:EF02", "-c", "0:BIOS-BOOT",
"--hybrid=1:2", "--print", bootImg.Name())
if err != nil {
return
}
err = setupBootImage(bootImg, ctx)
if err != nil {
return
@ -68,21 +56,43 @@ func buildBootImgGZ(out io.Writer, ctx *renderContext) (err error) {
}
func setupBootImage(bootImg *os.File, ctx *renderContext) (err error) {
path, err := ctx.distFetch("grub-support", "1.0.0")
if err != nil {
return
}
baseImage, err := os.Open(path)
if err != nil {
return
}
defer baseImage.Close()
baseImageGz, err := gzip.NewReader(baseImage)
if err != nil {
return
}
defer baseImageGz.Close()
_, err = io.Copy(bootImg, baseImageGz)
if err != nil {
return
}
devb, err := exec.Command("losetup", "--find", "--show", "--partscan", bootImg.Name()).CombinedOutput()
if err != nil {
return
}
dev := strings.TrimSpace(string(devb))
defer run("losetup", "-d", dev)
defer func() {
log.Print("detaching ", dev)
run("losetup", "-d", dev)
}()
log.Print("device: ", dev)
err = run("mkfs.vfat", "-n", "DKLBOOT", dev+"p1")
if err != nil {
return
}
tempDir := bootImg.Name() + ".p1.mount"
err = os.Mkdir(tempDir, 0755)
@ -91,7 +101,7 @@ func setupBootImage(bootImg *os.File, ctx *renderContext) (err error) {
}
defer func() {
log.Print("Removing ", tempDir)
log.Print("removing ", tempDir)
os.RemoveAll(tempDir)
}()
@ -101,16 +111,10 @@ func setupBootImage(bootImg *os.File, ctx *renderContext) (err error) {
}
defer func() {
log.Print("Unmounting ", tempDir)
log.Print("unmounting ", tempDir)
syscall.Unmount(tempDir, 0)
}()
// setup grub
err = run("/scripts/grub_install.sh", bootImg.Name(), dev, tempDir)
if err != nil {
return
}
// add system elements
tarOut, tarIn := io.Pipe()
go func() {

View File

@ -1,19 +1,18 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
cpio "github.com/cavaliercoder/go-cpio"
yaml "gopkg.in/yaml.v2"
)
func renderConfig(w http.ResponseWriter, r *http.Request, ctx *renderContext) error {
func renderConfig(w http.ResponseWriter, r *http.Request, ctx *renderContext, asJson bool) (err error) {
log.Printf("sending config for %q", ctx.Host.Name)
_, cfg, err := ctx.Config()
@ -21,14 +20,12 @@ func renderConfig(w http.ResponseWriter, r *http.Request, ctx *renderContext) er
return err
}
ba, err := yaml.Marshal(cfg)
if err != nil {
return err
if asJson {
err = json.NewEncoder(w).Encode(cfg)
} else {
err = yaml.NewEncoder(w).Encode(cfg)
}
w.Header().Set("Content-Type", "application/yaml")
http.ServeContent(w, r, "config.yaml", time.Unix(0, 0), bytes.NewReader(ba))
return nil
}

View File

@ -34,6 +34,9 @@ func (ws *wsHost) register(rws *restful.WebService, alterRB func(*restful.RouteB
Produces(mime.YAML).
Doc("Get the " + ws.hostDoc + "'s configuration"),
b("config.json").
Doc("Get the " + ws.hostDoc + "'s configuration (as JSON)"),
// metal/local HDD install
b("boot.img").
Produces(mime.DISK).
@ -127,7 +130,10 @@ func renderHost(w http.ResponseWriter, r *http.Request, what string, host *local
switch what {
case "config":
err = renderConfig(w, r, ctx)
err = renderConfig(w, r, ctx, false)
case "config.json":
err = renderConfig(w, r, ctx, true)
case "ipxe":
err = renderIPXE(w, ctx)