guests/rhel: Switch to predicable network interface names
This commit is contained in:
parent
4664930e06
commit
b621cc44fb
|
@ -1,4 +1,3 @@
|
||||||
require "set"
|
|
||||||
require "tempfile"
|
require "tempfile"
|
||||||
|
|
||||||
require_relative "../../../../lib/vagrant/util/retryable"
|
require_relative "../../../../lib/vagrant/util/retryable"
|
||||||
|
@ -12,122 +11,52 @@ module VagrantPlugins
|
||||||
include Vagrant::Util
|
include Vagrant::Util
|
||||||
|
|
||||||
def self.configure_networks(machine, networks)
|
def self.configure_networks(machine, networks)
|
||||||
network_scripts_dir = machine.guest.capability("network_scripts_dir")
|
comm = machine.communicate
|
||||||
|
|
||||||
virtual = false
|
network_scripts_dir = machine.guest.capability(:network_scripts_dir)
|
||||||
interface_names = Array.new
|
|
||||||
interface_names_by_slot = Array.new
|
interfaces = []
|
||||||
machine.communicate.sudo("/usr/sbin/biosdevname &>/dev/null; echo $?") do |_, result|
|
commands = []
|
||||||
# The above command returns:
|
|
||||||
# - '4' if /usr/sbin/biosdevname detects it is running in a virtual machine
|
comm.sudo("/sbin/ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout|
|
||||||
# - '127' if /usr/sbin/biosdevname doesn't exist
|
interfaces = stdout.split("\n")
|
||||||
virtual = true if ['4', '127'].include? result.chomp
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if virtual
|
networks.each.with_index do |network, i|
|
||||||
machine.communicate.sudo("ls -v /sys/class/net | egrep -v lo\\|docker") do |_, result|
|
network[:device] = interfaces[network[:interface]]
|
||||||
interface_names = result.split("\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
interface_names_by_slot = networks.map do |network|
|
# Render a new configuration
|
||||||
"#{interface_names[network[:interface]]}"
|
|
||||||
end
|
|
||||||
else
|
|
||||||
machine.communicate.sudo("/usr/sbin/biosdevname -d | grep Kernel | cut -f2 -d: | sed -e 's/ //;'") do |_, result|
|
|
||||||
interface_names = result.split("\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
interface_name_pairs = Array.new
|
|
||||||
interface_names.each do |interface_name|
|
|
||||||
machine.communicate.sudo("/usr/sbin/biosdevname --policy=all_ethN -i #{interface_name}") do |_, result|
|
|
||||||
interface_name_pairs.push([interface_name, result.gsub("\n", "")])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
setting_interface_names = networks.map do |network|
|
|
||||||
"eth#{network[:interface]}"
|
|
||||||
end
|
|
||||||
|
|
||||||
interface_names_by_slot = interface_names.dup
|
|
||||||
interface_name_pairs.each do |interface_name, previous_interface_name|
|
|
||||||
if setting_interface_names.index(previous_interface_name) == nil
|
|
||||||
interface_names_by_slot.delete(interface_name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Read interface MAC addresses for later matching
|
|
||||||
mac_addresses = Array.new(interface_names.length)
|
|
||||||
interface_names.each_with_index do |ifname, index|
|
|
||||||
machine.communicate.sudo("cat /sys/class/net/#{ifname}/address") do |_, result|
|
|
||||||
mac_addresses[index] = result.strip
|
|
||||||
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.
|
|
||||||
interfaces = Set.new
|
|
||||||
networks.each do |network|
|
|
||||||
interface = nil
|
|
||||||
if network[:mac_address]
|
|
||||||
found_idx = mac_addresses.find_index(network[:mac_address])
|
|
||||||
# Ignore network if requested MAC address could not be found
|
|
||||||
next if found_idx.nil?
|
|
||||||
interface = interface_names[found_idx]
|
|
||||||
else
|
|
||||||
ifname_by_slot = interface_names_by_slot[network[:interface]-1]
|
|
||||||
# Don't overwrite if interface was already matched via MAC address
|
|
||||||
next if interfaces.include?(ifname_by_slot)
|
|
||||||
interface = ifname_by_slot
|
|
||||||
end
|
|
||||||
|
|
||||||
interfaces.add(interface)
|
|
||||||
network[:device] = interface
|
|
||||||
|
|
||||||
# Remove any previous vagrant configuration in this network
|
|
||||||
# interface's configuration files.
|
|
||||||
machine.communicate.sudo("touch #{network_scripts_dir}/ifcfg-#{interface}")
|
|
||||||
machine.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' #{network_scripts_dir}/ifcfg-#{interface} > /tmp/vagrant-ifcfg-#{interface}")
|
|
||||||
machine.communicate.sudo("cat /tmp/vagrant-ifcfg-#{interface} > #{network_scripts_dir}/ifcfg-#{interface}")
|
|
||||||
machine.communicate.sudo("rm -f /tmp/vagrant-ifcfg-#{interface}")
|
|
||||||
|
|
||||||
# Render and upload the network entry file to a deterministic
|
|
||||||
# temporary location.
|
|
||||||
entry = TemplateRenderer.render("guests/fedora/network_#{network[:type]}",
|
entry = TemplateRenderer.render("guests/fedora/network_#{network[:type]}",
|
||||||
options: network)
|
options: network,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Upload the new configuration
|
||||||
|
remote_path = "/tmp/vagrant-network-entry-#{network[:device]}-#{Time.now.to_i}-#{i}"
|
||||||
Tempfile.open("vagrant-fedora-configure-networks") do |f|
|
Tempfile.open("vagrant-fedora-configure-networks") do |f|
|
||||||
f.binmode
|
f.binmode
|
||||||
f.write(entry)
|
f.write(entry)
|
||||||
f.fsync
|
f.fsync
|
||||||
f.close
|
f.close
|
||||||
machine.communicate.upload(f.path, "/tmp/vagrant-network-entry_#{interface}")
|
machine.communicate.upload(f.path, remote_path)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Bring down all the interfaces we're reconfiguring. By bringing down
|
# Add the new interface and bring it back up
|
||||||
# each specifically, we avoid reconfiguring p7p (the NAT interface) so
|
final_path = "#{network_scripts_dir}/ifcfg-#{network[:device]}"
|
||||||
# SSH never dies.
|
commands << <<-EOH.gsub(/^ {14}/, '')
|
||||||
interfaces.each do |interface|
|
# Down the interface before munging the config file. This might
|
||||||
retryable(on: Vagrant::Errors::VagrantError, tries: 3, sleep: 2) do
|
# fail if the interface is not actually set up yet so ignore
|
||||||
machine.communicate.sudo(<<-SCRIPT, error_check: true)
|
# errors.
|
||||||
cat /tmp/vagrant-network-entry_#{interface} >> #{network_scripts_dir}/ifcfg-#{interface}
|
/sbin/ifdown '#{network[:device]}' || true
|
||||||
|
|
||||||
if command -v nmcli &>/dev/null; then
|
# Move new config into place
|
||||||
if command -v systemctl &>/dev/null && systemctl -q is-enabled NetworkManager &>/dev/null; then
|
mv '#{remote_path}' '#{final_path}'
|
||||||
nmcli c reload #{interface}
|
|
||||||
elif command -v service &>/dev/null && service NetworkManager status &>/dev/null; then
|
|
||||||
nmcli c reload #{interface}
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
/sbin/ifdown #{interface}
|
# Bring the interface up
|
||||||
/sbin/ifup #{interface}
|
ARPCHECK=no /sbin/ifup '#{network[:device]}'
|
||||||
|
EOH
|
||||||
|
end
|
||||||
|
|
||||||
rm -f /tmp/vagrant-network-entry_#{interface}
|
comm.sudo(commands.join("\n"))
|
||||||
SCRIPT
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
#VAGRANT-BEGIN
|
||||||
|
ifconfig_<%= options[:device] %>_ipv6="inet6 <%= options[:ip] %> prefixlen <%= options[:netmask] %>"
|
||||||
|
<% if options[:gateway] %>
|
||||||
|
ipv6_default_router="<%= options[:gateway] %>"
|
||||||
|
<% end %>
|
||||||
|
#VAGRANT-END
|
|
@ -2,5 +2,5 @@
|
||||||
# The contents below are automatically generated by Vagrant. Do not modify.
|
# The contents below are automatically generated by Vagrant. Do not modify.
|
||||||
BOOTPROTO=dhcp
|
BOOTPROTO=dhcp
|
||||||
ONBOOT=yes
|
ONBOOT=yes
|
||||||
DEVICE=eth<%= options[:interface] %>
|
DEVICE=<%= options[:device] %>
|
||||||
#VAGRANT-END
|
#VAGRANT-END
|
||||||
|
|
|
@ -5,7 +5,7 @@ BOOTPROTO=none
|
||||||
ONBOOT=yes
|
ONBOOT=yes
|
||||||
IPADDR=<%= options[:ip] %>
|
IPADDR=<%= options[:ip] %>
|
||||||
NETMASK=<%= options[:netmask] %>
|
NETMASK=<%= options[:netmask] %>
|
||||||
DEVICE=eth<%= options[:interface] %>
|
DEVICE=<%= options[:device] %>
|
||||||
<% if options[:gateway] %>
|
<% if options[:gateway] %>
|
||||||
GATEWAY=<%= options[:gateway] %>
|
GATEWAY=<%= options[:gateway] %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
#VAGRANT-BEGIN
|
||||||
|
# The contents below are automatically generated by Vagrant. Do not modify.
|
||||||
|
auto <%= options[:device] %>
|
||||||
|
iface <%= options[:device] %> inet6 static
|
||||||
|
address <%= options[:ip] %>
|
||||||
|
netmask <%= options[:netmask] %>
|
||||||
|
<% if options[:gateway] %>
|
||||||
|
gateway <%= options[:gateway] %>
|
||||||
|
<% end %>
|
||||||
|
#VAGRANT-END
|
Loading…
Reference in New Issue