LoginSignup
167
178

More than 5 years have passed since last update.

Vagrant | synced_folder でホストOSとゲストOSの任意のフォルダを同期する

Posted at

Vagrant | synced_folder でホストOSとゲストOSの任意のフォルダを同期する

概要

Vagrantでは synced_folder を利用することによって、

ホスト・ゲスト間でファイルの同期が可能です。

例えば、Vagrantで仮想環境を作成したが、

開発に利用する IDEやエディタは普段使い慣れたホストOSのものを利用したい

などといった場合に便利です。

構文

下記の構文でホストとゲストの同期が可能です。

options は0-n個の任意の数を指定可能です。

Vagrant.configure("2") do |config|
  # other config here

  config.vm.synced_folder "host_path", "guest_path", options...
end

有効化されるタイミング

vagrant up もしくは vagrant reload 時に同期が有効化されます

owner / groupの変更

自動作成されるフォルダの owner / group を app ユーザーに変更する場合は以下。

Vagrantfile

config.vm.synced_folder "host_path", "guest_path", owner: "app", group: "app"

オプション

option type 内容
create boolean host_pathが存在しなかった場合に作成するかどうか。デフォルトはfalse
disabled boolean 同期を無効化するかどうか。デフォルトはfalse
group string synced_folderのgroup指定。デフォルトはSSHユーザー
mount_options array A list of additional mount options to pass to the mount command.
owner string synced_folderのowner指定。デフォルトはSSHユーザー
type string synced_folderの種類

サンプル

仕様

  • ゲストOSは Ubuntu 12.04 LTS
  • gitをinstall
  • ホストOSのVagrantfileがあるフォルダの一階層上に、git_clonerディレクトリを作成
  • ゲストOSの /home/vagrant/git_cloner を作成
  • 双方のgit_clonerフォルダを synced_folder で同期する
  • ゲストOSの git_cloner ディレクトリに下記のリポジトリを clone する

    https://github.com/tbpgr/git_cloner.git
    ※git_clonerは自作のRuby gemです

サンプルコード

# -*- mode: ruby -*-
# vi: set ft=ruby :

$script = <<SCRIPT
sudo apt-get update
sudo apt-get -y install git
cd /home/vagrant/
git clone https://github.com/tbpgr/git_cloner.git
SCRIPT

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "precise64"
  config.vm.box_url = 'http://files.vagrantup.com/'+config.vm.box+'.box'
  config.vm.synced_folder "../git_cloner", "/home/vagrant/git_cloner",
    create: true, owner: "vagrant", group: "vagrant"

  config.vm.provision "shell", inline: "echo 'start provision'"
  config.vm.provision "shell", inline: $script
  config.vm.provision "shell", inline: "echo 'finish provision'"
end

VM起動後、同期フォルダの確認

vagrantでゲストOSを起動し、git_cloner ディレクトリが
owner:vagrant, group vagrant で作成されていることを確認

$ %project_root%/sample
$ vagrant up
$ vagrant ssh

# ここからゲストOS
$ ls -l
drwxrwxrwx 1 vagrant vagrant 4096 Jun 20 12:55 git_cloner
-rwxr-xr-x 1 vagrant vagrant 6487 Sep 14  2012 postinstall.sh

git_cloner を clone できていることを確認。

$ cd git_cloner
$ ls
bin  Gemfile  git_cloner.gemspec  lib  LICENSE.txt  Rakefile  README.md  spec

ホストOSに戻り、 git_cloner ディレクトリが作成され、
git_clonerリポジトリの中身も同期できていることを確認する。

$ exit

# ここからホストOS
$ cd ../
$ ls
git_cloner/  sample/
$ cd git_cloner
$ ls
bin/     git_cloner.gemspec  LICENSE.txt  README.md
Gemfile  lib/                Rakefile     spec/

ホストOSの git_cloner に hoge.txt を追加してみます。

$ echo hoge > hoge.txt

再度ゲストOSにログインして、git_cloner ディレクトリに
hoge.txt が追加されたことを確認。

$ cd sample
$ vagrant ssh

# ここからゲストOS
$ cd git_cloner
$ ls
bin      git_cloner.gemspec  lib          Rakefile   spec
Gemfile  hoge.txt            LICENSE.txt  README.md
$ cat hoge.txt
hoge

以上で同期の設定を確認出来ました。

参考資料

Vagrant公式サイト

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

167
178
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
167
178