File 0133-Allow-multiple-configurations-per-network-interface-.patch of Package dracut.16003

From 114aadff326cb6ac63d2b3926fafe944b1670a62 Mon Sep 17 00:00:00 2001
From: Julian Wolf <juwolf@suse.com>
Date: Thu, 31 Jul 2014 17:11:16 +0200
Subject: Allow multiple configurations per network interface bnc#887906

Signed-off-by: Julian Wolf <juwolf@suse.com>
---
 modules.d/40network/ifup.sh          | 90 +++++++++++++++++++++++-------------
 modules.d/40network/net-genrules.sh  |  2 +-
 modules.d/40network/net-lib.sh       |  4 +-
 modules.d/40network/parse-ip-opts.sh | 14 +++---
 4 files changed, 66 insertions(+), 44 deletions(-)

diff --git a/modules.d/40network/ifup.sh b/modules.d/40network/ifup.sh
index 1b71a3c..1cd1677 100755
--- a/modules.d/40network/ifup.sh
+++ b/modules.d/40network/ifup.sh
@@ -90,6 +90,7 @@ else
 fi
 
 dhcp_apply() {
+    unset IPADDR INTERFACE BROADCAST NETWORK PREFIXLEN ROUTES GATEWAYS HOSTNAME DNSDOMAIN DNSSEARCH DNSSERVERS
     if [ -f /tmp/leaseinfo.${netif}.dhcp.ipv${1:1:1} ]; then
         . /tmp/leaseinfo.${netif}.dhcp.ipv${1:1:1}
     else
@@ -131,23 +132,36 @@ dhcp_apply() {
     [ -n "${HOSTNAME}" ] && hostname "$HOSTNAME"
 
     # If nameserver= has not been specified, use what dhcp provides
-    if [ ! -s /tmp/net.$netif.resolv.conf ]; then
+    if [ ! -s /tmp/net.$netif.resolv.conf.ipv${1:1:1} ]; then
         if [ -n "${DNSDOMAIN}" ]; then
             echo domain "${DNSDOMAIN}"
-        fi >> /tmp/net.$netif.resolv.conf
+        fi >> /tmp/net.$netif.resolv.conf.ipv${1:1:1}
 
         if [ -n "${DNSSEARCH}" ]; then
             echo search "${DNSSEARCH}"
-        fi >> /tmp/net.$netif.resolv.conf
+        fi >> /tmp/net.$netif.resolv.conf.ipv${1:1:1}
 
         if  [ -n "${DNSSERVERS}" ] ; then
             for s in ${DNSSERVERS}; do
                 echo nameserver "$s"
             done
-        fi >> /tmp/net.$netif.resolv.conf
+        fi >> /tmp/net.$netif.resolv.conf.ipv${1:1:1}
+    fi
+    # copy resolv.conf if it doesn't exist yet, modify otherwise
+    if [ -e /tmp/net.$netif.resolv.conf.ipv${1:1:1} ] && [ ! -e /etc/resolv.conf ]; then
+        cp -f /tmp/net.$netif.resolv.conf.ipv${1:1:1} /etc/resolv.conf
+    else
+        if [ -n "$(sed -n '/^search .*$/p' /etc/resolv.conf)" ]; then
+            sed -i "s/\(^search .*\)$/\1 ${DNSSEARCH}/" /etc/resolv.conf
+        else
+            echo search ${DNSSEARCH} >> /etc/resolv.conf
+        fi
+        if  [ -n "${DNSSERVERS}" ] ; then
+            for s in ${DNSSERVERS}; do
+                echo nameserver "$s"
+            done
+        fi >> /etc/resolv.conf
     fi
-    [ -e /tmp/net.$netif.resolv.conf ] && \
-        cp -f /tmp/net.$netif.resolv.conf /etc/resolv.conf
 
     info "DHCP is finished successfully"
     return 0
@@ -175,9 +189,6 @@ do_dhcp() {
         dhclient="wickedd-dhcp6"
     fi
 
-    # Address changed
-    ip $1 addr flush dev "$netif"
-
     if ! iface_has_link $netif; then
         warn "No carrier detected"
         warn "Trying to set $netif up..."
@@ -191,7 +202,6 @@ do_dhcp() {
     $dhclient --test $netif > /tmp/leaseinfo.${netif}.dhcp.ipv${1:1:1}
     dhcp_apply $1 || return $?
 
-    echo $netif > /tmp/net.${netif}.did-setup
     echo $netif > /tmp/setup_net_${netif}.ok
     return 0
 }
@@ -222,6 +232,31 @@ do_ipv6auto() {
 
 # Handle static ip configuration
 do_static() {
+    if [ "$autoconf" = "static" ] &&
+        [ -e /etc/sysconfig/network/ifcfg-${netif} ] ; then
+        # Pull in existing static configuration
+        . /etc/sysconfig/network/ifcfg-${netif}
+
+        # loop over all configurations in ifcfg-$netif (IPADDR*) and apply
+        for conf in ${!IPADDR@}; do
+            ip=${!conf}
+            [ -z "$ip" ] && continue
+            ext=${conf#IPADDR}
+            concat="PREFIXLEN$ext" && [ -n "${!concat}" ] && mtu=${!concat}
+            concat="MTU$ext" && [ -n "${!concat}" ] && mtu=${!concat}
+            concat="REMOTE_IPADDR$ext" && [ -n "${!concat}" ] && server=${!concat}
+            concat="GATEWAY$ext" && [ -n "${!concat}" ] && gw=${!concat}
+            concat="BOOTPROTO$ext" && [ -n "${!concat}" ] && autoconf=${!concat}
+            do_static_setup
+        done
+    else
+        do_static_setup
+    fi
+
+    return 0
+}
+
+do_static_setup() {
     strglobin $ip '*:*:*' && load_ipv6
 
     if ! linkup $netif; then
@@ -258,7 +293,6 @@ if strglobin $ip '*:*:*'; then
         fi
         # Assume /24 prefix for IPv4
         [ -z "$prefix" ] && prefix=24
-        ip addr flush dev $netif
         ip addr add $ip/$prefix ${srv:+peer $srv} brd + dev $netif
     fi
 
@@ -277,8 +311,6 @@ if strglobin $ip '*:*:*'; then
     done
 
     [ -n "$hostname" ] && echo "echo $hostname > /proc/sys/kernel/hostname" > /tmp/net.$netif.hostname
-
-    return 0
 }
 
 # loopback is always handled the same way
@@ -427,6 +459,14 @@ for p in $(getargs ip=); do
     # skip ibft
     [ "$autoconf" = "ibft" ] && continue
 
+    # skip if same configuration appears twice
+    while read line
+    do
+      [ "$line" = "$p" ] && continue 2
+    done < /tmp/net.${netif}.conf
+
+    echo $p >> /tmp/net.${netif}.conf
+
     case "$dev" in
         ??:??:??:??:??:??)  # MAC address
             _dev=$(iface_for_mac $dev)
@@ -443,29 +483,12 @@ for p in $(getargs ip=); do
     [ "$use_bridge" != 'true' ] && \
     [ "$use_vlan" != 'true' ] && continue
 
-    if [ "$autoconf" = "static" ] &&
-        [ -e /etc/sysconfig/network/ifcfg-${netif} ] ; then
-        # Pull in existing static configuration
-        . /etc/sysconfig/network/ifcfg-${netif}
-        ip=${IPADDR}
-        prefix=${PREFIXLEN}
-        mtu=${MTU}
-        server=${REMOTE_IPADDR}
-        gw=${GATEWAY}
-        autoconf=${BOOTPROTO}
-    fi
-
     # setup nameserver
     for s in "$dns1" "$dns2" $(getargs nameserver); do
         [ -n "$s" ] || continue
         echo nameserver $s >> /tmp/net.$netif.resolv.conf
     done
 
-    # Store config for later use
-    for i in ip srv gw mask prefix hostname macaddr dns1 dns2; do
-        eval '[ "$'$i'" ] && echo '$i'="$'$i'"'
-    done > /tmp/net.$netif.override
-
     for autoopt in $(str_replace "$autoconf" "," " "); do
         case $autoopt in
             dhcp4|dhcp|on|any)
@@ -493,8 +516,6 @@ for p in $(getargs ip=); do
             /sbin/netroot $netif
         fi
     fi
-
-    exit 0
 done
 
 # netif isn't the top stack? Then we should exit here.
@@ -523,4 +544,9 @@ if [ ! -e /tmp/net.${netif}.up ]; then
     fi
 fi
 
+if [ -e /tmp/net.${netif}.up ]; then
+    > /tmp/net.$netif.did-setup
+    [ -e /sys/class/net/$netif/address ] && \
+        > /tmp/net.$(cat /sys/class/net/$netif/address).did-setup
+fi
 exit 0
diff --git a/modules.d/40network/net-genrules.sh b/modules.d/40network/net-genrules.sh
index 559b325..843bd98 100755
--- a/modules.d/40network/net-genrules.sh
+++ b/modules.d/40network/net-genrules.sh
@@ -96,7 +96,7 @@ command -v fix_bootif >/dev/null || . /lib/net-lib.sh
         # if you change the name of "91-default-net.rules", also change modules.d/80cms/cmssetup.sh
         if [ "$NEEDNET" = "1" ]; then
             echo "$cond, $runcmd" > /etc/udev/rules.d/91-default-net.rules
-            echo "[ -f /tmp/net.*.did-setup ]" >$hookdir/initqueue/finished/wait-network.sh
+            echo "ls -1 /tmp/net.*.did-setup >/dev/null 2>&1" >$hookdir/initqueue/finished/wait-network.sh
         fi
     fi
 
diff --git a/modules.d/40network/net-lib.sh b/modules.d/40network/net-lib.sh
index d963029..5aa1468 100755
--- a/modules.d/40network/net-lib.sh
+++ b/modules.d/40network/net-lib.sh
@@ -131,6 +131,7 @@ ifdown() {
     ip addr flush dev $netif
     echo "#empty" > /etc/resolv.conf
     rm -f -- /tmp/net.$netif.did-setup
+    rm -f -- /tmp/net.$netif.conf
     [ -e /sys/class/net/$netif/address ] && \
         rm -f -- /tmp/net.$(cat /sys/class/net/$netif/address).did-setup
     # TODO: send "offline" uevent?
@@ -203,9 +204,6 @@ setup_net() {
     fi
     unset layer2
 
-    > /tmp/net.$netif.did-setup
-    [ -e /sys/class/net/$netif/address ] && \
-        > /tmp/net.$(cat /sys/class/net/$netif/address).did-setup
 }
 
 save_netinfo() {
diff --git a/modules.d/40network/parse-ip-opts.sh b/modules.d/40network/parse-ip-opts.sh
index 099a21c..7926b6b 100755
--- a/modules.d/40network/parse-ip-opts.sh
+++ b/modules.d/40network/parse-ip-opts.sh
@@ -83,25 +83,23 @@ for p in $(getargs ip=); do
                     die "Sorry, automatic calculation of netmask is not yet supported"
                 ;;
             auto6);;
-            dhcp|dhcp6|on|any) \
-                [ -n "$NEEDBOOTDEV" ] && [ -z "$dev" ] && \
-                    die "Sorry, 'ip=$p' does not make sense for multiple interface configurations"
-                [ -n "$ip" ] && \
-                    die "For argument 'ip=$p'\nSorry, setting client-ip does not make sense for '$autoopt'"
-                ;;
+            dhcp|dhcp6|on|any);;
             *) die "For argument 'ip=$p'\nSorry, unknown value '$autoopt'";;
         esac
     done
 
+    dup=0
     if [ -n "$dev" ] ; then
         # We don't like duplicate device configs
         if [ -n "$IFACES" ] ; then
             for i in $IFACES ; do
-                [ "$dev" = "$i" ] && die "For argument 'ip=$p'\nDuplication configurations for '$dev'"
+                [ "$dev" = "$i" ] && dup=1 && break
             done
         fi
         # IFACES list for later use
-        IFACES="$IFACES $dev"
+        if [ $dup -eq 0 ]; then
+            IFACES="$IFACES $dev"
+        fi
     fi
 
     # Do we need to check for specific options?
-- 
2.6.6