From e2dd23b06ca8c94f175e248d2ca28d2cc3055983 Mon Sep 17 00:00:00 2001 From: Bjorn Brala Date: Wed, 15 Mar 2017 22:12:19 +0100 Subject: [PATCH] First implementation for Integration services for Hyper-V as discussed in https://github.com/mitchellh/vagrant/pull/6321 #6321 --- plugins/providers/hyperv/action/import.rb | 16 ++++++++ plugins/providers/hyperv/config.rb | 18 ++++----- plugins/providers/hyperv/driver.rb | 4 ++ .../scripts/set_vm_integration_services.ps1 | 37 +++++++++++++++++++ .../source/docs/hyperv/configuration.html.md | 18 +++++++++ 5 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 plugins/providers/hyperv/scripts/set_vm_integration_services.ps1 diff --git a/plugins/providers/hyperv/action/import.rb b/plugins/providers/hyperv/action/import.rb index d841242ff..e39855fd3 100644 --- a/plugins/providers/hyperv/action/import.rb +++ b/plugins/providers/hyperv/action/import.rb @@ -21,6 +21,7 @@ module VagrantPlugins differencing_disk = env[:machine].provider_config.differencing_disk auto_start_action = env[:machine].provider_config.auto_start_action auto_stop_action = env[:machine].provider_config.auto_stop_action + vm_integration_services = env[:machine].provider_config.vm_integration_services env[:ui].output("Configured Dynamic memory allocation, maxmemory is #{maxmemory}") if maxmemory env[:ui].output("Configured startup memory is #{memory}") if memory @@ -148,6 +149,21 @@ module VagrantPlugins env[:ui].detail("Creating and registering the VM...") server = env[:machine].provider.driver.import(options) + + env[:ui].detail("Setting VM Integration Services") + vm_integration_services.each do |key, value| + state = false + if value === true + state = "enabled" + elsif value === false + state = "disabled" + end + env[:ui].output("#{key} is #{state}") if state + end + + vm_integration_services[:VmId] = server["id"] + env[:machine].provider.driver.set_vm_integration_services(vm_integration_services) + env[:ui].detail("Successfully imported a VM with name: #{server['name']}") env[:machine].id = server["id"] @app.call(env) diff --git a/plugins/providers/hyperv/config.rb b/plugins/providers/hyperv/config.rb index ea11a52cd..e7459a502 100644 --- a/plugins/providers/hyperv/config.rb +++ b/plugins/providers/hyperv/config.rb @@ -3,7 +3,6 @@ require "vagrant" module VagrantPlugins module HyperV class Config < Vagrant.plugin("2", :config) - attr_accessor :ip_address_timeout # Time to wait for an IP address when booting, in seconds @return [Integer] attr_accessor :memory # Memory size in mb @return [Integer] attr_accessor :maxmemory # Maximal memory size in mb enables dynamical memory allocation @return [Integer] @@ -12,9 +11,9 @@ module VagrantPlugins attr_accessor :vlan_id # VLAN ID for network interface for the virtual machine. @return [Integer] attr_accessor :mac # MAC address for network interface for the virtual machine. @return [String] attr_accessor :differencing_disk # Create differencing disk instead of cloning whole VHD [Boolean] - attr_accessor :auto_start_action #action on automatic start of VM. Values: Nothing, StartIfRunning, Start - attr_accessor :auto_stop_action #action on automatic stop of VM. Values: ShutDown, TurnOff, Save - attr_accessor :vm_integration_service # Options for VMServiceIntegration + attr_accessor :auto_start_action #action on automatic start of VM. Values: Nothing, StartIfRunning, Start [String] + attr_accessor :auto_stop_action #action on automatic stop of VM. Values: ShutDown, TurnOff, Save [String] + attr_accessor :vm_integration_services # Options for VMServiceIntegration [Hash] def initialize @ip_address_timeout = UNSET_VALUE @@ -27,13 +26,13 @@ module VagrantPlugins @differencing_disk = UNSET_VALUE @auto_start_action = UNSET_VALUE @auto_stop_action = UNSET_VALUE - @vm_integration_service = { + @vm_integration_services = { guest_service_interface: UNSET_VALUE, heartbeat: UNSET_VALUE, key_value_pair_exchange: UNSET_VALUE, shutdown: UNSET_VALUE, - vss: UNSET_VALUE, - time_synchronization: UNSET_VALUE + time_synchronization: UNSET_VALUE, + vss: UNSET_VALUE } end @@ -51,9 +50,10 @@ module VagrantPlugins @auto_start_action = nil if @auto_start_action == UNSET_VALUE @auto_stop_action = nil if @auto_stop_action == UNSET_VALUE - @vm_integration_service.each { |key, value| - @vm_integration_service[key] = nil if value == UNSET_VALUE + @vm_integration_services.each { |key, value| + @vm_integration_services[key] = nil if value == UNSET_VALUE } + @vm_integration_services = nil if @vm_integration_services.length == 0 end def validate(machine) diff --git a/plugins/providers/hyperv/driver.rb b/plugins/providers/hyperv/driver.rb index abd353663..359e14b12 100644 --- a/plugins/providers/hyperv/driver.rb +++ b/plugins/providers/hyperv/driver.rb @@ -118,6 +118,10 @@ module VagrantPlugins execute("delete_snapshot.ps1", {VmID: vm_id, SnapName: snapshot_name}) end + def set_vm_integration_services(config) + execute("set_vm_integration_services.ps1", config) + end + protected def execute_powershell(path, options, &block) diff --git a/plugins/providers/hyperv/scripts/set_vm_integration_services.ps1 b/plugins/providers/hyperv/scripts/set_vm_integration_services.ps1 new file mode 100644 index 000000000..da4df1822 --- /dev/null +++ b/plugins/providers/hyperv/scripts/set_vm_integration_services.ps1 @@ -0,0 +1,37 @@ +param ( + [string] $VmId, + [string] $guest_service_interface = $null, + [string] $heartbeat = $null, + [string] $key_value_pair_exchange = $null, + [string] $shutdown = $null, + [string] $time_synchronization = $null, + [string] $vss = $null +) + +# Include the following modules +$Dir = Split-Path $script:MyInvocation.MyCommand.Path +. ([System.IO.Path]::Combine($Dir, "utils\write_messages.ps1")) + +$vm = Get-VM -Id $VmId -ErrorAction "stop" + +# Set the service based on value +function VmSetService +{ + param ([string] $Name, [string] $Value, [Microsoft.HyperV.PowerShell.VirtualMachine] $Vm) + + if ($Value -ne $null){ + if($Value -eq "true"){ + Enable-VMIntegrationService -VM $Vm -Name $Name + } + if($Value -eq "false"){ + Disable-VMIntegrationService -VM $Vm -Name $Name + } + } +} + +VmSetService -Name "Guest Service Interface" -Value $guest_service_interface -Vm $vm +VmSetService -Name "Heartbeat" -Value $heartbeat -Vm $vm +VmSetService -Name "Key-Value Pair Exchange" -Value $key_value_pair_exchange -Vm $vm +VmSetService -Name "Shutdown" -Value $shutdown -Vm $vm +VmSetService -Name "Time Synchronization" -Value $time_synchronization -Vm $vm +VmSetService -Name "VSS" -Value $vss -Vm $vm \ No newline at end of file diff --git a/website/source/docs/hyperv/configuration.html.md b/website/source/docs/hyperv/configuration.html.md index c6002ec34..2507a07ad 100644 --- a/website/source/docs/hyperv/configuration.html.md +++ b/website/source/docs/hyperv/configuration.html.md @@ -29,3 +29,21 @@ you may set. A complete reference is shown below: virtual machine to report an IP address. This defaults to 120 seconds. This may have to be increased if your VM takes longer to boot. * `differencing_disk` (boolean) - Switch to use differencing disk intead of cloning whole VHD. + * `vm_integration_services` (Hash) - Hash to set the state of integration services. + + Example: + + ```ruby + config.vm.provider "hyperv" do |h| + h.vm_integration_services = { + guest_service_interface: true, + heartbeat: true, + key_value_pair_exchange: false, + shutdown: true, + time_synchronization: true, + vss: true + } + end + ``` + + \ No newline at end of file