From cc01f016846ba4b0f7852725a425ba1698397290 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 23 Oct 2014 22:25:38 -0700 Subject: [PATCH] hosts/linux: don't use sudo to write NFS if writable [GH-2643] --- plugins/hosts/linux/cap/nfs.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/plugins/hosts/linux/cap/nfs.rb b/plugins/hosts/linux/cap/nfs.rb index 6b1c95c43..1216ce04f 100644 --- a/plugins/hosts/linux/cap/nfs.rb +++ b/plugins/hosts/linux/cap/nfs.rb @@ -38,9 +38,13 @@ module VagrantPlugins 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.split("\n").each do |line| line = Vagrant::Util::ShellQuote.escape(line, "'") - system(%Q[echo '#{line}' | sudo tee -a /etc/exports >/dev/null]) + system(%Q[echo '#{line}' | #{sudo_command}tee -a /etc/exports >/dev/null]) end if nfs_running?(nfs_check_command) @@ -92,9 +96,13 @@ module VagrantPlugins user = Regexp.escape(Process.uid.to_s) id = Regexp.escape(id.to_s) + # Only use "sudo" if we can't write to /etc/exports directly + sudo_command = "" + sudo_command = "sudo " if !File.writable?("/etc/exports") + # Use sed to just strip out the block of code which was inserted # by Vagrant - system("sudo sed -r -e '\\\x01^# VAGRANT-BEGIN:( #{user})? #{id}\x01,\\\x01^# VAGRANT-END:( #{user})? #{id}\x01 d' -ibak /etc/exports") + system("#{sudo_command}sed -r -e '\\\x01^# VAGRANT-BEGIN:( #{user})? #{id}\x01,\\\x01^# VAGRANT-END:( #{user})? #{id}\x01 d' -ibak /etc/exports") end def self.nfs_opts_setup(folders)