Merge pull request #7848 from chrisroberts/fix/eth-first-predictable

guests/linux: Place ethernet devices at start of device list
This commit is contained in:
Chris Roberts 2016-09-30 14:50:08 -07:00 committed by GitHub
commit 2e6b23c8fd
2 changed files with 30 additions and 0 deletions

View File

@ -2,6 +2,11 @@ module VagrantPlugins
module GuestLinux module GuestLinux
module Cap module Cap
class NetworkInterfaces class NetworkInterfaces
# Valid ethernet device prefix values.
# eth - classic prefix
# en - predictable interface names prefix
POSSIBLE_ETHERNET_PREFIXES = ["eth".freeze, "en".freeze].freeze
@@logger = Log4r::Logger.new("vagrant::guest::linux::network_interfaces") @@logger = Log4r::Logger.new("vagrant::guest::linux::network_interfaces")
# Get network interfaces as a list. The result will be something like: # Get network interfaces as a list. The result will be something like:
@ -15,10 +20,16 @@ module VagrantPlugins
s << data if type == :stdout s << data if type == :stdout
end end
ifaces = s.split("\n") ifaces = s.split("\n")
eth_prefix = nil
@@logger.debug("Unsorted list: #{ifaces.inspect}") @@logger.debug("Unsorted list: #{ifaces.inspect}")
# Break out integers from strings and sort the arrays to provide # Break out integers from strings and sort the arrays to provide
# a natural sort for the interface names # a natural sort for the interface names
ifaces = ifaces.map do |iface| ifaces = ifaces.map do |iface|
if eth_prefix.nil?
eth_prefix = POSSIBLE_ETHERNET_PREFIXES.detect do |prefix|
iface.start_with?(prefix)
end
end
iface.scan(/(.+?)(\d+)/).flatten.map do |iface_part| iface.scan(/(.+?)(\d+)/).flatten.map do |iface_part|
if iface_part.to_i.to_s == iface_part if iface_part.to_i.to_s == iface_part
iface_part.to_i iface_part.to_i
@ -28,6 +39,13 @@ module VagrantPlugins
end end
end.sort.map(&:join) end.sort.map(&:join)
@@logger.debug("Sorted list: #{ifaces.inspect}") @@logger.debug("Sorted list: #{ifaces.inspect}")
# Extract ethernet devices and place at start of list
if eth_prefix
eth_start = ifaces.index{|iface| iface.start_with?(eth_prefix) }
eth_end = ifaces.rindex{|iface| iface.start_with?(eth_prefix) }
ifaces.unshift(*ifaces.slice!(eth_start, eth_end - 1))
@@logger.debug("Ethernet preferred sorted list: #{ifaces.inspect}")
end
ifaces ifaces
end end
end end

View File

@ -44,5 +44,17 @@ describe "VagrantPlugins::GuestLinux::Cap::NetworkInterfaces" do
result = cap.network_interfaces(machine) result = cap.network_interfaces(machine)
expect(result).to eq(["enp0s3", "enp0s5", "enp0s8", "enp0s10", "enp1s3"]) expect(result).to eq(["enp0s3", "enp0s5", "enp0s8", "enp0s10", "enp1s3"])
end end
it "sorts ethernet devices discovered with classic naming first in list" do
expect(comm).to receive(:sudo).and_yield(:stdout, "eth1\neth2\ndocker0\nbridge0\neth0")
result = cap.network_interfaces(machine)
expect(result).to eq(["eth0", "eth1", "eth2", "bridge0", "docker0"])
end
it "sorts ethernet devices discovered with predictable network interfaces naming first in list" do
expect(comm).to receive(:sudo).and_yield(:stdout, "enp0s8\ndocker0\nenp0s3\nbridge0\nenp0s5")
result = cap.network_interfaces(machine)
expect(result).to eq(["enp0s3", "enp0s5", "enp0s8", "bridge0", "docker0"])
end
end end
end end