From 6ff81dc29ca15b066afaa210f14c170f1021fa5a Mon Sep 17 00:00:00 2001 From: cammoraton Date: Tue, 6 May 2014 08:44:15 -0400 Subject: [PATCH 1/3] Add interfaces_list capability to get list of interfaces via biosdevname or dmesg parsing. --- plugins/guests/redhat/cap/interfaces_list.rb | 32 ++++++++++++++++++++ plugins/guests/redhat/plugin.rb | 5 +++ 2 files changed, 37 insertions(+) create mode 100644 plugins/guests/redhat/cap/interfaces_list.rb diff --git a/plugins/guests/redhat/cap/interfaces_list.rb b/plugins/guests/redhat/cap/interfaces_list.rb new file mode 100644 index 000000000..d7e88702e --- /dev/null +++ b/plugins/guests/redhat/cap/interfaces_list.rb @@ -0,0 +1,32 @@ +module VagrantPlugins + module GuestRedHat + module Cap + class InterfacesList + def self.interfaces_list(machine) + version = String.new + machine.communicate.sudo("cat /etc/redhat-release | sed -e 's/.*release\ //' | cut -f1 -d' '") do |_, result| + # Only care about the major version for now + version = result.split('.').first + end + + interface_names = Array.new + + # In theory this would work with even older versions as dmesg has been relatively static for a long time + if version.to_i < 6 + machine.communicate.sudo("dmesg | cut -f2 -d: | sed -e 's/^\ //' | sed -e 's/\ .*$//' | grep eth") do |_, result| + # It has two results ? - Quick hack to compensate + interface_names = result.split("\n").uniq.sort if interface_names.empty? + end + else + machine.communicate.sudo("biosdevname -d | grep Kernel | cut -f2 -d: | sed -e 's/ //;'") do |_, result| + # The previous had two results. This one never has. Do the same check for now. + interface_names = result.split("\n") if interface_names.empty? + end + end + + return interface_names + end + end + end + end +end \ No newline at end of file diff --git a/plugins/guests/redhat/plugin.rb b/plugins/guests/redhat/plugin.rb index 56e6b8031..e6155ca73 100644 --- a/plugins/guests/redhat/plugin.rb +++ b/plugins/guests/redhat/plugin.rb @@ -35,6 +35,11 @@ module VagrantPlugins require_relative "cap/rsync" Cap::RSync end + + guest_capability("redhat", "interfaces_list") do + require_relative "cap/interfaces_list" + Cap::InterfacesList + end end end end From 5345c1479aad4ea985868495ca43b0f48ee73de4 Mon Sep 17 00:00:00 2001 From: cammoraton Date: Tue, 6 May 2014 08:44:36 -0400 Subject: [PATCH 2/3] Fix configure networks and templates to work off polled interface names. --- .../guests/redhat/cap/configure_networks.rb | 44 ++++++++----------- templates/guests/redhat/network_dhcp.erb | 2 +- templates/guests/redhat/network_static.erb | 2 +- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/plugins/guests/redhat/cap/configure_networks.rb b/plugins/guests/redhat/cap/configure_networks.rb index eb30474c5..c994076b6 100644 --- a/plugins/guests/redhat/cap/configure_networks.rb +++ b/plugins/guests/redhat/cap/configure_networks.rb @@ -13,29 +13,28 @@ module VagrantPlugins def self.configure_networks(machine, networks) network_scripts_dir = machine.guest.capability("network_scripts_dir") - + interface_names = machine.guest.capability("interfaces_list") + # 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| - interfaces.add(network[:interface]) - - # Down the interface before munging the config file. This might fail - # if the interface is not actually set up yet so ignore errors. - machine.communicate.sudo( - "/sbin/ifdown eth#{network[:interface]} 2> /dev/null", error_check: false) - + interface = interface_names[network[:interface]] + 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-eth#{network[:interface]}") - machine.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' #{network_scripts_dir}/ifcfg-eth#{network[:interface]} > /tmp/vagrant-ifcfg-eth#{network[:interface]}") - machine.communicate.sudo("cat /tmp/vagrant-ifcfg-eth#{network[:interface]} > #{network_scripts_dir}/ifcfg-eth#{network[:interface]}") - machine.communicate.sudo("rm -f /tmp/vagrant-ifcfg-eth#{network[:interface]}") - + 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 /tmp/vagrant-ifcfg-#{interface}") + # Render and upload the network entry file to a deterministic # temporary location. - entry = TemplateRenderer.render("guests/redhat/network_#{network[:type]}", + # use fedora for now + entry = TemplateRenderer.render("guests/fedora/network_#{network[:type]}", :options => network) temp = Tempfile.new("vagrant") @@ -43,25 +42,20 @@ module VagrantPlugins temp.write(entry) temp.close - machine.communicate.upload(temp.path, "/tmp/vagrant-network-entry_#{network[:interface]}") + machine.communicate.upload(temp.path, "/tmp/vagrant-network-entry_#{interface}") end # Bring down all the interfaces we're reconfiguring. By bringing down # each specifically, we avoid reconfiguring eth0 (the NAT interface) so # SSH never dies. interfaces.each do |interface| + puts interface.inspect retryable(:on => Vagrant::Errors::VagrantError, :tries => 3, :sleep => 2) do - # The interface should already be down so this probably - # won't do anything, so we run it with error_check false. - machine.communicate.sudo( - "/sbin/ifdown eth#{interface} 2> /dev/null", error_check: false) - - # Add the new interface and bring it up - machine.communicate.sudo("cat /tmp/vagrant-network-entry_#{interface} >> #{network_scripts_dir}/ifcfg-eth#{interface}") - machine.communicate.sudo("ARPCHECK=no /sbin/ifup eth#{interface} 2> /dev/null") + machine.communicate.sudo("cat /tmp/vagrant-network-entry_#{interface} >> #{network_scripts_dir}/ifcfg-#{interface}") + machine.communicate.sudo("/sbin/ifdown #{interface}", :error_check => true) + machine.communicate.sudo("/sbin/ifup #{interface}") end - - machine.communicate.sudo("rm -f /tmp/vagrant-network-entry_#{interface}") + machine.communicate.sudo("rm /tmp/vagrant-network-entry_#{interface}") end end end diff --git a/templates/guests/redhat/network_dhcp.erb b/templates/guests/redhat/network_dhcp.erb index 8bbaa62e4..b15250cc2 100644 --- a/templates/guests/redhat/network_dhcp.erb +++ b/templates/guests/redhat/network_dhcp.erb @@ -2,5 +2,5 @@ # The contents below are automatically generated by Vagrant. Do not modify. BOOTPROTO=dhcp ONBOOT=yes -DEVICE=eth<%= options[:interface] %> +DEVICE=<%= options[:device] %> #VAGRANT-END diff --git a/templates/guests/redhat/network_static.erb b/templates/guests/redhat/network_static.erb index c28dd74cb..7af20cd7a 100644 --- a/templates/guests/redhat/network_static.erb +++ b/templates/guests/redhat/network_static.erb @@ -5,6 +5,6 @@ BOOTPROTO=none ONBOOT=yes IPADDR=<%= options[:ip] %> NETMASK=<%= options[:netmask] %> -DEVICE=eth<%= options[:interface] %> +DEVICE=<%= options[:device] %> PEERDNS=no #VAGRANT-END From 6d6282f1becdf58bdb92b3c10560914190b41d57 Mon Sep 17 00:00:00 2001 From: cammoraton Date: Tue, 6 May 2014 08:48:01 -0400 Subject: [PATCH 3/3] Finished up fixes. Removed some artifacts. --- plugins/guests/redhat/cap/configure_networks.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/guests/redhat/cap/configure_networks.rb b/plugins/guests/redhat/cap/configure_networks.rb index c994076b6..58161e1be 100644 --- a/plugins/guests/redhat/cap/configure_networks.rb +++ b/plugins/guests/redhat/cap/configure_networks.rb @@ -34,7 +34,7 @@ module VagrantPlugins # Render and upload the network entry file to a deterministic # temporary location. # use fedora for now - entry = TemplateRenderer.render("guests/fedora/network_#{network[:type]}", + entry = TemplateRenderer.render("guests/redhat/network_#{network[:type]}", :options => network) temp = Tempfile.new("vagrant") @@ -49,7 +49,6 @@ module VagrantPlugins # each specifically, we avoid reconfiguring eth0 (the NAT interface) so # SSH never dies. interfaces.each do |interface| - puts interface.inspect retryable(:on => Vagrant::Errors::VagrantError, :tries => 3, :sleep => 2) do machine.communicate.sudo("cat /tmp/vagrant-network-entry_#{interface} >> #{network_scripts_dir}/ifcfg-#{interface}") machine.communicate.sudo("/sbin/ifdown #{interface}", :error_check => true)