diff --git a/moby/config.go b/moby/config.go index 4128005d6..2d7326e65 100644 --- a/moby/config.go +++ b/moby/config.go @@ -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 +} diff --git a/moby/main.go b/moby/main.go index 013f6144d..9d8a8c522 100644 --- a/moby/main.go +++ b/moby/main.go @@ -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") } diff --git a/tools/infrakit.hyperkit/vendor/github.com/Masterminds/sprig b/tools/infrakit.hyperkit/vendor/github.com/Masterminds/sprig new file mode 160000 index 000000000..2d2df7bd8 --- /dev/null +++ b/tools/infrakit.hyperkit/vendor/github.com/Masterminds/sprig @@ -0,0 +1 @@ +Subproject commit 2d2df7bd8bda53b5a55ed04422173cedd50500ea diff --git a/tools/infrakit.hyperkit/vendor/github.com/Sirupsen/logrus b/tools/infrakit.hyperkit/vendor/github.com/Sirupsen/logrus new file mode 160000 index 000000000..7f4b1adc7 --- /dev/null +++ b/tools/infrakit.hyperkit/vendor/github.com/Sirupsen/logrus @@ -0,0 +1 @@ +Subproject commit 7f4b1adc791766938c29457bed0703fb9134421a diff --git a/tools/infrakit.hyperkit/vendor/github.com/aokoli/goutils b/tools/infrakit.hyperkit/vendor/github.com/aokoli/goutils new file mode 160000 index 000000000..9c37978a9 --- /dev/null +++ b/tools/infrakit.hyperkit/vendor/github.com/aokoli/goutils @@ -0,0 +1 @@ +Subproject commit 9c37978a95bd5c709a15883b6242714ea6709e64 diff --git a/tools/infrakit.hyperkit/vendor/github.com/docker/infrakit b/tools/infrakit.hyperkit/vendor/github.com/docker/infrakit new file mode 160000 index 000000000..6516ace03 --- /dev/null +++ b/tools/infrakit.hyperkit/vendor/github.com/docker/infrakit @@ -0,0 +1 @@ +Subproject commit 6516ace03d405955f738f8965abde5d9ab37fef6 diff --git a/tools/infrakit.hyperkit/vendor/github.com/gorilla/context b/tools/infrakit.hyperkit/vendor/github.com/gorilla/context new file mode 160000 index 000000000..08b5f424b --- /dev/null +++ b/tools/infrakit.hyperkit/vendor/github.com/gorilla/context @@ -0,0 +1 @@ +Subproject commit 08b5f424b9271eedf6f9f0ce86cb9396ed337a42 diff --git a/tools/infrakit.hyperkit/vendor/github.com/gorilla/mux b/tools/infrakit.hyperkit/vendor/github.com/gorilla/mux new file mode 160000 index 000000000..999ef73f5 --- /dev/null +++ b/tools/infrakit.hyperkit/vendor/github.com/gorilla/mux @@ -0,0 +1 @@ +Subproject commit 999ef73f5d50979cf6d12afed1726325b63f9570 diff --git a/tools/infrakit.hyperkit/vendor/github.com/gorilla/rpc b/tools/infrakit.hyperkit/vendor/github.com/gorilla/rpc new file mode 160000 index 000000000..22c016f3d --- /dev/null +++ b/tools/infrakit.hyperkit/vendor/github.com/gorilla/rpc @@ -0,0 +1 @@ +Subproject commit 22c016f3df3febe0c1f6727598b6389507e03a18 diff --git a/tools/infrakit.hyperkit/vendor/github.com/inconshreveable/mousetrap b/tools/infrakit.hyperkit/vendor/github.com/inconshreveable/mousetrap new file mode 160000 index 000000000..76626ae9c --- /dev/null +++ b/tools/infrakit.hyperkit/vendor/github.com/inconshreveable/mousetrap @@ -0,0 +1 @@ +Subproject commit 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75 diff --git a/tools/infrakit.hyperkit/vendor/github.com/jmespath/go-jmespath b/tools/infrakit.hyperkit/vendor/github.com/jmespath/go-jmespath new file mode 160000 index 000000000..bd40a432e --- /dev/null +++ b/tools/infrakit.hyperkit/vendor/github.com/jmespath/go-jmespath @@ -0,0 +1 @@ +Subproject commit bd40a432e4c76585ef6b72d3fd96fb9b6dc7b68d diff --git a/tools/infrakit.hyperkit/vendor/github.com/satori/go.uuid b/tools/infrakit.hyperkit/vendor/github.com/satori/go.uuid new file mode 160000 index 000000000..b061729af --- /dev/null +++ b/tools/infrakit.hyperkit/vendor/github.com/satori/go.uuid @@ -0,0 +1 @@ +Subproject commit b061729afc07e77a8aa4fad0a2fd840958f1942a diff --git a/tools/infrakit.hyperkit/vendor/github.com/spf13/cobra b/tools/infrakit.hyperkit/vendor/github.com/spf13/cobra new file mode 160000 index 000000000..92ea23a83 --- /dev/null +++ b/tools/infrakit.hyperkit/vendor/github.com/spf13/cobra @@ -0,0 +1 @@ +Subproject commit 92ea23a837e66f46ac9e7d04fa826602b7b0a42d diff --git a/tools/infrakit.hyperkit/vendor/github.com/spf13/pflag b/tools/infrakit.hyperkit/vendor/github.com/spf13/pflag new file mode 160000 index 000000000..9ff6c6923 --- /dev/null +++ b/tools/infrakit.hyperkit/vendor/github.com/spf13/pflag @@ -0,0 +1 @@ +Subproject commit 9ff6c6923cfffbcd502984b8e0c80539a94968b7