Permalink
Browse files

netconfig: fix link detection

Link detection was failing due to some ethernet cards not returning
link status when in a "down" state. This is corrected by bringing
the interface up and waiting 10 seconds before checking for link.
  • Loading branch information...
1 parent 4f8d452 commit cb63f86dcd6dbee55f63f62d8757f2dbf4e41818 @asquared committed Jun 4, 2012
Showing with 15 additions and 17 deletions.
  1. +15 −17 config/includes.chroot/usr/local/bin/concerto-netconfig
@@ -1,12 +1,8 @@
+#!/usr/bin/env ruby
+
require 'rubygems'
require 'json'
-def read_sysfs(iface, var)
- # Read a sysfs variable (/sys/class/net/iface/var).
- path = "/sys/class/net/#{iface}/#{var}"
- IO.read(path).chomp
-end
-
class WiredConnection
def initialize(args)
if args['interface_name']
@@ -38,6 +34,15 @@ class WiredConnection
end
private
+ def interface_connected(iface)
+ results = `/sbin/mii-tool #{iface}`
+ if results =~ /link ok/
+ true
+ else
+ false
+ end
+ end
+
def wired_interfaces
# This is somewhat Linux specific, and may not be all encompassing.
devices = Dir.glob('/sys/class/net/eth*')
@@ -46,19 +51,12 @@ private
def scan_interfaces
wired_interfaces.each do |iface|
- # bring up interface if it was down
- if (read_sysfs(iface, 'operstate') != 'up')
- system "ifconfig #{iface} up"
- sleep 0.1
- end
- # check if cable is connected
- carrier = read_sysfs(iface, 'carrier')
- # bring interface back down
- system "ifconfig #{iface} down"
-
- if carrier == '1'
+ system("ifconfig #{iface} up")
+ sleep 10
+ if interface_connected(iface)
return iface
end
+ system("ifconfig #{iface} down")
end
# if we get here no interface was found with a cable attached

0 comments on commit cb63f86

Please sign in to comment.