From 3dd7c89bf04b0163581e7d9df2e7e49ac59ca86c Mon Sep 17 00:00:00 2001 From: cam Date: Sat, 10 Aug 2019 21:35:01 +1000 Subject: [PATCH 1/4] hyper-v: Hyper-V: add support for EnhancedSessionTransportType. Addresses #9823 --- plugins/providers/hyperv/action/configure.rb | 5 ++++ plugins/providers/hyperv/config.rb | 5 ++++ plugins/providers/hyperv/driver.rb | 8 +++++++ .../set_enhanced_session_transport_type.ps1 | 24 +++++++++++++++++++ .../providers/hyperv/action/configure_test.rb | 3 ++- .../docs/providers/hyperv/configuration.mdx | 1 + 6 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 plugins/providers/hyperv/scripts/set_enhanced_session_transport_type.ps1 diff --git a/plugins/providers/hyperv/action/configure.rb b/plugins/providers/hyperv/action/configure.rb index f25737f8a..1d05c2f72 100644 --- a/plugins/providers/hyperv/action/configure.rb +++ b/plugins/providers/hyperv/action/configure.rb @@ -97,6 +97,11 @@ module VagrantPlugins env[:machine].provider_config.vm_integration_services) end + if !env[:machine].provider_config.enhanced_session_transport_type.empty? + env[:ui].detail("Setting VM Enhanced session transport type") + env[:machine].provider.driver.set_enhanced_session_transport_type(env[:machine].provider_config.enhanced_session_transport_type) + end + @app.call(env) end end diff --git a/plugins/providers/hyperv/config.rb b/plugins/providers/hyperv/config.rb index 96014258c..4b7e05294 100644 --- a/plugins/providers/hyperv/config.rb +++ b/plugins/providers/hyperv/config.rb @@ -48,6 +48,8 @@ module VagrantPlugins attr_accessor :enable_virtualization_extensions # @return [Hash] Options for VMServiceIntegration attr_accessor :vm_integration_services + # @return [String] Set Enhanced session transport type + attr_accessor :enhanced_session_transport_type def initialize @ip_address_timeout = UNSET_VALUE @@ -65,6 +67,7 @@ module VagrantPlugins @enable_automatic_checkpoints = UNSET_VALUE @enable_checkpoints = UNSET_VALUE @vm_integration_services = {} + @enhanced_session_transport_type = UNSET_VALUE end def finalize! @@ -102,6 +105,8 @@ module VagrantPlugins # If automatic checkpoints are enabled, checkpoints will automatically be enabled @enable_checkpoints ||= @enable_automatic_checkpoints + + @enhanced_session_transport_type = nil if @enhanced_session_transport_type == UNSET_VALUE end def validate(machine) diff --git a/plugins/providers/hyperv/driver.rb b/plugins/providers/hyperv/driver.rb index 0b281eb3f..50b2aa842 100644 --- a/plugins/providers/hyperv/driver.rb +++ b/plugins/providers/hyperv/driver.rb @@ -289,6 +289,14 @@ module VagrantPlugins DiskSize: size_bytes) end + # Set enhanced session transport type of the VM + # + # @param [String] enhanced session transport type of the VM + # @return [nil] + def set_enhanced_session_transport_type(transport_type) + execute(:set_enhanced_session_transport_type, VmID: vm_id, type: transport_type) + end + protected def execute_powershell(path, options, &block) diff --git a/plugins/providers/hyperv/scripts/set_enhanced_session_transport_type.ps1 b/plugins/providers/hyperv/scripts/set_enhanced_session_transport_type.ps1 new file mode 100644 index 000000000..29191db37 --- /dev/null +++ b/plugins/providers/hyperv/scripts/set_enhanced_session_transport_type.ps1 @@ -0,0 +1,24 @@ +#Requires -Modules VagrantMessages + +param ( + [parameter (Mandatory=$true)] + [Guid] $VMID, + [parameter (Mandatory=$true)] + [string] $Type +) + +$ErrorActionPreference = "Stop" + +try { + $VM = Hyper-V\Get-VM -Id $VMID +} catch { + Write-ErrorMessage "Failed to locate VM: ${PSItem}" + exit 1 +} + +try { + Hyper-V\Set-VM -VM $VM -EnhancedSessionTransportType $Type +} catch { + Write-ErrorMessage "Failed to assign EnhancedSessionTransportType to ${Type}: ${PSItem}" + exit 1 +} diff --git a/test/unit/plugins/providers/hyperv/action/configure_test.rb b/test/unit/plugins/providers/hyperv/action/configure_test.rb index 504e416ef..0c695181c 100644 --- a/test/unit/plugins/providers/hyperv/action/configure_test.rb +++ b/test/unit/plugins/providers/hyperv/action/configure_test.rb @@ -28,7 +28,8 @@ describe VagrantPlugins::HyperV::Action::Configure do enable_checkpoints: false, enable_automatic_checkpoints: true, enable_virtualization_extensions: false, - vm_integration_services: vm_integration_services + vm_integration_services: vm_integration_services, + enhanced_session_transport_type: "HvSocket" ) } let(:vm_integration_services){ {} } diff --git a/website/pages/docs/providers/hyperv/configuration.mdx b/website/pages/docs/providers/hyperv/configuration.mdx index d05e3392b..0a4000f37 100644 --- a/website/pages/docs/providers/hyperv/configuration.mdx +++ b/website/pages/docs/providers/hyperv/configuration.mdx @@ -19,6 +19,7 @@ you may set. A complete reference is shown below: - `enable_virtualization_extensions` (boolean) - Enable virtualization extensions for the virtual CPUs. Default: false - `enable_checkpoints` (boolean) Enable checkpoints of the VM. Default: true - `enable_automatic_checkpoints` (boolean) Enable automatic checkpoints of the VM. Default: false +- `enhanced_session_transport_type` (VMBus, HvSocket) - Enhanced session transport type for the VM. Default: Hyper-V Default (currently VMBus). - `ip_address_timeout` (integer) - Number of seconds to wait for the VM to report an IP address. Default: 120. - `linked_clone` (boolean) - Use differencing disk instead of cloning entire VHD. Default: false - `mac` (string) - MAC address for the guest network interface From f6875818dc1ee0c793371e683569f86e859bcc0e Mon Sep 17 00:00:00 2001 From: cam Date: Sun, 18 Aug 2019 13:10:53 +1000 Subject: [PATCH 2/4] Hyperv: moving session transport type config from string to boolean for enhanced mode aka HvSocker --- plugins/providers/hyperv/action/configure.rb | 4 ++-- plugins/providers/hyperv/config.rb | 8 ++++---- .../providers/hyperv/action/configure_test.rb | 19 ++++++++++++++++++- .../plugins/providers/hyperv/config_test.rb | 14 ++++++++++++++ .../docs/providers/hyperv/configuration.mdx | 2 +- 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/plugins/providers/hyperv/action/configure.rb b/plugins/providers/hyperv/action/configure.rb index 1d05c2f72..f9bd26555 100644 --- a/plugins/providers/hyperv/action/configure.rb +++ b/plugins/providers/hyperv/action/configure.rb @@ -97,9 +97,9 @@ module VagrantPlugins env[:machine].provider_config.vm_integration_services) end - if !env[:machine].provider_config.enhanced_session_transport_type.empty? + if env[:machine].provider_config.enable_enhanced_session_mode env[:ui].detail("Setting VM Enhanced session transport type") - env[:machine].provider.driver.set_enhanced_session_transport_type(env[:machine].provider_config.enhanced_session_transport_type) + env[:machine].provider.driver.set_enhanced_session_transport_type("HvSocket") end @app.call(env) diff --git a/plugins/providers/hyperv/config.rb b/plugins/providers/hyperv/config.rb index 4b7e05294..ab56eef33 100644 --- a/plugins/providers/hyperv/config.rb +++ b/plugins/providers/hyperv/config.rb @@ -48,8 +48,8 @@ module VagrantPlugins attr_accessor :enable_virtualization_extensions # @return [Hash] Options for VMServiceIntegration attr_accessor :vm_integration_services - # @return [String] Set Enhanced session transport type - attr_accessor :enhanced_session_transport_type + # @return [Boolean] Enable Enhanced session mode + attr_accessor :enable_enhanced_session_mode def initialize @ip_address_timeout = UNSET_VALUE @@ -67,7 +67,7 @@ module VagrantPlugins @enable_automatic_checkpoints = UNSET_VALUE @enable_checkpoints = UNSET_VALUE @vm_integration_services = {} - @enhanced_session_transport_type = UNSET_VALUE + @enable_enhanced_session_mode = UNSET_VALUE end def finalize! @@ -106,7 +106,7 @@ module VagrantPlugins # If automatic checkpoints are enabled, checkpoints will automatically be enabled @enable_checkpoints ||= @enable_automatic_checkpoints - @enhanced_session_transport_type = nil if @enhanced_session_transport_type == UNSET_VALUE + @enable_enhanced_session_mode = false if @enable_enhanced_session_mode == UNSET_VALUE end def validate(machine) diff --git a/test/unit/plugins/providers/hyperv/action/configure_test.rb b/test/unit/plugins/providers/hyperv/action/configure_test.rb index 0c695181c..2a9053e35 100644 --- a/test/unit/plugins/providers/hyperv/action/configure_test.rb +++ b/test/unit/plugins/providers/hyperv/action/configure_test.rb @@ -29,10 +29,11 @@ describe VagrantPlugins::HyperV::Action::Configure do enable_automatic_checkpoints: true, enable_virtualization_extensions: false, vm_integration_services: vm_integration_services, - enhanced_session_transport_type: "HvSocket" + enable_enhanced_session_mode: enable_enhanced_session_mode ) } let(:vm_integration_services){ {} } + let(:enable_enhanced_session_mode){ false } let(:subject){ described_class.new(app, env) } @@ -117,6 +118,22 @@ describe VagrantPlugins::HyperV::Action::Configure do end end + context "without enhanced session transport type" do + it "should not call the driver to set enhanced session transport type" do + expect(driver).not_to receive(:set_enhanced_session_transport_type) + subject.call(env) + end + end + + context "with enhanced session transport type" do + let(:enable_enhanced_session_mode) { true } + + it "should call the driver to set enhanced session transport type" do + expect(driver).to receive(:set_enhanced_session_transport_type) + subject.call(env) + end + end + context "without available switches" do let(:switches){ [] } diff --git a/test/unit/plugins/providers/hyperv/config_test.rb b/test/unit/plugins/providers/hyperv/config_test.rb index 4b57a508a..f8f3868ed 100644 --- a/test/unit/plugins/providers/hyperv/config_test.rb +++ b/test/unit/plugins/providers/hyperv/config_test.rb @@ -241,4 +241,18 @@ describe VagrantPlugins::HyperV::Config do expect(result["Hyper-V"]).not_to be_empty end end + + + describe "#enable_enhanced_session_mode" do + it "is false by default" do + subject.finalize! + expect(subject.enable_enhanced_session_mode).to eq(false) + end + + it "can be set" do + subject.enable_enhanced_session_mode = true + subject.finalize! + expect(subject.enable_enhanced_session_mode).to eq(true) + end + end end diff --git a/website/pages/docs/providers/hyperv/configuration.mdx b/website/pages/docs/providers/hyperv/configuration.mdx index 0a4000f37..57f16f43e 100644 --- a/website/pages/docs/providers/hyperv/configuration.mdx +++ b/website/pages/docs/providers/hyperv/configuration.mdx @@ -19,7 +19,7 @@ you may set. A complete reference is shown below: - `enable_virtualization_extensions` (boolean) - Enable virtualization extensions for the virtual CPUs. Default: false - `enable_checkpoints` (boolean) Enable checkpoints of the VM. Default: true - `enable_automatic_checkpoints` (boolean) Enable automatic checkpoints of the VM. Default: false -- `enhanced_session_transport_type` (VMBus, HvSocket) - Enhanced session transport type for the VM. Default: Hyper-V Default (currently VMBus). +- `enable_enhanced_session_mode` (boolean) - Enable enhanced session transport type for the VM. Default: false - `ip_address_timeout` (integer) - Number of seconds to wait for the VM to report an IP address. Default: 120. - `linked_clone` (boolean) - Use differencing disk instead of cloning entire VHD. Default: false - `mac` (string) - MAC address for the guest network interface From 41948a1ca977441a3e1513a89541000908dde03f Mon Sep 17 00:00:00 2001 From: Cameron Jack Date: Sat, 2 May 2020 09:35:37 +1000 Subject: [PATCH 3/4] Moving hyper-v enhanced session message to I18n --- plugins/providers/hyperv/action/configure.rb | 2 +- templates/locales/en.yml | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/providers/hyperv/action/configure.rb b/plugins/providers/hyperv/action/configure.rb index f9bd26555..4b8299c6b 100644 --- a/plugins/providers/hyperv/action/configure.rb +++ b/plugins/providers/hyperv/action/configure.rb @@ -98,7 +98,7 @@ module VagrantPlugins end if env[:machine].provider_config.enable_enhanced_session_mode - env[:ui].detail("Setting VM Enhanced session transport type") + env[:ui].detail(I18n.t("vagrant.hyperv_setting_enhanced_session")) env[:machine].provider.driver.set_enhanced_session_transport_type("HvSocket") end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index de816d2cc..9de373a0c 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -165,6 +165,8 @@ en: -- Detected newer image for container '%{name}', restarting... docker_starting_containers: |- Starting Docker containers... + hyperv_setting_enhanced_session: |- + Setting VM Enhanced session transport type inserted_key: |- Key inserted! Disconnecting and reconnecting using new SSH key... inserting_insecure_detected: |- From 67fdc3855f0d22d0669fe90a7aff3103e7838311 Mon Sep 17 00:00:00 2001 From: cam Date: Fri, 8 May 2020 11:27:10 +1000 Subject: [PATCH 4/4] HyperV - Adding ability to disable Enhanced session transport type --- plugins/providers/hyperv/action/configure.rb | 5 ++++- templates/locales/en.yml | 6 ++++-- .../unit/plugins/providers/hyperv/action/configure_test.rb | 7 ++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/plugins/providers/hyperv/action/configure.rb b/plugins/providers/hyperv/action/configure.rb index 4b8299c6b..ba322838f 100644 --- a/plugins/providers/hyperv/action/configure.rb +++ b/plugins/providers/hyperv/action/configure.rb @@ -98,8 +98,11 @@ module VagrantPlugins end if env[:machine].provider_config.enable_enhanced_session_mode - env[:ui].detail(I18n.t("vagrant.hyperv_setting_enhanced_session")) + env[:ui].detail(I18n.t("vagrant.hyperv_enable_enhanced_session")) env[:machine].provider.driver.set_enhanced_session_transport_type("HvSocket") + else + env[:ui].detail(I18n.t("vagrant.hyperv_disable_enhanced_session")) + env[:machine].provider.driver.set_enhanced_session_transport_type("VMBus") end @app.call(env) diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 9de373a0c..0a1308b3d 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -165,8 +165,10 @@ en: -- Detected newer image for container '%{name}', restarting... docker_starting_containers: |- Starting Docker containers... - hyperv_setting_enhanced_session: |- - Setting VM Enhanced session transport type + hyperv_enable_enhanced_session: |- + Setting VM Enhanced session transport type to HvSocket + hyperv_disable_enhanced_session: |- + Setting VM Enhanced session transport type to disabled/default (VMBus) inserted_key: |- Key inserted! Disconnecting and reconnecting using new SSH key... inserting_insecure_detected: |- diff --git a/test/unit/plugins/providers/hyperv/action/configure_test.rb b/test/unit/plugins/providers/hyperv/action/configure_test.rb index 2a9053e35..09f93a9e8 100644 --- a/test/unit/plugins/providers/hyperv/action/configure_test.rb +++ b/test/unit/plugins/providers/hyperv/action/configure_test.rb @@ -47,6 +47,7 @@ describe VagrantPlugins::HyperV::Action::Configure do allow(data_dir).to receive(:join).and_return(sentinel) allow(sentinel).to receive(:file?).and_return(false) allow(sentinel).to receive(:open) + allow(driver).to receive(:set_enhanced_session_transport_type).with("VMBus") end it "should call the app on success" do @@ -119,8 +120,8 @@ describe VagrantPlugins::HyperV::Action::Configure do end context "without enhanced session transport type" do - it "should not call the driver to set enhanced session transport type" do - expect(driver).not_to receive(:set_enhanced_session_transport_type) + it "should call the driver to set enhanced session transport type back to default" do + expect(driver).to receive(:set_enhanced_session_transport_type).with("VMBus") subject.call(env) end end @@ -129,7 +130,7 @@ describe VagrantPlugins::HyperV::Action::Configure do let(:enable_enhanced_session_mode) { true } it "should call the driver to set enhanced session transport type" do - expect(driver).to receive(:set_enhanced_session_transport_type) + expect(driver).to receive(:set_enhanced_session_transport_type).with("HvSocket") subject.call(env) end end