Add initial support for specifying the output type

Currently only supports kernel+initrd output but will add the rest
soon.

Signed-off-by: Justin Cormack <justin.cormack@docker.com>
This commit is contained in:
Justin Cormack 2017-03-03 14:49:47 -08:00
parent 890097dc8e
commit 2c789d84d6
14 changed files with 111 additions and 18 deletions

View File

@ -1,6 +1,10 @@
package main
import (
"archive/tar"
"bytes"
"errors"
"path"
"strconv"
"strings"
@ -8,12 +12,15 @@ import (
)
type Moby struct {
Kernel string
Init string
System []MobyImage
Database []struct {
File string
Value string
Kernel string
Init string
System []MobyImage
Files []struct {
Path string
Contents string
}
Outputs []struct {
Format string
}
}
@ -67,3 +74,54 @@ func ConfigToRun(image *MobyImage) []string {
return args
}
func Filesystem(m *Moby) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
defer tw.Close()
for _, f := range m.Files {
if f.Path == "" {
return buf, errors.New("Did not specify path for file")
}
if f.Contents == "" {
return buf, errors.New("Contents of file not specified")
}
// we need all the leading directories
parts := strings.Split(path.Dir(f.Path), "/")
root := ""
for _, p := range parts {
if p == "." || p == "/" {
continue
}
if root == "" {
root = p
} else {
root = root + "/" + p
}
hdr := &tar.Header{
Name: root,
Typeflag: tar.TypeDir,
Mode: 0700,
}
err := tw.WriteHeader(hdr)
if err != nil {
return buf, err
}
}
hdr := &tar.Header{
Name: f.Path,
Mode: 0600,
Size: int64(len(f.Contents)),
}
err := tw.WriteHeader(hdr)
if err != nil {
return buf, err
}
_, err = tw.Write([]byte(f.Contents))
if err != nil {
return buf, err
}
}
return buf, nil
}

View File

@ -69,8 +69,8 @@ func containersInitrd(containers []*bytes.Buffer) (*bytes.Buffer, error) {
return w, nil
}
func build() {
config, err := ioutil.ReadFile("moby.yaml")
func build(configfile string) {
config, err := ioutil.ReadFile(configfile)
if err != nil {
log.Fatalf("Cannot open config file: %v", err)
}
@ -131,22 +131,45 @@ func build() {
containers = append(containers, buffer)
}
// add files
buffer, err = Filesystem(m)
if err != nil {
log.Fatalf("failed to add filesystem parts: %v", err)
}
containers = append(containers, buffer)
initrd, err := containersInitrd(containers)
if err != nil {
log.Fatalf("Failed to make initrd %v", err)
}
// TODO should we tar these up? Also output to other formats
err = ioutil.WriteFile("initrd.img", initrd.Bytes(), os.FileMode(0644))
if err != nil {
log.Fatalf("could not write initrd: %v", err)
}
err = ioutil.WriteFile("bzImage", bzimage.Bytes(), os.FileMode(0644))
if err != nil {
log.Fatalf("could not write kernel: %v", err)
for _, o := range m.Outputs {
switch o.Format {
case "kernel+initrd":
err = OutputKernelInitrd(bzimage.Bytes(), initrd.Bytes())
if err != nil {
log.Fatalf("Error writing %s output: %v", o.Format, err)
}
case "":
log.Fatalf("No format specified for output")
default:
log.Fatalf("Unknown output type %s", o.Format)
}
}
}
func main() {
build()
func OutputKernelInitrd(bzimage []byte, initrd []byte) error {
err := ioutil.WriteFile("initrd.img", initrd, os.FileMode(0644))
if err != nil {
return err
}
err = ioutil.WriteFile("bzImage", bzimage, os.FileMode(0644))
if err != nil {
return err
}
return nil
}
func main() {
build("moby.yaml")
}

@ -0,0 +1 @@
Subproject commit 2d2df7bd8bda53b5a55ed04422173cedd50500ea

@ -0,0 +1 @@
Subproject commit 7f4b1adc791766938c29457bed0703fb9134421a

@ -0,0 +1 @@
Subproject commit 9c37978a95bd5c709a15883b6242714ea6709e64

@ -0,0 +1 @@
Subproject commit 6516ace03d405955f738f8965abde5d9ab37fef6

@ -0,0 +1 @@
Subproject commit 08b5f424b9271eedf6f9f0ce86cb9396ed337a42

@ -0,0 +1 @@
Subproject commit 999ef73f5d50979cf6d12afed1726325b63f9570

@ -0,0 +1 @@
Subproject commit 22c016f3df3febe0c1f6727598b6389507e03a18

@ -0,0 +1 @@
Subproject commit 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75

@ -0,0 +1 @@
Subproject commit bd40a432e4c76585ef6b72d3fd96fb9b6dc7b68d

@ -0,0 +1 @@
Subproject commit b061729afc07e77a8aa4fad0a2fd840958f1942a

@ -0,0 +1 @@
Subproject commit 92ea23a837e66f46ac9e7d04fa826602b7b0a42d

@ -0,0 +1 @@
Subproject commit 9ff6c6923cfffbcd502984b8e0c80539a94968b7