LoginSignup
10
11

More than 5 years have passed since last update.

CentOS6系のVagrantで共有ディレクトリがコケる

Last updated at Posted at 2017-04-26

Vagrantの共有ディレクトリの繊細さには困ったものです。

VagrantかVirtualBoxのバージョンアップの度に、闇ノウハウがどこかに献上されます。これもそのひとつです。

再現

こんなVagrantfileを用意して、未だに6系なのかというのは置くとしてですね、

Vagrant.configure(2) do |config|
    config.vm.box = "boxcutter/centos68"
    config.vm.synced_folder "../", "/share/hogehoge"
    config.vm.provision :shell, inline: <<-EOT
        yum -y install nginx
        service nginx start
EOT
end

vagrant upすると、

vagrant plugin install vagrant-vbguest
vagrant up

死にます。

Copy iso file /usr/share/virtualbox/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
Installing Virtualbox Guest Additions 5.1.20 - guest version is 5.1.10
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.20 Guest Additions for Linux...........
VirtualBox Guest Additions installer
Removing installed version 5.1.10 of VirtualBox Guest Additions...
vboxadd-service.sh: Stopping VirtualBox Guest Addition service.
vboxadd.sh: Stopping VirtualBox Additions.
You may need to restart your guest system to finish removing the guest drivers.
Copying additional installer modules ...
Installing additional modules ...
vboxadd.sh: Starting the VirtualBox Guest Additions.
An error occurred during installation of VirtualBox Guest Additions 5.1.20. 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: Mounting shared folders...
    default: /share => /home/sasasin
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 dmode=777,fmode=666,uid=500,gid=500 share_ /share

The error output from the command was:

mount: unknown filesystem type 'vboxsf'

手作業でどうにかする

このときVMは起動しているので、

sasasin@uluru340:~/hoge$ vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

ちょろっと手作業入れてvagrant reloadで、guest additionsのインストールをリトライしてくれます。

sasasin@uluru340:~/hoge$ vagrant ssh
----------------------------------------------------------------
  CentOS 6.8                                  built 2016-12-15
----------------------------------------------------------------
[vagrant@localhost ~]$ sudo yum -y update kernel*
[vagrant@localhost ~]$ sudo yum -y install kernel-headers kernel-devel
[vagrant@localhost ~]$ exit
sasasin@uluru340:~/hoge$ vagrant reload

Vagrantfileでなんとかする

vagrant upでコケるのを見届けて、vagrant sshで手作業というのはダルいので自動化したいですね。

Vagrantfileを下記のように変更します。

class CustomRedhat < VagrantVbguest::Installers::RedHat
  def install(opts=nil, &block)
    communicate.sudo('yum -y update kernel*', opts, &block)
    communicate.sudo('yum -y update kernel-devel kernel-heders', opts, &block)
    super
  end
end
Vagrant.configure(2) do |config|
  config.vbguest.installer = CustomRedhat
  config.vm.box = "boxcutter/centos68"
  config.vm.synced_folder "../", "/share/hogehoge"
  config.vm.provision :shell, inline: <<-EOT
    yum -y install nginx
    service nginx start
EOT
end

vagrantコマンドは下記のとおり。

vagrant plugin install vagrant-vbguest
vagrant destroy -f
vagrant up
(コケるのを見届けて、、)
vagrant reload

VagrantVbguest::Installers::RedHat は、VagrantVbguestが用意している、RedHat系box用のguest additionsインストーラークラスです。これに先行してyumを仕込みたいので、継承したクラスを用意して、使用するよう設定してやります。

vagrant up初回は、動いてるkernelと、インストールしたkernel/kernel-devel/kernel-headerのバージョンがズレているため、guest additionsのビルドには成功するものの、カーネルモジュールとしてアタッチなどに失敗するので、全体として失敗します。

vagrant reloadすると、動いてるのとインストールしたkernelのバージョンが一致するので、guest additionsのビルドが成功し、共有ディレクトリもマウントできるようになります。

Virtualbox 5.1.20 の追加作業

手作業、Vagrantfilleともに、Virtualbox 5.1.20なら、さらに下記の手作業が必要です。

sasasin@uluru340:~/hoge$ vagrant ssh
[vagrant@localhost ~]$ sudo rm /sbin/mount.vboxsf && sudo ln -s /usr/lib/VBoxGuestAdditions/mount.vboxsf /sbin/mount.vboxsf && exit
[vagrant@localhost ~]$ exit
sasasin@uluru340:~/hoge$ vagrant reload

参考 VirtualBox 5.1.20で共有フォルダーが動かなくなる対策 - Qiita

結論

開発環境にはDockerかな

10
11
0

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
10
11