guests/linux: Ensure NFS retries when mounting
Fixes GH-7443
This commit is contained in:
parent
22c24d619c
commit
851eff1288
|
@ -9,39 +9,42 @@ module VagrantPlugins
|
||||||
def self.mount_nfs_folder(machine, ip, folders)
|
def self.mount_nfs_folder(machine, ip, folders)
|
||||||
comm = machine.communicate
|
comm = machine.communicate
|
||||||
|
|
||||||
commands = []
|
|
||||||
|
|
||||||
folders.each do |name, opts|
|
folders.each do |name, opts|
|
||||||
# Expand the guest path so we can handle things like "~/vagrant"
|
# Mount each folder separately so we can retry.
|
||||||
expanded_guest_path = machine.guest.capability(
|
commands = ["set -e"]
|
||||||
:shell_expand_guest_path, opts[:guestpath])
|
|
||||||
|
|
||||||
# Do the actual creating and mounting
|
# Shellescape the paths in case they do not have special characters.
|
||||||
commands << "mkdir -p '#{expanded_guest_path}'"
|
guest_path = Shellwords.escape(opts[:guestpath])
|
||||||
|
host_path = Shellwords.escape(opts[:hostpath])
|
||||||
|
|
||||||
# Mount
|
# Build the list of mount options.
|
||||||
hostpath = opts[:hostpath].dup
|
mount_opts = []
|
||||||
hostpath.gsub!("'", "'\\\\''")
|
mount_opts << "vers=#{opts[:nfs_version]}" if opts[:nfs_version]
|
||||||
|
|
||||||
# Figure out any options
|
|
||||||
mount_opts = ["vers=#{opts[:nfs_version]}"]
|
|
||||||
mount_opts << "udp" if opts[:nfs_udp]
|
mount_opts << "udp" if opts[:nfs_udp]
|
||||||
if opts[:mount_options]
|
if opts[:mount_options]
|
||||||
mount_opts = opts[:mount_options].dup
|
mount_opts = mount_opts + opts[:mount_options].dup
|
||||||
end
|
end
|
||||||
|
mount_opts = mount_opts.join(",")
|
||||||
|
|
||||||
commands << "mount -o #{mount_opts.join(",")} '#{ip}:#{hostpath}' '#{expanded_guest_path}'"
|
# Make the directory on the guest.
|
||||||
|
commands << "mkdir -p #{guest_path}"
|
||||||
|
|
||||||
|
# Perform the mount operation.
|
||||||
|
commands << "mount -o #{mount_opts} #{ip}:#{host_path} #{guest_path}"
|
||||||
|
|
||||||
# Emit a mount event
|
# Emit a mount event
|
||||||
commands << <<-EOH.gsub(/^ {14}/, '')
|
commands << <<-EOH.gsub(/^ {14}/, '')
|
||||||
if command -v /sbin/init && /sbin/init --version | grep upstart; then
|
if command -v /sbin/init && /sbin/init --version | grep upstart; then
|
||||||
/sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT='#{expanded_guest_path}'
|
/sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT=#{guest_path}
|
||||||
fi
|
fi
|
||||||
EOH
|
EOH
|
||||||
end
|
|
||||||
|
|
||||||
retryable(on: Vagrant::Errors::NFSMountFailed, tries: 8, sleep: 3) do
|
# Run the command, raising a specific error.
|
||||||
comm.sudo(commands.join("\n"), error_class: Vagrant::Errors::NFSMountFailed)
|
retryable(on: Vagrant::Errors::NFSMountFailed, tries: 3, sleep: 5) do
|
||||||
|
machine.communicate.sudo(commands.join("\n"),
|
||||||
|
error_class: Vagrant::Errors::NFSMountFailed,
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -42,8 +42,8 @@ describe "VagrantPlugins::GuestLinux::Cap::MountNFS" do
|
||||||
}
|
}
|
||||||
cap.mount_nfs_folder(machine, ip, folders)
|
cap.mount_nfs_folder(machine, ip, folders)
|
||||||
|
|
||||||
expect(comm.received_commands[0]).to match(/mkdir -p '#{guestpath}'/)
|
expect(comm.received_commands[0]).to match(/mkdir -p #{guestpath}/)
|
||||||
expect(comm.received_commands[0]).to match(/'1.2.3.4:#{hostpath}' '#{guestpath}'/)
|
expect(comm.received_commands[0]).to match(/1.2.3.4:#{hostpath} #{guestpath}/)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "mounts with options" do
|
it "mounts with options" do
|
||||||
|
@ -72,7 +72,20 @@ describe "VagrantPlugins::GuestLinux::Cap::MountNFS" do
|
||||||
cap.mount_nfs_folder(machine, ip, folders)
|
cap.mount_nfs_folder(machine, ip, folders)
|
||||||
|
|
||||||
expect(comm.received_commands[0]).to include(
|
expect(comm.received_commands[0]).to include(
|
||||||
"/sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT='#{guestpath}'")
|
"/sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT=#{guestpath}")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "escapes host and guest paths" do
|
||||||
|
folders = {
|
||||||
|
"/vagrant-nfs" => {
|
||||||
|
guestpath: "/guest with spaces",
|
||||||
|
hostpath: "/host's",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cap.mount_nfs_folder(machine, ip, folders)
|
||||||
|
|
||||||
|
expect(comm.received_commands[0]).to match(/host\\\'s/)
|
||||||
|
expect(comm.received_commands[0]).to match(/guest\\\ with\\\ spaces/)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue