Vagrantエラーあれこれに関する対処法


### はじめに

---

　Vagrantでの開発環境作りで対処したエラーあれこれについて書きます。[この前書いた記事](http://qiita.com/alancodvo/items/e2a0cdfa87f872faf643)も書きなおさなくちゃかな。。。
　基本的に、ホストマシン時の操作がzshで、仮想マシン時の操作がbashです。

　やった手順通りに書いたつもりだけど、色々試した後に書いたから、もしかしたら間違えてるかも。。。

### 最初のエラー

---

#### vagrant up時

```zsh
$ vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'chef/centos-6.5' is up to date...
==> default: There was a problem while downloading the metadata for your box
==> default: to check for updates. This is not an error, since it is usually due
==> default: to temporary network problems. This is just a warning. The problem
==> default: encountered was:
==> default: 
==> default: Failed to connect to atlas.hashicorp.com port 443: Network is unreachable
==> default: 
==> default: If you want to check for box updates, verify your network connection
==> default: is valid and try again.
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 80 => 8080 (adapter 1)
    default: 22 => 2222 (adapter 1)
==> 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: 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: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /Users/Alan/Documents/MyVagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

```

　vagrant up時のネットワークエラー。あとで対処法書くけど、エラーメッセージちゃんと読めば多分すぐ対応できた。

#### setup時のエラー

```bash
[vagrant@localhost ~]$ sudo /etc/init.d/vboxadd setup
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
The gcc utility was not found. If the following module compilation fails then
this could be the reason and you should try installing it.

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.32-431.el6.x86_64

Building the main Guest Additions module                   [失敗]
(Look at /var/log/vboxadd-install.log to find out what went wrong)
Doing non-kernel setup of the Guest Additions              [  OK  ]
[vagrant@localhost ~]$ exit
logout
Connection to 127.0.0.1 closed.
```

　Guest Additionsのエラー。これもエラーメッセージにカーネルインストールしろって書いてある。

### Guest Additionsのエラー

---

　まずとりかかったのが、このエラーです。


　エラーメッセージの通り、素直にカーネルをインストールします。

```bash
[vagrant@localhost ~]$ yum install http://vault.centos.org/6.5/updates/x86_64/Packages/kernel-devel-2.6.32-431.3.1.el6.x86_64.rpm
Loaded plugins: fastestmirror
You need to be root to perform this command.
```

　ルート権限がないって言われているから、sudoをつけて再インストール。

```bash
[vagrant@localhost ~]$ sudo yum install http://vault.centos.org/6.5/updates/x86_64/Packages/kernel-devel-2.6.32-431.3.1.el6.x86_64.rpm
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os error was
14: PYCURL ERROR 7 - "Failed to connect to 2a02:2498:1:3d:5054:ff:fed3:e91a: ネットワークに届きません"
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=extras error was
14: PYCURL ERROR 7 - "Failed to connect to 2a02:2498:1:3d:5054:ff:fed3:e91a: ネットワークに届きません"
以下続く。。。
```

　ネットワークに届かない？？。。。またプロキシかってことで、いつもの魔法のコマンド(研究室に伝わるexport proxy...)ってやつをやったけど、同じエラーが発生。
　ってことで、vagrantのプロキシ設定に続く。

#### vagrantのプロキシ設定編

　どうやら、vagrant-proxyconfってのをインストールするといいみたいなので、ホストマシンに戻ってインストール。

```zsh
vagrant plugin install vagrant-proxyconf
vagrant plugin list # これで、インストールされているプラグインをチェック。
# vagrant-proxyconfが書いてあったらおっけー。
```


　そしたら、VagrantFileにプロキシを記述。

```vi:VagrantFile
Vagrant.configure("2") do |config| # この下の3行を追加。
  config.proxy.http     = "http://~~~~:ポート番号"
  config.proxy.https    = "http://~~~~:ポート番号"
  config.proxy.no_proxy = "localhost,127.0.0.1"
```

　この後、vagrant upすると、proxy for ...というメッセージが出てくるはずなのに出てこない！！
　vagrant reloadしなきゃって気づいてしたら、

```bash
==> default: Configuring proxy environment variables...
==> default: Configuring proxy for npm...
==> default: Configuring proxy for Yum...
```

　プロキシは無事通ったみたい！

#### カーネル入ってないよ！編

　プロキシも通ったので、カーネルとgccのインストール。

```bash
[vagrant@localhost ~]$ sudo yum install http://vault.centos.org/6.5/updates/x86_64/Packages/kernel-devel-2.6.32-431.3.1.el6.x86_64.rpm
[vagrant@localhost ~]$ sudo yum install gcc
```

　どっちもインストール成功！しかし、まだエラーが。。。
　カーネルインストールしたわ！って思いながらググると、/etc/yum.confに以下のことを追加すればいいみたいってのを発見したから追加。

```vi:/etc/yum.conf
# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d
exclude=kernel* # ←これを追加。
```

　確認してみると、

```bash
[vagrant@localhost ~]$ sudo yum list kernel-devel       
```

　入ってる！
　これで、setupも成功するかと思いきや。。。まだエラー。
　そうか、vagrant reloadしなきゃだ！と思って、したら。。。


### vagrant up時のエラー

---

　ここからは、set upを一旦置いといて、vagrant up時のエラーにとりかかります。

　vagrant reloadしてみると、

```zsh
$ vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'chef/centos-6.5' is up to date...
==> default: There was a problem while downloading the metadata for your box
==> default: to check for updates. This is not an error, since it is usually due
==> default: to temporary network problems. This is just a warning. The problem
==> default: encountered was:
==> default: 
==> default: The requested URL returned error: 404 Not Found
==> default: 
==> default: If you want to check for box updates, verify your network connection
==> default: is valid and try again.

```

　最初から出てて、後回しにしてたネットワークエラーみたいのが気になる。。。

#### centosのバージョン対応してない編

　ググると、chef/centos-6.5は、virtualbox5.0だと対応してないみたい。確かに、updateしてくれって書いてある。
　ってことで、VagrantFileを書き換えて、bento/centos-6.7に変更。
　vagrant reloadすると、

```zsh
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 80 => 8080 (adapter 1)
    default: 22 => 2222 (adapter 1)
==> 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: 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: Configuring and enabling network interfaces...
==> default: Configuring proxy environment variables...
==> default: Configuring proxy for npm...
==> default: Configuring proxy for Yum...
==> default: Mounting shared folders...
    default: /vagrant => /Users/Alan/Documents/MyVagrant
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

```

　今度は、ネットワークエラーは解消できた。

　でもまだエラーある。。。

#### 共有フォルダのmountに失敗してるよ！編

　さっきのログの最後のところ。ググると、共有フォルダがどうとかってエラーみたいなんだけど、今VagrantFileで共有フォルダの設定してない。。。

```vi:VagrantFile
# config.vm.synced_folder "host_path", "guest_path"
```

　この行で共有フォルダの設定すると思うんだけど、コメントアウトしてるのに。。。
　なぜ。。。

#### vagrant-vbguestでどうだ！編

　本書を読み返してみたら、困ったときはこれ入れてみてって書いてあったので、インストールしてみました。

```zsh
$ vagrant plugin install vagrant-vbguest
```

　これで、vagrant reloadしてみたら。。。まだエラーは治らない。。
　で、色々ググったのを試す。

```bash
[vagrant@localhost ~]$ sudo yum -y update kernel
[vagrant@localhost ~]$ yes | sudo yum -y install kernel-devel kernel-headers dkms gcc gcc-c++
```

　これで、一旦ログアウトしてから起動してみたら。。。

```zsh
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 80 => 8080 (adapter 1)
    default: 22 => 2222 (adapter 1)
==> 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: Machine booted and ready!
==> default: Configuring proxy environment variables...
==> default: Configuring proxy for npm...
==> default: Configuring proxy for Yum...
GuestAdditions versions on your host (5.0.4) and guest (4.3.8) do not match.
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * extras: centos.usonyx.net
 * updates: centos.usonyx.net
Setting up Install Process
Package kernel-devel-2.6.32-573.3.1.el6.x86_64 already installed and latest version
Package gcc-4.4.7-16.el6.x86_64 already installed and latest version
Package 1:make-3.81-20.el6.x86_64 already installed and latest version
Package 4:perl-5.10.1-141.el6.x86_64 already installed and latest version
Nothing to do
Copy iso file /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
Installing Virtualbox Guest Additions 5.0.4 - guest version is 4.3.8
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.0.4 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 5.0.4 of VirtualBox Guest Additions...
Stopping VirtualBox Additions [  OK  ]
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
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.4. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /Users/Alan/Documents/MyVagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
```

　なんかめちゃめちゃログ出てきたけど、大丈夫なのか？と不安に思いながらもsetupしてみた。

```bash
[vagrant@localhost ~]$ 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. The module is not built but the others are.)
Doing non-kernel setup of the Guest Additions              [  OK  ]
You should restart your guest to make sure the new modules are actually used
```

　きたー！！！！！！！！！！！！
　OpenGLでエラー出てるけど、当分使う予定ないし元々何もインポートしてないんだから、多分出てて大丈夫なエラーだと思う！

### 最後に

---

　というわけで、すべてのエラーが解消されました！！長かった。。。
　エラーメッセージに書いてあることも多かったから英語好き嫌いせずちゃんと立ち向かおう。あと、公式のリファレンスちゃんと読むのがいいかも！

　/etc/yum.confのところにもプロキシ記述するとこあったけど、もうしてあるし、ログでyumのプロキシオッケーって出てるから大丈夫だよね。




### 参考

---

- [Vagrant 立ち上げると mount エラー が表示されちゃったときの解決方法（new - Qiita](http://qiita.com/murachi1208/items/1dea1416ec5729d03632)
- [VagrantのかんたんProxy設定 - Qiita](http://qiita.com/shouta-dev/items/7b8a462504f7d6c91e99)
- ["yum install kernel-devel"を実行したら"No package kernel-devel available."が返ってきた件 - わすれっぽいきみえ](http://kimikimi714.hatenablog.com/entry/2014/03/30/%22yum_install_kernel-devel%22%E3%82%92%E5%AE%9F%E8%A1%8C%E3%81%97%E3%81%9F%E3%82%89%22No_package_kernel-devel_available.%22%E3%81%8C%E8%BF%94%E3%81%A3%E3%81%A6%E3%81%8D%E3%81%9F%E4%BB%B6)
- [vagrant up しましたらエラーが出てしまいました。ど素人なので対処が出来ませ... - Yahoo!知恵袋](http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q13149648838)知恵袋から答えもらえると思ってなかった笑
- [vagrant up実行時のエラー](http://harmoniadev.blogspot.jp/2015/08/vagrant-up.html)
- [vagrant で遭遇したエラーまとめ - あいつの日誌β](http://okamuuu.hatenablog.com/entry/2015/07/16/123120)
- [VirtualBoxの共有フォルダのマウントに失敗する - Qiita](http://qiita.com/amagitakayosi/items/07ff50afac1cd6f62c1f)
- [Vagrant で VirtualBoxの Guest Additionsをインストールし直す際のメモ - tanacasinoのメモ](http://tanacasino.hatenablog.com/entry/2014/08/15/111014)