Can I modify the ownership for a shared folder in

2019-01-13 12:58发布

I use vagrant and chef to develop my own blog in a virtual machine. To have easy access to the wordpress folder I created a shared folder.

Basically the wordpress folder is on my host and gets mounted as shared folder in /var/www/wordpress in the VM. The configuration is similar to:

config.vm.share_folder "foo", "/guest/path", "/host/path"

My problem is that the ownership in my VM is always vagrant:vagrant even if I change it on my host. Ownership changes in the VM get ignored.

I cannot use chown to set the ownership of the upload directory to www-data:www-data.

It is possible to use chmod and change the access restrictions to 777, but this is a really ugly hack.

Here is what I actually want. Is this possible?:

  • Development: Access to the shared folder from my host.
  • Access Restriction: On the VM all files and folders should have proper and secure ownership and access restrictions.

标签: acl chef vagrant
4条回答
beautiful°
2楼-- · 2019-01-13 13:32

@john-syrinek

in 1.2+

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

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

查看更多
SAY GOODBYE
3楼-- · 2019-01-13 13:33

As @StephenKing suggests you can change the options of the whole directory.

The relevant function is not documented but the source tells us:

# File 'lib/vagrant/config/vm.rb', line 53

def share_folder(name, guestpath, hostpath, opts=nil)
  @shared_folders[name] = {
    :guestpath => guestpath.to_s,
    :hostpath => hostpath.to_s,
    :create => false,
    :owner => nil,
    :group => nil,
    :nfs   => false,
    :transient => false,
    :extra => nil
  }.merge(opts || {})
end 

Basically you can set group, owner and acl for the whole folder which is way better than setting everything to world writable on the host. I have not found any method to change the ownership of a nested directory.

Here is a quickfix:

config.vm.share_folder "v-wordpress", "/var/www/wordpress", "/host/path", :owner => "www-data", :group => "www-data"
查看更多
淡お忘
4楼-- · 2019-01-13 13:42

You can allow changing the ownership inside the guest:

config.vm.share_folder "foo", "/guest/path", "/host/path", {:extra => 'dmode=777,fmode=777'}
查看更多
We Are One
5楼-- · 2019-01-13 13:51

Following up on @StephenKing and @aycokoster awesome tips, I had a use-case for mounting another directory read-only.

I added

config.vm.share_folder "foo", "/guest/path", "/host/path", :extra => 'ro'

and

# discard exit status because chown `id -u vagrant`:`id -g vagrant` /host/path is okay

vagrant up || true 
查看更多
登录 后发表回答