Merge pull request #9456 from briancain/exit-if-hyperv-enabled-vbox

Exit if Hyper-V is enabled and VirtualBox provider is used
This commit is contained in:
Brian Cain 2018-02-14 08:49:06 -08:00 committed by GitHub
commit b8d8cf7572
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 0 deletions

View File

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

View File

@ -97,6 +97,22 @@ module Vagrant
return @_windows_hyperv_admin
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
# Cygwin style path.
#

View File

@ -78,6 +78,12 @@ module VagrantPlugins
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
@vboxmanage_path ||= "VBoxManage"
@logger.info("VBoxManage path: #{@vboxmanage_path}")

View File

@ -846,6 +846,10 @@ en:
("%{value}") could not be found. Please verify that the plugin is
installed which implements this host and that the value you used
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: |-
Vagrant exited after cleanup due to external interrupt.
local_data_dir_not_accessible: |-

View File

@ -214,4 +214,19 @@ describe Vagrant::Util::Platform do
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