From ed708645b2129c3ce83e90a81a8828db9b9558ba Mon Sep 17 00:00:00 2001 From: David O'Rourke Date: Thu, 5 May 2016 14:41:03 +0100 Subject: [PATCH 1/3] prepare_nfs_settings: Try harder to get all machine IPs. Vagrant was not behaving correctly in configurations where there was a static IP on a VirtualBox `intnet` interface and a DHCP `:hostonly` interface configured. Since `machine_ip` attempted to get static addresses `||` dynamic addresses, it would simply use the static machine address and continue. This commit corrects this behaviour by collecting all static and dynamic addresses into the `machine_ip` array instead of just one or the other. The result of this is a correctly generated `/etc/exports` on the host machine, allowing NFS mounts to work correctly in this type of environment. --- .../virtualbox/action/prepare_nfs_settings.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/plugins/providers/virtualbox/action/prepare_nfs_settings.rb b/plugins/providers/virtualbox/action/prepare_nfs_settings.rb index d3edf3e93..0abf5ab4e 100644 --- a/plugins/providers/virtualbox/action/prepare_nfs_settings.rb +++ b/plugins/providers/virtualbox/action/prepare_nfs_settings.rb @@ -36,8 +36,19 @@ 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 || read_dynamic_machine_ip(adapter) + adapter, host_ip = find_host_only_adapter + machine_ip = read_static_machine_ips + dynamic_machine_ip = read_dynamic_machine_ip(adapter) + + if machine_ip.nil? + # If there were no static IPs, attempt to use the dynamic IP + # instead. + machine_ip = dynamic_machine_ip + else + # If we did get some static machine IPs, add the dynamic IPs to + # that list too. + machine_ip.push(dynamic_machine_ip) unless dynamic_machine_ip.nil? + end raise Vagrant::Errors::NFSNoHostonlyNetwork if !host_ip || !machine_ip From 985f1d4dda3b2964c5503600be43528d65eefa3b Mon Sep 17 00:00:00 2001 From: David O'Rourke Date: Tue, 10 May 2016 11:16:21 +0100 Subject: [PATCH 2/3] prepare_nfs_settings: Fix add_ips_to_env!, spec test passes again. --- .../virtualbox/action/prepare_nfs_settings.rb | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/plugins/providers/virtualbox/action/prepare_nfs_settings.rb b/plugins/providers/virtualbox/action/prepare_nfs_settings.rb index 0abf5ab4e..2ee5e9c54 100644 --- a/plugins/providers/virtualbox/action/prepare_nfs_settings.rb +++ b/plugins/providers/virtualbox/action/prepare_nfs_settings.rb @@ -36,18 +36,27 @@ 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 - dynamic_machine_ip = read_dynamic_machine_ip(adapter) + adapter, host_ip = find_host_only_adapter + machine_ip = read_static_machine_ips - if machine_ip.nil? - # If there were no static IPs, attempt to use the dynamic IP - # instead. - machine_ip = dynamic_machine_ip + if !machine_ip + # No static IP, attempt to use the dynamic IP. + machine_ip = read_dynamic_machine_ip(adapter) else - # If we did get some static machine IPs, add the dynamic IPs to - # that list too. - machine_ip.push(dynamic_machine_ip) unless dynamic_machine_ip.nil? + # We have static IPs, also attempt to read any dynamic IPs. + # If there is no dynamic IP on the adapter, it doesn't matter. We + # already have a static IP. + begin + dynamic_ip = read_dynamic_machine_ip(adapter) + rescue Vagrant::Errors::NFSNoGuestIP + dynamic_ip = nil + end + + # If we found a dynamic IP and we didn't include it in the + # machine_ip array yet, do so. + if dynamic_ip && !machine_ip.include?(dynamic_ip) + machine_ip.push(dynamic_ip) + end end raise Vagrant::Errors::NFSNoHostonlyNetwork if !host_ip || !machine_ip From c5f975838d636f4fb7f89f2f464bf8912b80033e Mon Sep 17 00:00:00 2001 From: David O'Rourke Date: Tue, 10 May 2016 11:40:16 +0100 Subject: [PATCH 3/3] prepare_nfs_settings: Add spec test for static/dynamic IP combo --- .../virtualbox/action/prepare_nfs_settings_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) 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 3cead74f4..c358f6720 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 @@ -116,5 +116,14 @@ describe VagrantPlugins::ProviderVirtualBox::Action::PrepareNFSSettings do expect(env[:nfs_host_ip]).to eq("1.2.3.4") expect(env[:nfs_machine_ip]).to eq(["11.12.13.14"]) end + + it "allows statically configured guest IPs to co-exist with dynamic host only IPs for NFS" do + env[:machine].config.vm.network :private_network, ip: "11.12.13.14" + + subject.call(env) + + expect(env[:nfs_host_ip]).to eq("1.2.3.4") + expect(env[:nfs_machine_ip]).to eq(["11.12.13.14", "2.3.4.5"]) + end end end