(#9258) Exit if Hyper-V is enabled and VirtualBox provider is used

This commit adds a function on windows for the VirtualBox provider to
check if Hyper-V is enabled. If so, exit and display a warning to the
user that going forward will result in a system crash.
This commit is contained in:
Brian Cain 2018-02-07 16:46:13 -08:00
parent 0d0a7e5a40
commit e8708232a5
No known key found for this signature in database
GPG Key ID: 43D51080D357A001
5 changed files with 45 additions and 0 deletions

View File

@ -372,6 +372,10 @@ module Vagrant
error_key(:home_dir_unknown_version) error_key(:home_dir_unknown_version)
end end
class HypervVirtualBoxError < VagrantError
error_key(:hyperv_virtualbox_error)
end
class ForwardPortAdapterNotFound < VagrantError class ForwardPortAdapterNotFound < VagrantError
error_key(:forward_port_adapter_not_found) error_key(:forward_port_adapter_not_found)
end end

View File

@ -97,6 +97,22 @@ module Vagrant
return @_windows_hyperv_admin return @_windows_hyperv_admin
end end
# Checks if Hyper-V is enabled on the host system and returns true
# if enabled.
#
# @return [Boolean]
def windows_hyperv_enabled?
return @_windows_hyperv_enabled if defined?(@_windows_hyperv_enabled)
@_windows_hyperv_enabled = -> {
ps_cmd = "$(Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online).State"
output = Vagrant::Util::PowerShell.execute_cmd(ps_cmd)
return output == 'Enabled'
}.call
return @_windows_hyperv_enabled
end
# This takes any path and converts it from a Windows path to a # This takes any path and converts it from a Windows path to a
# Cygwin style path. # Cygwin style path.
# #

View File

@ -78,6 +78,12 @@ module VagrantPlugins
end end
end end
if Vagrant::Util::Platform.windows? && Vagrant::Util::Platform.windows_hyperv_enabled?
@logger.error("Virtualbox and Hyper-V cannot be used together at the same time on Windows and will result in a system crash.")
raise Vagrant::Errors::HypervVirtualBoxError
end
# Fall back to hoping for the PATH to work out # Fall back to hoping for the PATH to work out
@vboxmanage_path ||= "VBoxManage" @vboxmanage_path ||= "VBoxManage"
@logger.info("VBoxManage path: #{@vboxmanage_path}") @logger.info("VBoxManage path: #{@vboxmanage_path}")

View File

@ -846,6 +846,10 @@ en:
("%{value}") could not be found. Please verify that the plugin is ("%{value}") could not be found. Please verify that the plugin is
installed which implements this host and that the value you used installed which implements this host and that the value you used
for `config.vagrant.host` is correct. for `config.vagrant.host` is correct.
hyperv_virtualbox_error: |-
Hyper-V and VirtualBox cannot be used together and will result in a
system crash. Vagrant will now exit. Please disable Hyper-V if you wish
to use VirtualBox.
interrupted: |- interrupted: |-
Vagrant exited after cleanup due to external interrupt. Vagrant exited after cleanup due to external interrupt.
local_data_dir_not_accessible: |- local_data_dir_not_accessible: |-

View File

@ -214,4 +214,19 @@ describe Vagrant::Util::Platform do
end end
end end
end end
describe ".windows_hyperv_enabled?" do
it "should return true if enabled" do
allow(Vagrant::Util::PowerShell).to receive(:execute_cmd).and_return('Enabled')
expect(Vagrant::Util::Platform.windows_hyperv_enabled?).to be_truthy
end
it "should return false if disabled" do
Vagrant::Util::Platform.reset!
allow(Vagrant::Util::PowerShell).to receive(:execute_cmd).and_return('Disabled')
expect(Vagrant::Util::Platform.windows_hyperv_enabled?).to be_falsey
end
end
end end