はじめに

Mac/windows/linux側(ホスト)とVagrant側(ゲスト)間でファイルのやり取りを行う場合に、VagrantではSYNCED機能を用意しています。そのため、ftpなどのソフトを介してデータのやり取りを行う必要がなくなります。
またパフォーマンス面もVartualBoxの共有フォルダ機能や、FTPソフトよりもベンチマークで早い結果がでています。

SYNCED機能を使うことで、開発はホストPCで行い、実行はVagrant側のゲストで実行することが簡単にできるようになります。

公式ドキュメント※Synced Folder
http://docs.vagrantup.com/v2/synced-folders/

Vagrantfileの設定(共有/同期)

共有は設定を行わない場合もデフォルトでホストのVagrantfileが置いてあるディレクトリとVagrantの/vagrantが同期状態にあります。
Vagrantfileのsynced_folder設定を変更することで任意に指定することが可能になります。

synced_folderフォーマット

フォーマット:

config.vm.synced_folder “ホストPATH”, “ゲストPATH(Vagrant側)”

例:

config.vm.synced_folder “src/”, “/srv/website”

http://docs.vagrantup.com/v2/synced-folders/basic_usage.html
この場合、ホストのsrcディレクトリとゲストのsrv/websiteディレクトリで同期されます。

synced_folderのオプション

公式ドキュメントにオプションの詳細が記載されています。

create (boolean) – If true, the host path will be created if it does not exist. Defaults to false.

disabled (boolean) – If true, this synced folder will be disabled and won’t be setup. This can be used to disable a previously defined synced folder or to conditionally disable a definition based on some external factor.

group (string) – The group that will own the synced folder. By default this will be the SSH user. Some synced folder types don’t support modifying the group.

mount_options (array) – A list of additional mount options to pass to the mount command.

owner (string) – The user who should be the owner of this synced folder. By default this will be the SSH user. Some synced folder types don’t support modifying the owner.

type (string) – The type of synced folder. If this is not specified, Vagrant will automatically choose the best synced folder option for your environment. Otherwise, you can specify a specific type such as “nfs”.

http://docs.vagrantup.com/v2/synced-folders/basic_usage.html

createオプション

ホストパスが存在しない場合にホストのパスを自動作成する場合に設定します。デフォルトはfalse設定です。

指定方法:

config.vm.synced_folder “src/”, “/srv/website”, create: true

disabledオプション

同期を無効化にする場合に設定します。デフォルトはfalse設定です。

指定方法:

config.vm.synced_folder “src/”, “/srv/website”, disabled: true

groupオプション

ディレクトリを所有するグループを設定します。デフォルトはSSHユーザ(vagrant)です。

指定方法:

config.vm.synced_folder “src/”, “/srv/website”, group: “root”

普通ownerと合わせて使用します

config.vm.synced_folder “src/”, “/srv/website”, group: “root”, owner: “root”

mount_optionsオプション

mountのオプションを設定します。配列でオプションを渡します。デフォルトは空です。

指定方法:

mount_options: ["dmode=777", "fmode=777"]

ディレクトリ・ファイルのパーミッションを777設定にしてます

ownerオプション

ディレクトリを所有する所有者を設定します。デフォルトはSSHユーザ(vagrant)です。

指定方法:

config.vm.synced_folder “src/”, “/srv/website”, owner: “root”

typeオプション

同期化されたディレクトリの種類を設定します。vagrantは自動的に環境にあったタイプを設定してくれます。
typeは4種類あり、virtualbox,rsync,nfs,smbです。
virtualboxはvirtualbox付属の共有機能を使い、smbはwindows専用、smbはmac/linux専用となります。
そのためmacの場合はnfsかrsyncを使うことになります。virturalboxは無視しておきます。

virtualbox:
http://docs.vagrantup.com/v2/synced-folders/virtualbox.html

rsync:
http://docs.vagrantup.com/v2/synced-folders/rsync.html

nfs:
http://docs.vagrantup.com/v2/synced-folders/nfs.html

smb:
http://docs.vagrantup.com/v2/synced-folders/smb.html

指定方法:

config.vm.synced_folder “src/”, “/srv/website”, type: “nfs”

typeオプションのrsync設定

typeをrsyncにした場合、linuxコマンドのrsyncを使って同期を行います。
https://download.samba.org/pub/rsync/rsync.html
同期はホスト側でvagrant up / vagrant reload / vagrant rsync / vagrant provision のいずれかのコマンドで行います。

config.vm.synced_folder “src/”, “/srv/website”, type: “rsync”

rsyncコマンドでパラメータとして渡しているデフォルト値は[“–verbose”, “–archive”, “–delete”, “-z”, “–copy-links”]になります。

rsyncのその他設定情報についてはドキュメントを参照ください
http://docs.vagrantup.com/v2/synced-folders/rsync.html

その他おすすめの備忘録

Tagged with:
 

One Response to [Vagrant]ホスト/ゲスト間のファイルの共有/同期機能(SYNCED FOLDERS)について

コメントを残す