Vagrantで共有フォルダのマウントが失敗するときの対処方法(ffiをインストールして解決）


# **現象**
vagrant upで仮想マシンを起動する際、以下のようなメッセージが出て、共有フォルダのマウントに失敗することがある。

```dummy:エラーメッセージ
Failed to mount folders in Linux guest. This is usually because
the "vboxsf" file system is not available. Please verify that
the guest additions are properly installed in the guest and
can work properly. The command attempted was:

mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant

The error output from the last command was:

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

**原因**

ホスト⇔ゲスト間のフォルダ共有機能はGuest Additionsが提供しているが
ホスト(VirtualBox)とゲスト(Guest Additions)でバージョン不一致があるとマウントエラーが発生するようだ。

**実行環境**
Vagrant: 1.8.1
VirtualBox: 5.0.16
GuestAdditions: 4.1.6

# **対策**

vagrant up時に自動的に新しいバージョンのGuest Additionsに更新してくれる「vagrant-vbguest」というプラグインがあるので、インストールしておくと良い。

## **インストール手順**

```dummy:コマンド
vagrant plugin install vagrant-vbguest
```

しかし、'ffi'がインストール出来ないとのエラーが出た。

```dummy:エラーメッセージ
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Bundler, the underlying system Vagrant uses to install plugins,
reported an error. The error is shown below. These errors are usually
caused by misconfigured plugin installations or transient network
issues. The error from Bundler is:

An error occurred while installing ffi (1.9.14), and Bundler cannot continue.
Make sure that `gem install ffi -v '1.9.14'` succeeds before bundling.

Warning: this Gemfile contains multiple primary sources. Using `source` more than once without a block is a security risk, and may result in installing unexpected gems. To resolve this warning, use a block to indicate which gems should come from the secondary source. To upgrade this warning to an error, run `bundle config disable_multisource true`.Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /opt/vagrant/embedded/bin/ruby -r ./siteconf20160922-2725-1e3o1nc.rb extconf.rb
checking for ffi.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/opt/vagrant/embedded/bin/$(RUBY_BASE_NAME)
	--with-ffi_c-dir
	--without-ffi_c-dir
	--with-ffi_c-include
	--without-ffi_c-include=${ffi_c-dir}/include
	--with-ffi_c-lib
	--without-ffi_c-lib=${ffi_c-dir}/lib
	--with-libffi-config
	--without-libffi-config
	--with-pkg-config
	--without-pkg-config
/opt/vagrant/embedded/lib/ruby/2.2.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
	from /opt/vagrant/embedded/lib/ruby/2.2.0/mkmf.rb:571:in `block in try_compile'
	from /opt/vagrant/embedded/lib/ruby/2.2.0/mkmf.rb:524:in `with_werror'
	from /opt/vagrant/embedded/lib/ruby/2.2.0/mkmf.rb:571:in `try_compile'
	from /opt/vagrant/embedded/lib/ruby/2.2.0/mkmf.rb:1060:in `block in have_header'
	from /opt/vagrant/embedded/lib/ruby/2.2.0/mkmf.rb:911:in `block in checking_for'
	from /opt/vagrant/embedded/lib/ruby/2.2.0/mkmf.rb:351:in `block (2 levels) in postpone'
	from /opt/vagrant/embedded/lib/ruby/2.2.0/mkmf.rb:321:in `open'
	from /opt/vagrant/embedded/lib/ruby/2.2.0/mkmf.rb:351:in `block in postpone'
	from /opt/vagrant/embedded/lib/ruby/2.2.0/mkmf.rb:321:in `open'
	from /opt/vagrant/embedded/lib/ruby/2.2.0/mkmf.rb:347:in `postpone'
	from /opt/vagrant/embedded/lib/ruby/2.2.0/mkmf.rb:910:in `checking_for'
	from /opt/vagrant/embedded/lib/ruby/2.2.0/mkmf.rb:1059:in `have_header'
	from extconf.rb:16:in `<main>'

extconf failed, exit code 1

```

以下のコマンドで'ffi'をインストールする。

```dummy:コマンド
gem install --install-dir ~/.vagrant.d/gems 'ffi' -v '1.9.14'
```

再度インストールを試みる。

```dummy:コマンド
vagrant plugin install vagrant-vbguest
```

```dummy:実行結果
Installed the plugin 'vagrant-vbguest (0.13.0)'!
```

vbguestのステータスを確認する。

```dummy:コマンド
vagrant vbguest --status
```

```dummy:実行結果
[default] GuestAdditions versions on your host (5.0.16) and guest (4.1.6) do not match.
```

ゲストの方がホストより古いため、ゲスト側をアップデートする。

```dummy:コマンド
vagrant vbguest
```

```dummy:実行結果
[default] GuestAdditions versions on your host (5.0.16) and guest (4.1.6) do not match.

--中略--

Complete!
Copy iso file /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
Installing Virtualbox Guest Additions 5.0.16 - guest version is 4.1.6
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.0.16 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 4.1.6 of VirtualBox Guest Additions...
Copying additional installer modules ...
Installing additional modules ...
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[FAILED]
(Look at /var/log/vboxadd-install.log to find out what went wrong. The module is not built but the others are.)
Doing non-kernel setup of the Guest Additions[  OK  ]
Starting the VirtualBox Guest Additions [  OK  ]
Installing the Window System drivers
Could not find the X.Org or XFree86 Window System, skipping.
An error occurred during installation of VirtualBox Guest Additions 5.0.16. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.
```

**Building the shared folder support module[ OK ]** と出ていればOK。
(Building the OpenGL support moduleのエラーは無視してOK)

最後に、仮想マシンを再起動する。

```dummy:コマンド
vagrant reload
```

仮想マシンの起動時に、共有フォルダのマウント失敗が解消された。

```dummy:実行結果
--中略--
[default] GuestAdditions 5.0.16 running --- OK.
```

参考: Vagrantで共有フォルダのマウントに失敗するときの対処方法
http://qiita.com/ozawan/items/9751dcfd9bd4c470cd82