local-server/cmd/dkl-local-server/boot-img.go

169 lines
2.7 KiB
Go
Raw Normal View History

2018-11-13 03:44:15 +00:00
package main
import (
"archive/tar"
2018-11-15 07:07:10 +00:00
"compress/gzip"
2019-02-27 23:15:35 +00:00
"fmt"
2018-11-13 03:44:15 +00:00
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"github.com/pierrec/lz4"
)
func buildBootImg(out io.Writer, ctx *renderContext) (err error) {
bootImg, err := ioutil.TempFile(os.TempDir(), "boot.img-")
if err != nil {
return
}
defer rmTempFile(bootImg)
err = setupBootImage(bootImg, ctx)
if err != nil {
return
}
// send the result
bootImg.Seek(0, os.SEEK_SET)
2018-11-15 07:07:10 +00:00
io.Copy(out, bootImg)
return
}
2018-11-13 03:44:15 +00:00
2018-11-15 07:07:10 +00:00
func buildBootImgLZ4(out io.Writer, ctx *renderContext) (err error) {
2018-11-13 03:44:15 +00:00
lz4Out := lz4.NewWriter(out)
2018-11-15 07:07:10 +00:00
if err = buildBootImg(lz4Out, ctx); err != nil {
return
}
2018-11-13 03:44:15 +00:00
lz4Out.Close()
2018-11-15 07:07:10 +00:00
return
}
func buildBootImgGZ(out io.Writer, ctx *renderContext) (err error) {
gzOut := gzip.NewWriter(out)
if err = buildBootImg(gzOut, ctx); err != nil {
return
}
2018-11-13 03:44:15 +00:00
2018-11-15 07:07:10 +00:00
gzOut.Close()
2018-11-13 03:44:15 +00:00
return
}
func setupBootImage(bootImg *os.File, ctx *renderContext) (err error) {
2019-02-06 03:27:20 +00:00
path, err := ctx.distFetch("grub-support", "1.0.0")
2018-11-13 03:44:15 +00:00
if err != nil {
return
}
2019-02-06 03:27:20 +00:00
baseImage, err := os.Open(path)
if err != nil {
return
}
2018-11-13 03:44:15 +00:00
2019-02-06 03:27:20 +00:00
defer baseImage.Close()
baseImageGz, err := gzip.NewReader(baseImage)
if err != nil {
return
}
defer baseImageGz.Close()
_, err = io.Copy(bootImg, baseImageGz)
2018-11-13 03:44:15 +00:00
if err != nil {
return
}
2019-02-06 03:27:20 +00:00
devb, err := exec.Command("losetup", "--find", "--show", "--partscan", bootImg.Name()).CombinedOutput()
if err != nil {
return
}
dev := strings.TrimSpace(string(devb))
defer func() {
log.Print("detaching ", dev)
run("losetup", "-d", dev)
}()
log.Print("device: ", dev)
2018-11-13 03:44:15 +00:00
tempDir := bootImg.Name() + ".p1.mount"
err = os.Mkdir(tempDir, 0755)
if err != nil {
return
}
defer func() {
2019-02-06 03:27:20 +00:00
log.Print("removing ", tempDir)
2018-11-13 03:44:15 +00:00
os.RemoveAll(tempDir)
}()
err = syscall.Mount(dev+"p1", tempDir, "vfat", 0, "")
if err != nil {
2019-02-27 23:15:35 +00:00
return fmt.Errorf("failed to mount %s to %s: %v", dev+"p1", tempDir, err)
2018-11-13 03:44:15 +00:00
}
defer func() {
2019-02-06 03:27:20 +00:00
log.Print("unmounting ", tempDir)
2018-11-13 03:44:15 +00:00
syscall.Unmount(tempDir, 0)
}()
// add system elements
tarOut, tarIn := io.Pipe()
go func() {
err2 := buildBootTar(tarIn, ctx)
tarIn.Close()
if err2 != nil {
err = err2
}
}()
defer tarOut.Close()
tarRd := tar.NewReader(tarOut)
for {
hdr, err := tarRd.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
log.Print("tar: extracting ", hdr.Name)
outPath := filepath.Join(tempDir, hdr.Name)
os.MkdirAll(filepath.Dir(outPath), 0755)
f, err := os.Create(outPath)
if err != nil {
return err
}
_, err = io.Copy(f, tarRd)
f.Close()
if err != nil {
return err
}
}
return
}
func run(program string, args ...string) (err error) {
cmd := exec.Command(program, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}