From a16d1683f88e6891434eef0bfa5047a7accaf54a Mon Sep 17 00:00:00 2001 From: Rolf Neugebauer Date: Mon, 15 May 2017 11:37:29 +0100 Subject: [PATCH] cmd: Add a -state argument to VMware runner The state directory is used to store any user data disk images and the .vmx file. At run time, it is also used to keep various other files, such as the nvram, log and lock files, etc. While at it, also clean up the code a little more and fix the handling of the "-disk-size" argument when no "-disk" argument is specified. This brings it in line with the other runners. Signed-off-by: Rolf Neugebauer --- .gitignore | 2 ++ src/cmd/linuxkit/run_vmware.go | 39 ++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 981e11477..6673d67d4 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ Dockerfile.media *.tag *.iso *.vhd +*.vmdk +*.vmdk.lck *.tar *.gz *.vhdx diff --git a/src/cmd/linuxkit/run_vmware.go b/src/cmd/linuxkit/run_vmware.go index 864c63df7..c4a7f726f 100644 --- a/src/cmd/linuxkit/run_vmware.go +++ b/src/cmd/linuxkit/run_vmware.go @@ -78,6 +78,7 @@ func runVMware(args []string) { mem := flags.Int("mem", 1024, "Amount of memory in MB") disk := flags.String("disk", "", "Path to disk image to use") diskSz := flags.String("disk-size", "", "Size of the disk to create, only created if it doesn't exist") + state := flags.String("state", "", "Path to directory to keep VM state in") if err := flags.Parse(args); err != nil { log.Fatal("Unable to parse args") @@ -91,19 +92,26 @@ func runVMware(args []string) { } prefix := remArgs[0] + if *state == "" { + *state = prefix + "-state" + } + if err := os.MkdirAll(*state, 0755); err != nil { + log.Fatalf("Could not create state directory: %v", err) + } + var vmrunPath, vmDiskManagerPath string var vmrunArgs []string if runtime.GOOS == "windows" { vmrunPath = "C:\\Program\\ files\\VMware Workstation\\vmrun.exe" vmDiskManagerPath = "C:\\Program\\ files\\VMware Workstation\\vmware-vdiskmanager.exe" - vmrunArgs = []string{"-T", "ws", "start", prefix + ".vmx"} + vmrunArgs = []string{"-T", "ws", "start"} } if runtime.GOOS == "darwin" { vmrunPath = "/Applications/VMware Fusion.app/Contents/Library/vmrun" vmDiskManagerPath = "/Applications/VMware Fusion.app/Contents/Library/vmware-vdiskmanager" - vmrunArgs = []string{"-T", "fusion", "start", prefix + ".vmx"} + vmrunArgs = []string{"-T", "fusion", "start"} } if runtime.GOOS == "linux" { @@ -115,7 +123,7 @@ func runVMware(args []string) { log.Fatalf("Unable to find %s within the $PATH", vmrunPath) } vmrunPath = fullVMrunPath - vmrunArgs = []string{"-T", "ws", "start", prefix + ".vmx"} + vmrunArgs = []string{"-T", "ws", "start"} } // Check vmrunPath exists before attempting to execute @@ -123,6 +131,9 @@ func runVMware(args []string) { log.Fatalf("ERROR VMware executables can not be found, ensure software is installed") } + if *disk == "" && *diskSz != "" { + *disk = filepath.Join(*state, "disk.vmdk") + } if *disk != "" { // Check vmDiskManagerPath exist before attempting to execute if _, err := os.Stat(vmDiskManagerPath); os.IsNotExist(err) { @@ -138,7 +149,7 @@ func runVMware(args []string) { log.Infof("Creating new VMware disk [%s]", *disk) vmDiskCmd := exec.Command(vmDiskManagerPath, "-c", "-s", *diskSz, "-a", "lsilogic", "-t", "0", *disk) if err = vmDiskCmd.Run(); err != nil { - log.Fatalf("Error creating disk [%s]: %s", *disk, err.Error()) + log.Fatalf("Error creating disk [%s]: %v", *disk, err) } } } else { @@ -148,23 +159,22 @@ func runVMware(args []string) { // Build the contents of the VMWare .vmx file vmx := buildVMX(*cpus, *mem, *disk, prefix) - if vmx == "" { log.Fatalf("VMware .vmx file could not be generated, please confirm inputs") } // Create the .vmx file - err := ioutil.WriteFile(prefix+".vmx", []byte(vmx), 0644) - + vmxPath := filepath.Join(*state, "linuxkit.vmx") + err := ioutil.WriteFile(vmxPath, []byte(vmx), 0644) if err != nil { - log.Fatalf("Error writing .vmx file") + log.Fatalf("Error writing .vmx file: %v", err) } + vmrunArgs = append(vmrunArgs, vmxPath) cmd := exec.Command(vmrunPath, vmrunArgs...) out, err := cmd.Output() - if err != nil { - log.Fatalf("Error starting vmrun") + log.Fatalf("Error starting vmrun: %v", err) } // check there is output to push to logging @@ -185,7 +195,10 @@ func buildVMX(cpus int, mem int, persistentDisk string, prefix string) string { returnString += fmt.Sprintf(vmxCdrom, cdromPath) } - vmdkPath := prefix + ".vmdk" + vmdkPath, err := filepath.Abs(prefix + ".vmdk") + if err != nil { + log.Fatalf("Unable get absolute path for boot vmdk: %v", err) + } if _, err := os.Stat(vmdkPath); err != nil { if os.IsPermission(err) { log.Fatalf("Unable to read file [%s], please check permissions", vmdkPath) @@ -198,6 +211,10 @@ func buildVMX(cpus int, mem int, persistentDisk string, prefix string) string { } // Add persistentDisk to the vmx if it has been specified in the args. if persistentDisk != "" { + persistentDisk, err = filepath.Abs(persistentDisk) + if err != nil { + log.Fatalf("Unable get absolute path for persistent disk: %v", err) + } returnString += fmt.Sprintf(vmxDiskPersistent, persistentDisk) }