Vagrant VirtualBoxでの共有フォルダのエラーについて

経緯。 Windows10 VirtualBox 5.1.20 に Vagrant 1.9.4 で CentOS7をインストール

Vagrant box centos/7 | Atlas by HashiCorp

https://seven.centos.org/2017/04/updated-centos-vagrant-images-available-v1703-01/

を見ると、既知の問題点として

1.VirtualBoxでゲスト・ホスト間の共有フォルダを提供する「VirtualBox Guest Additions」はプレインストールされていません。必要な場合は、vagrant-vbguestプラグインをインストールし、Vagrantfileに次の行を追加してください。

config.vm.synced_folder “.”, “/vagrant”, type: “virtualbox”

補足)config.vm.synced_folder “ホストの共有フォルダ”, “ゲストのマウント先”, type: “virtualbox”です。

可能であれば、VirtualBox共有フォルダの代わりにNFSを使用することをお勧めします(VirtualBox共有フォルダは高負荷なので)。また、vagrant-sshfsプラグインを使用することもできます。これはNFSと異なり、すべてのオペレーティングシステムで動作します。

2.Guest Additionsが入っていない場合、私たちのイメージは同期されたフォルダのためにrsyncを使うようにあらかじめ設定されています。Windowsユーザーは、同期フォルダにSMBを使用するか、Vagrantfile に下記の通り記載することで同期ディレクトリを無効にすることができます。 config.vm.synced_folder “.”, “/vagrant”, disabled: true

と記載されています。というわけで

>vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Fetching: micromachine-2.0.0.gem (100%)
Fetching: vagrant-vbguest-0.13.0.gem (100%)
Installed the plugin 'vagrant-vbguest (0.13.0)'!

>vagrant init centos/7

の後で、vagrantfile を編集。中ほどの当該箇所を

config.vm.synced_folder “.”, “/vagrant”, type: “virtualbox”

とする。そして

>vagrant up --provider virtualbox

しかし

:
vboxadd.sh: Starting the VirtualBox Guest Additions.
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/hoge/MyVagrant/CentOS7
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 vagrant /vagrant

The error output from the command was:

mount: wrong fs type, bad option, bad superblock on vagrant,
       missing codepage or helper program, or other error

       In some cases useful info is found in syslog - try
       dmesg | tail or so.

となりマウントできません。

VagrantVirtualBox共有フォルダーをマウントできません。これは通常"vboxsf(VirtualBOXSharedFolder)ファイルシステム"への非対応が原因です。このファイルシステムは"Guest Additions"とカーネルモジュール間で利用されます。"Gust Additions"が正しくインストールされているか確認してください。 これはVagrantのバグではなく、大抵は欠陥のある"Vagrant Box"によって引き起こされます。背景としては、次のコマンドが試みられました--

ホスト(VirtualBox)とゲスト(Guest Additions)のバージョン不一致で発生することがあり、その場合>vagrant vbguest –status

GuestAdditions versions on your host (5.1.20) and guest (5.0.18) do not match.

といったエラーが出て、マウントも

sudo mount -t vboxsf vagrant /vagrant
/sbin/mount.vboxsf: mounting failed with the error: No such device

とできません。ただ今回は

>vagrant vbguest --status
[default] GuestAdditions 5.1.20 running --- OK.

で私のVirtualBoxも5.1.20なので、ここは問題ありませんでした。 しかし、確かに指摘されたコマンドで以下のようなアウトプットになります。

[vagrant@localhost ~]$ sudo mount -t vboxsf -o uid=1000,gid=1000 vagrant /vagrant
mount: wrong fs type, bad option, bad superblock on vagrant,
       missing codepage or helper program, or other error

       In some cases useful info is found in syslog - try
       dmesg | tail or so.

$mount -t vboxsf <共有フォルダ名> <マウント先ディレクトリ>

ディレクトリやファイルの権限、パーミッションを設定するときは下記のように -o で追加オプションを指定する。 uid、gid についてはユーザー名は指定できない。

$mount -t vboxsf <共有フォルダ名> <マウント先ディレクトリ> -o uid=$UID,gid=$GID,fmode=644,dmode=755

Virtualbox の 共有フォルダ を マウント/アンマウント したい - Qiita

では dmesg | tail

[vagrant@localhost ~]$ sudo dmesg | tail
:
[  701.855307] sf_read_super_aux err=-22

sf_read_super_aux err=-22ググる

VirtualBoxの共有フォルダのマウントに失敗する - Qiita

見てみるとビンゴっぽかったのでsymlink作りなおす

$ ll /sbin/mount.vboxsf

lrwxrwxrwx 1 root root 42 8月 10 18:25 /sbin/mount.vboxsf -> /usr/lib64/VBoxGuestAdditions/mount.vboxsf

$ sudo rm /sbin/mount.vboxsf

$ sudo ln -s /usr/lib/VBoxGuestAdditions/mount.vboxsf /sbin/mount.vboxsf

$ sudo mount -t vboxsf HOGE hoge

成功

やったー

とのこと。やってみる。

$ ls -l /sbin/mount.vboxsf (mount.vboxsfとはなんぞや?)
/sbin/mount.vboxsf -> /opt/VBoxGuestAdditions-5.1.20/other/mount.vboxsf
(mount.vboxsf は /opt/VBoxGuestAdditions-5.1.20/other/mount.vboxsf へのシンボリックリンクである
という表示だがリンク先に実体が無い)

$ sudo rm /sbin/mount.vboxsf (シンボリックリンクを一度削除。または以下3行を飛ばして ln -sfでも良い)
$ ls -l /sbin/mount.vboxsf
ls: cannot access /sbin/mount.vboxsf: No such file or directory

$ sudo ln -s /usr/lib/VBoxGuestAdditions/mount.vboxsf /sbin/mount.vboxsf
(/usr/lib/VBoxGuestAdditions/mount.vboxsfへのシンボリックリンク/sbin/mount.vboxsfを作成する)

$ ls -l /sbin/mount.vboxsf
/sbin/mount.vboxsf -> /usr/lib/VBoxGuestAdditions/mount.vboxsf
(mount.vboxsf は /usr/lib/VBoxGuestAdditions/mount.vboxsf へのシンボリックリンクである)

$ sudo mount -t vboxsf vagrant /vagrant
[vagrant@localhost ~]$ ls /vagrant/
test.txt  Vagrantfile

やったー。

/usr/lib/VBoxGuestAdditions/ は 64bit なら /usr/lib64/VBoxGuestAdditions/ が正しいがバグとして残ってる?みたい。 #5665 (Wrong simlink in Fedora 64 bits guest => Fixed in SVN) – Oracle VM VirtualBox

vboxsf: VirtualBOXSharedFolder: VirtualBox外部にあるフォルダをVirtualBox内から読み書きできるようにするためのファイルシステム

mount -t vboxsf [マウント元デバイス:ホストの共有フォルダー名] [マウント先:ゲストの共有フォルダー]

これはVagrantfileでconfig.vm.synced_folder “.”, “/vagrant”, type: “virtualbox”と設定した部分です。カレントフォルダー".“の[フォルダー名]もvagrantです(この設定はどこに記載されているのだろう…。VirturalBoxのGUIにはあったけど)

まあ初めに「可能であれば、VirtualBox共有フォルダの代わりにNFSを使用することをお勧めします」と書かれてるので、その方がいいんでしょうね。

こちらも参考に beyondjapan.com

www.vagrantup.com

シンボリックリンク

同期されたフォルダの実装とホスト/ゲストの組み合わせのシンボリックリンクのサポートは一貫していません。Vagrantは、シンボリックリンクVirtualBoxなどのさまざまなハイパーバイザを構成するように機能するよう最善を尽くしていますが、ホストとゲストの組み合わせによってはまだ正しく動作しません。これは、シンボリックリンクに依存するいくつかの開発環境に影響を与える可能性があります。

必要であれば、フォルダを同期するすべてのホスト/ゲストの組み合わせでシンボリックリンクをテストすることをお勧めします。

ちなみに「問題点2」に記載されているように「VirtualBox Guest Additions」をインストールせずにそのまま vagrant up すると、同期に rsync が使われます。

:
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Rsyncing folder: /cygdrive/c/Users/hoge/MyVagrant/CentOS7/ => /vagrant

www.vagrantup.com

/* -----codeの行番号----- */