diff --git a/builder/hyperv/common/driver.go b/builder/hyperv/common/driver.go index 571214acd2a..e968f091d46 100644 --- a/builder/hyperv/common/driver.go +++ b/builder/hyperv/common/driver.go @@ -66,7 +66,7 @@ type Driver interface { DeleteVirtualSwitch(string) error - CreateVirtualMachine(string, string, string, string, int64, int64, string, uint, bool) error + CreateVirtualMachine(string, string, string, string, int64, int64, bool, string, uint, bool) error AddVirtualMachineHardDrive(string, string, string, int64, string) error diff --git a/builder/hyperv/common/driver_ps_4.go b/builder/hyperv/common/driver_ps_4.go index a6c1b73523e..ff98c277815 100644 --- a/builder/hyperv/common/driver_ps_4.go +++ b/builder/hyperv/common/driver_ps_4.go @@ -178,8 +178,8 @@ func (d *HypervPS4Driver) AddVirtualMachineHardDrive(vmName string, vhdFile stri return hyperv.AddVirtualMachineHardDiskDrive(vmName, vhdFile, vhdName, vhdSizeBytes, controllerType) } -func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, harddrivePath string, vhdPath string, ram int64, diskSize int64, switchName string, generation uint, diffDisks bool) error { - return hyperv.CreateVirtualMachine(vmName, path, harddrivePath, vhdPath, ram, diskSize, switchName, generation, diffDisks) +func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, harddrivePath string, vhdPath string, ram int64, diskSize int64, networkLegacy bool, switchName string, generation uint, diffDisks bool) error { + return hyperv.CreateVirtualMachine(vmName, path, harddrivePath, vhdPath, ram, diskSize, networkLegacy, switchName, generation, diffDisks) } func (d *HypervPS4Driver) CloneVirtualMachine(cloneFromVmxcPath string, cloneFromVmName string, cloneFromSnapshotName string, cloneAllSnapshots bool, vmName string, path string, harddrivePath string, ram int64, switchName string) error { diff --git a/builder/hyperv/common/step_create_vm.go b/builder/hyperv/common/step_create_vm.go index f046d1a48bc..a950771888f 100644 --- a/builder/hyperv/common/step_create_vm.go +++ b/builder/hyperv/common/step_create_vm.go @@ -23,6 +23,7 @@ type StepCreateVM struct { DiskSize uint Generation uint Cpu uint + NetworkLegacy bool EnableMacSpoofing bool EnableDynamicMemory bool EnableSecureBoot bool @@ -65,7 +66,7 @@ func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste ramSize := int64(s.RamSize * 1024 * 1024) diskSize := int64(s.DiskSize * 1024 * 1024) - err := driver.CreateVirtualMachine(s.VMName, path, harddrivePath, vhdPath, ramSize, diskSize, s.SwitchName, s.Generation, s.DifferencingDisk) + err := driver.CreateVirtualMachine(s.VMName, path, harddrivePath, vhdPath, ramSize, diskSize, s.NetworkLegacy, s.SwitchName, s.Generation, s.DifferencingDisk) if err != nil { err := fmt.Errorf("Error creating virtual machine: %s", err) state.Put("error", err) diff --git a/builder/hyperv/iso/builder.go b/builder/hyperv/iso/builder.go index b92e779df36..af1d8e46dd7 100644 --- a/builder/hyperv/iso/builder.go +++ b/builder/hyperv/iso/builder.go @@ -78,6 +78,7 @@ type Config struct { VlanId string `mapstructure:"vlan_id"` Cpu uint `mapstructure:"cpu"` Generation uint `mapstructure:"generation"` + NetworkLegacy bool `mapstructure:"network_legacy"` EnableMacSpoofing bool `mapstructure:"enable_mac_spoofing"` EnableDynamicMemory bool `mapstructure:"enable_dynamic_memory"` EnableSecureBoot bool `mapstructure:"enable_secure_boot"` @@ -354,6 +355,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe DiskSize: b.config.DiskSize, Generation: b.config.Generation, Cpu: b.config.Cpu, + NetworkLegacy: b.config.NetworkLegacy, EnableMacSpoofing: b.config.EnableMacSpoofing, EnableDynamicMemory: b.config.EnableDynamicMemory, EnableSecureBoot: b.config.EnableSecureBoot, diff --git a/common/powershell/hyperv/hyperv.go b/common/powershell/hyperv/hyperv.go index d5b58f5db51..0930bc25920 100644 --- a/common/powershell/hyperv/hyperv.go +++ b/common/powershell/hyperv/hyperv.go @@ -187,7 +187,7 @@ Hyper-V\Set-VMFloppyDiskDrive -VMName $vmName -Path $null return err } -func CreateVirtualMachine(vmName string, path string, harddrivePath string, vhdRoot string, ram int64, diskSize int64, switchName string, generation uint, diffDisks bool) error { +func CreateVirtualMachine(vmName string, path string, harddrivePath string, vhdRoot string, ram int64, diskSize int64, networkLegacy bool, switchName string, generation uint, diffDisks bool) error { if generation == 2 { var script = ` @@ -213,7 +213,7 @@ if ($harddrivePath){ return DisableAutomaticCheckpoints(vmName) } else { var script = ` -param([string]$vmName, [string]$path, [string]$harddrivePath, [string]$vhdRoot, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [string]$switchName, [string]$diffDisks) +param([string]$vmName, [string]$path, [string]$harddrivePath, [string]$vhdRoot, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [string]$switchName, [string]$diffDisks, [string]$networkLegacy) $vhdx = $vmName + '.vhdx' $vhdPath = Join-Path -Path $vhdRoot -ChildPath $vhdx if ($harddrivePath){ @@ -227,9 +227,14 @@ if ($harddrivePath){ } else { Hyper-V\New-VM -Name $vmName -Path $path -MemoryStartupBytes $memoryStartupBytes -NewVHDPath $vhdPath -NewVHDSizeBytes $newVHDSizeBytes -SwitchName $switchName } +if ($networkLegacy -eq "true") { + $VMNetAdapter = Get-VMNetworkAdapter -VMName $vmName + Remove-VMNetworkAdapter -VMName $vmName -Name $VMNetAdapter.Name + Add-VMNetworkAdapter -VMName $vmName -IsLegacy $true -Name $VMNetAdapter.Name -SwitchName $switchName +} ` var ps powershell.PowerShellCmd - if err := ps.Run(script, vmName, path, harddrivePath, vhdRoot, strconv.FormatInt(ram, 10), strconv.FormatInt(diskSize, 10), switchName, strconv.FormatBool(diffDisks)); err != nil { + if err := ps.Run(script, vmName, path, harddrivePath, vhdRoot, strconv.FormatInt(ram, 10), strconv.FormatInt(diskSize, 10), switchName, strconv.FormatBool(diffDisks), strconv.FormatBool(networkLegacy)); err != nil { return err } diff --git a/website/source/docs/builders/hyperv-iso.html.md b/website/source/docs/builders/hyperv-iso.html.md index e1454fea7fe..655c9c28066 100644 --- a/website/source/docs/builders/hyperv-iso.html.md +++ b/website/source/docs/builders/hyperv-iso.html.md @@ -103,6 +103,9 @@ can be configured for this builder. - `skip_export` (boolean) - If true skips VM export. If you are interested only in the vhd/vhdx files, you can enable this option. This will create inline disks which improves the build performance. There will not be any copying of source vhds to temp directory. This defauls to false. +- `network_legacy` (boolean) - If true removes the network adapter created by default and adds a legacy one with the same name. + This defaults to false. + - `enable_dynamic_memory` (boolean) - If true enable dynamic memory for virtual machine. This defaults to false.