diff --git a/plugins/guests/gentoo/cap/configure_networks.rb b/plugins/guests/gentoo/cap/configure_networks.rb index 1d4a26b56..ab27a58e3 100644 --- a/plugins/guests/gentoo/cap/configure_networks.rb +++ b/plugins/guests/gentoo/cap/configure_networks.rb @@ -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 - # Configure each network interface - networks.each do |network| - entry = TemplateRenderer.render("guests/gentoo/network_#{network[:type]}", - options: network) + commands = [] + interfaces = [] - # 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") - end + # Remove any previous network additions to the configuration file. + commands << "sed -i'' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/conf.d/net" - # 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 + 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]] + + entry = TemplateRenderer.render("guests/gentoo/network_#{network[:type]}", + options: network, + ) + + remote_path = "/tmp/vagrant-network-#{network[:device]}-#{Time.now.to_i}-#{i}" + + Tempfile.open("vagrant-gentoo-configure-networks") do |f| + f.binmode + f.write(entry) + f.fsync + f.close + comm.upload(f.path, remote_path) + 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 diff --git a/templates/guests/gentoo/network_dhcp.erb b/templates/guests/gentoo/network_dhcp.erb index df1092987..18cea064e 100644 --- a/templates/guests/gentoo/network_dhcp.erb +++ b/templates/guests/gentoo/network_dhcp.erb @@ -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 diff --git a/templates/guests/gentoo/network_static.erb b/templates/guests/gentoo/network_static.erb index fe6c77194..ed68c911d 100644 --- a/templates/guests/gentoo/network_static.erb +++ b/templates/guests/gentoo/network_static.erb @@ -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 diff --git a/templates/guests/gentoo/network_static6.erb b/templates/guests/gentoo/network_static6.erb new file mode 100644 index 000000000..6467a57cd --- /dev/null +++ b/templates/guests/gentoo/network_static6.erb @@ -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