From d6e50e72d0c25a5b0f0a1cde221d13fc61190a28 Mon Sep 17 00:00:00 2001 From: Rolf Neugebauer Date: Wed, 22 Mar 2017 14:27:56 +0000 Subject: [PATCH] moby: Add a 'run' command to execute an image on hyperkit 'moby run' will use the kernel and initrd image produced by 'moby build' and, on macOS, will run it inside a hyperkit VM. This assumes that you have a recent version of Docker for Mac installed as it re-uses the hyperkit and VPNKit from it. Signed-off-by: Rolf Neugebauer --- src/cmd/moby/main.go | 21 +++++++++++++++ src/cmd/moby/run_darwin.go | 51 ++++++++++++++++++++++++++++++++++++ src/cmd/moby/run_fallback.go | 11 ++++++++ 3 files changed, 83 insertions(+) create mode 100644 src/cmd/moby/run_darwin.go create mode 100644 src/cmd/moby/run_fallback.go diff --git a/src/cmd/moby/main.go b/src/cmd/moby/main.go index debb9fc64..6a050202a 100644 --- a/src/cmd/moby/main.go +++ b/src/cmd/moby/main.go @@ -100,6 +100,7 @@ func main() { fmt.Printf("USAGE: %s COMMAND\n\n", os.Args[0]) fmt.Printf("Commands:\n") fmt.Printf(" build Build a Moby image from a YAML file\n") + fmt.Printf(" run Run a Moby image on a local hypervisor\n") fmt.Printf(" help Print this message\n") fmt.Printf("\n") fmt.Printf("Run '%s COMMAND --help' for more information on the command\n", os.Args[0]) @@ -114,6 +115,23 @@ func main() { } buildName := buildCmd.String("name", "", "Name to use for output files") + runCmd := flag.NewFlagSet("run", flag.ExitOnError) + runCmd.Usage = func() { + fmt.Printf("USAGE: %s run [options] [prefix]\n\n", os.Args[0]) + fmt.Printf("'prefix' specifies the path to the VM image.\n") + fmt.Printf("It defaults to './moby'.\n") + fmt.Printf("\n") + fmt.Printf("Options:\n") + runCmd.PrintDefaults() + fmt.Printf("\n") + fmt.Printf("If 'data' is supplied or if 'background' is selected\n") + fmt.Printf("some per VM state is kept in a sub-directory in the ~/.moby\n") + } + runCPUs := runCmd.Int("cpus", 1, "Number of CPUs") + runMem := runCmd.Int("mem", 1024, "Amount of memory in MB") + runDiskSz := runCmd.Int("disk-size", 0, "Size of Disk in MB") + runDisk := runCmd.String("disk", "", "Path to disk image to used") + if len(os.Args) < 2 { fmt.Printf("Please specify a command.\n\n") flag.Usage() @@ -124,6 +142,9 @@ func main() { case "build": buildCmd.Parse(os.Args[2:]) build(*buildName, buildCmd.Args()) + case "run": + runCmd.Parse(os.Args[2:]) + run(*runCPUs, *runMem, *runDiskSz, *runDisk, runCmd.Args()) case "help": flag.Usage() default: diff --git a/src/cmd/moby/run_darwin.go b/src/cmd/moby/run_darwin.go new file mode 100644 index 000000000..7d8676c35 --- /dev/null +++ b/src/cmd/moby/run_darwin.go @@ -0,0 +1,51 @@ +// +build darwin + +package main + +import ( + "io/ioutil" + "log" + "os" + "os/user" + + "github.com/docker/hyperkit/go" +) + +func run(cpus, mem, diskSz int, disk string, args []string) { + prefix := "moby" + if len(args) > 0 { + prefix = args[0] + } + + cmdline, err := ioutil.ReadFile(prefix + "-cmdline") + if err != nil { + log.Fatalf("Cannot open cmdline file: %v", err) + } + + if diskSz != 0 && disk == "" { + disk = prefix + "-disk.img" + } + + h, err := hyperkit.New("", "", "auto", disk) + if err != nil { + log.Fatalln("Error creating hyperkit: ", err) + } + + h.Kernel = prefix + "-bzImage" + h.Initrd = prefix + "-initrd.img" + h.CPUs = cpus + h.Memory = mem + h.DiskSize = diskSz + + err = h.Run(string(cmdline)) + if err != nil { + log.Fatalf("Cannot run hyperkit: %v", err) + } +} + +func getHome() string { + if usr, err := user.Current(); err == nil { + return usr.HomeDir + } + return os.Getenv("HOME") +} diff --git a/src/cmd/moby/run_fallback.go b/src/cmd/moby/run_fallback.go new file mode 100644 index 000000000..992ed509f --- /dev/null +++ b/src/cmd/moby/run_fallback.go @@ -0,0 +1,11 @@ +// +build !darwin + +package main + +import ( + "log" +) + +func run(cpus, mem, diskSz int, userData string, args []string) { + log.Fatalf("'run' is not support yet on your OS") +}