LoginSignup
8
9

More than 5 years have passed since last update.

Vagrant環境作る時にカーネルアップデートが動いて共有フォルダのマウントに失敗する時の対応

Posted at

Railsのステージング環境をローカルのVirtualBox+Vagrantで構築しようと思って作業をしていたらちょっとハマったので、エラーの原因と対応についてまとめてみました。

ちなみに環境については以下の通りです

はじめに実現したいこと

  • Vagrant一発でRailsを実行するのに必要なものがインストールされた環境を仮想マシン上に作る
  • OS設定、ミドルウェア類のインストール&設定はAnsibleを利用する

Vagrantfileの作成からvagrant upでコケるまでの流れ

Vagrantの環境構築しようと思った時の流れをステップ・バイ・ステップで書いておきます。

Vagrantfileを作成する

HashiCorpのbento/centos-7.1を利用することにしたので、以下の様なVagrantfileを作成しました。

Vagrant.configure(2) do |config|
  config.vm.box = 'bento/centos-7.1'
  config.vm.network :private_network, ip: '192.168.33.37'
  config.vm.provision 'ansible_local' do |ansible|
    ansible.playbook = vagrant.yml'
    ansible.groups = {
      'vagrant' => ['default']
    }
    ansible.verbose  = true
    ansible.install  = true
  end
end

起動させる

お馴染みの以下のコマンドでVagrantを起動させます

vagrant up

ターミナル上で色々メッセージが出るかと思うのですが最終的にこんな感じのメッセージが表示されました。

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

Vagrantfileを作成してるフォルダに仮想マシン上からもアクセスできるようにするための共有フォルダのマウントの処理の所でエラーが出ているようです。

なぜ共有フォルダのマウントに失敗するか?

上記のエラーメッセージの

This is usually because the "vboxsf" file system is not available

というメッセージに加えて、ちょっと話が戻ってしまいますが、vagrant upした時のターミナル上のメッセージで

==> default: Machine booted and ready!
〜中略〜
---> Package kernel-headers.x86_64 0:3.10.0-327.10.1.el7 will be installed
〜中略〜
  Installing : kernel-headers-3.10.0-327.10.1.el7.x86_64                  34/44

という状況からすると以下のようなことが生じてるのかなと考えました

  • bento/centos-7.1のboxを利用して環境構築する時に、このboxの設定だと思うのですがyum update が実行されてしまう。
  • yum updateでカーネルが更新されたことでVirtualBoxのGuest Additionsが動作しなくなる
  • Guest Additionsが動作してないので、共有フォルダのマウントが出来ない

自分の考えが合ってるかどうか確認する

CentOSのバージョンが異なりますが、症状的には同じだったので以下の情報を参考に作業しようと思ったのですが記事では/etc/init.d/vboxadd が存在してる前提になってました

もう一度ゲストOSにログインして、Guest Additionsを再ビルドします。

Vagrantの上のCentOS 6.5でyum updateした後、vagrant upで発生したエラーの原因と対処より

所が、自分の環境では、以下のとおり、仮想マシン上の/etc/init.d/配下にはvboxaddが存在してません。。。

vagrant ssh
ls -al /etc/init.d/
合計 36
drwxr-xr-x.  2 root root    66  3月  9  2015 .
drwxr-xr-x. 10 root root  4096  3月  9  2015 ..
-rw-r--r--.  1 root root  1160  3月  6  2015 README
-rw-r--r--.  1 root root 13430  1月 15  2015 functions
-rwxr-xr-x.  1 root root  2989  1月 15  2015 netconsole
-rwxr-xr-x.  1 root root  6470  1月 15  2015 network

インストールログを手がかりにして対処方法を考える

カーネルが更新されたタイミングで問題が生じてるような感じだったので、kernelのインストールを再度行うことにしました。

sudo yum -y install kernel
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
依存性の解決をしています
〜中略〜
依存性を更新しました:
  dracut.x86_64 0:033-360.el7_2               dracut-config-rescue.x86_64 0:033-360.el7_2
  dracut-network.x86_64 0:033-360.el7_2       kmod.x86_64 0:20-5.el7
  libgudev1.x86_64 0:219-19.el7_2.4           linux-firmware.noarch 0:20150904-43.git6ebf5d5.el7
  systemd.x86_64 0:219-19.el7_2.4             systemd-libs.x86_64 0:219-19.el7_2.4
  systemd-sysv.x86_64 0:219-19.el7_2.4

完了しました!

上記メッセージからすると、yum update実行されたタイミングで、そもそもカーネルのインストールがうまくいってなかったのかもしれません。

この状態でVirtualBox Guest Additionsがインストールされるかどうか確認するためひとまず一旦仮想マシンから抜けて仮想マシンを一旦停止&起動しました。

仮想マシンから一旦抜ける

exit
ログアウト
Connection to 127.0.0.1 closed.

ホストマシン上で、vagrantの停止&起動

vagrant halt
vagrant up

vagrant upした後に以下のようなメッセージが順番に表示されて、無事に共有フォルダのマウントに成功しました!

Resolving Dependencies
--> Running transaction check
---> Package kernel-devel.x86_64 0:3.10.0-327.10.1.el7 will be installed
--> Finished Dependency Resolution
〜中略〜
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.10. 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: [vagrant-hostsupdater] Checking for host entries
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /Users/myuser/path/to/project

まとめ

  • HashiCorpのbento/centos-7.1を利用して環境を構築してvagrant upすると、自動的にyum updateが実行されるけど、どうもカーネルの更新(?)がうまくいかなくなるみたいで、それが要因でVirtualBox Guest Additionsのインストールが出来なくなるみたいです
  • 上記のような状態になったら仮想マシンにsshして、カーネルのインストールを手動で行うことで、問題回避できます
8
9
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9