vagrant up実行時に共有フォルダを使えないエラー発生と、プラグインで解決したら起動が遅い問題

Vagrantの仮想マシンをyum updateしたら、仮想マシン起動時に「Vagrant was unable to mount VirtualBox shared folders」というエラーが出るようになり、共有ディレクトリが使えなくなったので、その問題解決をした時の事について書いています。

※ 本ページはプロモーションが含まれています。

エラー内容と動作環境

最終更新日:2021/10/15

VagrantでCentOS7.6のBoxの仮想マシン(VM)をyum updateしたら、vagrant upで仮想マシンを起動するたびに、エラーメッセージが出るようになってしまいました。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'bento/centos-7.6' version '201907.24.0' is up to date...
〜
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /Users/hogeuser/testCentOS76VM
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=1000,gid=1000 vagrant /vagrant

The error output from the command was:

/sbin/mount.vboxsf: mounting failed with the error: No such device

エラー内容ですが、共有フォルダをマウントしようとして失敗しています。ちなみに共有フォルダのマウントエラーだけで、仮想マシン自体は起動できています。

Vagrantは初期設定で、ホスト側のPCのVagrantfileが置いてあるディレクトリと、仮想マシンの/vagrantディレクトリが共有されますが、その共有ができていないというエラーメッセージです。実際、sshで仮想マシンにログインして確認してみたけど、ディレクトリの共有ができていませんでした。

◾️動作環境とバージョン情報です
OS:macOS Big Sur(バージョン11.6)
仮想マシン(VM)のBox:bento/centos-7.6

$ vagrant -v
Vagrant 2.2.7

$ VirtualBox -h
Oracle VM VirtualBox VM Selector v6.1.16
仮想マシン上のOS
・yum update実行前
$ cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 

・yum update実行後
$ cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

エラーの解決。vagrant-vbguestプラグインのインストール

問題のエラーメッセージ「Vagrant was unable to mount VirtualBox shared folders」でググって調べたら、vagrant-vbguestというプラグインをインストールすれば解決できるという事だったので、vagrant-vbguestをインストールします。

$ vagrant plugin install vagrant-vbguest
〜
$ vagrant plugin list
〜
vagrant-vbguest (0.30.0, global)
無事インストールできました!

再度VMを起動してみる。エラーは出なくなったが、、VMの起動が遅い

プラグインをインストールしたので、再度仮想マシン(VM)を起動してみます。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
〜
==> default: Machine booted and ready!
[default] GuestAdditions seems to be installed (6.1.16) correctly, but not running.
Redirecting to /bin/systemctl start vboxadd.service
Redirecting to /bin/systemctl start vboxadd-service.service
VirtualBox Guest Additions: Starting.
VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel 
modules.  This may take a while.
VirtualBox Guest Additions: To build modules for other installed kernels, run
〜
==> default: Mounting shared folders...
    default: /vagrant => /Users/hogeuser/testCentOS76VM
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
起動しました。エラーメッセージは出なくなり、共有ディレクトリも問題なく機能するようになりました。

ただ起動時間が長い。あまりにも遅い。。初回だけかなと思って。何度か仮想マシンを起動し直してみましたが、毎回遅い。
このプラグインをインストールする前は30秒かからなかった仮想マシンの起動時間が、プラグインをインストール後は毎回数分かかるようになってしまいました。

vagrant-vbguestプラグインをアンインストールしたら、すべて解決!

vagrant upするたびに、毎回これだけ待たされるのはキツイ・我慢できないので、一旦vagrant-vbguestプラグインをアンインストールします。

$ vagrant plugin uninstall vagrant-vbguest
〜
$ vagrant plugin list
〜

アンインストールできたので、確認のため再度仮想マシンを起動してみます。
すると今度は最初のエラーメッセージは出なくなり問題なく起動できましたし、起動時間も元に戻って早く起動できるようになりました。

正直何故プラグインをアンインストールした事でエラーが消えたのかは解りませんが、これで全て問題は解決しましたのでオッケーです。