Skip to content

Commit a92fbc8

Browse files
Merge pull request #13490 from allisonlarson/nfsd-update-fix
Use `nfsd update` command for restart
2 parents 9df95a2 + 380eccc commit a92fbc8

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

plugins/hosts/bsd/cap/nfs.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class NFS
1414
def self.nfs_export(environment, ui, id, ips, folders)
1515
nfs_exports_template = environment.host.capability(:nfs_exports_template)
1616
nfs_restart_command = environment.host.capability(:nfs_restart_command)
17+
nfs_status_command = environment.host.capability(:nfs_status_command)
18+
nfs_update_command = environment.host.capability(:nfs_update_command)
1719
logger = Log4r::Logger.new("vagrant::hosts::bsd")
1820

1921
nfs_checkexports! if File.file?("/etc/exports")
@@ -117,9 +119,12 @@ def self.nfs_export(environment, ui, id, ips, folders)
117119
"#{sudo_command}/usr/bin/tee -a /etc/exports >/dev/null")
118120
end
119121

120-
# We run restart here instead of "update" just in case nfsd
121-
# is not starting
122-
system(*nfs_restart_command)
122+
# Check if nfsd is running, and update or restart depending on the result
123+
if nfs_running?(nfs_status_command)
124+
system(*nfs_update_command)
125+
else
126+
system(*nfs_restart_command)
127+
end
123128
end
124129

125130
def self.nfs_exports_template(environment)
@@ -159,10 +164,22 @@ def self.nfs_prune(environment, ui, valid_ids)
159164
raise Vagrant::Errors::NFSCantReadExports
160165
end
161166

167+
def self.nfs_running?(check_command)
168+
Vagrant::Util::Subprocess.execute(*check_command).exit_code == 0
169+
end
170+
162171
def self.nfs_restart_command(environment)
163172
["sudo", "nfsd", "restart"]
164173
end
165174

175+
def self.nfs_update_command(environment)
176+
["sudo", "nfsd", "update"]
177+
end
178+
179+
def self.nfs_status_command(environment)
180+
["sudo", "nfsd", "status"]
181+
end
182+
166183
protected
167184

168185
def self.nfs_cleanup(id)

plugins/hosts/bsd/plugin.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ class Plugin < Vagrant.plugin("2")
3939
Cap::NFS
4040
end
4141

42+
host_capability("bsd", "nfs_update_command") do
43+
require_relative "cap/nfs"
44+
Cap::NFS
45+
end
46+
47+
host_capability("bsd", "nfs_status_command") do
48+
require_relative "cap/nfs"
49+
Cap::NFS
50+
end
51+
4252
host_capability("bsd", "resolve_host_path") do
4353
require_relative "cap/path"
4454
Cap::Path

plugins/synced_folders/nfs/synced_folder.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ def enable(machine, folders, nfsopts)
103103
mount_folders = {}
104104
folders.each do |id, opts|
105105
mount_folders[id] = opts.dup if opts[:guestpath]
106-
end
107106

108-
machine.ui.detail(I18n.t("vagrant.actions.vm.nfs.mounting_entry",
109-
guestpath: opts[:guestpath],
110-
hostpath: opts[:hostpath]
111-
))
107+
machine.ui.detail(I18n.t("vagrant.actions.vm.nfs.mounting_entry",
108+
guestpath: opts[:guestpath],
109+
hostpath: opts[:hostpath]
110+
))
111+
end
112112

113113
# Mount them!
114114
if machine.guest.capability?(:nfs_pre)

test/unit/plugins/hosts/bsd/cap/nfs_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
allow(described_class).to receive(:sleep)
2323
allow(described_class).to receive(:nfs_cleanup)
2424
allow(described_class).to receive(:system)
25+
allow(described_class).to receive(:nfs_running?).and_return(true)
2526
allow(File).to receive(:writable?).with("/etc/exports")
2627

2728
allow(Vagrant::Util::Subprocess).to receive(:execute).with("nfsd", "checkexports").
@@ -49,5 +50,27 @@
4950
described_class.nfs_export(environment, ui, id, ips, folders)
5051
end
5152
end
53+
54+
context "when nfsd is not running" do
55+
before {
56+
allow(described_class).to receive(:nfs_running?).and_return(false)
57+
}
58+
it "should restart nfsd" do
59+
expect(host).to receive(:capability).with(:nfs_restart_command).and_return(["restart"])
60+
expect(described_class).to receive(:system).with("restart")
61+
described_class.nfs_export(environment, ui, id, ips, folders)
62+
end
63+
end
64+
65+
context "when nfsd is running" do
66+
before {
67+
allow(described_class).to receive(:nfs_running?).and_return(true)
68+
}
69+
it "should update nfsd" do
70+
expect(host).to receive(:capability).with(:nfs_update_command).and_return(["update"])
71+
expect(described_class).to receive(:system).with("update")
72+
described_class.nfs_export(environment, ui, id, ips, folders)
73+
end
74+
end
5275
end
5376
end

0 commit comments

Comments
 (0)