From 156aab8b7dbe078238a6bc3ee9b112797df67857 Mon Sep 17 00:00:00 2001 From: Rolf Neugebauer Date: Sun, 2 Apr 2017 19:30:57 +0100 Subject: [PATCH] cli: Add support for passing meta data to a hyperkit VM Add a -data option to the HyperKit "run" backend. This either adds a string or a file to a ISO which is attached to the VM. Signed-off-by: Rolf Neugebauer --- src/cmd/moby/run_hyperkit.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/cmd/moby/run_hyperkit.go b/src/cmd/moby/run_hyperkit.go index 4d994edc4..ab9ded647 100644 --- a/src/cmd/moby/run_hyperkit.go +++ b/src/cmd/moby/run_hyperkit.go @@ -8,6 +8,7 @@ import ( log "github.com/Sirupsen/logrus" "github.com/docker/hyperkit/go" + "github.com/rneugeba/iso9660wrap" ) // Process the run arguments and execute run @@ -25,6 +26,7 @@ func runHyperKit(args []string) { mem := hyperkitCmd.Int("mem", 1024, "Amount of memory in MB") diskSz := hyperkitCmd.Int("disk-size", 0, "Size of Disk in MB") disk := hyperkitCmd.String("disk", "", "Path to disk image to used") + data := hyperkitCmd.String("data", "", "Metadata to pass to VM (either a path to a file or a string)") hyperkitCmd.Parse(args) remArgs := hyperkitCmd.Args() @@ -35,6 +37,29 @@ func runHyperKit(args []string) { } prefix := remArgs[0] + isoPath := "" + if *data != "" { + var d []byte + if _, err := os.Stat(*data); os.IsNotExist(err) { + d = []byte(*data) + } else { + d, err = ioutil.ReadFile(*data) + if err != nil { + log.Fatalf("Cannot read user data: %v", err) + } + } + isoPath = prefix + "-data.iso" + outfh, err := os.OpenFile(isoPath, os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + log.Fatalf("Cannot create user data ISO: %v", err) + } + err = iso9660wrap.WriteBuffer(outfh, d, "config") + if err != nil { + log.Fatalf("Cannot write user data ISO: %v", err) + } + outfh.Close() + } + // Run cmdline, err := ioutil.ReadFile(prefix + "-cmdline") if err != nil { @@ -52,6 +77,7 @@ func runHyperKit(args []string) { h.Kernel = prefix + "-bzImage" h.Initrd = prefix + "-initrd.img" + h.ISOImage = isoPath h.CPUs = *cpus h.Memory = *mem h.DiskSize = *diskSz