Vagrant 2.2.5 和VirtualBox6.1不兼容问题解决办法

1 在安装完毕vagrant和virtualbox后,执行命令 vagrant up时出现

  1. No usable default provider could be found for your system.
  2. Vagrant relies on interactions with 3rd party systems, known as
  3. "providers", to provide Vagrant with resources to run development
  4. environments. Examples are VirtualBox, VMware, Hyper-V.
  5. The easiest solution to this message is to install VirtualBox, which
  6. is available for free on all major platforms.
  7. If you believe you already have a provider available, make sure it
  8. is properly installed and configured. You can see more details about
  9. why a particular provider isnt working by forcing usage with
  10. `vagrant up --provider=PROVIDER`, which should give you a more specific
  11. error message for that particular provider.

出现此错误提示未安装virtualbox,但实际已安装完毕。查资料发现存在版本兼容问题。
需要修改三处内容
1 在C:\software\Vagrant\embedded\gems\2.2.5\gems\vagrant-2.2.5\plugins\providers\virtualbox\plugin.rb的文件内新增红色部分 (添加 下面62 行内容autoload :Version61, File.expand_path(“../driver/version_6_1”, __FILE))

  1. require "vagrant"
  2. module VagrantPlugins
  3. module ProviderVirtualBox
  4. class Plugin < Vagrant.plugin("2")
  5. name "VirtualBox provider"
  6. description <<-EOF
  7. The VirtualBox provider allows Vagrant to manage and control
  8. VirtualBox-based virtual machines.
  9. EOF
  10. provider(:virtualbox, priority: 6) do
  11. require File.expand_path("../provider", __FILE__)
  12. Provider
  13. end
  14. config(:virtualbox, :provider) do
  15. require File.expand_path("../config", __FILE__)
  16. Config
  17. end
  18. synced_folder(:virtualbox) do
  19. require File.expand_path("../synced_folder", __FILE__)
  20. SyncedFolder
  21. end
  22. provider_capability(:virtualbox, :forwarded_ports) do
  23. require_relative "cap"
  24. Cap
  25. end
  26. provider_capability(:virtualbox, :nic_mac_addresses) do
  27. require_relative "cap"
  28. Cap
  29. end
  30. provider_capability(:virtualbox, :public_address) do
  31. require_relative "cap/public_address"
  32. Cap::PublicAddress
  33. end
  34. provider_capability(:virtualbox, :snapshot_list) do
  35. require_relative "cap"
  36. Cap
  37. end
  38. end
  39. autoload :Action, File.expand_path("../action", __FILE__)
  40. # Drop some autoloads in here to optimize the performance of loading
  41. # our drivers only when they are needed.
  42. module Driver
  43. autoload :Meta, File.expand_path("../driver/meta", __FILE__)
  44. autoload :Version_4_0, File.expand_path("../driver/version_4_0", __FILE__)
  45. autoload :Version_4_1, File.expand_path("../driver/version_4_1", __FILE__)
  46. autoload :Version_4_2, File.expand_path("../driver/version_4_2", __FILE__)
  47. autoload :Version_4_3, File.expand_path("../driver/version_4_3", __FILE__)
  48. autoload :Version_5_0, File.expand_path("../driver/version_5_0", __FILE__)
  49. autoload :Version_5_1, File.expand_path("../driver/version_5_1", __FILE__)
  50. autoload :Version_5_2, File.expand_path("../driver/version_5_2", __FILE__)
  51. autoload :Version_6_0, File.expand_path("../driver/version_6_0", __FILE__)
  52. autoload :Version_6_1, File.expand_path("../driver/version_6_1", __FILE__)
  53. end
  54. module Model
  55. autoload :ForwardedPort, File.expand_path("../model/forwarded_port", __FILE__)
  56. end
  57. module Util
  58. autoload :CompileForwardedPorts, File.expand_path("../util/compile_forwarded_ports", __FILE__)
  59. end
  60. end
  61. end

2 在C:\software\Vagrant\embedded\gems\2.2.5\gems\vagrant-2.2.5\plugins\providers\virtualbox\driver 下添加如下内容( 67行添加 内容”6.1” => Version_6_1,)

  1. require "forwardable"
  2. require "thread"
  3. require "log4r"
  4. require "vagrant/util/retryable"
  5. require File.expand_path("../base", __FILE__)
  6. module VagrantPlugins
  7. module ProviderVirtualBox
  8. module Driver
  9. class Meta < Base
  10. # This is raised if the VM is not found when initializing a driver
  11. # with a UUID.
  12. class VMNotFound < StandardError; end
  13. # We use forwardable to do all our driver forwarding
  14. extend Forwardable
  15. # We cache the read VirtualBox version here once we have one,
  16. # since during the execution of Vagrant, it likely doesn't change.
  17. @@version = nil
  18. @@version_lock = Mutex.new
  19. # The UUID of the virtual machine we represent
  20. attr_reader :uuid
  21. # The version of virtualbox that is running.
  22. attr_reader :version
  23. include Vagrant::Util::Retryable
  24. def initialize(uuid=nil)
  25. # Setup the base
  26. super()
  27. @logger = Log4r::Logger.new("vagrant::provider::virtualbox::meta")
  28. @uuid = uuid
  29. @@version_lock.synchronize do
  30. if !@@version
  31. # Read and assign the version of VirtualBox we know which
  32. # specific driver to instantiate.
  33. begin
  34. @@version = read_version
  35. rescue Vagrant::Errors::CommandUnavailable,
  36. Vagrant::Errors::CommandUnavailableWindows
  37. # This means that VirtualBox was not found, so we raise this
  38. # error here.
  39. raise Vagrant::Errors::VirtualBoxNotDetected
  40. end
  41. end
  42. end
  43. # Instantiate the proper version driver for VirtualBox
  44. @logger.debug("Finding driver for VirtualBox version: #{@@version}")
  45. driver_map = {
  46. "4.0" => Version_4_0,
  47. "4.1" => Version_4_1,
  48. "4.2" => Version_4_2,
  49. "4.3" => Version_4_3,
  50. "5.0" => Version_5_0,
  51. "5.1" => Version_5_1,
  52. "5.2" => Version_5_2,
  53. "6.0" => Version_6_0,
  54. "6.1" => Version_6_1,
  55. }
  56. if @@version.start_with?("4.2.14")
  57. # VirtualBox 4.2.14 just doesn't work with Vagrant, so show error
  58. raise Vagrant::Errors::VirtualBoxBrokenVersion040214
  59. end
  60. driver_klass = nil
  61. driver_map.each do |key, klass|
  62. if @@version.start_with?(key)
  63. driver_klass = klass
  64. break
  65. end
  66. end
  67. if !driver_klass
  68. supported_versions = driver_map.keys.sort.join(", ")
  69. raise Vagrant::Errors::VirtualBoxInvalidVersion,
  70. supported_versions: supported_versions
  71. end
  72. @logger.info("Using VirtualBox driver: #{driver_klass}")
  73. @driver = driver_klass.new(@uuid)
  74. @version = @@version
  75. if @uuid
  76. # Verify the VM exists, and if it doesn't, then don't worry
  77. # about it (mark the UUID as nil)
  78. raise VMNotFound if !@driver.vm_exists?(@uuid)
  79. end
  80. end
  81. def_delegators :@driver, :clear_forwarded_ports,
  82. :clear_shared_folders,
  83. :clonevm,
  84. :create_dhcp_server,
  85. :create_host_only_network,
  86. :create_snapshot,
  87. :delete,
  88. :delete_snapshot,
  89. :delete_unused_host_only_networks,
  90. :discard_saved_state,
  91. :enable_adapters,
  92. :execute_command,
  93. :export,
  94. :forward_ports,
  95. :halt,
  96. :import,
  97. :list_snapshots,
  98. :read_forwarded_ports,
  99. :read_bridged_interfaces,
  100. :read_dhcp_servers,
  101. :read_guest_additions_version,
  102. :read_guest_ip,
  103. :read_guest_property,
  104. :read_host_only_interfaces,
  105. :read_mac_address,
  106. :read_mac_addresses,
  107. :read_machine_folder,
  108. :read_network_interfaces,
  109. :read_state,
  110. :read_used_ports,
  111. :read_vms,
  112. :reconfig_host_only,
  113. :remove_dhcp_server,
  114. :restore_snapshot,
  115. :resume,
  116. :set_mac_address,
  117. :set_name,
  118. :share_folders,
  119. :ssh_port,
  120. :start,
  121. :suspend,
  122. :verify!,
  123. :verify_image,
  124. :vm_exists?
  125. protected
  126. # This returns the version of VirtualBox that is running.
  127. #
  128. # @return [String]
  129. def read_version
  130. # The version string is usually in one of the following formats:
  131. #
  132. # * 4.1.8r1234
  133. # * 4.1.8r1234_OSE
  134. # * 4.1.8_MacPortsr1234
  135. #
  136. # Below accounts for all of these.
  137. # Note: We split this into multiple lines because apparently "".split("_")
  138. # is [], so we have to check for an empty array in between.
  139. output = ""
  140. retryable(on: Vagrant::Errors::VirtualBoxVersionEmpty, tries: 3, sleep: 1) do
  141. output = execute("--version")
  142. if output =~ /vboxdrv kernel module is not loaded/ ||
  143. output =~ /VirtualBox kernel modules are not loaded/i
  144. raise Vagrant::Errors::VirtualBoxKernelModuleNotLoaded
  145. elsif output =~ /Please install/
  146. # Check for installation incomplete warnings, for example:
  147. # "WARNING: The character device /dev/vboxdrv does not
  148. # exist. Please install the virtualbox-ose-dkms package and
  149. # the appropriate headers, most likely linux-headers-generic."
  150. raise Vagrant::Errors::VirtualBoxInstallIncomplete
  151. elsif output.chomp == ""
  152. # This seems to happen on Windows for uncertain reasons.
  153. # Raise an error otherwise the error is that they have an
  154. # incompatible version of VirtualBox which isn't true.
  155. raise Vagrant::Errors::VirtualBoxVersionEmpty,
  156. vboxmanage: @vboxmanage_path.to_s
  157. end
  158. end
  159. parts = output.split("_")
  160. return nil if parts.empty?
  161. parts[0].split("r")[0]
  162. end
  163. end
  164. end
  165. end
  166. end

3 在C:\software\Vagrant\embedded\gems\2.2.5\gems\vagrant-2.2.5\plugins\providers\virtualbox\driver 下新建version_6_1.rb 内容如下

  1. require File.expand_path("../version_6_0", __FILE__)
  2. module VagrantPlugins
  3. module ProviderVirtualBox
  4. module Driver
  5. # Driver for VirtualBox 6.1.x
  6. class Version_6_1 < Version_6_0
  7. def initialize(uuid)
  8. super
  9. @logger = Log4r::Logger.new("vagrant::provider::virtualbox_6_1")
  10. end
  11. end
  12. end
  13. end
  14. end

以上修改完成后可以正常执行