VagrantBoxのCentOS5.10で/vagrantがマウント失敗した時の対処

ちょっと古いシステムを見てくれと言われて、今更ながらCentOS5.x系の環境が必要になりました。
みんな大好きVagrantで[Vagrantbox.es](http://www.vagrantbox.es)からboxイメージ落としてきてサクッと用意しようと思ったらハマったのでメモ。

環境など
===

* MacOSX 10.11
* VirtualBox 5.0.2
* Vagrant 1.7.4
* Boxファイルは以下のファイル  
http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-5.10_chef-provisionerless.box

問題
===

```bash
$ vagrant add centos5_10 http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-5.10_chef-provisionerless.box
$ vagrant init
$ vagrant up
```

普通にBox追加して立ち上げようとしたらこんなログが出た。

```bash
Copy iso file /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
Installing Virtualbox Guest Additions 5.0.2 - guest version is 4.3.8
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.0.2 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 4.3.8 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
The headers for the current running kernel were not found. If the following
module compilation fails then this could be the reason.
The missing package can be probably installed with
yum install kernel-devel-2.6.18-371.el5

Building the main Guest Additions module[FAILED]
(Look at /var/log/vboxadd-install.log to find out what went wrong)
Doing non-kernel setup of the 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.2. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.

Restarting VM to apply changes...
==> default: Attempting graceful shutdown of VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.8
    default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
    default: /vagrant => /Users/hogehoge/vm/uchimo-admin-centos5
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
```

`vboxsf`が使えなくて/vagrantがマウントできなかったということらしい。

```bash
$ 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
```

これ叩け、って書いてあるけど解決せず。
よくよくログを読んでみると上の方に GuestAdditions のアップデートに失敗したから /var/log/vboxadd-install.log 読め、とか書いてあるので見てみると

```
/tmp/vbox.0/Makefile.include.header:106: *** Error: KERN_DIR does not point to a directory.  Stop.
Creating user for the Guest Additions.
Creating udev rule for the Guest Additions kernel module.
```

こんな感じで怒られていた。
Google先生にどういうことか聞いてみると`yum install kernel-devel`してカーネルのソースへのパスを`KERN_DIR`に設定しないとダメらしい。

```bash
[vagrant@localhost ~]$ sudo yum install kernel-devel -y
[vagrant@localhost ~]$ ls /usr/src/kernels/
2.6.18-406.el5-x86_64
[vagrant@localhost ~]$ su -
[root@localhost ~]# echo "KERN_DIR=/usr/src/kernels/2.6.18-406.el5-x86_64" >> .bash_profile
```

KERN_DIRは`uname -r`と同じ値にする必要があるらしいので確認。

```bash
[root@localhost ~]# uname -r
2.6.18-371.el5
```

え……使ってるカーネルの方が古かったので、（一応`yum info kernel`でバージョン確認してから）カーネルをアップデート。

```bash
[root@localhost ~]# yum update kernel -y
[root@localhost ~]# shutdown -r now
```

アップデートが終わったら GuestAdditions をアップデートして、再起動したら正常に /vagrant がマウントされた。

```bash
$ vagrant vbguest
$ vagrant halt
$ vagrant up
```

めでたしめでたし。