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 <rolf.neugebauer@docker.com>
This commit is contained in:
Rolf Neugebauer 2017-05-15 11:37:29 +01:00
parent 243967db6f
commit a16d1683f8
2 changed files with 30 additions and 11 deletions

2
.gitignore vendored
View File

@ -6,6 +6,8 @@ Dockerfile.media
*.tag *.tag
*.iso *.iso
*.vhd *.vhd
*.vmdk
*.vmdk.lck
*.tar *.tar
*.gz *.gz
*.vhdx *.vhdx

View File

@ -78,6 +78,7 @@ func runVMware(args []string) {
mem := flags.Int("mem", 1024, "Amount of memory in MB") mem := flags.Int("mem", 1024, "Amount of memory in MB")
disk := flags.String("disk", "", "Path to disk image to use") 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") 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 { if err := flags.Parse(args); err != nil {
log.Fatal("Unable to parse args") log.Fatal("Unable to parse args")
@ -91,19 +92,26 @@ func runVMware(args []string) {
} }
prefix := remArgs[0] 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 vmrunPath, vmDiskManagerPath string
var vmrunArgs []string var vmrunArgs []string
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
vmrunPath = "C:\\Program\\ files\\VMware Workstation\\vmrun.exe" vmrunPath = "C:\\Program\\ files\\VMware Workstation\\vmrun.exe"
vmDiskManagerPath = "C:\\Program\\ files\\VMware Workstation\\vmware-vdiskmanager.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" { if runtime.GOOS == "darwin" {
vmrunPath = "/Applications/VMware Fusion.app/Contents/Library/vmrun" vmrunPath = "/Applications/VMware Fusion.app/Contents/Library/vmrun"
vmDiskManagerPath = "/Applications/VMware Fusion.app/Contents/Library/vmware-vdiskmanager" 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" { if runtime.GOOS == "linux" {
@ -115,7 +123,7 @@ func runVMware(args []string) {
log.Fatalf("Unable to find %s within the $PATH", vmrunPath) log.Fatalf("Unable to find %s within the $PATH", vmrunPath)
} }
vmrunPath = fullVMrunPath vmrunPath = fullVMrunPath
vmrunArgs = []string{"-T", "ws", "start", prefix + ".vmx"} vmrunArgs = []string{"-T", "ws", "start"}
} }
// Check vmrunPath exists before attempting to execute // 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") log.Fatalf("ERROR VMware executables can not be found, ensure software is installed")
} }
if *disk == "" && *diskSz != "" {
*disk = filepath.Join(*state, "disk.vmdk")
}
if *disk != "" { if *disk != "" {
// Check vmDiskManagerPath exist before attempting to execute // Check vmDiskManagerPath exist before attempting to execute
if _, err := os.Stat(vmDiskManagerPath); os.IsNotExist(err) { if _, err := os.Stat(vmDiskManagerPath); os.IsNotExist(err) {
@ -138,7 +149,7 @@ func runVMware(args []string) {
log.Infof("Creating new VMware disk [%s]", *disk) log.Infof("Creating new VMware disk [%s]", *disk)
vmDiskCmd := exec.Command(vmDiskManagerPath, "-c", "-s", *diskSz, "-a", "lsilogic", "-t", "0", *disk) vmDiskCmd := exec.Command(vmDiskManagerPath, "-c", "-s", *diskSz, "-a", "lsilogic", "-t", "0", *disk)
if err = vmDiskCmd.Run(); err != nil { 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 { } else {
@ -148,23 +159,22 @@ func runVMware(args []string) {
// Build the contents of the VMWare .vmx file // Build the contents of the VMWare .vmx file
vmx := buildVMX(*cpus, *mem, *disk, prefix) vmx := buildVMX(*cpus, *mem, *disk, prefix)
if vmx == "" { if vmx == "" {
log.Fatalf("VMware .vmx file could not be generated, please confirm inputs") log.Fatalf("VMware .vmx file could not be generated, please confirm inputs")
} }
// Create the .vmx file // 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 { 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...) cmd := exec.Command(vmrunPath, vmrunArgs...)
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {
log.Fatalf("Error starting vmrun") log.Fatalf("Error starting vmrun: %v", err)
} }
// check there is output to push to logging // 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) 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 _, err := os.Stat(vmdkPath); err != nil {
if os.IsPermission(err) { if os.IsPermission(err) {
log.Fatalf("Unable to read file [%s], please check permissions", vmdkPath) 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. // Add persistentDisk to the vmx if it has been specified in the args.
if persistentDisk != "" { 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) returnString += fmt.Sprintf(vmxDiskPersistent, persistentDisk)
} }