Vagrant 2.2.6 兼容 VirtualBox 6.1 方案

Vagrant 2.2.6 兼容 VirtualBox 6.1 方案

最近清理电脑的时候,手贱将 VirtualBox 从 6.0.* 升级到了 6.1.10,升级后发现无法启动项目的开发环境了。

启动虚拟机

启动项目开发环境时,提示如下错误,可以发现 Vagrant 仅支持到 VirtualBox 6.0。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ vagrant up

The provider 'virtualbox' that was requested to back the machine
'default' is reporting that it isn't usable on this system. The
reason is shown below:

Vagrant has detected that you have a version of VirtualBox installed
that is not supported by this version of Vagrant. Please install one of
the supported versions listed below to use Vagrant:

4.0, 4.1, 4.2, 4.3, 5.0, 5.1, 5.2, 6.0

A Vagrant update may also be available that adds support for the version
you specified. Please check www.vagrantup.com/downloads.html to download
the latest version.

那么下面我么就来魔改一下 Vagrant 的代码,是其支持最新版本的 VirtualBox。

修改 Vagrant 源码

plugin.rb

1
2
3
4
5
# macOS 系统下文件路径
/opt/vagrant/embedded/gems/2.2.6/gems/vagrant-2.2.6/plugins/providers/virtualbox/plugin.rb

# Windows 系统下文件路径
C:\HashiCorp\Vagrant\embedded\gems\2.2.6\gems\vagrant-2.2.6\plugins\providers\virtualbox\plugin.rb

修改后内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
module Driver
autoload :Meta, File.expand_path("../driver/meta", __FILE__)
autoload :Version_4_0, File.expand_path("../driver/version_4_0", __FILE__)
autoload :Version_4_1, File.expand_path("../driver/version_4_1", __FILE__)
autoload :Version_4_2, File.expand_path("../driver/version_4_2", __FILE__)
autoload :Version_4_3, File.expand_path("../driver/version_4_3", __FILE__)
autoload :Version_5_0, File.expand_path("../driver/version_5_0", __FILE__)
autoload :Version_5_1, File.expand_path("../driver/version_5_1", __FILE__)
autoload :Version_5_2, File.expand_path("../driver/version_5_2", __FILE__)
autoload :Version_6_0, File.expand_path("../driver/version_6_0", __FILE__)
autoload :Version_6_1, File.expand_path("../driver/version_6_1", __FILE__)
end

如上所示,在 Driver 中增加了一行 autoload :Version_6_1, File.expand_path("../driver/version_6_1", __FILE__)

meta.rb

1
2
3
4
5
# macOS 系统下文件路径
/opt/vagrant/embedded/gems/2.2.6/gems/vagrant-2.2.6/plugins/providers/virtualbox/driver/meta.rb

# Windows 系统下文件路径
C:\HashiCorp\Vagrant\embedded\gems\2.2.6\gems\vagrant-2.2.6\plugins\providers\virtualbox\driver\meta.rb

修改后的内容如下:

1
2
3
4
5
6
7
8
9
10
11
driver_map   = {
"4.0" => Version_4_0,
"4.1" => Version_4_1,
"4.2" => Version_4_2,
"4.3" => Version_4_3,
"5.0" => Version_5_0,
"5.1" => Version_5_1,
"5.2" => Version_5_2,
"6.0" => Version_6_0,
"6.1" => Version_6_1,
}

增加了最后一行 "6.1" => Version_6_1,

version_6_1.rb

在下面目录中创建 version_6_1.rb 文件:

1
2
3
4
5
# macOS 系统下文件路径
/opt/vagrant/embedded/gems/2.2.6/gems/vagrant-2.2.6/plugins/providers/virtualbox/driver

# Windows 系统下文件路径
C:\HashiCorp\Vagrant\embedded\gems\2.2.6\gems\vagrant-2.2.6\plugins\providers\virtualbox\driver

并将下面内容填充到 version_6_1.rb 文件中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
require File.expand_path("../version_6_0", __FILE__)

module VagrantPlugins
module ProviderVirtualBox
module Driver
# Driver for VirtualBox 6.1.x
class Version_6_1 < Version_6_0
def initialize(uuid)
super

@logger = Log4r::Logger.new("vagrant::provider::virtualbox_6_1")
end
end
end
end
end

到这里就算完成魔改了,赶快在项目中试一下能否正常启动开发环境吧。

评论