]> git.ipfire.org Git - thirdparty/dracut.git/commitdiff
40network: add net-lib.sh
authorWill Woods <wwoods@redhat.com>
Tue, 14 Feb 2012 17:38:02 +0000 (12:38 -0500)
committerHarald Hoyer <harald@redhat.com>
Wed, 15 Feb 2012 14:46:24 +0000 (15:46 +0100)
net-lib.sh is a library of useful functions for network stuff.

More things may get added/moved here in the future.

Signed-off-by: Will Woods <wwoods@redhat.com>
modules.d/40network/module-setup.sh
modules.d/40network/net-lib.sh [new file with mode: 0644]

index ef7818ab3f2d3f01e41d4907640b1a3f7c2b80fe..2f4d93d9f6fabcb18e2ce87f136fbc5d233c461f 100755 (executable)
@@ -76,6 +76,7 @@ install() {
     inst "$moddir/ifup" "/sbin/ifup"
     inst "$moddir/netroot" "/sbin/netroot"
     inst "$moddir/dhclient-script" "/sbin/dhclient-script"
+    inst "$moddir/net-lib.sh" "/lib/net-lib.sh"
     inst_simple "$moddir/dhclient.conf" "/etc/dhclient.conf"
     inst_hook pre-udev 50 "$moddir/ifname-genrules.sh"
     inst_hook pre-udev 60 "$moddir/net-genrules.sh"
diff --git a/modules.d/40network/net-lib.sh b/modules.d/40network/net-lib.sh
new file mode 100644 (file)
index 0000000..0ed80b8
--- /dev/null
@@ -0,0 +1,33 @@
+#!/bin/bash
+
+get_ip() {
+    local iface="$1" ip=""
+    ip=$(ip -o -f inet addr show $iface)
+    ip=${ip%%/*}
+    ip=${ip##* }
+}
+
+iface_for_remote_addr() {
+    set -- $(ip -o route get to $1)
+    echo $5
+}
+
+iface_for_mac() {
+    local interface="" mac="$(echo $1 | tr '[:upper:]' '[:lower:]')"
+    for interface in /sys/class/net/*; do
+        if [ $(cat $interface/address) = "$mac" ]; then
+            echo ${interface##*/}
+        fi
+    done
+}
+
+iface_has_link() {
+    local interface="$1" flags=""
+    [ -n "$interface" ] || return 2
+    interface="/sys/class/net/$interface"
+    [ -d "$interface" ] || return 2
+    flags=$(cat $interface/flags)
+    echo $(($flags|0x41)) > $interface/flags # 0x41: IFF_UP|IFF_RUNNING
+    [ "$(cat $interface/carrier)" = 1 ] || return 1
+    # XXX Do we need to reset the flags here? anaconda never bothered..
+}