First implementation for Integration services for Hyper-V as discussed in https://github.com/mitchellh/vagrant/pull/6321 #6321

This commit is contained in:
Bjorn Brala 2017-03-15 22:12:19 +01:00
parent 765494a2bf
commit e2dd23b06c
5 changed files with 84 additions and 9 deletions

View File

@ -21,6 +21,7 @@ module VagrantPlugins
differencing_disk = env[:machine].provider_config.differencing_disk differencing_disk = env[:machine].provider_config.differencing_disk
auto_start_action = env[:machine].provider_config.auto_start_action auto_start_action = env[:machine].provider_config.auto_start_action
auto_stop_action = env[:machine].provider_config.auto_stop_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 Dynamic memory allocation, maxmemory is #{maxmemory}") if maxmemory
env[:ui].output("Configured startup memory is #{memory}") if memory env[:ui].output("Configured startup memory is #{memory}") if memory
@ -148,6 +149,21 @@ module VagrantPlugins
env[:ui].detail("Creating and registering the VM...") env[:ui].detail("Creating and registering the VM...")
server = env[:machine].provider.driver.import(options) 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[:ui].detail("Successfully imported a VM with name: #{server['name']}")
env[:machine].id = server["id"] env[:machine].id = server["id"]
@app.call(env) @app.call(env)

View File

@ -3,7 +3,6 @@ require "vagrant"
module VagrantPlugins module VagrantPlugins
module HyperV module HyperV
class Config < Vagrant.plugin("2", :config) 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 :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 :memory # Memory size in mb @return [Integer]
attr_accessor :maxmemory # Maximal memory size in mb enables dynamical memory allocation @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 :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 :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 :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_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 attr_accessor :auto_stop_action #action on automatic stop of VM. Values: ShutDown, TurnOff, Save [String]
attr_accessor :vm_integration_service # Options for VMServiceIntegration attr_accessor :vm_integration_services # Options for VMServiceIntegration [Hash]
def initialize def initialize
@ip_address_timeout = UNSET_VALUE @ip_address_timeout = UNSET_VALUE
@ -27,13 +26,13 @@ module VagrantPlugins
@differencing_disk = UNSET_VALUE @differencing_disk = UNSET_VALUE
@auto_start_action = UNSET_VALUE @auto_start_action = UNSET_VALUE
@auto_stop_action = UNSET_VALUE @auto_stop_action = UNSET_VALUE
@vm_integration_service = { @vm_integration_services = {
guest_service_interface: UNSET_VALUE, guest_service_interface: UNSET_VALUE,
heartbeat: UNSET_VALUE, heartbeat: UNSET_VALUE,
key_value_pair_exchange: UNSET_VALUE, key_value_pair_exchange: UNSET_VALUE,
shutdown: UNSET_VALUE, shutdown: UNSET_VALUE,
vss: UNSET_VALUE, time_synchronization: UNSET_VALUE,
time_synchronization: UNSET_VALUE vss: UNSET_VALUE
} }
end end
@ -51,9 +50,10 @@ module VagrantPlugins
@auto_start_action = nil if @auto_start_action == UNSET_VALUE @auto_start_action = nil if @auto_start_action == UNSET_VALUE
@auto_stop_action = nil if @auto_stop_action == UNSET_VALUE @auto_stop_action = nil if @auto_stop_action == UNSET_VALUE
@vm_integration_service.each { |key, value| @vm_integration_services.each { |key, value|
@vm_integration_service[key] = nil if value == UNSET_VALUE @vm_integration_services[key] = nil if value == UNSET_VALUE
} }
@vm_integration_services = nil if @vm_integration_services.length == 0
end end
def validate(machine) def validate(machine)

View File

@ -118,6 +118,10 @@ module VagrantPlugins
execute("delete_snapshot.ps1", {VmID: vm_id, SnapName: snapshot_name}) execute("delete_snapshot.ps1", {VmID: vm_id, SnapName: snapshot_name})
end end
def set_vm_integration_services(config)
execute("set_vm_integration_services.ps1", config)
end
protected protected
def execute_powershell(path, options, &block) def execute_powershell(path, options, &block)

View File

@ -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

View File

@ -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. 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. 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. * `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
```