net-lib: add configured_ifaces()
authorWill Woods <wwoods@redhat.com>
Thu, 13 Sep 2012 14:52:33 +0000 (10:52 -0400)
committerHarald Hoyer <harald@redhat.com>
Thu, 13 Jun 2013 12:07:17 +0000 (14:07 +0200)
configured_ifaces is a function that returns the names of each interface
that the user wanted configured.

Currently, this is accomplished by reading the list from
/tmp/net.ifaces. But if we want to allow the user to specify an
interface by its MAC address or IP or something, we need a function that
will read the cache and convert the MACs etc. to names.

(Obviously this conversion only works once udev starts, so it will warn
you if you try it too early.)

modules.d/40network/net-lib.sh

index 7b8d1d7..7fff83a 100644 (file)
@@ -14,6 +14,11 @@ iface_for_remote_addr() {
     echo $5
 }
 
+iface_for_ip() {
+    set -- $(ip -o addr show to $1)
+    echo $2
+}
+
 iface_for_mac() {
     local interface="" mac="$(echo $1 | sed 'y/ABCDEF/abcdef/')"
     for interface in /sys/class/net/*; do
@@ -47,6 +52,32 @@ find_iface_with_link() {
     return 1
 }
 
+# get the iface name for the given identifier - either a MAC, IP, or iface name
+iface_name() {
+    case $1 in
+        ??:??:??:??:??:??|??-??-??-??-??-??) iface_for_mac $1 ;;
+        *:*:*|*.*.*.*) iface_for_ip $1 ;;
+        *) echo $1 ;;
+    esac
+}
+
+# list the configured interfaces
+configured_ifaces() {
+    local IFACES="" iface_id="" rv=1
+    [ -e "/tmp/net.ifaces" ] && read IFACES < /tmp/net.ifaces
+    if { pidof udevd || pidof systemd-udevd; } > /dev/null; then
+        for iface_id in $IFACES; do
+            echo $(iface_name $iface_id)
+            rv=0
+        done
+    else
+        warn "configured_ifaces called before udev is running"
+        echo $IFACES
+        [ -n "$IFACES" ] && rv=0
+    fi
+    return $rv
+}
+
 all_ifaces_up() {
     local iface="" IFACES=""
     [ -e "/tmp/net.ifaces" ] && read IFACES < /tmp/net.ifaces