2018-09-12

tobijibu

Vagrantの共通ディレクトリマウント失敗→プラグインインストール不可をなんとかする

突然vagrant sshでログインできなくなってしまい、 いろいろ試したのですが解決できず、結局環境を再構築することになりました。 その際にVirtualBoxとVagrantを再インストールしたところ、 共有ディレクトリをマウントすることができない現象が発生しました。 その現象を解決しようとvagrant-vbguestのインストールを試みましたが、プラグインがインストール出来ない状態でした。 何とか解決して環境を再構築することができたので、その方法を書いておきます。


環境 Windows7
VirtualBox 5.2.18
Vagrant 2.1.4

現象

Vagrantを再インストールしてvagrant upを実行したところ、 共有ディレクトリをマウントすることができないというエラーが発生しました。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'centos/7' is up to date...
...
==> default: Mounting shared folders...
    default: /home/vagrant/dotfiles => C:/Users/tobijibu/dotfiles
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 home_vagrant_dotfiles /home/vagrant/dotfiles

The error output from the command was:

mount: unknown filesystem type 'vboxsf'

エラーの内容から察するに"VirtualBox Guest Additions"をインストールすれば良いと考えられます。そういえば前回もインストールしました。 そこで以下を実行。すると・・・

$ vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Vagrant failed to load a configured plugin source. This can be caused
by a variety of issues including: transient connectivity issues, proxy
filtering rejecting access to a configured plugin source, or a configured
plugin source not responding correctly. Please review the error message
below to help resolve the issue:

  Errno::ECONNRESET: An existing connection was forcibly closed by the remote host. - SSL_connect (https://api.rubygems.org/specs.4.8.gz)

Source: https://rubygems.org/

接続が切られてしまいvagrant-vbguestをインストールすることができません。

解決策

同じような現象で解決策を紹介している記事がありました。
Errno::ECONNRESET: Connection reset by peer - SSL_connect (https://api.rubygems.org/specs.4.8.gz)
vagrant plugin installではなく、gemファイルを落として直接インストールすれば良いようです。

gemをダウンロード

vagrant-vbguestのバージョンはどれが良いのか不明でしたが、 とりあえず、(2018/09/12現在)最新のv0.16.0をインストールすることにしました。 結果的にこのバージョンで問題無いようです。

$ wget http://rubygems.org/downloads/vagrant-vbguest-0.16.0.gem

もう一つ、依存関係としてmicromachine ~> 2.0.0という記載があるので、micromachineのgemも落とします。

$ wget http://rubygems.org/downloads/micromachine-2.0.0.gem

ちなみに、~> 2.0.02.0.xであれば問題ありませんが、 2.1.x3.x.x等をインストールしてしまうとvagrant-vbguestをインストールすることができません。 今回~>の意味を初めて知りました。

~>は指定されている末尾のバージョンより上位のバージョンが変わるとインストールできないことを意味しています。 つまり、2.0.x(2.0.02.0.92.0.[0-9]+)であれば問題無いのですが、2.1.0は不可になります。 以下の記事に詳しく書かれています。
セマンティック・バージョニングと、Gemfileのバージョン指定方法 - Gemfileでよく見る`~<`を使いこなす
Railsドキュメント - gemfile

インストール

落としてきたgemをそれぞれインストールします。

$ vagrant plugin install --plugin-clean-sources micromachine-2.0.0.gem
$ vagrant plugin install --plugin-clean-sources vagrant-vbguest-0.16.0.gem
$ vagrant plugin list
micromachine (2.0.0, global)
  - Version Constraint: 2.0.0
vagrant-vbguest (0.16.0, global)
  - Version Constraint: 0.16.0

https://rubygems.org/への接続を回避するために--plugin-clean-sourcesを指定します。 そして、gemファイルへのパスを指定します。 今回はwgetでgemを取得してきた場所にいるのでファイル名のみを指定しています。

最後にvagrant plugin listでプラグインを確認しておきましょう。


vagrantを起動

Vagrantを起動します。共通フォルダがマウントされ、起動できれば問題ありません。 これで完了です。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
...
==> default: Mounting shared folders...
    default: /home/vagrant/dotfiles => C:/Users/tobijibu/dotfiles
    default: /home/vagrant/projects => C:/Users/tobijibu/projects
...


今回の問題が発生したあと、解決策が全くわからず何度もVagrantを再インストールしました。 Vagrantはインストールとアンインストールは結構な時間が掛かります。 また、それぞれが終わるたびに再起動が必要なので、待ち時間が多くて大変でした。

結果的にはプラグインをどうにかして入れるという結論に行き着き、 最新のバージョンを使うことが出来たので結果オーライとしましょう。