From: Will Woods Date: Tue, 14 Feb 2012 17:38:02 +0000 (-0500) Subject: 40network: add net-lib.sh X-Git-Tag: 016~11 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9d169a07ce234ac53a0725200e1a8b429e2f8ee0;p=thirdparty%2Fdracut.git 40network: add net-lib.sh 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 --- diff --git a/modules.d/40network/module-setup.sh b/modules.d/40network/module-setup.sh index ef7818ab3..2f4d93d9f 100755 --- a/modules.d/40network/module-setup.sh +++ b/modules.d/40network/module-setup.sh @@ -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 index 000000000..0ed80b87d --- /dev/null +++ b/modules.d/40network/net-lib.sh @@ -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.. +}