Skip to content

Commit

Permalink
Add new option 'network_legacy'
Browse files Browse the repository at this point in the history
- Use a legacy network adapter for the builder VM.
  • Loading branch information
tuxillo committed Mar 7, 2018
1 parent 9eeb1c3 commit f67c265
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion builder/hyperv/common/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions builder/hyperv/common/driver_ps_4.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion builder/hyperv/common/step_create_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type StepCreateVM struct {
DiskSize uint
Generation uint
Cpu uint
NetworkLegacy bool
EnableMacSpoofing bool
EnableDynamicMemory bool
EnableSecureBoot bool
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions builder/hyperv/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions common/powershell/hyperv/hyperv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand All @@ -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){
Expand All @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions website/source/docs/builders/hyperv-iso.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit f67c265

Please sign in to comment.