Vagrantで Vagrant was unable to mount VirtualBox shared folders. エラーが起こった際の解決法

 

ひょんなことからVagrantを使用する機会があったのですが、何度か再起動していると次のようなエラーが出るようになりました。

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 dmode=777,fmode=777,uid=500,gid=500 var_www_html /var/www/html

The error output from the command was:

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

VirtualBoxの共有フォルダをマウントすることができませんでした、ということですね。そこで、上記の中にも書かれている

mount -t vboxsf -o dmode=777,fmode=777,uid=500,gid=500 var_www_html /var/www/html

を実行してみても改善されなかったのですが、次の記事にある対処法で解決できました。

qiita.com

 

手順は、

$ vagrant up

で上記エラーが出ても構わず

$ vagrant ssh

でログインし、次を実行。私の環境では以下のようになりました。OpenGL supportはコケていてもそもそも必要がないので気にしないことにします。

$ sudo /etc/init.d/vboxadd setup
Removing existing VirtualBox non-DKMS kernel modules [ OK ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module [ OK ]
Building the shared folder support module [ OK ]
Building the OpenGL support module [失敗]
(Look at /var/log/vboxadd-install.log to find out what went wrong)
Doing non-kernel setup of the Guest Additions [ OK ]

この後、一旦ログアウトして

$ vagrant reload

とやるとエラーは無くなりました。