[NW scripts] Choose alternative interface if NFSroot is used.
authorKeir Fraser <keir@xensource.com>
Fri, 5 Oct 2007 09:31:25 +0000 (10:31 +0100)
committerKeir Fraser <keir@xensource.com>
Fri, 5 Oct 2007 09:31:25 +0000 (10:31 +0100)
Allow the network bridging setup script to choose an alternative
network interface if it is determined that the root is mounted using
NFS, which is assumed to be done over the NIC serving the default
gw. In this case the prefix of the device is used to find other
devices, i.e., if eth0 is the default device an alternative device may
be an other ones starting with 'eth'.

Signed-off-by: Stefan Berger <stefanb@us.ibm.com>
tools/examples/network-bridge

index 5440f51c1ef1abebe1ff32757681bb0e28b229cc..c287760328bbf28843cae2ec3461cf3e8edb433d 100755 (executable)
@@ -56,8 +56,32 @@ dir=$(dirname "$0")
 findCommand "$@"
 evalVariables "$@"
 
+is_network_root () {
+    local rootfs=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $3; }}' /etc/mtab)
+    local rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' /etc/mtab)
+
+    [[ "$rootfs" =~ "^nfs" ]] || [[ "$rootopts" =~ "_netdev" ]] && return 0 || return 1
+}
+
+find_alt_device () {
+    local interf=$1
+    local prefix=${interf%[[:digit:]]}
+    local ifs=$(ip link show | grep " $prefix" |\
+                gawk '{ printf ("%s",substr($2,1,length($2)-1)) }' |\
+                sed s/$interf//)
+    echo "$ifs"
+}
+
 netdev=${netdev:-$(ip route list | awk '/^default / { print $NF }' |
                   sed 's/.* dev //')}
+if is_network_root ; then
+    altdevs=$(find_alt_device $netdev)
+    for netdev in $altdevs; do break; done
+    if [ -z "$netdev" ]; then
+        [ -x /usr/bin/logger ] && /usr/bin/logger "network-bridge: bridging not supported on network root; not starting"
+        exit
+    fi
+fi
 netdev=${netdev:-eth0}
 bridge=${bridge:-${netdev}}
 antispoof=${antispoof:-no}
@@ -173,23 +197,11 @@ show_status () {
     echo '============================================================'
 }
 
-is_network_root () {
-    local rootfs=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $3; }}' /etc/mtab)
-    local rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' /etc/mtab)
-
-    [[ "$rootfs" =~ "^nfs" ]] || [[ "$rootopts" =~ "_netdev" ]] && return 0 || return 1
-}
-
 op_start () {
     if [ "${bridge}" = "null" ] ; then
        return
     fi
 
-    if is_network_root ; then
-        [ -x /usr/bin/logger ] && /usr/bin/logger "network-bridge: bridging not supported on network root; not starting"
-        return
-    fi
-
     if link_exists "$pdev"; then
         # The device is already up.
         return