From 4bc8cc7563d0c08cea91a4bf895c8c8fcbe64506 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Wed, 19 Jul 2017 17:02:01 -0700 Subject: [PATCH] Filter any machine_ip when preparing the NFS settings When preparing the NFS settings on VirtualBox the guest IP addresses are pulled from VirtualBox directly and any static addresses are pulled as well. This can lead to aquiring a host IP and machine IP but results in a failure of NFS mount because the IPs are not on the same network. This filters the machine IP result to validate it is within the host adapter IP range. --- .../virtualbox/action/prepare_nfs_settings.rb | 18 +++++++++++ .../action/prepare_nfs_settings_test.rb | 32 +++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/plugins/providers/virtualbox/action/prepare_nfs_settings.rb b/plugins/providers/virtualbox/action/prepare_nfs_settings.rb index c8f6ee436..5ddbfb442 100644 --- a/plugins/providers/virtualbox/action/prepare_nfs_settings.rb +++ b/plugins/providers/virtualbox/action/prepare_nfs_settings.rb @@ -1,3 +1,4 @@ +require "ipaddr" require "vagrant/action/builtin/mixin_synced_folders" module VagrantPlugins @@ -59,6 +60,23 @@ module VagrantPlugins end end + if host_ip && !machine_ip.empty? + interface = @machine.provider.driver.read_host_only_interfaces.detect do |iface| + iface[:ip] == host_ip + end + host_ipaddr = IPAddr.new("#{host_ip}/#{interface.fetch(:netmask, "0.0.0.0")}") + + case machine_ip + when String + machine_ip = nil if !host_ipaddr.include?(machine_ip) + when Array + machine_ip.delete_if do |m_ip| + !host_ipaddr.include?(m_ip) + end + machine_ip = nil if machine_ip.empty? + end + end + raise Vagrant::Errors::NFSNoHostonlyNetwork if !host_ip || !machine_ip env[:nfs_host_ip] = host_ip 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 d4eaf654c..c878b0227 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 @@ -47,6 +47,10 @@ describe VagrantPlugins::ProviderVirtualBox::Action::PrepareNFSSettings do end context "with an nfs synced folder" do + let(:host_only_interfaces) { + [{name: "vmnet2", ip: "1.2.3.4"}] + } + before do # We can't be on Windows, because NFS gets disabled on Windows Vagrant::Util::Platform.stub(windows?: false) @@ -58,9 +62,7 @@ describe VagrantPlugins::ProviderVirtualBox::Action::PrepareNFSSettings do driver.stub(read_network_interfaces: { 2 => {type: :hostonly, hostonly: "vmnet2"}, }) - driver.stub(read_host_only_interfaces: [ - {name: "vmnet2", ip: "1.2.3.4"}, - ]) + driver.stub(read_host_only_interfaces: host_only_interfaces) allow(driver).to receive(:read_guest_ip).with(1).and_return("2.3.4.5") # override sleep to 0 so test does not take seconds @@ -68,6 +70,30 @@ describe VagrantPlugins::ProviderVirtualBox::Action::PrepareNFSSettings do allow(subject).to receive(:retry_options).and_return(retry_options.merge(sleep: 0)) end + context "with host interface netmask defined" do + context "with machine IP included within host interface range" do + let(:host_only_interfaces) { + [{name: "vmnet2", ip: "2.3.4.1", netmask: "255.255.255.0"}] + } + + it "sets nfs_host_ip and nfs_machine_ip properly" do + subject.call(env) + + expect(env[:nfs_host_ip]).to eq("2.3.4.1") + expect(env[:nfs_machine_ip]).to eq("2.3.4.5") + end + end + context "with machine IP included within host interface range" do + let(:host_only_interfaces) { + [{name: "vmnet2", ip: "1.2.3.4", netmask: "255.255.255.0"}] + } + + it "raises an error when the machine IP is not within host interface range" do + expect{ subject.call(env) }.to raise_error(Vagrant::Errors::NFSNoHostonlyNetwork) + end + end + end + it "sets nfs_host_ip and nfs_machine_ip properly" do subject.call(env)