diff --git a/CHANGELOG.md b/CHANGELOG.md index 231bf4a36..29c44db98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,9 @@ FEATURES: This decreases Vagrant's initial startup time considerably. - Allow "file://" URLs for box URLs. [GH-1087] - Emit "vagrant-mount" upstart event when NFS shares are mounted. [GH-1118] + - Add a VirtualBox provider config `auto_nat_dns_proxy` which when set to + false will not attempt to automatically manage NAT DNS proxy settings + with VirtualBox. [GH-1313] IMPROVEMENTS / BUG FIXES: diff --git a/plugins/providers/virtualbox/action/sane_defaults.rb b/plugins/providers/virtualbox/action/sane_defaults.rb index 64a17a409..f964941c1 100644 --- a/plugins/providers/virtualbox/action/sane_defaults.rb +++ b/plugins/providers/virtualbox/action/sane_defaults.rb @@ -25,34 +25,21 @@ module VagrantPlugins ] attempt_and_log(command, "Enabling the Host I/O cache on the SATA controller...") - enable_dns_proxy = true - begin - contents = File.read("/etc/resolv.conf") + if env[:machine].provider_config.auto_nat_dns_proxy + @logger.info("Automatically figuring out whether to enable/disable NAT DNS proxy...") - if contents =~ /^nameserver 127\.0\.(0|1)\.1$/ - # The use of both natdnsproxy and natdnshostresolver break on - # Ubuntu 12.04 and 12.10 that uses resolvconf with localhost. When used - # VirtualBox will give the client dns server 10.0.2.3, while - # not binding to that address itself. Therefore disable this - # feature if host uses the resolvconf server 127.0.0.1 or - # 127.0.1.1 - @logger.info("Disabling DNS proxy since resolv.conf contains 127.0.0.1 or 127.0.1.1") - enable_dns_proxy = false + # Enable/disable the NAT DNS proxy as necessary + if enable_dns_proxy? + command = ["modifyvm", env[:machine].id, "--natdnsproxy1", "on"] + attempt_and_log(command, "Enable the NAT DNS proxy on adapter 1...") + else + command = [ "modifyvm", env[:machine].id, "--natdnsproxy1", "off" ] + attempt_and_log(command, "Disable the NAT DNS proxy on adapter 1...") + command = [ "modifyvm", env[:machine].id, "--natdnshostresolver1", "off" ] + attempt_and_log(command, "Disable the NAT DNS resolver on adapter 1...") end - rescue Errno::ENOENT; end - - # Enable/disable the NAT DNS proxy as necessary - if enable_dns_proxy - command = [ - "modifyvm", env[:machine].id, - "--natdnsproxy1", "on" - ] - attempt_and_log(command, "Enable the NAT DNS proxy on adapter 1...") else - command = [ "modifyvm", env[:machine].id, "--natdnsproxy1", "off" ] - attempt_and_log(command, "Disable the NAT DNS proxy on adapter 1...") - command = [ "modifyvm", env[:machine].id, "--natdnshostresolver1", "off" ] - attempt_and_log(command, "Disable the NAT DNS resolver on adapter 1...") + @logger.info("NOT trying to automatically manage NAT DNS proxy.") end @app.call(env) @@ -70,6 +57,30 @@ module VagrantPlugins result = @env[:machine].provider.driver.execute_command(command) @logger.info("#{log} (exit status = #{result.exit_code})") end + + # This uses some heuristics to determine if the NAT DNS proxy should + # be enabled or disabled. See the comments within the function body + # itself to see the checks it does. + # + # @return [Boolean] + def enable_dns_proxy? + begin + contents = File.read("/etc/resolv.conf") + + if contents =~ /^nameserver 127\.0\.(0|1)\.1$/ + # The use of both natdnsproxy and natdnshostresolver break on + # Ubuntu 12.04 and 12.10 that uses resolvconf with localhost. When used + # VirtualBox will give the client dns server 10.0.2.3, while + # not binding to that address itself. Therefore disable this + # feature if host uses the resolvconf server 127.0.0.1 or + # 127.0.1.1 + @logger.info("Disabling DNS proxy since resolv.conf contains 127.0.0.1 or 127.0.1.1") + return false + end + rescue Errno::ENOENT; end + + return true + end end end end diff --git a/plugins/providers/virtualbox/config.rb b/plugins/providers/virtualbox/config.rb index 0897d4ab3..4df7e834b 100644 --- a/plugins/providers/virtualbox/config.rb +++ b/plugins/providers/virtualbox/config.rb @@ -1,6 +1,13 @@ module VagrantPlugins module ProviderVirtualBox class Config < Vagrant.plugin("2", :config) + # Vagrant by default will make "smart" decisions to enable/disable + # the NAT DNS proxy. If this is set to `true`, then the DNS proxy + # will not be enabled, and it is up to the end user to do it. + # + # @return [Boolean] + attr_accessor :auto_nat_dns_proxy + # An array of customizations to make on the VM prior to booting it. # # @return [Array] @@ -17,6 +24,7 @@ module VagrantPlugins attr_reader :network_adapters def initialize + @auto_nat_dns_proxy = UNSET_VALUE @customizations = [] @network_adapters = {} @gui = UNSET_VALUE @@ -53,6 +61,9 @@ module VagrantPlugins # This is the hook that is called to finalize the object before it # is put into use. def finalize! + # Default is to auto the DNS proxy + @auto_nat_dns_proxy = true if @auto_nat_dns_proxy == UNSET_VALUE + # Default is to not show a GUI @gui = false if @gui == UNSET_VALUE end