From 2a487af68d968c75a6c3fc04406d00a54b2ad50a Mon Sep 17 00:00:00 2001 From: Tomas Srnka Date: Sun, 11 Jan 2015 18:04:17 +0100 Subject: [PATCH 1/3] Added config option to specify VLanId for Network Adapter when using HyperV provider --- plugins/providers/hyperv/action.rb | 2 ++ .../providers/hyperv/action/net_set_vlan.rb | 19 +++++++++++++++++++ plugins/providers/hyperv/config.rb | 10 ++++++++++ plugins/providers/hyperv/driver.rb | 4 ++++ .../hyperv/scripts/set_network_vlan.ps1 | 18 ++++++++++++++++++ .../plugins/providers/hyperv/config_test.rb | 12 ++++++++++++ 6 files changed, 65 insertions(+) create mode 100644 plugins/providers/hyperv/action/net_set_vlan.rb create mode 100644 plugins/providers/hyperv/scripts/set_network_vlan.ps1 diff --git a/plugins/providers/hyperv/action.rb b/plugins/providers/hyperv/action.rb index 49e2be33f..64fbec180 100644 --- a/plugins/providers/hyperv/action.rb +++ b/plugins/providers/hyperv/action.rb @@ -116,6 +116,7 @@ module VagrantPlugins end b2.use Provision + b2.use NetSetVLan b2.use StartInstance b2.use WaitForIPAddress b2.use WaitForCommunicator, [:running] @@ -216,6 +217,7 @@ module VagrantPlugins autoload :StopInstance, action_root.join('stop_instance') autoload :SuspendVM, action_root.join("suspend_vm") autoload :WaitForIPAddress, action_root.join("wait_for_ip_address") + autoload :NetSetVLan, action_root.join("net_set_vlan") autoload :MessageWillNotDestroy, action_root.join("message_will_not_destroy") end end diff --git a/plugins/providers/hyperv/action/net_set_vlan.rb b/plugins/providers/hyperv/action/net_set_vlan.rb new file mode 100644 index 000000000..9ce26d324 --- /dev/null +++ b/plugins/providers/hyperv/action/net_set_vlan.rb @@ -0,0 +1,19 @@ +module VagrantPlugins + module HyperV + module Action + class NetSetVLan + def initialize(app, env) + @app = app + end + + def call(env) + vlan_id = env[:machine].provider_config.vlan_id + + env[:ui].info("[Settings] [Network Adapter] Setting Vlan ID to: #{vlan_id}") + env[:machine].provider.driver.net_set_vlan(vlan_id) + @app.call(env) + end + end + end + end +end diff --git a/plugins/providers/hyperv/config.rb b/plugins/providers/hyperv/config.rb index ad69e3059..65a198afc 100644 --- a/plugins/providers/hyperv/config.rb +++ b/plugins/providers/hyperv/config.rb @@ -9,14 +9,24 @@ module VagrantPlugins # @return [Integer] attr_accessor :ip_address_timeout + # The default VLAN ID for network interface for the virtual machine. + # + # @return [Integer] + attr_accessor :vlan_id + def initialize @ip_address_timeout = UNSET_VALUE + @vlan_id = UNSET_VALUE end def finalize! if @ip_address_timeout == UNSET_VALUE @ip_address_timeout = 120 end + + if @vlan_id == UNSET_VALUE + @vlan_id = 0 + end end def validate(machine) diff --git a/plugins/providers/hyperv/driver.rb b/plugins/providers/hyperv/driver.rb index 9f11881e6..db99d5dfe 100644 --- a/plugins/providers/hyperv/driver.rb +++ b/plugins/providers/hyperv/driver.rb @@ -77,6 +77,10 @@ module VagrantPlugins execute('import_vm.ps1', options) end + def net_set_vlan(vlan_id) + execute("set_network_vlan.ps1", { VmId: vm_id, VlanId: vlan_id }) + end + protected def execute_powershell(path, options, &block) diff --git a/plugins/providers/hyperv/scripts/set_network_vlan.ps1 b/plugins/providers/hyperv/scripts/set_network_vlan.ps1 new file mode 100644 index 000000000..a2b271b91 --- /dev/null +++ b/plugins/providers/hyperv/scripts/set_network_vlan.ps1 @@ -0,0 +1,18 @@ +param ( + [string]$VmId = $(throw "-VmId is required."), + [int]$VlanId = $(throw "-VlanId ") + ) + +# Include the following modules +$presentDir = Split-Path -parent $PSCommandPath +$modules = @() +$modules += $presentDir + "\utils\write_messages.ps1" +forEach ($module in $modules) { . $module } + +try { + $vm = Get-VM -Id $VmId -ErrorAction "stop" + Set-VMNetworkAdapterVlan $vm -Access -Vlanid $VlanId +} +catch { + Write-Error-Message "Failed to set VM's Vlan ID $_" +} diff --git a/test/unit/plugins/providers/hyperv/config_test.rb b/test/unit/plugins/providers/hyperv/config_test.rb index 5177dca59..57a83743f 100644 --- a/test/unit/plugins/providers/hyperv/config_test.rb +++ b/test/unit/plugins/providers/hyperv/config_test.rb @@ -14,5 +14,17 @@ describe VagrantPlugins::HyperV::Config do subject.finalize! expect(subject.ip_address_timeout).to eq(120) end + + describe "#vlan_id" do + it "can be set" do + subject.vlan_id = 100 + subject.finalize! + expect(subject.vlan_id).to eq(100) + end + + it "defaults to a number" do + subject.finalize! + expect(subject.vlan_id).to eq(0) + end end end From 4c35f6b07041ece1506b799dbc6a0941b965b9b1 Mon Sep 17 00:00:00 2001 From: Tomas Srnka Date: Sun, 11 Jan 2015 18:10:46 +0100 Subject: [PATCH 2/3] Revert "Added config option to specify VLanId for Network Adapter when using HyperV provider" This reverts commit 2a487af68d968c75a6c3fc04406d00a54b2ad50a. --- plugins/providers/hyperv/action.rb | 2 -- .../providers/hyperv/action/net_set_vlan.rb | 19 ------------------- plugins/providers/hyperv/config.rb | 10 ---------- plugins/providers/hyperv/driver.rb | 4 ---- .../hyperv/scripts/set_network_vlan.ps1 | 18 ------------------ .../plugins/providers/hyperv/config_test.rb | 12 ------------ 6 files changed, 65 deletions(-) delete mode 100644 plugins/providers/hyperv/action/net_set_vlan.rb delete mode 100644 plugins/providers/hyperv/scripts/set_network_vlan.ps1 diff --git a/plugins/providers/hyperv/action.rb b/plugins/providers/hyperv/action.rb index 64fbec180..49e2be33f 100644 --- a/plugins/providers/hyperv/action.rb +++ b/plugins/providers/hyperv/action.rb @@ -116,7 +116,6 @@ module VagrantPlugins end b2.use Provision - b2.use NetSetVLan b2.use StartInstance b2.use WaitForIPAddress b2.use WaitForCommunicator, [:running] @@ -217,7 +216,6 @@ module VagrantPlugins autoload :StopInstance, action_root.join('stop_instance') autoload :SuspendVM, action_root.join("suspend_vm") autoload :WaitForIPAddress, action_root.join("wait_for_ip_address") - autoload :NetSetVLan, action_root.join("net_set_vlan") autoload :MessageWillNotDestroy, action_root.join("message_will_not_destroy") end end diff --git a/plugins/providers/hyperv/action/net_set_vlan.rb b/plugins/providers/hyperv/action/net_set_vlan.rb deleted file mode 100644 index 9ce26d324..000000000 --- a/plugins/providers/hyperv/action/net_set_vlan.rb +++ /dev/null @@ -1,19 +0,0 @@ -module VagrantPlugins - module HyperV - module Action - class NetSetVLan - def initialize(app, env) - @app = app - end - - def call(env) - vlan_id = env[:machine].provider_config.vlan_id - - env[:ui].info("[Settings] [Network Adapter] Setting Vlan ID to: #{vlan_id}") - env[:machine].provider.driver.net_set_vlan(vlan_id) - @app.call(env) - end - end - end - end -end diff --git a/plugins/providers/hyperv/config.rb b/plugins/providers/hyperv/config.rb index 65a198afc..ad69e3059 100644 --- a/plugins/providers/hyperv/config.rb +++ b/plugins/providers/hyperv/config.rb @@ -9,24 +9,14 @@ module VagrantPlugins # @return [Integer] attr_accessor :ip_address_timeout - # The default VLAN ID for network interface for the virtual machine. - # - # @return [Integer] - attr_accessor :vlan_id - def initialize @ip_address_timeout = UNSET_VALUE - @vlan_id = UNSET_VALUE end def finalize! if @ip_address_timeout == UNSET_VALUE @ip_address_timeout = 120 end - - if @vlan_id == UNSET_VALUE - @vlan_id = 0 - end end def validate(machine) diff --git a/plugins/providers/hyperv/driver.rb b/plugins/providers/hyperv/driver.rb index db99d5dfe..9f11881e6 100644 --- a/plugins/providers/hyperv/driver.rb +++ b/plugins/providers/hyperv/driver.rb @@ -77,10 +77,6 @@ module VagrantPlugins execute('import_vm.ps1', options) end - def net_set_vlan(vlan_id) - execute("set_network_vlan.ps1", { VmId: vm_id, VlanId: vlan_id }) - end - protected def execute_powershell(path, options, &block) diff --git a/plugins/providers/hyperv/scripts/set_network_vlan.ps1 b/plugins/providers/hyperv/scripts/set_network_vlan.ps1 deleted file mode 100644 index a2b271b91..000000000 --- a/plugins/providers/hyperv/scripts/set_network_vlan.ps1 +++ /dev/null @@ -1,18 +0,0 @@ -param ( - [string]$VmId = $(throw "-VmId is required."), - [int]$VlanId = $(throw "-VlanId ") - ) - -# Include the following modules -$presentDir = Split-Path -parent $PSCommandPath -$modules = @() -$modules += $presentDir + "\utils\write_messages.ps1" -forEach ($module in $modules) { . $module } - -try { - $vm = Get-VM -Id $VmId -ErrorAction "stop" - Set-VMNetworkAdapterVlan $vm -Access -Vlanid $VlanId -} -catch { - Write-Error-Message "Failed to set VM's Vlan ID $_" -} diff --git a/test/unit/plugins/providers/hyperv/config_test.rb b/test/unit/plugins/providers/hyperv/config_test.rb index 57a83743f..5177dca59 100644 --- a/test/unit/plugins/providers/hyperv/config_test.rb +++ b/test/unit/plugins/providers/hyperv/config_test.rb @@ -14,17 +14,5 @@ describe VagrantPlugins::HyperV::Config do subject.finalize! expect(subject.ip_address_timeout).to eq(120) end - - describe "#vlan_id" do - it "can be set" do - subject.vlan_id = 100 - subject.finalize! - expect(subject.vlan_id).to eq(100) - end - - it "defaults to a number" do - subject.finalize! - expect(subject.vlan_id).to eq(0) - end end end From 21e2f33fb0e6ed9acd23f279b2806d0d9761e609 Mon Sep 17 00:00:00 2001 From: Tomas Srnka Date: Sat, 17 Jan 2015 21:31:37 +0100 Subject: [PATCH 3/3] Fix for: [hyperv]: VM import process does not respect Secure Boot option from image #5209 --- plugins/providers/hyperv/scripts/import_vm.ps1 | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/plugins/providers/hyperv/scripts/import_vm.ps1 b/plugins/providers/hyperv/scripts/import_vm.ps1 index 5aea63875..791daecd7 100644 --- a/plugins/providers/hyperv/scripts/import_vm.ps1 +++ b/plugins/providers/hyperv/scripts/import_vm.ps1 @@ -90,6 +90,9 @@ Switch ((Select-Xml -xml $vmconfig -XPath "//boot").node.device0."#text") { "Default" { $bootdevice = "IDE" } } #switch +# Determine secure boot options +$secure_boot_enabled = (Select-Xml -xml $vmconfig -XPath "//secure_boot_enabled").Node."#text" + # Define a hash map of parameter values for New-VM $vm_params = @{ @@ -133,6 +136,13 @@ $vm | Set-VM @more_vm_params -Passthru # Add drives to the virtual machine $controllers = Select-Xml -xml $vmconfig -xpath "//*[starts-with(name(.),'controller')]" +# Set EFI secure boot +if ($secure_boot_enabled -eq "True") { + Set-VMFirmware -VM $vm -EnableSecureBoot On +} else { + Set-VMFirmware -VM $vm -EnableSecureBoot Off +} + # A regular expression pattern to pull the number from controllers [regex]$rx="\d"