Blame 0034-network-refactor-stuff-from-netroot-parse-ip-opts-to.patch

Harald Hoyer ee76a0
From 25aa3c5a6eb48d14972b5c658cc7231d8f100ea8 Mon Sep 17 00:00:00 2001
Harald Hoyer ee76a0
From: Will Woods <wwoods@redhat.com>
Harald Hoyer ee76a0
Date: Tue, 6 Mar 2012 18:25:24 -0500
Harald Hoyer ee76a0
Subject: [PATCH] network: refactor stuff from netroot/parse-ip-opts to
Harald Hoyer ee76a0
 net-lib
Harald Hoyer ee76a0
Harald Hoyer ee76a0
Add new functions: all_ifaces_up, get_netroot_ip, ip_is_local, ifdown,
Harald Hoyer ee76a0
setup_net, set_ifname, ibft_to_cmdline
Harald Hoyer ee76a0
Harald Hoyer ee76a0
Use them in netroot.sh and parse-ip-opts.sh.
Harald Hoyer ee76a0
Harald Hoyer ee76a0
There's also a couple little unrelated cleanups.
Harald Hoyer ee76a0
---
Harald Hoyer ee76a0
 modules.d/40network/net-lib.sh       |  108 ++++++++++++++++++++++++++++++++++
Harald Hoyer ee76a0
 modules.d/40network/netroot.sh       |   71 ++--------------------
Harald Hoyer ee76a0
 modules.d/40network/parse-ip-opts.sh |   46 ++-------------
Harald Hoyer ee76a0
 3 files changed, 120 insertions(+), 105 deletions(-)
Harald Hoyer ee76a0
Harald Hoyer ee76a0
diff --git a/modules.d/40network/net-lib.sh b/modules.d/40network/net-lib.sh
Harald Hoyer ee76a0
index e3987a4..e51ce94 100644
Harald Hoyer ee76a0
--- a/modules.d/40network/net-lib.sh
Harald Hoyer ee76a0
+++ b/modules.d/40network/net-lib.sh
Harald Hoyer ee76a0
@@ -31,3 +31,111 @@ iface_has_link() {
Harald Hoyer ee76a0
     [ "$(cat $interface/carrier)" = 1 ] || return 1
Harald Hoyer ee76a0
     # XXX Do we need to reset the flags here? anaconda never bothered..
Harald Hoyer ee76a0
 }
Harald Hoyer ee76a0
+
Harald Hoyer ee76a0
+all_ifaces_up() {
Harald Hoyer ee76a0
+    local iface="" IFACES=""
Harald Hoyer ee76a0
+    [ -e "/tmp/net.ifaces" ] && read IFACES < /tmp/net.ifaces
Harald Hoyer ee76a0
+    for iface in $IFACES; do
Harald Hoyer ee76a0
+        [ -e /tmp/net.$iface.up ] || return 1
Harald Hoyer ee76a0
+    done
Harald Hoyer ee76a0
+}
Harald Hoyer ee76a0
+
Harald Hoyer ee76a0
+get_netroot_ip() {
Harald Hoyer ee76a0
+    local prefix="" server="" rest=""
Harald Hoyer ee76a0
+    splitsep "$1" ":" prefix server rest
Harald Hoyer ee76a0
+    case $server in
Harald Hoyer ee76a0
+        [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*) echo "$server"; return 0 ;;
Harald Hoyer ee76a0
+    esac
Harald Hoyer ee76a0
+    return 1
Harald Hoyer ee76a0
+}
Harald Hoyer ee76a0
+
Harald Hoyer ee76a0
+ip_is_local() {
Harald Hoyer ee76a0
+    strstr "$(ip route get $1 2>/dev/null)" " via "
Harald Hoyer ee76a0
+}
Harald Hoyer ee76a0
+
Harald Hoyer ee76a0
+ifdown() {
Harald Hoyer ee76a0
+    local netif="$1"
Harald Hoyer ee76a0
+    # ip down/flush ensures that routing info goes away as well
Harald Hoyer ee76a0
+    ip link set $netif down
Harald Hoyer ee76a0
+    ip addr flush dev $netif
Harald Hoyer ee76a0
+    echo "#empty" > /etc/resolv.conf
Harald Hoyer ee76a0
+    # TODO: send "offline" uevent?
Harald Hoyer ee76a0
+}
Harald Hoyer ee76a0
+
Harald Hoyer ee76a0
+setup_net() {
Harald Hoyer ee76a0
+    local netif="$1" f="" gw_ip="" netroot_ip="" iface="" IFACES=""
Harald Hoyer ee76a0
+    [ -e /tmp/net.$netif.up ] || return 1
Harald Hoyer ee76a0
+    [ -e "/tmp/net.ifaces" ] && read IFACES < /tmp/net.ifaces
Harald Hoyer ee76a0
+    [ -z "$IFACES" ] && IFACES="$netif"
Harald Hoyer ee76a0
+    for iface in $IFACES ; do
Harald Hoyer ee76a0
+        . /tmp/net.$iface.up
Harald Hoyer ee76a0
+    done
Harald Hoyer ee76a0
+    # run the scripts written by ifup
Harald Hoyer ee76a0
+    [ -e /tmp/net.$netif.gw ]            && . /tmp/net.$netif.gw
Harald Hoyer ee76a0
+    [ -e /tmp/net.$netif.hostname ]      && . /tmp/net.$netif.hostname
Harald Hoyer ee76a0
+    [ -e /tmp/net.$netif.override ]      && . /tmp/net.$netif.override
Harald Hoyer ee76a0
+    [ -e /tmp/dhclient.$netif.dhcpopts ] && . /tmp/dhclient.$netif.dhcpopts
Harald Hoyer ee76a0
+    # set up resolv.conf
Harald Hoyer ee76a0
+    [ -e /tmp/net.$netif.resolv.conf ] && \
Harald Hoyer ee76a0
+        cp -f /tmp/net.$netif.resolv.conf /etc/resolv.conf
Harald Hoyer ee76a0
+
Harald Hoyer ee76a0
+    # Handle STP Timeout: arping the default gateway.
Harald Hoyer ee76a0
+    # (or the root server, if a) it's local or b) there's no gateway.)
Harald Hoyer ee76a0
+    # Note: This assumes that if no router is present the
Harald Hoyer ee76a0
+    # root server is on the same subnet.
Harald Hoyer ee76a0
+
Harald Hoyer ee76a0
+    # Get DHCP-provided router IP, or the cmdline-provided "gw=" argument
Harald Hoyer ee76a0
+    [ -n "$new_routers" ] && gw_ip=${new_routers%%,*}
Harald Hoyer ee76a0
+    [ -n "$gw" ] && gw_ip=$gw
Harald Hoyer ee76a0
+
Harald Hoyer ee76a0
+    # Get the "netroot" IP (if there's an IP address in there)
Harald Hoyer ee76a0
+    netroot_ip=$(get_netroot_ip $netroot)
Harald Hoyer ee76a0
+
Harald Hoyer ee76a0
+    # try netroot if it's local (or there's no gateway)
Harald Hoyer ee76a0
+    if ip_is_local $netroot_ip || [ -z "$gw_ip" ]; then
Harald Hoyer ee76a0
+        dest="$netroot_ip"
Harald Hoyer ee76a0
+    else
Harald Hoyer ee76a0
+        dest="$gw_ip"
Harald Hoyer ee76a0
+    fi
Harald Hoyer ee76a0
+    if [ -n "$dest" ] && ! arping -q -f -w 60 -I $netif $dest ; then
Harald Hoyer ee76a0
+        info "Resolving $dest via ARP on $netif failed"
Harald Hoyer ee76a0
+    fi
Harald Hoyer ee76a0
+}
Harald Hoyer ee76a0
+
Harald Hoyer ee76a0
+set_ifname() {
Harald Hoyer ee76a0
+    local name="$1" mac="$2" num=0 n=""
Harald Hoyer ee76a0
+    # if it's already set, return the existing name
Harald Hoyer ee76a0
+    for n in $(getargs ifname=); do
Harald Hoyer ee76a0
+        strstr "$n" "$mac" && echo ${n%%:*} && return
Harald Hoyer ee76a0
+    done
Harald Hoyer ee76a0
+    # otherwise, pick a new name and use that
Harald Hoyer ee76a0
+    while [ -e /sys/class/$name$num ]; do num=$(($num+1)); done
Harald Hoyer ee76a0
+    echo "ifname=$name$num:$mac" >> /etc/cmdline.d/45-ifname.conf
Harald Hoyer ee76a0
+    echo "$name$num"
Harald Hoyer ee76a0
+}
Harald Hoyer ee76a0
+
Harald Hoyer ee76a0
+ibft_to_cmdline() {
Harald Hoyer ee76a0
+    local iface="" mac="" dev=""
Harald Hoyer ee76a0
+    local dhcp="" ip="" gw="" mask="" hostname=""
Harald Hoyer ee76a0
+    modprobe -q iscsi_ibft
Harald Hoyer ee76a0
+    (
Harald Hoyer ee76a0
+        for iface in /sys/firmware/ibft/ethernet*; do
Harald Hoyer ee76a0
+            [ -e ${iface}/mac ] || continue
Harald Hoyer ee76a0
+            mac=$(read a < ${iface}/mac; echo $a)
Harald Hoyer ee76a0
+            [ -z "$ifname_mac" ] && continue
Harald Hoyer ee76a0
+            dev=$(set_ifname ibft $ifname_mac)
Harald Hoyer ee76a0
+            dhcp=$(read a < ${iface}/dhcp; echo $a)
Harald Hoyer ee76a0
+            if [ -n "$dhcp" ]; then
Harald Hoyer ee76a0
+                echo "ip=$dev:dhcp"
Harald Hoyer ee76a0
+            else
Harald Hoyer ee76a0
+                ip=$(read a < ${iface}/ip-addr; echo $a)
Harald Hoyer ee76a0
+                gw=$(read a < ${iface}/gateway; echo $a)
Harald Hoyer ee76a0
+                mask=$(read a < ${iface}/subnet-mask; echo $a)
Harald Hoyer ee76a0
+                hostname=$(read a < ${iface}/hostname; echo $a)
Harald Hoyer ee76a0
+                echo "ip=$ip::$gw:$mask:$hostname:$dev:none"
Harald Hoyer ee76a0
+            fi
Harald Hoyer ee76a0
+        done
Harald Hoyer ee76a0
+    ) >> /etc/cmdline.d/40-ibft.conf
Harald Hoyer ee76a0
+    # reread cmdline
Harald Hoyer ee76a0
+    unset CMDLINE
Harald Hoyer ee76a0
+}
Harald Hoyer ee76a0
diff --git a/modules.d/40network/netroot.sh b/modules.d/40network/netroot.sh
Harald Hoyer ee76a0
index c5ee84c..ac1c215 100755
Harald Hoyer ee76a0
--- a/modules.d/40network/netroot.sh
Harald Hoyer ee76a0
+++ b/modules.d/40network/netroot.sh
Harald Hoyer ee76a0
@@ -3,14 +3,12 @@
Harald Hoyer ee76a0
 # ex: ts=8 sw=4 sts=4 et filetype=sh
Harald Hoyer ee76a0
 
Harald Hoyer ee76a0
 PATH=/usr/sbin:/usr/bin:/sbin:/bin
Harald Hoyer ee76a0
-type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
Harald Hoyer ee76a0
+command -v getarg >/dev/null    || . /lib/dracut-lib.sh
Harald Hoyer ee76a0
+command -v setup_net >/dev/null || . /lib/net-lib.sh
Harald Hoyer ee76a0
 
Harald Hoyer ee76a0
 # Huh? Empty $1?
Harald Hoyer ee76a0
 [ -z "$1" ] && exit 1
Harald Hoyer ee76a0
 
Harald Hoyer ee76a0
-# Huh? No interface config?
Harald Hoyer ee76a0
-[ ! -e /tmp/net.$1.up ] && exit 1
Harald Hoyer ee76a0
-
Harald Hoyer ee76a0
 # [ ! -z $2 ] means this is for manually bringing up network
Harald Hoyer ee76a0
 # instead of real netroot; If It's called without $2, then there's
Harald Hoyer ee76a0
 # no sense in doing something if no (net)root info is available
Harald Hoyer ee76a0
@@ -23,10 +21,7 @@ fi
Harald Hoyer ee76a0
 # Let's see if we have to wait for other interfaces
Harald Hoyer ee76a0
 # Note: exit works just fine, since the last interface to be
Harald Hoyer ee76a0
 #       online'd should see all files
Harald Hoyer ee76a0
-[ -e "/tmp/net.ifaces" ] && read IFACES < /tmp/net.ifaces
Harald Hoyer ee76a0
-for iface in $IFACES ; do
Harald Hoyer ee76a0
-    [ -e /tmp/net.$iface.up ] || exit 1
Harald Hoyer ee76a0
-done
Harald Hoyer ee76a0
+all_ifaces_up || exit 1
Harald Hoyer ee76a0
 
Harald Hoyer ee76a0
 # Set or override primary interface
Harald Hoyer ee76a0
 netif=$1
Harald Hoyer ee76a0
@@ -78,62 +73,13 @@ if [ -z "$2" ]; then
Harald Hoyer ee76a0
 fi
Harald Hoyer ee76a0
 
Harald Hoyer ee76a0
 # We're here, so we can assume that upping interfaces is now ok
Harald Hoyer ee76a0
-[ -z "$IFACES" ] && IFACES="$netif"
Harald Hoyer ee76a0
-for iface in $IFACES ; do
Harald Hoyer ee76a0
-    . /tmp/net.$iface.up
Harald Hoyer ee76a0
-done
Harald Hoyer ee76a0
-
Harald Hoyer ee76a0
-[ -e /tmp/net.$netif.gw ]          && . /tmp/net.$netif.gw
Harald Hoyer ee76a0
-[ -e /tmp/net.$netif.hostname ]    && . /tmp/net.$netif.hostname
Harald Hoyer ee76a0
-[ -e /tmp/net.$netif.resolv.conf ] && cp -f /tmp/net.$netif.resolv.conf /etc/resolv.conf
Harald Hoyer ee76a0
-
Harald Hoyer ee76a0
-# Load interface options
Harald Hoyer ee76a0
-[ -e /tmp/net.$netif.override ] && . /tmp/net.$netif.override
Harald Hoyer ee76a0
-[ -e /tmp/dhclient.$netif.dhcpopts ] && . /tmp/dhclient.$netif.dhcpopts
Harald Hoyer ee76a0
-
Harald Hoyer ee76a0
-# Handle STP Timeout: arping the default router if root server is
Harald Hoyer ee76a0
-# unknown or not local, or if not available the root server.
Harald Hoyer ee76a0
-# Note: This assumes that if no router is present the
Harald Hoyer ee76a0
-# root server is on the same subnet.
Harald Hoyer ee76a0
-#
Harald Hoyer ee76a0
-# TODO There's some netroot variants that don't (yet) have their
Harald Hoyer ee76a0
-# server-ip netroot
Harald Hoyer ee76a0
-
Harald Hoyer ee76a0
-# Get router IP if set
Harald Hoyer ee76a0
-[ -n "$new_routers" ] && gw_ip=${new_routers%%,*}
Harald Hoyer ee76a0
-[ -n "$gw" ] && gw_ip=$gw
Harald Hoyer ee76a0
-# Get root server IP if set
Harald Hoyer ee76a0
-if [ -n "$netroot" ]; then
Harald Hoyer ee76a0
-    dummy=${netroot#*:}
Harald Hoyer ee76a0
-    dummy=${dummy%%:*}
Harald Hoyer ee76a0
-    case "$dummy" in
Harald Hoyer ee76a0
-        [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*) netroot_ip=$dummy;;
Harald Hoyer ee76a0
-    esac
Harald Hoyer ee76a0
-fi
Harald Hoyer ee76a0
-# Default arping dest to router
Harald Hoyer ee76a0
-dest="$gw_ip"
Harald Hoyer ee76a0
-# Change to arping root server if appropriate
Harald Hoyer ee76a0
-if [ -n "$netroot_ip" ]; then
Harald Hoyer ee76a0
-    if [ -z "$dest" ]; then
Harald Hoyer ee76a0
-         # no gateway so check root server
Harald Hoyer ee76a0
-        dest="$netroot_ip"
Harald Hoyer ee76a0
-    else
Harald Hoyer ee76a0
-        r=$(ip route get "$netroot_ip")
Harald Hoyer ee76a0
-        if ! strstr "$r" ' via ' ; then
Harald Hoyer ee76a0
-            # local root server, so don't arping gateway
Harald Hoyer ee76a0
-            dest="$netroot_ip"
Harald Hoyer ee76a0
-        fi
Harald Hoyer ee76a0
-    fi
Harald Hoyer ee76a0
-fi
Harald Hoyer ee76a0
-if [ -n "$dest" ] && ! arping -q -f -w 60 -I $netif $dest ; then
Harald Hoyer ee76a0
-    dinfo "Resolving $dest via ARP on $netif failed"
Harald Hoyer ee76a0
-fi
Harald Hoyer ee76a0
+setup_net $netif
Harald Hoyer ee76a0
 
Harald Hoyer ee76a0
 # exit in case manually bring up network
Harald Hoyer ee76a0
 [ -n "$2" ] && exit 0
Harald Hoyer ee76a0
 
Harald Hoyer ee76a0
 # Source netroot hooks before we start the handler
Harald Hoyer ee76a0
-source_all $hookdir/netroot
Harald Hoyer ee76a0
+source_hook netroot
Harald Hoyer ee76a0
 
Harald Hoyer ee76a0
 # Run the handler; don't store the root, it may change from device to device
Harald Hoyer ee76a0
 # XXX other variables to export?
Harald Hoyer ee76a0
@@ -149,11 +95,6 @@ if $handler $netif $netroot $NEWROOT; then
Harald Hoyer ee76a0
 else
Harald Hoyer ee76a0
     warn "Mounting root via '$netif' failed"
Harald Hoyer ee76a0
     # If we're trying with multiple interfaces, put that one down.
Harald Hoyer ee76a0
-    # ip down/flush ensures that routeing info goes away as well
Harald Hoyer ee76a0
-    if [ -z "$BOOTDEV" ] ; then
Harald Hoyer ee76a0
-        ip link set $netif down
Harald Hoyer ee76a0
-        ip addr flush dev $netif
Harald Hoyer ee76a0
-        echo "#empty" > /etc/resolv.conf
Harald Hoyer ee76a0
-    fi
Harald Hoyer ee76a0
+    [ -z "$BOOTDEV" ] && ifdown $netif
Harald Hoyer ee76a0
 fi
Harald Hoyer ee76a0
 exit 0
Harald Hoyer ee76a0
diff --git a/modules.d/40network/parse-ip-opts.sh b/modules.d/40network/parse-ip-opts.sh
Harald Hoyer ee76a0
index c97941e..7851329 100755
Harald Hoyer ee76a0
--- a/modules.d/40network/parse-ip-opts.sh
Harald Hoyer ee76a0
+++ b/modules.d/40network/parse-ip-opts.sh
Harald Hoyer ee76a0
@@ -14,7 +14,8 @@
Harald Hoyer ee76a0
 # routing,dns,dhcp-options,etc.
Harald Hoyer ee76a0
 #
Harald Hoyer ee76a0
 
Harald Hoyer ee76a0
-type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
Harald Hoyer ee76a0
+command -v getarg >/dev/null          || . /lib/dracut-lib.sh
Harald Hoyer ee76a0
+command -v ibft_to_cmdline >/dev/null || . /lib/net-lib.sh
Harald Hoyer ee76a0
 
Harald Hoyer ee76a0
 # Check if ip= lines should be used
Harald Hoyer ee76a0
 if getarg ip= >/dev/null ; then
Harald Hoyer ee76a0
@@ -52,50 +53,15 @@ if [ -n "$NEEDBOOTDEV" ] ; then
Harald Hoyer ee76a0
     [ -z "$BOOTDEV" ] && die "Bootdev argument is empty"
Harald Hoyer ee76a0
 fi
Harald Hoyer ee76a0
 
Harald Hoyer ee76a0
-if [ "ibft" = "$(getarg ip=)" ]; then
Harald Hoyer ee76a0
-    modprobe iscsi_ibft
Harald Hoyer ee76a0
-    num=0
Harald Hoyer ee76a0
-    (
Harald Hoyer ee76a0
-	for iface in /sys/firmware/ibft/ethernet*; do
Harald Hoyer ee76a0
-	    [ -e ${iface}/mac ] || continue
Harald Hoyer ee76a0
-            ifname_mac=$(read a < ${iface}/mac; echo $a)
Harald Hoyer ee76a0
-	    [ -z "$ifname_mac" ] && continue
Harald Hoyer ee76a0
-            unset dev
Harald Hoyer ee76a0
-            for ifname in $(getargs ifname=); do
Harald Hoyer ee76a0
-		if strstr "$ifname" "$ifname_mac"; then
Harald Hoyer ee76a0
-		    dev=${ifname%%:*}
Harald Hoyer ee76a0
-                    break
Harald Hoyer ee76a0
-                fi
Harald Hoyer ee76a0
-	    done
Harald Hoyer ee76a0
-            if [ -z "$dev" ]; then
Harald Hoyer ee76a0
-		ifname_if=ibft$num
Harald Hoyer ee76a0
-		num=$(( $num + 1 ))
Harald Hoyer ee76a0
-		echo "ifname=$ifname_if:$ifname_mac"
Harald Hoyer ee76a0
-		dev=$ifname_if
Harald Hoyer ee76a0
-	    fi
Harald Hoyer ee76a0
-
Harald Hoyer ee76a0
-	    dhcp=$(read a < ${iface}/dhcp; echo $a)
Harald Hoyer ee76a0
-	    if [ -n "$dhcp" ]; then
Harald Hoyer ee76a0
-		echo "ip=$dev:dhcp"
Harald Hoyer ee76a0
-	    else
Harald Hoyer ee76a0
-		ip=$(read a < ${iface}/ip-addr; echo $a)
Harald Hoyer ee76a0
-		gw=$(read a < ${iface}/gateway; echo $a)
Harald Hoyer ee76a0
-		mask=$(read a < ${iface}/subnet-mask; echo $a)
Harald Hoyer ee76a0
-		hostname=$(read a < ${iface}/hostname; echo $a)
Harald Hoyer ee76a0
-		echo "ip=$ip::$gw:$mask:$hostname:$dev:none"
Harald Hoyer ee76a0
-	    fi
Harald Hoyer ee76a0
-	done
Harald Hoyer ee76a0
-    ) >> /etc/cmdline
Harald Hoyer ee76a0
-    # reread cmdline
Harald Hoyer ee76a0
-    unset CMDLINE
Harald Hoyer ee76a0
-fi
Harald Hoyer ee76a0
+# If ibft is requested, read ibft vals and write ip=XXX cmdline args
Harald Hoyer ee76a0
+[ "ibft" = "$(getarg ip=)" ] && ibft_to_cmdline
Harald Hoyer ee76a0
 
Harald Hoyer ee76a0
 # Check ip= lines
Harald Hoyer ee76a0
 # XXX Would be nice if we could errorcheck ip addresses here as well
Harald Hoyer ee76a0
 for p in $(getargs ip=); do
Harald Hoyer ee76a0
     ip_to_var $p
Harald Hoyer ee76a0
 
Harald Hoyer ee76a0
-    # skip ibft
Harald Hoyer ee76a0
+    # skip ibft since we did it above
Harald Hoyer ee76a0
     [ "$autoconf" = "ibft" ] && continue
Harald Hoyer ee76a0
 
Harald Hoyer ee76a0
     # We need to have an ip= line for the specified bootdev
Harald Hoyer ee76a0
@@ -111,7 +77,7 @@ for p in $(getargs ip=); do
Harald Hoyer ee76a0
     case $autoconf in
Harald Hoyer ee76a0
         error) die "Error parsing option 'ip=$p'";;
Harald Hoyer ee76a0
         bootp|rarp|both) die "Sorry, ip=$autoconf is currenty unsupported";;
Harald Hoyer ee76a0
-        none|off) \
Harald Hoyer ee76a0
+        none|off)
Harald Hoyer ee76a0
             [ -z "$ip" ] && \
Harald Hoyer ee76a0
             die "For argument 'ip=$p'\nValue '$autoconf' without static configuration does not make sense"
Harald Hoyer ee76a0
             [ -z "$mask" ] && \