diff --git a/CHANGELOG.md b/CHANGELOG.md index d08f94a59..ebfd53f36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ IMPROVEMENTS: more easily. Vagrant will login for you if you specify auth. [GH-4042] - providers/docker: `stop_timeout` can be used to modify the `docker stop` timeout. [GH-4504] + - synced\_folders/nfs: Won't use `sudo` to write to /etc/exports if there + are write privileges. [GH-2643] - synced\_folders/smb: Credentials from one SMB will be copied to the rest. [GH-4675] BUG FIXES: diff --git a/plugins/hosts/bsd/cap/nfs.rb b/plugins/hosts/bsd/cap/nfs.rb index df37f559d..6042320ae 100644 --- a/plugins/hosts/bsd/cap/nfs.rb +++ b/plugins/hosts/bsd/cap/nfs.rb @@ -102,10 +102,16 @@ module VagrantPlugins # First, clean up the old entry nfs_cleanup(id) + # Only use "sudo" if we can't write to /etc/exports directly + sudo_command = "" + sudo_command = "sudo " if !File.writable?("/etc/exports") + # Output the rendered template into the exports output.split("\n").each do |line| line = Vagrant::Util::ShellQuote.escape(line, "'") - system("echo '#{line}' | sudo tee -a /etc/exports >/dev/null") + system( + "echo '#{line}' | " + + "#{sudo_command}tee -a /etc/exports >/dev/null") end # We run restart here instead of "update" just in case nfsd @@ -165,12 +171,19 @@ module VagrantPlugins user = Process.uid + command = [] + command << "sudo" if !File.writable?("/etc/exports") + command += [ + "sed", "-E", "-e", + "/^# VAGRANT-BEGIN:( #{user})? #{id}/," + + "/^# VAGRANT-END:( #{user})? #{id}/ d", + "-ibak", + "/etc/exports" + ] + # Use sed to just strip out the block of code which was inserted # by Vagrant, and restart NFS. - system( - "sudo", "sed", "-E", "-e", - "/^# VAGRANT-BEGIN:( #{user})? #{id}/,/^# VAGRANT-END:( #{user})? #{id}/ d", - "-ibak", "/etc/exports") + system(*command) end def self.nfs_checkexports!