Add support for predictable network interfaces names
This commit is contained in:
parent
bde0e3fb2a
commit
10090bf4dc
|
@ -10,8 +10,28 @@ module VagrantPlugins
|
||||||
include Vagrant::Util
|
include Vagrant::Util
|
||||||
|
|
||||||
def self.configure_networks(machine, networks)
|
def self.configure_networks(machine, networks)
|
||||||
|
interfaces = Array.new
|
||||||
|
machine.communicate.sudo("ip -o -0 addr | grep -v LOOPBACK | awk
|
||||||
|
'{print $2}' | sed 's/://'") do |_, result|
|
||||||
|
interfaces = result.split("\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
# Cleaning of some dirty hacks documented here :
|
||||||
|
# (http://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/)
|
||||||
|
machine.communicate.sudo("rm -f
|
||||||
|
/etc/udev/rules.d/*-net-name-slot.rules")
|
||||||
|
machine.communicate.sudo("rm -f
|
||||||
|
/etc/udev/rules.d/*-persistent-net.rules")
|
||||||
|
machine.communicate.sudo("udevadm control --reload")
|
||||||
|
|
||||||
networks.each do |network|
|
networks.each do |network|
|
||||||
entry = TemplateRenderer.render("guests/arch/network_#{network[:type]}",
|
# We use :device in the template instead of
|
||||||
|
# eth#{network[:interface]} in order to support Predictable
|
||||||
|
# Network Interfaces
|
||||||
|
network[:device] = interfaces[network[:interface]]
|
||||||
|
|
||||||
|
entry =
|
||||||
|
TemplateRenderer.render("guests/arch/network_#{network[:type]}",
|
||||||
options: network)
|
options: network)
|
||||||
|
|
||||||
temp = Tempfile.new("vagrant")
|
temp = Tempfile.new("vagrant")
|
||||||
|
@ -20,26 +40,10 @@ module VagrantPlugins
|
||||||
temp.close
|
temp.close
|
||||||
|
|
||||||
machine.communicate.upload(temp.path, "/tmp/vagrant_network")
|
machine.communicate.upload(temp.path, "/tmp/vagrant_network")
|
||||||
machine.communicate.sudo("ln -sf /dev/null /etc/udev/rules.d/80-net-name-slot.rules")
|
machine.communicate.sudo("mv /tmp/vagrant_network
|
||||||
machine.communicate.sudo("udevadm control --reload")
|
/etc/netctl/#{network[:device]}")
|
||||||
machine.communicate.sudo("mv /tmp/vagrant_network /etc/netctl/eth#{network[:interface]}")
|
machine.communicate.sudo("ip link set #{network[:device]} down &&
|
||||||
|
netctl start #{network[:device]}")
|
||||||
# Only consider nth line of sed's output below. There's always an
|
|
||||||
# offset of two lines in the below sed command given the current
|
|
||||||
# interface number -> 1: lo, 2: nat device,
|
|
||||||
snth = network[:interface] + 2
|
|
||||||
|
|
||||||
# A hack not to rely on udev rule 80-net-name-slot.rules masking
|
|
||||||
# (ln -sf /dev/null /etc/udev/80-net-name-slot.rules).
|
|
||||||
# I assume this to be the most portable solution because
|
|
||||||
# otherwise we would need to rely on the Virtual Machine implementation
|
|
||||||
# to provide details on the configured interfaces, e.g mac address
|
|
||||||
# to write a custom udev rule. Templating the netcfg files and
|
|
||||||
# replacing the correct interface name within ruby seems more
|
|
||||||
# complicted too (I'm far from being a ruby expert though).
|
|
||||||
machine.communicate.sudo("sed -i \"s/eth#{network[:interface]}/`ip link | sed -n 's/.*:\\s\\(.*\\): <.*/\\1/p' | sed -n #{snth}p`/g\" /etc/netctl/eth#{network[:interface]}")
|
|
||||||
machine.communicate.sudo("ip link set eth#{network[:interface]} down")
|
|
||||||
machine.communicate.sudo("netctl start eth#{network[:interface]}")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
Description='A basic dhcp ethernet connection'
|
Description='A basic dhcp ethernet connection'
|
||||||
Interface=eth<%= options[:interface] %>
|
Interface=<%= options[:device] %>
|
||||||
Connection=ethernet
|
Connection=ethernet
|
||||||
IP=dhcp
|
IP=dhcp
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
Connection=ethernet
|
Connection=ethernet
|
||||||
Description='A basic static ethernet connection'
|
Description='A basic static ethernet connection'
|
||||||
Interface=eth<%= options[:interface] %>
|
Interface=<%= options[:device] %>
|
||||||
IP=static
|
IP=static
|
||||||
Address=('<%= options[:ip]%>/24')
|
Address=('<%= options[:ip]%>/24')
|
||||||
|
|
Loading…
Reference in New Issue