1. TOP
  2. web
  3. ローカル開発環境
  4. Vagrant起動エラーを解決

Vagrant起動エラーを解決

|

Laravelをきちんとモノにするため、vagrantで立ち上げたローカル環境であれこれやっているが、 vagrant up すると、下のようなエラーが出る。

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=500,gid=500 vagrant /vagrantThe error output from the command was:

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

しかし、もう一度 vagrant up すると、何事もなかったかのように起動に成功し、vagrant ssh もできる。

原因は「ホスト(VirtualBox)のバージョンとホスト(VirtualBox)にインストールされているゲスト(Guest Additions)のバージョン不一致によるマウントエラー」。

解決方法は、vagrant-vbguest というプラグインをインストールすることにより、起動時 $ vagrant up に自動的にホスト(VirtualBox)のバージョンに合わせて、ゲスト(Guest Additions)のバージョンを更新してくれるらしい。

$ vagrant plugin install vagrant-vbguest

vagrant をいったん停止 $ vagrant halt して、vagrant-vbguest をインストールした後、$ vangrant up するとエラーが出ることなく起動に成功。しかし、明らかに起動に時間がかかるようになった。測ってみると1分45〜2分ぐらい。その前は測っていないので分からないが、「待つ」という感覚はなかった。せいぜい20秒ぐらいだった気がする。

起動中に

[default] GuestAdditions seems to be installed (6.0.6) correctly, but not running.

Restarting VM to apply changes…

などをするようになったためと思われる。

vagrant up時に毎回確認するのを避けるためには、自動更新するかどうかをVagrantfile内に記述できるらしい。

Vagrantfile
# vagrant-vbguestプラグインが入っている場合、自動更新を無効にする設定
if Vagrant.has_plugin?("vagrant-vbguest") then
config.vbguest.auto_update = false
end
vagrant up時に毎回確認すると遅くなって嫌だという人は、自動更新するかどうかをVagrantfile内に記述できるので必要に応じて設定可能です。

Vagrantで共有フォルダのマウントに失敗するときの対処方法

上記コードを Vagrantfile の最後に追記して vagrant up した。しかし構文エラーが出て起動できなくなった。

Vagrant failed to initialize at a very early stage:

There is a syntax error in the following Vagrantfile. The syntax error
message is reproduced below for convenience:

/Users/hideyo_pr/MyVagrant/MyCentOS/Vagrantfile:73: syntax error, unexpected end-of-input, expecting keyword_end

Vagrantfile を元に戻したら、無事に起動するので、明らかに、追記した部分が問題を起こしている。

問題はrubyの「end抜け」にあった!

unexpected とは「予期せぬ」「期待せぬ」という意味の形容詞。end-of-input は「input の終わり(終端)」ということだが,この input は今の場合,スクリプトファイルのこと。
意訳すれば「いきなりスクリプトの終わりに来ちゃったじゃねえか!」と怒っているんである。

Ruby のエラーメッセージを読み解く(初心者向け)その 1

ファイルの最後に end があるのになぜ??と思ったが、Vagrantfile全体を見てわかった。最後についている end は 4行目の do に対応しているのだ。doに対応するend の手前に、if に対応する end を書かなくてはならない。正しくはこうなる。これで無事起動。起動時間も45秒前後に短縮された。

Vagrant.configure("2") do |config|
  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "bento/centos-6.8"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10"

  # vagrant-vbguestプラグインが入っている場合、自動更新を無効にする設定
  if Vagrant.has_plugin?("vagrant-vbguest") then
    config.vbguest.auto_update = false
  end
end
まつもとさんが if でも while でもブロックでも,何でもかんでも end にしたのが悪いんだ。endif や endwhile にしてくれていれば良かったのにぃ。

Ruby のエラーメッセージを読み解く(初心者向け)その 1

ということで、ruby(Vagrantfileはrubyで書かれている)の”end抜け”について勉強になった。