From 2e821da84b4f6eaa5e60076348019279e0c16ae0 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Thu, 13 Jun 2019 14:09:45 -0700 Subject: [PATCH] check to make sure a vm-name isn't already in use before trying to launch a vm with said name. --- builder/hyperv/common/driver.go | 2 ++ builder/hyperv/common/driver_mock.go | 8 ++++++++ builder/hyperv/common/driver_ps_4.go | 4 ++++ builder/hyperv/common/step_create_vm.go | 10 +++++++++- common/powershell/hyperv/hyperv.go | 15 ++++++++++++++- 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/builder/hyperv/common/driver.go b/builder/hyperv/common/driver.go index c8bf506aa..29b282611 100644 --- a/builder/hyperv/common/driver.go +++ b/builder/hyperv/common/driver.go @@ -73,6 +73,8 @@ type Driver interface { DeleteVirtualSwitch(string) error + CheckVMName(string) error + CreateVirtualMachine(string, string, string, int64, int64, int64, string, uint, bool, bool, string) error AddVirtualMachineHardDrive(string, string, string, int64, int64, string) error diff --git a/builder/hyperv/common/driver_mock.go b/builder/hyperv/common/driver_mock.go index 290687829..4005b35e2 100644 --- a/builder/hyperv/common/driver_mock.go +++ b/builder/hyperv/common/driver_mock.go @@ -110,6 +110,9 @@ type DriverMock struct { DeleteVirtualSwitch_SwitchName string DeleteVirtualSwitch_Err error + CheckVMName_Called bool + CheckVMName_Err error + CreateVirtualSwitch_Called bool CreateVirtualSwitch_SwitchName string CreateVirtualSwitch_SwitchType string @@ -421,6 +424,11 @@ func (d *DriverMock) AddVirtualMachineHardDrive(vmName string, vhdFile string, v return d.AddVirtualMachineHardDrive_Err } +func (d *DriverMock) CheckVMName(vmName string) error { + d.CheckVMName_Called = true + return d.CheckVMName_Err +} + func (d *DriverMock) CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint, diffDisks bool, fixedVHD bool, version string) error { diff --git a/builder/hyperv/common/driver_ps_4.go b/builder/hyperv/common/driver_ps_4.go index 118c429ec..d365ae2e7 100644 --- a/builder/hyperv/common/driver_ps_4.go +++ b/builder/hyperv/common/driver_ps_4.go @@ -186,6 +186,10 @@ func (d *HypervPS4Driver) AddVirtualMachineHardDrive(vmName string, vhdFile stri diskBlockSize, controllerType) } +func (d *HypervPS4Driver) CheckVMName(vmName string) error { + return hyperv.CheckVMName(vmName) +} + func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint, diffDisks bool, fixedVHD bool, version string) error { diff --git a/builder/hyperv/common/step_create_vm.go b/builder/hyperv/common/step_create_vm.go index a9371c245..f9a05f797 100644 --- a/builder/hyperv/common/step_create_vm.go +++ b/builder/hyperv/common/step_create_vm.go @@ -48,6 +48,14 @@ func (s *StepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis path = v.(string) } + err := driver.CheckVMName(s.VMName) + if err != nil { + s.KeepRegistered = true + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + // Determine if we even have an existing virtual harddrive to attach harddrivePath := "" if harddrivePathRaw, ok := state.GetOk("iso_path"); ok { @@ -66,7 +74,7 @@ func (s *StepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis diskSize := int64(s.DiskSize) * 1024 * 1024 diskBlockSize := int64(s.DiskBlockSize) * 1024 * 1024 - err := driver.CreateVirtualMachine(s.VMName, path, harddrivePath, ramSize, diskSize, diskBlockSize, + err = driver.CreateVirtualMachine(s.VMName, path, harddrivePath, ramSize, diskSize, diskBlockSize, s.SwitchName, s.Generation, s.DifferencingDisk, s.FixedVHD, s.Version) if err != nil { err := fmt.Errorf("Error creating virtual machine: %s", err) diff --git a/common/powershell/hyperv/hyperv.go b/common/powershell/hyperv/hyperv.go index 25bd635c8..aa5ee1db3 100644 --- a/common/powershell/hyperv/hyperv.go +++ b/common/powershell/hyperv/hyperv.go @@ -283,10 +283,23 @@ Hyper-V\New-VM -Name "{{ .VMName }}" -Path "{{ .Path }}" -MemoryStartupBytes {{ return final, nil } +func CheckVMName(vmName string) error { + // Check that no vm with the same name is registered, to prevent + // namespace collisions + var gs powershell.PowerShellCmd + getVMCmd := fmt.Sprintf(`Hyper-V\Get-VM -Name "%s"`, vmName) + if err := gs.Run(getVMCmd); err == nil { + return fmt.Errorf("A virtual machine with the name %s is already"+ + " defined in Hyper-V. To avoid a name collision, please set your "+ + "vm_name to a unique value", vmName) + } + + return nil +} + func CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint, diffDisks bool, fixedVHD bool, version string) error { - opts := scriptOptions{ Version: version, VMName: vmName,