From a694edebc0238612ae325401e63f85476ab5df2e Mon Sep 17 00:00:00 2001
From: Marius Tomaschewski <mt@suse.de>
Date: Mon, 31 May 2010 14:07:11 +0200
Subject: [PATCH 1/5] Do not fail when ifup-bonding is called twice

Fixed ifup-bonding to handle ifup called twice correctly,
instead to destroy the bonding (bnc#609809).

Signed-off-by: Marius Tomaschewski <mt@suse.de>
---
 scripts/ifup-bonding |   44 +++++++++++++++++++++++++++++++-------------
 1 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/scripts/ifup-bonding b/scripts/ifup-bonding
index bec1f93..ed23925 100755
--- a/scripts/ifup-bonding
+++ b/scripts/ifup-bonding
@@ -143,7 +143,7 @@ load_bond() {
 		done
 		NIF=`cat "$sysfs_npath/bonding_masters"`
 		if [ -n "$NIF" -a "$NIF" != "$INTERFACE" ] ; then
-			nameif -r $INTERFACE $NIF
+			ip link set "$NIF" name $INTERFACE
 		fi
 	fi
 
@@ -233,6 +233,8 @@ remove_bond () {
 #
 case $ACTION in
 up)
+	[ -d "$sysfs_iface/bonding" ] && created=no || created=yes
+
 	if ! load_bond $INTERFACE $BONDING_MODULE_OPTS ; then
 		logerror "Bonding: could not get interface $INTERFACE"
 		exit $R_NODEV
@@ -247,22 +249,38 @@ up)
 		BONDING_SLAVE=${!BSVAR}
 		[ "x$BONDING_SLAVE" = x ] && continue
 
-		# slave has to be down while it gets added
-		if is_iface_up $BONDING_SLAVE ; then
-			ip link set down dev $BONDING_SLAVE
+		sysfs_slave="$sysfs_npath/$BONDING_SLAVE"
+		if [ ! -d "$sysfs_slave" ] ; then
+			logerror "Slave interface '$BONDING_SLAVE' is not available. Skipped."
+			continue
 		fi
-		echo "+$BONDING_SLAVE" > "$sysfs_iface/bonding/slaves"
-		if [ "$?" -gt 0 ]; then
-			logerror "Could not enslave interface '$BONDING_SLAVE'"
-			if ! is_iface_available "$BONDING_SLAVE" ; then
-				logerror "Slave interface '$BONDING_SLAVE' is not available. Skipped."
+
+		enslaved=no
+		for iface in `cat "$sysfs_iface/bonding/slaves" 2>/dev/null` ;
+		do
+			test "x$iface" = "x$BONDING_SLAVE" && enslaved=yes
+		done
+
+		if [ "$enslaved" = no ] ; then
+			# slave has to be down while it gets added
+			if is_iface_up $BONDING_SLAVE ; then
+				ip link set down dev $BONDING_SLAVE
+			fi
+
+			echo "+$BONDING_SLAVE" > "$sysfs_iface/bonding/slaves"
+			if [ "$?" -gt 0 ]; then
+				logerror "Could not enslave interface '$BONDING_SLAVE'"
+				if [ "$created" = yes ] ; then
+					logerror "Removing bonding interface '$INTERFACE'"
+					ifdown $CONFIG $INTERFACE ${OPTIONS:+-o $OPTIONS}
+					exit $R_ERROR
+				fi
 			else
-				logerror "Removing bonding interface '$INTERFACE'"
-				ifdown $CONFIG $INTERFACE ${OPTIONS:+-o $OPTIONS}
-				exit $R_ERROR
+				message "`printf "    %-9s enslaved interface: %s" \
+					"$INTERFACE" "$BONDING_SLAVE"`"
 			fi
 		else
-			message "`printf "    %-9s enslaved interface: %s" \
+			message "`printf "    %-9s already enslaved interface: %s" \
 				"$INTERFACE" "$BONDING_SLAVE"`"
 		fi
 	done
-- 
1.6.4.2

From 755fdb3e89328d494831f78ff022632f6156c519 Mon Sep 17 00:00:00 2001
From: Marius Tomaschewski <mt@suse.de>
Date: Wed, 2 Jun 2010 14:55:31 +0200
Subject: [PATCH 2/5] ifup-bonding: Do not fail when ifup is called twice

Improved bonding parameter handling (bnc#572367).
Improved failure handling to not to fail when ifup is
executed twice even for invalid parameter (bnc#609809).
---
 scripts/ifup-bonding |   90 +++++++++++++++++++++++++++++++-------------------
 1 files changed, 56 insertions(+), 34 deletions(-)

diff --git a/scripts/ifup-bonding b/scripts/ifup-bonding
index ed23925..c2b921e 100755
--- a/scripts/ifup-bonding
+++ b/scripts/ifup-bonding
@@ -120,8 +120,8 @@ load_bond() {
 	local NIF OPT OPT_NAME OPT_VALUE OLD_OPT_VALUE OLD_OPT_VALUE_2 NOFAIL
 	local INTERFACE
 	NOFAIL=0
-	if [ "$1" == _no_fail_ ] ; then
-		NOFAIL=1
+	if [ "x$1" = "x--no-fail" ] ; then
+		NOFAIL=$1
 		shift
 	fi
 
@@ -161,15 +161,23 @@ load_bond() {
 	# Set options
 	for OPT in $*; do
 		read OPT_NAME OPT_VALUE < <(IFS==; echo $OPT)
-		if [ "$OPT_NAME" == max_bonds ] ; then
-			err_mesg "Don't use option max_bonds."
-			continue
-		fi
-		if [ ! -w "$sysfs_iface/bonding/$OPT_NAME" ] ; then
+		case $OPT_NAME in
+		    # not options -- read-only files providing info
+		    mii_status|ad_aggregator|ad_num_ports|\
+		    ad_actor_key|ad_partner_key|ad_partner_mac)
 			err_mesg "There is no option '$OPT_NAME' for interface '$INTERFACE'."
-			echo "-$INTERFACE" > "$sysfs_npath/bonding_masters"
+			[ "x$NOFAIL" = "x--no-fail" ] && continue
+			remove_bond "$INTERFACE"
 			return 1 # or continue? I guess its better to fail completely
-		fi
+		    ;;
+		    # max_bonds is documented in networking/bonding.txt,
+		    # but is a module option and not in sysfs (any more?),
+                    # slaves should be set via BONDING_SLAVE_<X> variable.
+		    slaves|max_bonds)
+			err_mesg "Don't use $OPT_NAME option."
+			continue
+		    ;;
+		esac
 		# Some options may only be changed if the interface is up and slaves are
 		# already assigned. Others may only be changed if it is down. To avoid
 		# unneccessary error messages or warnings we check first if the option
@@ -177,36 +185,50 @@ load_bond() {
 		# Special case for option 'mode': this sysfs attribute contains two
 		# words. A string describing the mode and the corresponding number. We
 		# have to compare both.
-		read OLD_OPT_VALUE OLD_OPT_VALUE_2 < /sys/class/net/$INTERFACE/bonding/$OPT_NAME
-		if [ "$OPT_NAME" == arp_ip_target ]; then
-		    for a in `cat "$sysfs_iface/bonding/$OPT_NAME"`; do
-			if ! echo "-$a" > "$sysfs_iface/bonding/$OPT_NAME"
+		read -a OLD_OPT_VAL < "$sysfs_iface/bonding/$OPT_NAME"
+		case $OPT_NAME in
+		arp_ip_target)
+		    for a in "${OLD_OPT_VAL[@]}" ; do
+			if ! echo "-$a" 2>/dev/null > "$sysfs_iface/bonding/$OPT_NAME" ;
 			then
 			    err_mesg "Option '$OPT_NAME': can't remove $a"
 			fi
 		    done
 		    for a in ${OPT_VALUE//,/ } ; do
-			if ! echo "+$a" > "$sysfs_iface/bonding/$OPT_NAME" ; then
+			if ! echo "+$a" 2>/dev/null > "$sysfs_iface/bonding/$OPT_NAME" ;
+			then
 			    err_mesg "Option '$OPT_NAME': can't add $a"
 			fi
 		    done
 		    continue
-		fi
-		if [    "$OLD_OPT_VALUE" == "$OPT_VALUE" \
-		     -o \( "$OPT_NAME" == mode -a "$OLD_OPT_VALUE_2" == "$OPT_VALUE" \) \
-		   ] ; then
-			info_mesg "Bonding interface '$INTERFACE':" \
-			          "option '$OPT_NAME' is already set to '$OPT_VALUE'"
+		;;
+		ad_select|arp_validate|fail_over_mac|lacp_rate|mode|xmit_hash_policy)
+		    if [ "$OPT_VALUE" = "${OLD_OPT_VAL[0]}" -o \
+			 "$OPT_VALUE" = "${OLD_OPT_VAL[1]}" ] ; then
+			info_mesg "Bonding interface '$INTERFACE': option" \
+			          " '$OPT_NAME' is already set to '$OPT_VALUE'"
 			continue
-		fi
+		    fi
+		    # else fall through and set it
+		;;
+		miimon|primary|updelay|downdelay|use_carrier|\
+		num_grat_arp|num_unsol_na|arp_interval|*)
+		    if [ "$OPT_VALUE" = "${OLD_OPT_VAL[0]}" ] ; then
+			info_mesg "Bonding interface '$INTERFACE': option" \
+			          " '$OPT_NAME' is already set to '$OPT_VALUE'"
+			continue
+		    fi
+		    # else fall through and set it
+		;;
+		esac
 		info_mesg "Bonding interface '$INTERFACE':" \
 		          "Setting option '$OPT_NAME' to '$OPT_VALUE'"
-		if ! echo "$OPT_VALUE" > "$sysfs_iface/bonding/$OPT_NAME" 2>/dev/null ; then
-			err_mesg "Option '$OPT_NAME' of interface '$INTERFACE' cannot be set to" \
-			         "'$OPT_VALUE'."
+		if ! echo "$OPT_VALUE" 2>/dev/null > "$sysfs_iface/bonding/$OPT_NAME" ;
+		then
+			err_mesg "Can not set '$OPT_NAME' to '$OPT_VALUE' for interface '$INTERFACE'."
 			# Should we continue? Its better to fail if not requested differently
-			test "$NOFAIL" == 1 && continue
-			echo "-$INTERFACE" > "$sysfs_npath/bonding_masters"
+			[ "x$NOFAIL" = "x--no-fail" ] && continue
+			remove_bond "$INTERFACE"
 			return 1
 		fi
 	done
@@ -224,7 +246,7 @@ remove_bond () {
 		return 1 # Interface is not a bonding master
 	fi
 	ip link set down dev $INTERFACE &>/dev/null
-	{ echo "-$INTERFACE" > "$sysfs_npath/bonding_masters" ; } &>/dev/null
+	echo "-$INTERFACE" 2>/dev/null > "$sysfs_npath/bonding_masters"
 }
 
 
@@ -233,10 +255,10 @@ remove_bond () {
 #
 case $ACTION in
 up)
-	[ -d "$sysfs_iface/bonding" ] && created=no || created=yes
+	[ -d "$sysfs_iface" ] && NOFAIL='--no-fail' || NOFAIL=
 
-	if ! load_bond $INTERFACE $BONDING_MODULE_OPTS ; then
-		logerror "Bonding: could not get interface $INTERFACE"
+	if ! load_bond $NOFAIL $INTERFACE $BONDING_MODULE_OPTS ; then
+		logerror "Bonding: could not create interface $INTERFACE"
 		exit $R_NODEV
 	fi
 
@@ -267,10 +289,10 @@ up)
 				ip link set down dev $BONDING_SLAVE
 			fi
 
-			echo "+$BONDING_SLAVE" > "$sysfs_iface/bonding/slaves"
-			if [ "$?" -gt 0 ]; then
+			echo "+$BONDING_SLAVE" 2>/dev/null > "$sysfs_iface/bonding/slaves"
+			if [ $? -gt 0 ]; then
 				logerror "Could not enslave interface '$BONDING_SLAVE'"
-				if [ "$created" = yes ] ; then
+				if [ "x$NOFAIL" = "x" ] ; then
 					logerror "Removing bonding interface '$INTERFACE'"
 					ifdown $CONFIG $INTERFACE ${OPTIONS:+-o $OPTIONS}
 					exit $R_ERROR
@@ -287,7 +309,7 @@ up)
 
 	# Some option have to be changed after enslaving (e.g. primary)
 	# Therefore we call load_bond() a second time
-	load_bond "$INTERFACE" "$BONDING_MODULE_OPTS"
+	load_bond $NOFAIL "$INTERFACE" "$BONDING_MODULE_OPTS"
 ;;
 down)
 	# bonding interface must be up for removing slaves
-- 
1.6.4.2

From 319c6c11320d3487edb7a5736422aeb371f32db2 Mon Sep 17 00:00:00 2001
From: Marius Tomaschewski <mt@suse.de>
Date: Wed, 2 Jun 2010 15:13:07 +0200
Subject: [PATCH 3/5] ifcfg-bonding.5: Updated documentation

Signed-off-by: Marius Tomaschewski <mt@suse.de>
---
 doc/ifcfg-bonding.5 |   21 +++++++++------------
 1 files changed, 9 insertions(+), 12 deletions(-)

diff --git a/doc/ifcfg-bonding.5 b/doc/ifcfg-bonding.5
index bc2ad04..7a4a043 100644
--- a/doc/ifcfg-bonding.5
+++ b/doc/ifcfg-bonding.5
@@ -18,21 +18,18 @@ the usual network settings. But you must add additional variables
 must be set to 'yes' to identify this interface as a bonding interface
 .TP
 .B BONDING_SLAVE_<X>
-here you specify the interface name representing the slave network device.
-.TP
-.B BONDING_OPTS
-contains additional options for ifenslave, this one is optional and not important
-for most cases. For additional information take a look into the documentation
-mentioned at the bottom of this section.
+here you specify the interface name representing the slave network interfaces.
 .TP
 .B BONDING_MODULE_OPTS
-contains additional options for the bonding kernel module. Here you can set
-interface timeouts or working modes ('mode=1' for backup mode). For additional
-information take a look into the documentation mentioned at the bottom.
-
+contains bonding options. Here you can set interface timeouts or working modes
+('mode=active-backup' for backup mode). For additional information take a look
+into the documentation mentioned at the bottom.
+Note, that this options are not passed as parameters to the bonding kernel
+module any more, but set via sysfs interface. This variable will be renamed
+in the feature.
 
 .SH Example 
-Example for a bonding device on eth0 and eth1 using the backup mode
+Example for a bonding interface on eth0 and eth1 using the backup mode
 
 .I ifcfg-bond0
 .nf
@@ -43,7 +40,7 @@ Example for a bonding device on eth0 and eth1 using the backup mode
    BONDING_MASTER='yes'
    BONDING_SLAVE_0='eth0'
    BONDING_SLAVE_1='eth1'
-   BONDING_MODULE_OPTS='mode=1'            # backup mode
+   BONDING_MODULE_OPTS='mode=active-backup miimon=100'
 .fi
 
 .SH Additional Information
-- 
1.6.4.2

