From d9b8352a58c3119606baeab82a4ae4e4da967e5f Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 6 Jun 2016 11:36:59 -0400 Subject: [PATCH] guests/debian: Use predictable naming for network configuration --- .../guests/debian/cap/configure_networks.rb | 27 +++++++++---------- templates/guests/debian/network_dhcp.erb | 4 +-- templates/guests/debian/network_static.erb | 4 +-- templates/guests/debian/network_static6.erb | 4 +-- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/plugins/guests/debian/cap/configure_networks.rb b/plugins/guests/debian/cap/configure_networks.rb index e398ba7f2..e7fabcb68 100644 --- a/plugins/guests/debian/cap/configure_networks.rb +++ b/plugins/guests/debian/cap/configure_networks.rb @@ -11,14 +11,16 @@ module VagrantPlugins def self.configure_networks(machine, networks) comm = machine.communicate - interfaces = {} + commands = [] entries = [] + interfaces = [] + + comm.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout| + interfaces = stdout.split("\n") + end - # Accumulate the configurations to add to the interfaces file as - # well as what interfaces we're actually configuring since we use that - # later. networks.each do |network| - interfaces[network[:interface]] = true + network[:device] = interfaces[network[:interface]] entry = TemplateRenderer.render("guests/debian/network_#{network[:type]}", options: network, @@ -34,17 +36,12 @@ module VagrantPlugins comm.upload(f.path, "/tmp/vagrant-network-entry") end - commands = [] - - # Bring down all the interfaces we're reconfiguring. By bringing down - # each specifically, we avoid reconfiguring eth0 (the NAT interface) - # so SSH never dies. - interfaces.each do |interface, _| + networks.each do |network| # Ubuntu 16.04+ returns an error when downing an interface that # does not exist. The `|| true` preserves the behavior that older # Ubuntu versions exhibit and Vagrant expects (GH-7155) - commands << "/sbin/ifdown 'eth#{interface}' 2> /dev/null || true" - commands << "/sbin/ip addr flush dev 'eth#{interface}' 2> /dev/null" + commands << "/sbin/ifdown '#{network[:device]}' || true" + commands << "/sbin/ip addr flush dev '#{network[:device]}'" end # Reconfigure /etc/network/interfaces. @@ -65,8 +62,8 @@ module VagrantPlugins EOH # Bring back up each network interface, reconfigured. - interfaces.each do |interface, _| - commands << "/sbin/ifup 'eth#{interface}'" + networks.each do |network| + commands << "/sbin/ifup '#{network[:device]}'" end # Run all the commands in one session to prevent partial configuration diff --git a/templates/guests/debian/network_dhcp.erb b/templates/guests/debian/network_dhcp.erb index 4dc4f8672..6c305270a 100644 --- a/templates/guests/debian/network_dhcp.erb +++ b/templates/guests/debian/network_dhcp.erb @@ -1,7 +1,7 @@ #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. -auto eth<%= options[:interface] %> -iface eth<%= options[:interface] %> inet dhcp +auto <%= options[:device] %> +iface <%= options[:device] %> inet dhcp <% if !options[:use_dhcp_assigned_default_route] %> post-up route del default dev $IFACE || true <% else %> diff --git a/templates/guests/debian/network_static.erb b/templates/guests/debian/network_static.erb index 91f0cd62e..bf9b9c6b3 100644 --- a/templates/guests/debian/network_static.erb +++ b/templates/guests/debian/network_static.erb @@ -1,7 +1,7 @@ #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. -auto eth<%= options[:interface] %> -iface eth<%= options[:interface] %> inet static +auto <%= options[:device] %> +iface <%= options[:device] %> inet static address <%= options[:ip] %> netmask <%= options[:netmask] %> <% if options[:gateway] %> diff --git a/templates/guests/debian/network_static6.erb b/templates/guests/debian/network_static6.erb index 7b9e8a694..288da4396 100644 --- a/templates/guests/debian/network_static6.erb +++ b/templates/guests/debian/network_static6.erb @@ -1,7 +1,7 @@ #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. -auto eth<%= options[:interface] %> -iface eth<%= options[:interface] %> inet6 static +auto <%= options[:device] %> +iface <%= options[:device] %> inet6 static address <%= options[:ip] %> netmask <%= options[:netmask] %> <% if options[:gateway] %>