From b95123f45765915512342e1fa5f0cefd259e1490 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 10 Dec 2018 14:46:16 -0800 Subject: [PATCH] add new copy_in_compare flag so users can set the copy var to whatever they need in order to get the compatibility report to work. --- builder/hyperv/common/driver.go | 2 +- builder/hyperv/common/driver_mock.go | 5 ++++- builder/hyperv/common/driver_ps_4.go | 4 ++-- builder/hyperv/common/step_clone_vm.go | 6 ++++-- builder/hyperv/vmcx/builder.go | 2 ++ common/powershell/hyperv/hyperv.go | 19 ++++++++++++------- .../docs/builders/hyperv-vmcx.html.md.erb | 9 +++++++++ 7 files changed, 34 insertions(+), 13 deletions(-) diff --git a/builder/hyperv/common/driver.go b/builder/hyperv/common/driver.go index 0001bd989..5f96398ae 100644 --- a/builder/hyperv/common/driver.go +++ b/builder/hyperv/common/driver.go @@ -74,7 +74,7 @@ type Driver interface { AddVirtualMachineHardDrive(string, string, string, int64, int64, string) error - CloneVirtualMachine(string, string, string, bool, string, string, string, int64, string) error + CloneVirtualMachine(string, string, string, bool, string, string, string, int64, string, bool) error DeleteVirtualMachine(string) error diff --git a/builder/hyperv/common/driver_mock.go b/builder/hyperv/common/driver_mock.go index 5bef1b3e4..71b7a0750 100644 --- a/builder/hyperv/common/driver_mock.go +++ b/builder/hyperv/common/driver_mock.go @@ -143,6 +143,7 @@ type DriverMock struct { CloneVirtualMachine_HarddrivePath string CloneVirtualMachine_Ram int64 CloneVirtualMachine_SwitchName string + CloneVirtualMachine_Copy bool CloneVirtualMachine_Err error DeleteVirtualMachine_Called bool @@ -425,7 +426,7 @@ func (d *DriverMock) CreateVirtualMachine(vmName string, path string, harddriveP func (d *DriverMock) CloneVirtualMachine(cloneFromVmcxPath string, cloneFromVmName string, cloneFromSnapshotName string, cloneAllSnapshots bool, vmName string, path string, - harddrivePath string, ram int64, switchName string) error { + harddrivePath string, ram int64, switchName string, copyTF bool) error { d.CloneVirtualMachine_Called = true d.CloneVirtualMachine_CloneFromVmcxPath = cloneFromVmcxPath d.CloneVirtualMachine_CloneFromVmName = cloneFromVmName @@ -436,6 +437,8 @@ func (d *DriverMock) CloneVirtualMachine(cloneFromVmcxPath string, cloneFromVmNa d.CloneVirtualMachine_HarddrivePath = harddrivePath d.CloneVirtualMachine_Ram = ram d.CloneVirtualMachine_SwitchName = switchName + d.CloneVirtualMachine_Copy = copyTF + return d.CloneVirtualMachine_Err } diff --git a/builder/hyperv/common/driver_ps_4.go b/builder/hyperv/common/driver_ps_4.go index b1803971f..0ef7accf4 100644 --- a/builder/hyperv/common/driver_ps_4.go +++ b/builder/hyperv/common/driver_ps_4.go @@ -190,9 +190,9 @@ func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, hardd func (d *HypervPS4Driver) CloneVirtualMachine(cloneFromVmcxPath string, cloneFromVmName string, cloneFromSnapshotName string, cloneAllSnapshots bool, vmName string, path string, harddrivePath string, - ram int64, switchName string) error { + ram int64, switchName string, copyTF bool) error { return hyperv.CloneVirtualMachine(cloneFromVmcxPath, cloneFromVmName, cloneFromSnapshotName, - cloneAllSnapshots, vmName, path, harddrivePath, ram, switchName) + cloneAllSnapshots, vmName, path, harddrivePath, ram, switchName, copyTF) } func (d *HypervPS4Driver) DeleteVirtualMachine(vmName string) error { diff --git a/builder/hyperv/common/step_clone_vm.go b/builder/hyperv/common/step_clone_vm.go index f245cb4aa..09e063db3 100644 --- a/builder/hyperv/common/step_clone_vm.go +++ b/builder/hyperv/common/step_clone_vm.go @@ -22,6 +22,7 @@ type StepCloneVM struct { CloneAllSnapshots bool VMName string SwitchName string + CompareCopy bool RamSize uint Cpu uint EnableMacSpoofing bool @@ -55,8 +56,9 @@ func (s *StepCloneVM) Run(_ context.Context, state multistep.StateBag) multistep // convert the MB to bytes ramSize := int64(s.RamSize * 1024 * 1024) - err := driver.CloneVirtualMachine(s.CloneFromVMCXPath, s.CloneFromVMName, s.CloneFromSnapshotName, - s.CloneAllSnapshots, s.VMName, path, harddrivePath, ramSize, s.SwitchName) + err := driver.CloneVirtualMachine(s.CloneFromVMCXPath, s.CloneFromVMName, + s.CloneFromSnapshotName, s.CloneAllSnapshots, s.VMName, path, + harddrivePath, ramSize, s.SwitchName, s.CompareCopy) if err != nil { err := fmt.Errorf("Error cloning virtual machine: %s", err) state.Put("error", err) diff --git a/builder/hyperv/vmcx/builder.go b/builder/hyperv/vmcx/builder.go index 1178061d3..e207c548f 100644 --- a/builder/hyperv/vmcx/builder.go +++ b/builder/hyperv/vmcx/builder.go @@ -81,6 +81,7 @@ type Config struct { DifferencingDisk bool `mapstructure:"differencing_disk"` SwitchName string `mapstructure:"switch_name"` + CompareCopy bool `mapstructure:"copy_in_compare"` SwitchVlanId string `mapstructure:"switch_vlan_id"` MacAddress string `mapstructure:"mac_address"` VlanId string `mapstructure:"vlan_id"` @@ -430,6 +431,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe CloneAllSnapshots: b.config.CloneAllSnapshots, VMName: b.config.VMName, SwitchName: b.config.SwitchName, + CompareCopy: b.config.CompareCopy, RamSize: b.config.RamSize, Cpu: b.config.Cpu, EnableMacSpoofing: b.config.EnableMacSpoofing, diff --git a/common/powershell/hyperv/hyperv.go b/common/powershell/hyperv/hyperv.go index 816dad9ca..89489f8d9 100644 --- a/common/powershell/hyperv/hyperv.go +++ b/common/powershell/hyperv/hyperv.go @@ -366,10 +366,10 @@ Hyper-V\Set-VMNetworkAdapter $vmName -staticmacaddress $mac } func ImportVmcxVirtualMachine(importPath string, vmName string, harddrivePath string, - ram int64, switchName string) error { + ram int64, switchName string, copyTF bool) error { var script = ` -param([string]$importPath, [string]$vmName, [string]$harddrivePath, [long]$memoryStartupBytes, [string]$switchName) +param([string]$importPath, [string]$vmName, [string]$harddrivePath, [long]$memoryStartupBytes, [string]$switchName, [string]$copy) $VirtualHarddisksPath = Join-Path -Path $importPath -ChildPath 'Virtual Hard Disks' if (!(Test-Path $VirtualHarddisksPath)) { @@ -395,7 +395,13 @@ if (!$VirtualMachinePath){ $VirtualMachinePath = Get-ChildItem -Path $importPath -Filter *.xml -Recurse -ErrorAction SilentlyContinue | select -First 1 | %{$_.FullName} } -$compatibilityReport = Hyper-V\Compare-VM -Path $VirtualMachinePath -VirtualMachinePath $importPath -SmartPagingFilePath $importPath -SnapshotFilePath $importPath -VhdDestinationPath $VirtualHarddisksPath -GenerateNewId +$copyBool = $false +switch($copy) { + "true" { $copyBool = $true } + default { $copyBool = $false } +} + +$compatibilityReport = Hyper-V\Compare-VM -Path $VirtualMachinePath -VirtualMachinePath $importPath -SmartPagingFilePath $importPath -SnapshotFilePath $importPath -VhdDestinationPath $VirtualHarddisksPath -GenerateNewId -Copy:$false if ($vhdPath){ Copy-Item -Path $harddrivePath -Destination $vhdPath $existingFirstHarddrive = $compatibilityReport.VM.HardDrives | Select -First 1 @@ -415,16 +421,15 @@ if ($vm) { $result = Hyper-V\Rename-VM -VM $vm -NewName $VMName } ` - var ps powershell.PowerShellCmd - err := ps.Run(script, importPath, vmName, harddrivePath, strconv.FormatInt(ram, 10), switchName) + err := ps.Run(script, importPath, vmName, harddrivePath, strconv.FormatInt(ram, 10), switchName, strconv.FormatBool(copyTF)) return err } func CloneVirtualMachine(cloneFromVmcxPath string, cloneFromVmName string, cloneFromSnapshotName string, cloneAllSnapshots bool, vmName string, - path string, harddrivePath string, ram int64, switchName string) error { + path string, harddrivePath string, ram int64, switchName string, copyTF bool) error { if cloneFromVmName != "" { if err := ExportVmcxVirtualMachine(path, cloneFromVmName, @@ -439,7 +444,7 @@ func CloneVirtualMachine(cloneFromVmcxPath string, cloneFromVmName string, } } - if err := ImportVmcxVirtualMachine(path, vmName, harddrivePath, ram, switchName); err != nil { + if err := ImportVmcxVirtualMachine(path, vmName, harddrivePath, ram, switchName, copyTF); err != nil { return err } diff --git a/website/source/docs/builders/hyperv-vmcx.html.md.erb b/website/source/docs/builders/hyperv-vmcx.html.md.erb index 040f86217..fc1568657 100644 --- a/website/source/docs/builders/hyperv-vmcx.html.md.erb +++ b/website/source/docs/builders/hyperv-vmcx.html.md.erb @@ -116,6 +116,15 @@ builder. This setting only has an effect if using `clone_from_vm_name` and is ignored otherwise. +- `copy_in_compare` - (bool) When cloning a vm to build from, we run a powershell + Compare-VM command, which, depending on your version of Windows, may need + the "Copy" flag to be set to true or false. Defaults to "false". Command: + + `$compatibilityReport = Hyper-V\Compare-VM -Path $VirtualMachinePath -VirtualMachinePath $importPath -SmartPagingFilePath $importPath -SnapshotFilePath $importPath -VhdDestinationPath $VirtualHarddisksPath -GenerateNewId -Copy:$copy` + + Where $copy is replaced with either true or false depending on the value of + "copy_in_compare". + - `cpu` (number) - The number of CPUs the virtual machine should use. If this isn't specified, the default is 1 CPU.