升级 VirtualBox 后 Vagrant 无法启动

最新发布的 Vagrant v2.4.2 已经支持 VirtualBox 7.1 了。

近期将本地安装的 VirtualBox 升级到 7.1 版本,昨天启动开发环境时时发现 Vagrant 2.4.1 不支持 VirtualBox 7.1,错误提示如下:

The provider 'virtualbox' that was requested to back the machine
'xxxxyyyyyzzzz' 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, 6.1, 7.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 2.4.1 最高支持到 7.0 版本的 VirtualBox。如果要等 Vagrant 更新版本还不知道要到猴年马月,将 VirtualBox 退回到 7.0 版本也怕把开发环境弄坏。好在只要稍稍动一下手脚就能让 Vagrant 支持 VirtualBox 7.1 了。

修改 Vagrant 安装目录下的 embedded\gems\gems\vagrant-2.4.1\plugins\providers\virtualbox\driver\meta.rb 文件,在第 71 行下新增如下一行代码:

ruby          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,
            "7.0" => Version_7_0,
            "7.1" => Version_7_0,
          }

保存后再次启动 Vagrant,顺利骗过了程序,VirtualBox 虚拟机正常启动了。