LoginSignup
19
23

More than 5 years have passed since last update.

Vagrant共有ディレクトリをnfsモードで起動する

Last updated at Posted at 2014-04-20

Goal

* MaxOSX(Host)/VirtualBox(VM)/CentOS(Guest) Vagrant環境でRails開発している
* rails serverコマンドでwebrick起動するのが異様に遅く、1分以上かかる
* 起動の高速化の方法として、Host-Guestのディレクトリ共有をnfsモードにすると速いという情報を発見
* (Host)MacOSX側をnfs server,(Guest)CentOSをnfs clientと見立てて、nfsマウントする

Problem

* synced_folderをnfsモードにするだけだと以下のエラーが出てマウントに失敗
[default] Mounting NFS shared folders...


The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

mount -o 'vers=3,udp' 192.168.33.1:'/Users/metheglin/Documents/workspace-vagrant/rvprov-orion' /vagrant

Stdout from the command:



Stderr from the command:

mount.nfs: access denied by server while mounting 192.168.33.1:/Users/metheglin/Documents/workspace-vagrant/rvprov-orion

Fix

nfsdの起動を確認

(Host)MacOSX
sudo nfsd status
  # runningになってることを確認
  # nfsd service is enabled
  # nfsd is running (pid 79090, 8 threads)

  # 起動させる場合
  # sudo nfsd enable

/etc/exportsをtouchする

(Host)MacOSX
sudo touch /etc/exports

synced_folder設定を追加する

Vagrantfile
config.vm.synced_folder ".", "/vagrant", type: 'nfs'
  # Vagrantfile配置ディレクトリをGuestの/vagrantでnfsマウントする
  # 一応、Vagrantfile配置ディレクトリがotherユーザのr,x権限がついていることを確認したほうがよい
  # 自分の環境では/User/metheglin/Documentsがrwx------になってた

/etc/hostsにlocalhost IPアドレスを追加する

(Host)MacOSX,/etc/hosts
192.168.33.1    localhost
  # エラーメッセージを見て、192.168.33.1でnfs serverに接続しようとしているので、このIPを追加

:exclamation:

/etc/hostsの編集は、うまくいったりいかなかったりする。
記事一番下のPSの手順をやるのが一番確実かも。

vagrant再起動

vagrant reload

Result

rais serverでの起動がめちゃくちゃ速くなった
特にvagrant upしてから2回目以降の起動

Reference

PS

その後、再起動など繰り返すとnfsマウントに失敗することが何度か発生

試してみること

  • Host側で/etc/exportsの定義を変更してみる
/etc/exports
## vagrant upすると、VAGRANT-BEGIN, VAGRANT-ENDのコメントが自動的に書き込まれるが、そのコメントの外に記載
"/Users/metheglin/Documents/workspace-vagrant/rvprov-orion" 192.168.33.11 -alldirs -mapall=metheglin:metheglin

# =====================
# PATH HOSTNAME OPTIONS
# =====================
# PATH: 共有したいnfsサーバ(Host)のディレクトリのパス
#       ダブルクォーテーションでくくったほうが無難
# HOSTNAME: マウントを許可するホスト名またはIPアドレス(Vagrantfileで指定したGuest起動時のprivate IPアドレス)
#           ホスト名を使いたい場合は、/etc/hostsに記載しておく
# OPTIONS:
#  -alldirs: サブディレクトリをマウントポイントとすることができる
#  -mapall: nfsクライアント(Guest)からのアクセスがnfsサーバ(Host)ではここで指定したuser:groupになる
  • Host側でcheckexports / sudo nfsd checkexports
  • Host側でnfs再起動 / sudo nfsd restart

■参考
* http://www.freebsd.org/cgi/man.cgi?query=exports&sektion=5
* http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/network-nfs.html

19
23
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
19
23