guests/gentoo: Use predictable network interface names

This commit is contained in:
Seth Vargo 2016-06-22 18:33:59 -07:00
parent ea9b28f9b6
commit caafb936f0
No known key found for this signature in database
GPG Key ID: 905A90C2949E8787
4 changed files with 49 additions and 29 deletions

View File

@ -9,34 +9,47 @@ module VagrantPlugins
include Vagrant::Util
def self.configure_networks(machine, networks)
machine.communicate.tap do |comm|
# Remove any previous host only network additions to the interface file
comm.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/conf.d/net > /tmp/vagrant-network-interfaces")
comm.sudo("cat /tmp/vagrant-network-interfaces > /etc/conf.d/net")
comm.sudo("rm -f /tmp/vagrant-network-interfaces")
comm = machine.communicate
commands = []
interfaces = []
# Remove any previous network additions to the configuration file.
commands << "sed -i'' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/conf.d/net"
comm.sudo("ifconfig -a | grep -o ^[0-9a-z]* | grep -v '^lo'") do |_, stdout|
interfaces = stdout.split("\n")
end
networks.each_with_index do |network, i|
network[:device] = interfaces[network[:interface]]
# Configure each network interface
networks.each do |network|
entry = TemplateRenderer.render("guests/gentoo/network_#{network[:type]}",
options: network)
options: network,
)
remote_path = "/tmp/vagrant-network-#{network[:device]}-#{Time.now.to_i}-#{i}"
# Upload the entry to a temporary location
Tempfile.open("vagrant-gentoo-configure-networks") do |f|
f.binmode
f.write(entry)
f.fsync
f.close
comm.upload(f.path, "/tmp/vagrant-network-entry")
comm.upload(f.path, remote_path)
end
# Configure the interface
comm.sudo("ln -fs /etc/init.d/net.lo /etc/init.d/net.eth#{network[:interface]}")
comm.sudo("/etc/init.d/net.eth#{network[:interface]} stop")
comm.sudo("cat /tmp/vagrant-network-entry >> /etc/conf.d/net")
comm.sudo("rm -f /tmp/vagrant-network-entry")
comm.sudo("/etc/init.d/net.eth#{network[:interface]} start")
end
end
commands << <<-EOH.gsub(/^ {14}/, '')
ln -sf /etc/init.d/net.lo /etc/init.d/net.#{network[:device]}
/etc/init.d/net.#{network[:device]} stop || true
cat '#{remote_path}' >> /etc/conf.d/net
rm -f '#{remote_path}'
/etc/init.d/net.#{network[:device]} start
EOH
end
comm.sudo(commands.join("\n"))
end
end
end

View File

@ -1,4 +1,4 @@
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
config_eth<%= options[:interface] %>="dhcp"
config_<%= options[:device] %>="dhcp"
#VAGRANT-END

View File

@ -1,7 +1,7 @@
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
config_eth<%= options[:interface] %>=("<%= options[:ip] %> netmask <%= options[:netmask] %>")
<% if options[:gateway] %>
gateways_eth<%= options[:interface] %>="<%= options[:gateway] %>"
<% end %>
config_<%= options[:device] %>=("<%= options[:ip] %> netmask <%= options[:netmask] %>")
<% if options[:gateway] -%>
gateways_<%= options[:device] %>="<%= options[:gateway] %>"
<% end -%>
#VAGRANT-END

View File

@ -0,0 +1,7 @@
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
config_<%= options[:device] %>="<%= options[:ip] %>/<%= options[:netmask] %>"
<% if options[:gateway] -%>
gateways_<%= options[:device] %>="<%= options[:gateway] %>"
<% end -%>
#VAGRANT-END