From e1db3f8d9b7a7382c5868c59ff5e9dda689dbc96 Mon Sep 17 00:00:00 2001 From: Doug Neal Date: Mon, 18 Apr 2016 16:33:53 +0100 Subject: [PATCH 1/2] Handle new 'predictable' network interface naming style Explicitly discover the names of the ethernet interfaces, and don't assume that they are eth0, eth1 etc as this does not hold true under newer systemd-based distributions. --- .../guests/debian/cap/configure_networks.rb | 19 +++++++++++++++---- templates/guests/debian/network_dhcp.erb | 10 +++++----- templates/guests/debian/network_static.erb | 4 ++-- templates/guests/debian/network_static6.erb | 4 ++-- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/plugins/guests/debian/cap/configure_networks.rb b/plugins/guests/debian/cap/configure_networks.rb index 0580bc2932b..6637e1ac893 100644 --- a/plugins/guests/debian/cap/configure_networks.rb +++ b/plugins/guests/debian/cap/configure_networks.rb @@ -16,6 +16,17 @@ def self.configure_networks(machine, networks) comm.sudo("sed -e '/^#VAGRANT-BEGIN/,$ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces.pre") comm.sudo("sed -ne '/^#VAGRANT-END/,$ p' /etc/network/interfaces | tac | sed -e '/^#VAGRANT-END/,$ d' | tac > /tmp/vagrant-network-interfaces.post") + kernel_ifnames = [] + comm.sudo("/usr/bin/find /sys/class/net -type l -print | sort") do |type, data| + if type == :stdout then + data.split("\n").each do |line| + if (line =~ /^\/sys\/class\/net\/(enp[0-9]+s[0-9]+|eth[0-9]+)$/) then + kernel_ifnames << $1 + end + end + end + end + # Accumulate the configurations to add to the interfaces file as # well as what interfaces we're actually configuring since we use that # later. @@ -24,7 +35,7 @@ def self.configure_networks(machine, networks) networks.each do |network| interfaces.add(network[:interface]) entry = TemplateRenderer.render("guests/debian/network_#{network[:type]}", - options: network) + options: network, kernel_ifnames: kernel_ifnames) entries << entry end @@ -45,8 +56,8 @@ def self.configure_networks(machine, networks) # 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) - comm.sudo("/sbin/ifdown eth#{interface} 2> /dev/null || true") - comm.sudo("/sbin/ip addr flush dev eth#{interface} 2> /dev/null") + comm.sudo("/sbin/ifdown #{kernel_ifnames[interface]} 2> /dev/null || true") + comm.sudo("/sbin/ip addr flush dev #{kernel_ifnames[interface]} 2> /dev/null") end comm.sudo('cat /tmp/vagrant-network-interfaces.pre /tmp/vagrant-network-entry /tmp/vagrant-network-interfaces.post > /etc/network/interfaces') @@ -54,7 +65,7 @@ def self.configure_networks(machine, networks) # Bring back up each network interface, reconfigured interfaces.each do |interface| - comm.sudo("/sbin/ifup eth#{interface}") + comm.sudo("/sbin/ifup #{kernel_ifnames[interface]}") end end end diff --git a/templates/guests/debian/network_dhcp.erb b/templates/guests/debian/network_dhcp.erb index 4dc4f86724b..89547d42ced 100644 --- a/templates/guests/debian/network_dhcp.erb +++ b/templates/guests/debian/network_dhcp.erb @@ -1,13 +1,13 @@ #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. -auto eth<%= options[:interface] %> -iface eth<%= options[:interface] %> inet dhcp +auto <%= kernel_ifnames[options[:interface]] %> +iface <%= kernel_ifnames[options[:interface]] %> inet dhcp <% if !options[:use_dhcp_assigned_default_route] %> post-up route del default dev $IFACE || true <% else %> - # We need to disable eth0, see GH-2648 - post-up route del default dev eth0 || true + # We need to disable <%= kernel_ifnames[0] %>, see GH-2648 + post-up route del default dev <%= kernel_ifnames[0] %> || true post-up dhclient $IFACE - pre-down route add default dev eth0 + pre-down route add default dev <%= kernel_ifnames[0] %> <% end %> #VAGRANT-END diff --git a/templates/guests/debian/network_static.erb b/templates/guests/debian/network_static.erb index 91f0cd62ece..7f45e9ffa30 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 <%= kernel_ifnames[options[:interface]] %> +iface <%= kernel_ifnames[options[:interface]] %> 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 7b9e8a6949b..72ee2f5e01e 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 <%= kernel_ifnames[options[:interface]] %> +iface <%= kernel_ifnames[options[:interface]] %> inet6 static address <%= options[:ip] %> netmask <%= options[:netmask] %> <% if options[:gateway] %> From 61aeb020cb29f55244c910ce2f512f1cb76f9de4 Mon Sep 17 00:00:00 2001 From: Doug Neal Date: Mon, 18 Apr 2016 17:02:53 +0100 Subject: [PATCH 2/2] Update unit tests for Debian /etc/network/interfaces templates --- .../guests/debian/network_dhcp_test.rb | 24 ++++++++------ .../guests/debian/network_static_test.rb | 32 +++++++++++-------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/test/unit/templates/guests/debian/network_dhcp_test.rb b/test/unit/templates/guests/debian/network_dhcp_test.rb index a5a0eba8da9..f09908d66b9 100644 --- a/test/unit/templates/guests/debian/network_dhcp_test.rb +++ b/test/unit/templates/guests/debian/network_dhcp_test.rb @@ -6,14 +6,17 @@ let(:template) { "guests/debian/network_dhcp" } it "renders the template" do - result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", + result = Vagrant::Util::TemplateRenderer.render(template, { + kernel_ifnames: ['eth0'], + options: { + interface: 0 + } }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. - auto eth - iface eth inet dhcp + auto eth0 + iface eth0 inet dhcp post-up route del default dev $IFACE || true #VAGRANT-END EOH @@ -21,15 +24,18 @@ context "when use_dhcp_assigned_default_route is set" do it "renders the template" do - result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", - use_dhcp_assigned_default_route: true, + result = Vagrant::Util::TemplateRenderer.render(template, { + kernel_ifnames: ['eth0'], + options: { + interface: 0, + use_dhcp_assigned_default_route: true, + } }) expect(result).to eq <<-EOH.gsub(/^ {8}/, "") #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. - auto eth - iface eth inet dhcp + auto eth0 + iface eth0 inet dhcp # We need to disable eth0, see GH-2648 post-up route del default dev eth0 || true post-up dhclient $IFACE diff --git a/test/unit/templates/guests/debian/network_static_test.rb b/test/unit/templates/guests/debian/network_static_test.rb index 936a6a806aa..a76d0658005 100644 --- a/test/unit/templates/guests/debian/network_static_test.rb +++ b/test/unit/templates/guests/debian/network_static_test.rb @@ -6,16 +6,19 @@ let(:template) { "guests/debian/network_static" } it "renders the template" do - result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", - ip: "1.1.1.1", - netmask: "255.255.0.0", + result = Vagrant::Util::TemplateRenderer.render(template, { + kernel_ifnames: ['eth0'], + options: { + interface: 0, + ip: "1.1.1.1", + netmask: "255.255.0.0", + } }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. - auto eth - iface eth inet static + auto eth0 + iface eth0 inet static address 1.1.1.1 netmask 255.255.0.0 #VAGRANT-END @@ -23,17 +26,20 @@ end it "includes the gateway" do - result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", - ip: "1.1.1.1", - netmask: "255.255.0.0", - gateway: "1.2.3.4", + result = Vagrant::Util::TemplateRenderer.render(template, { + kernel_ifnames: ['eth0'], + options: { + interface: 0, + ip: "1.1.1.1", + netmask: "255.255.0.0", + gateway: "1.2.3.4", + } }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. - auto eth - iface eth inet static + auto eth0 + iface eth0 inet static address 1.1.1.1 netmask 255.255.0.0 gateway 1.2.3.4