経緯
これまでファイル転送ツールCyberduckを使用して開発を行っていたが、起動が煩わしく感じるようになったため、マウントさせてhost側とguest(Vagrant)側でファイルを共有することにした
やりたいこと
host側のファイルとguest側のファイルを共有することで、hostとguest双方で編集をできるようにする。
今回はhost側の~/sample
ディレクトリとguest側の/share
ディレクトリを共有させることにする(~/samle
配下と/share
配下を共有)
環境
Windows10 / macOS Mojave 10.14.6
VirtualBox 6.0.10
Vagrant 2.2.5
guestへ共有したいディレクトリをhostに生成する
$ mkdir ~/sample
Vagrantfile生成
# 初期化してVagrantfile生成
$ vagrant init
Vagrantfile
の中身を編集する
Vagrantfile
(編集前)
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
(編集後)
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
config.vm.synced_folder "~/sample", "/share"
設定完了後はリロードしてSSH接続
$ vagrant reload
$ vagrant up
$ vagrant ssh
以上でhost側の~/sample
やguest上の/share
での編集が共有されるようになる
マウントエラー
vagrant reload
や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=1000,gid=1000 share /share
The error output from the command was:
mount: unknown filesystem type 'vboxsf'
hostとguest間のフォルダ共有はGuest Additionsが提供しており、このバージョンが不一致の場合上記のエラーが発生する。詳しくはこちら。
対処
$ vagrant plugin install vagrant-vbguest
$ vagrant vbguest
$ vagrant vbguest --status
GuestAdditions 5.0.16 running --- OK.
$ vagrant reload
以上でguest(vagrant)`/share'ディレクトリが生成され、ファイル共有が完了