LoginSignup
20
19

More than 5 years have passed since last update.

vagrant で synced_folder の権限変更

Last updated at Posted at 2016-03-11

使ってる version は、vagrant-1.6.3

vagrant の VM 内で、ディレクトリに chmod しても反映されない現象。
今回は、/home/vagrant/shared_vagrant の権限を変えたい。

[vagrant@localhost vagrant]$ ls -la /home/vagrant | grep shared
drwxr-xr-x  1 vagrant vagrant   102 Mar 11 22:21 shared_vagrant
[vagrant@localhost vagrant]$ chmod 777 shared_vagrant
[vagrant@localhost vagrant]$ ls -la /home/vagrant | grep shared
drwxr-xr-x  1 vagrant vagrant   102 Mar 11 22:21 shared_vagrant # 755 のまま…

どうやら、synced_folder として設定していたのが原因のようだった。

config.vm.synced_folder "./shared_host", "/home/vagrant/shared_vagrant"

synced_folder の option に、mount_options というのがあるので、権限を変更したければ、これを指定してあげるのがよさそう。
https://www.vagrantup.com/docs/synced-folders/basic_usage.html

Vagrantfile に下記のように記述を追加。
dmode がディレクトリの権限で、fmode がファイルの権限のご様子。
権限はよしなに指定する。今回は雑に 777 で。

config.vm.synced_folder "./shared_host", "/home/vagrant/shared_vagrant", :mount_options => ['dmode=777', 'fmode=777']

編集が終わったら、一度ホスト側で reload する。

% vagrant reload

ssh して対象ディレクトリの権限を確認すると、指定した権限が反映されている。

[vagrant@localhost vagrant]$ ls -la /home/vagrant | grep shared
drwxrwxrwx  1 vagrant vagrant   102 Mar 11 22:21 shared_vagrant

補足

mount_options に何を指定すればよいのかわからなかったので、調査した時のメモ。
まずはソースコード見てみる。

% grep -nri mount_options /Applications/Vagrant/

見つかったこのファイルを確認したところ、mount -t vboxsf に、そのまま渡しているように見える。
/Applications/Vagrant//embedded/gems/gems/vagrant-1.6.3/plugins/guests/linux/cap/mount_virtualbox_shared_folder.rb

vboxfs は virtualbox ぽいなってことぐらいしかわからなかったので、mount.vboxsf ってコマンドの option を調べた。

[vagrant@localhost vagrant]$ sudo mount.vboxsf
Usage: mount.vboxsf [OPTIONS] NAME MOUNTPOINT
Mount the VirtualBox shared folder NAME from the host system to MOUNTPOINT.

  -w                    mount the shared folder writable (the default)
  -r                    mount the shared folder read-only
  -n                    do not create an mtab entry
  -o OPTION[,OPTION...] use the mount options specified

Available mount options are:
     rw                 mount writable (the default)
     ro                 mount read only
     uid=UID            set the default file owner user id to UID
     gid=GID            set the default file owner group id to GID
     ttl=TTL            set the "time to live" to TID for the dentry
     dmode=MODE         override the mode of all directories to (octal) MODE
     fmode=MODE         override the mode of all regular files to (octal) MODE
     umask=UMASK        set the umask to (octal) UMASK
     dmask=UMASK        set the umask applied to directories only
     fmask=UMASK        set the umask applied to regular files only
     iocharset CHARSET  use the character set CHARSET for I/O operations
                        (default set is utf8)
     convertcp CHARSET  convert the folder name from CHARSET to utf8

Less common used options:
     noexec,exec,nodev,dev,nosuid,suid

これで、dmode/fmode てところまでたどり着いた、つもり。

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