ひよこインフラてっく!

ひよこインフラエンジニア「ひよこ大佐」による技術ブログ的なもの。インフラ技術や仮想化、Pythonなど。

Fedora 31でVagrantが使えなくなったときの対処と、Vagrant 2.2.6をVirtualBox 6.1に対応させる方法

どうもひよこ大佐です。

タイトルの通り、Fedora 31でカーネルのアップデートに伴ってVagrantが使えなくなってしまったので、その際の対処法をご紹介したいと思います。

発生した事象

メインのPC(Intel Hades Canyon NUC)でFedora 31を利用しています。この環境ではAnsible Towerの検証用にVagrantVirtualboxを利用しているのですが、いつも通り dnf updateカーネルの更新を含む更新パッケージを適用し再起動したところ、vagrant upしようとすると以下のエラーが発生するようになりました。

カーネルバージョン:

$ uname -r
5.4.7-200.fc31.x86_64

発生したエラー:

VirtualBox is complaining that the kernel module is not loaded. Please
run `VBoxManage --version` or open the VirtualBox GUI to see the error
message which should contain instructions on how to fix this error.

エラーの指定通り VBoxManage --version を実行すると、以下のWarningが出力されます。

$ VBoxManage --version
WARNING: The vboxdrv kernel module is not loaded. Either there is no module
         available for the current kernel (5.4.7-200.fc31.x86_64) or it failed to
         load. Please recompile the kernel module and install it by

           sudo /sbin/vboxconfig

         You will not be able to start VMs until this problem is fixed.
6.0.14r133895

上記指示通りに sudo /sbin/vboxconfig を実行するのですが、以下の通りエラーになります。

$ sudo /sbin/vboxconfig

vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Starting VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
vboxdrv.sh: failed: Look at /var/log/vbox-setup.log to find out what went wrong.

There were problems setting up VirtualBox.  To re-start the set-up process, run
  /sbin/vboxconfig
as root.  If your system is using EFI Secure Boot you may need to sign the
kernel modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) before you can load
them. Please see your Linux system's documentation for more information.

原因

Virtualbox 6.0はLinux Kernel 5.4.xに対応していないため、エラーが発生します。 以下のChangelogにも、Virtualbox 6.1からサポートしている旨が明記されています。

https://www.virtualbox.org/wiki/Changelog-6.1

Linux host and guest: Support Linux 5.4

Virtualbox 6.1への更新のため、公式サイトからrpmをダウンロードしインストールしました。

Linux_Downloads – Oracle VM VirtualBox

Vagrantでエラー発生

インストールしたところVirtualboxは正常に動作するようになりましたが、今度はVagrantでエラーが発生するようになってしまいました。

Vagrantのバージョン:

$ vagrant --version
Vagrant 2.2.6

発生したエラー

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

どうやら、現在(2020/1/10時点)リリースされている最新版のVagrant 2.2.6では、Virtualbox 6.1に対応していないようです。

ワークアラウンド

以下のGitHubのIssueに記載されているワークアラウンドにしたがって対処します。 Vagrant 2.2.6 doesn't work with VirtualBox 6.1.0 · Issue #178 · oracle/vagrant-boxes · GitHub

1. plugin.rb の編集

/usr/share/vagrant/gems/gems/vagrant-2.2.6/plugins/providers/virtualbox/ 内の plugin.rb 内の module Driver (52行目) のブロックの末尾に、autoload :Version_6_1, File.expand_path("../driver/version_6_1", __FILE__) の行を追記します。

   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

2. meta.rb の編集

編集が完了したら、 /usr/share/vagrant/gems/gems/vagrant-2.2.6/plugins/providers/virtualbox/driver/に移動し、 meta.rb 内の driver_map (58行目) のブロックに "6.1" => Version_6_1, の行を追記します。

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,
}

3. version_6_1.rb の作成

最後に、2の手順と同じディレクトリ( /usr/share/vagrant/gems/gems/vagrant-2.2.6/plugins/providers/virtualbox/driver/ )で以下のコマンドを実行し、 version_6_1.rb を作成します。

$ sudo wget https://raw.githubusercontent.com/briancain/vagrant/fb4e6985e142da56bad143d70600cd3695c91757/plugins/providers/virtualbox/driver/version_6_1.rb

実行後、念の為パーミッションやオーナーが他のファイルと同じになっているか確認してください。

$ ls -la
合計 172
drwxr-xr-x. 2 root root  4096  1月 10 13:38 .
drwxr-xr-x. 7 root root  4096  1月 10 13:32 ..
-rw-r--r--. 1 root root 15850 11月  8 07:41 base.rb
-rw-r--r--. 1 root root  6188  1月 10 13:34 meta.rb
-rw-r--r--. 1 root root 18521 11月  8 07:41 version_4_0.rb
-rw-r--r--. 1 root root 22909 11月  8 07:41 version_4_1.rb
-rw-r--r--. 1 root root 23969 11月  8 07:41 version_4_2.rb
-rw-r--r--. 1 root root 25784 11月  8 07:41 version_4_3.rb
-rw-r--r--. 1 root root 28392 11月  8 07:41 version_5_0.rb
-rw-r--r--. 1 root root   357 11月  8 07:41 version_5_1.rb
-rw-r--r--. 1 root root   357 11月  8 07:41 version_5_2.rb
-rw-r--r--. 1 root root  3811 11月  8 07:41 version_6_0.rb
-rw-r--r--. 1 root root   357  1月 10 13:38 version_6_1.rb

これで、エラーが解消されました。同じ事象に悩んでいる方は参考にしていただければ幸いです。