diff --git a/plugins/hosts/bsd/cap/nfs.rb b/plugins/hosts/bsd/cap/nfs.rb index c6c003db006..60794d85d7e 100644 --- a/plugins/hosts/bsd/cap/nfs.rb +++ b/plugins/hosts/bsd/cap/nfs.rb @@ -14,6 +14,8 @@ class NFS def self.nfs_export(environment, ui, id, ips, folders) nfs_exports_template = environment.host.capability(:nfs_exports_template) nfs_restart_command = environment.host.capability(:nfs_restart_command) + nfs_status_command = environment.host.capability(:nfs_status_command) + nfs_update_command = environment.host.capability(:nfs_update_command) logger = Log4r::Logger.new("vagrant::hosts::bsd") nfs_checkexports! if File.file?("/etc/exports") @@ -117,9 +119,12 @@ def self.nfs_export(environment, ui, id, ips, folders) "#{sudo_command}/usr/bin/tee -a /etc/exports >/dev/null") end - # We run restart here instead of "update" just in case nfsd - # is not starting - system(*nfs_restart_command) + # Check if nfsd is running, and update or restart depending on the result + if nfs_running?(nfs_status_command) + system(*nfs_update_command) + else + system(*nfs_restart_command) + end end def self.nfs_exports_template(environment) @@ -159,10 +164,22 @@ def self.nfs_prune(environment, ui, valid_ids) raise Vagrant::Errors::NFSCantReadExports end + def self.nfs_running?(check_command) + Vagrant::Util::Subprocess.execute(*check_command).exit_code == 0 + end + def self.nfs_restart_command(environment) ["sudo", "nfsd", "restart"] end + def self.nfs_update_command(environment) + ["sudo", "nfsd", "update"] + end + + def self.nfs_status_command(environment) + ["sudo", "nfsd", "status"] + end + protected def self.nfs_cleanup(id) diff --git a/plugins/hosts/bsd/plugin.rb b/plugins/hosts/bsd/plugin.rb index 6501767a786..8bbb1c06cc2 100644 --- a/plugins/hosts/bsd/plugin.rb +++ b/plugins/hosts/bsd/plugin.rb @@ -39,6 +39,16 @@ class Plugin < Vagrant.plugin("2") Cap::NFS end + host_capability("bsd", "nfs_update_command") do + require_relative "cap/nfs" + Cap::NFS + end + + host_capability("bsd", "nfs_status_command") do + require_relative "cap/nfs" + Cap::NFS + end + host_capability("bsd", "resolve_host_path") do require_relative "cap/path" Cap::Path diff --git a/plugins/synced_folders/nfs/synced_folder.rb b/plugins/synced_folders/nfs/synced_folder.rb index 4d4d0a686a0..44efcf37a46 100644 --- a/plugins/synced_folders/nfs/synced_folder.rb +++ b/plugins/synced_folders/nfs/synced_folder.rb @@ -103,12 +103,12 @@ def enable(machine, folders, nfsopts) mount_folders = {} folders.each do |id, opts| mount_folders[id] = opts.dup if opts[:guestpath] - end - machine.ui.detail(I18n.t("vagrant.actions.vm.nfs.mounting_entry", - guestpath: opts[:guestpath], - hostpath: opts[:hostpath] - )) + machine.ui.detail(I18n.t("vagrant.actions.vm.nfs.mounting_entry", + guestpath: opts[:guestpath], + hostpath: opts[:hostpath] + )) + end # Mount them! if machine.guest.capability?(:nfs_pre) diff --git a/test/unit/plugins/hosts/bsd/cap/nfs_test.rb b/test/unit/plugins/hosts/bsd/cap/nfs_test.rb index 877ff218e59..7744e38e540 100644 --- a/test/unit/plugins/hosts/bsd/cap/nfs_test.rb +++ b/test/unit/plugins/hosts/bsd/cap/nfs_test.rb @@ -22,6 +22,7 @@ allow(described_class).to receive(:sleep) allow(described_class).to receive(:nfs_cleanup) allow(described_class).to receive(:system) + allow(described_class).to receive(:nfs_running?).and_return(true) allow(File).to receive(:writable?).with("/etc/exports") allow(Vagrant::Util::Subprocess).to receive(:execute).with("nfsd", "checkexports"). @@ -49,5 +50,27 @@ described_class.nfs_export(environment, ui, id, ips, folders) end end + + context "when nfsd is not running" do + before { + allow(described_class).to receive(:nfs_running?).and_return(false) + } + it "should restart nfsd" do + expect(host).to receive(:capability).with(:nfs_restart_command).and_return(["restart"]) + expect(described_class).to receive(:system).with("restart") + described_class.nfs_export(environment, ui, id, ips, folders) + end + end + + context "when nfsd is running" do + before { + allow(described_class).to receive(:nfs_running?).and_return(true) + } + it "should update nfsd" do + expect(host).to receive(:capability).with(:nfs_update_command).and_return(["update"]) + expect(described_class).to receive(:system).with("update") + described_class.nfs_export(environment, ui, id, ips, folders) + end + end end end