From 7ac5ab8f1484f79a6b5e3efed9e9433b49ed4c88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Ja=CC=88ger?= Date: Sun, 10 Feb 2019 21:26:49 +0100 Subject: [PATCH] implement nfs shared folders for ipv6 --- plugins/guests/linux/cap/nfs.rb | 9 +++++++- .../virtualbox/action/prepare_nfs_settings.rb | 22 ++++++++++++++---- .../guests/linux/cap/mount_nfs_test.rb | 18 +++++++++++++++ .../action/prepare_nfs_settings_test.rb | 23 ++++++++++++++++++- 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/plugins/guests/linux/cap/nfs.rb b/plugins/guests/linux/cap/nfs.rb index 9263688e5..61aa6c975 100644 --- a/plugins/guests/linux/cap/nfs.rb +++ b/plugins/guests/linux/cap/nfs.rb @@ -1,3 +1,4 @@ +require "ipaddr" require_relative "../../../synced_folders/unix_mount_helpers" module VagrantPlugins @@ -30,7 +31,13 @@ module VagrantPlugins machine.communicate.sudo("mkdir -p #{guest_path}") - command = "mount -o #{mount_opts} #{ip}:#{host_path} #{guest_path}" + addr = IPAddr.new(ip) + + if addr.ipv6? + command = "mount -o #{mount_opts} [#{ip}]:#{host_path} #{guest_path}" + else + command = "mount -o #{mount_opts} #{ip}:#{host_path} #{guest_path}" + end # Run the command, raising a specific error. retryable(on: Vagrant::Errors::NFSMountFailed, tries: 3, sleep: 5) do diff --git a/plugins/providers/virtualbox/action/prepare_nfs_settings.rb b/plugins/providers/virtualbox/action/prepare_nfs_settings.rb index 5ddbfb442..e9df7818c 100644 --- a/plugins/providers/virtualbox/action/prepare_nfs_settings.rb +++ b/plugins/providers/virtualbox/action/prepare_nfs_settings.rb @@ -37,8 +37,8 @@ module VagrantPlugins # # The ! indicates that this method modifies its argument. def add_ips_to_env!(env) - adapter, host_ip = find_host_only_adapter - machine_ip = read_static_machine_ips + adapter, host_ip, is_ipv6 = find_host_only_adapter + machine_ip = read_static_machine_ips if !machine_ip # No static IP, attempt to use the dynamic IP. @@ -62,9 +62,17 @@ module VagrantPlugins if host_ip && !machine_ip.empty? interface = @machine.provider.driver.read_host_only_interfaces.detect do |iface| - iface[:ip] == host_ip + if is_ipv6 + iface[:ipv6] == host_ip + else + iface[:ip] == host_ip + end + end + if is_ipv6 + host_ipaddr = IPAddr.new("#{host_ip}/#{interface.fetch(:ipv6_prefix, "64")}") + else + host_ipaddr = IPAddr.new("#{host_ip}/#{interface.fetch(:netmask, "0.0.0.0")}") end - host_ipaddr = IPAddr.new("#{host_ip}/#{interface.fetch(:netmask, "0.0.0.0")}") case machine_ip when String @@ -92,7 +100,11 @@ module VagrantPlugins if opts[:type] == :hostonly @machine.provider.driver.read_host_only_interfaces.each do |interface| if interface[:name] == opts[:hostonly] - return adapter, interface[:ip] + if interface[:ipv6] + return adapter, interface[:ipv6], true + else + return adapter, interface[:ip], false + end end end end diff --git a/test/unit/plugins/guests/linux/cap/mount_nfs_test.rb b/test/unit/plugins/guests/linux/cap/mount_nfs_test.rb index f1f0a7254..eed136f80 100644 --- a/test/unit/plugins/guests/linux/cap/mount_nfs_test.rb +++ b/test/unit/plugins/guests/linux/cap/mount_nfs_test.rb @@ -32,6 +32,24 @@ describe "VagrantPlugins::GuestLinux::Cap::MountNFS" do ) end + context "with ipv6" do + let(:ip) { "fd63:acd4:5c42:0530::1" } + + it "mounts the folder" do + folders = { + "/vagrant-nfs" => { + type: :nfs, + guestpath: "/guest", + hostpath: "/host", + } + } + cap.mount_nfs_folder(machine, ip, folders) + + expect(comm.received_commands[0]).to match(/mkdir -p #{guestpath}/) + expect(comm.received_commands[1]).to match(/\[fd63:acd4:5c42:0530::1\]:#{hostpath} #{guestpath}/) + end + end + it "mounts the folder" do folders = { "/vagrant-nfs" => { diff --git a/test/unit/plugins/providers/virtualbox/action/prepare_nfs_settings_test.rb b/test/unit/plugins/providers/virtualbox/action/prepare_nfs_settings_test.rb index 2a854671b..7f7540e45 100644 --- a/test/unit/plugins/providers/virtualbox/action/prepare_nfs_settings_test.rb +++ b/test/unit/plugins/providers/virtualbox/action/prepare_nfs_settings_test.rb @@ -53,6 +53,10 @@ describe VagrantPlugins::ProviderVirtualBox::Action::PrepareNFSSettings do [{name: "vmnet2", ip: "1.2.3.4"}] } + let(:guest_ip) { + "2.3.4.5" + } + before do # We can't be on Windows, because NFS gets disabled on Windows allow(Vagrant::Util::Platform).to receive(:windows?).and_return(false) @@ -65,13 +69,30 @@ describe VagrantPlugins::ProviderVirtualBox::Action::PrepareNFSSettings do 2 => {type: :hostonly, hostonly: "vmnet2"}, }) allow(driver).to receive(:read_host_only_interfaces).and_return(host_only_interfaces) - allow(driver).to receive(:read_guest_ip).with(1).and_return("2.3.4.5") + allow(driver).to receive(:read_guest_ip).with(1).and_return(guest_ip) # override sleep to 0 so test does not take seconds retry_options = subject.retry_options allow(subject).to receive(:retry_options).and_return(retry_options.merge(sleep: 0)) end + context "with ipv6 addresses" do + let(:host_only_interfaces) { + [{name: "vmnet2", ipv6: "fd63:acd4:5c42:0530::1"}] + } + + let(:guest_ip) { + "fd63:acd4:5c42:0530::2" + } + + it "sets nfs_host_ip and nfs_machine_ip properly" do + subject.call(env) + + expect(env[:nfs_host_ip]).to eq("fd63:acd4:5c42:0530::1") + expect(env[:nfs_machine_ip]).to eq("fd63:acd4:5c42:0530::2") + end + end + context "with host interface netmask defined" do context "with machine IP included within host interface range" do let(:host_only_interfaces) {