From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1259807-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id 99FA313835A
	for <garchives@archives.gentoo.org>; Thu, 11 Mar 2021 16:54:25 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id D7FF4E086B;
	Thu, 11 Mar 2021 16:54:24 +0000 (UTC)
Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id AC07EE086B
	for <gentoo-commits@lists.gentoo.org>; Thu, 11 Mar 2021 16:54:24 +0000 (UTC)
Received: from oystercatcher.gentoo.org (unknown [IPv6:2a01:4f8:202:4333:225:90ff:fed9:fc84])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 00AC8335C5A
	for <gentoo-commits@lists.gentoo.org>; Thu, 11 Mar 2021 16:54:23 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id A4840590
	for <gentoo-commits@lists.gentoo.org>; Thu, 11 Mar 2021 16:54:21 +0000 (UTC)
From: "Lars Wendler" <polynomial-c@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Lars Wendler" <polynomial-c@gentoo.org>
Message-ID: <1615481613.2eca000593194ef3bcf099b8bb1003bb5ea4292f.polynomial-c@OpenRC>
Subject: [gentoo-commits] proj/netifrc:master commit in: net/
X-VCS-Repository: proj/netifrc
X-VCS-Files: net/bonding.sh
X-VCS-Directories: net/
X-VCS-Committer: polynomial-c
X-VCS-Committer-Name: Lars Wendler
X-VCS-Revision: 2eca000593194ef3bcf099b8bb1003bb5ea4292f
X-VCS-Branch: master
Date: Thu, 11 Mar 2021 16:54:21 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: e5104251-35ec-4146-91f0-cd82833ef6e6
X-Archives-Hash: b492ede3847de7e87652cff6b1b85a1b

commit:     2eca000593194ef3bcf099b8bb1003bb5ea4292f
Author:     Patrick McLean <chutzpah <AT> gentoo <DOT> org>
AuthorDate: Sat Nov 21 06:52:48 2020 +0000
Commit:     Lars Wendler <polynomial-c <AT> gentoo <DOT> org>
CommitDate: Thu Mar 11 16:53:33 2021 +0000
URL:        https://gitweb.gentoo.org/proj/netifrc.git/commit/?id=2eca0005

net/bonding.sh: Add support for network namespaces

Signed-off-by: Lars Wendler <polynomial-c <AT> gentoo.org>

 net/bonding.sh | 57 +++++++++++++++++++++++++++++++--------------------------
 1 file changed, 31 insertions(+), 26 deletions(-)

diff --git a/net/bonding.sh b/net/bonding.sh
index 880f69b..1757a89 100644
--- a/net/bonding.sh
+++ b/net/bonding.sh
@@ -12,13 +12,18 @@ bonding_depend()
 	if [ ! -d /sys/class/net ]; then
 		program /sbin/ifenslave
 	fi
+
+	local netns
+	eval netns="\$netns_${IFVAR}"
+	[ -n "${netns}" ] && program ip
+	return 0
 }
 
 _config_vars="$_config_vars slaves"
 
 _is_bond()
 {
-	[ -f "/proc/net/bonding/${IFACE}" ]
+	_netns [ -f "/proc/net/bonding/${IFACE}" ]
 }
 
 bonding_pre_start()
@@ -52,7 +57,7 @@ bonding_pre_start()
 	# We can create the interface name we like now, but this
 	# requires sysfs
 	if ! _exists && [ -d /sys/class/net ]; then
-		echo "+${IFACE}" > /sys/class/net/bonding_masters
+		_netns echo "+${IFACE}" \> /sys/class/net/bonding_masters
 	fi
 	_exists true || return 1
 
@@ -68,13 +73,13 @@ bonding_pre_start()
 	# we configure all other options
 	# mode needs to be done before all other options.
 	# miimon needs to be done BEFORE downdelay
-	[ -d /sys/class/net ] && for n in mode miimon; do
+	_netns [ -d /sys/class/net ] && for n in mode miimon; do
 		x=/sys/class/net/"${IFACE}"/bonding/$n
-		[ -f "${x}" ] || continue
+		_netns [ -f "${x}" ] || continue
 		eval s=\$${n}_${IFVAR}
 		if [ -n "${s}" ]; then
 			einfo "Setting ${n}: ${s}"
-			echo "${s}" >"${x}" || \
+			_netns echo "${s}" \>"${x}" || \
 			eerror "Failed to configure $n (${n}_${IFVAR})"
 		fi
 	done
@@ -82,19 +87,19 @@ bonding_pre_start()
 	# With a leading '+' as a prefix.
 	n=arp_ip_target
 	x=/sys/class/net/"${IFACE}"/bonding/$n
-	[ -d /sys/class/net ] && if [ -f "$x" ]; then
+	_netns [ -d /sys/class/net ] && if _netns [ -f "$x" ]; then
 		eval s=\$${n}_${IFVAR}
 		if [ -n "${s}" ]; then
 			for i in $s; do
 				einfo "Adding ${n}: ${i}"
-				echo "+${i/+/}" >"${x}" || \
+				_netns echo "+${i/+/}" \>"${x}" || \
 				eerror "Failed to add $i (${n}_${IFVAR})"
 			done
 		fi
 	fi
 	# Nice and dynamic for remaining options:)
-	[ -d /sys/class/net ] && for x in /sys/class/net/"${IFACE}"/bonding/*; do
-		[ -f "${x}" ] || continue
+	_netns [ -d /sys/class/net ] && for x in $(_netns glob /sys/class/net/"${IFACE}"/bonding/\*); do
+		_netns [ -f "${x}" ] || continue
 		n=${x##*/}
 		# These entries are already handled above.
 		case "$n" in
@@ -106,7 +111,7 @@ bonding_pre_start()
 		eval s=\$${n}_${IFVAR}
 		if [ -n "${s}" ]; then
 			einfo "Setting ${n}: ${s}"
-			echo "${s}" >"${x}" || \
+			_netns echo "${s}" \>"${x}" || \
 			eerror "Failed to configure $n (${n}_${IFVAR})"
 		fi
 	done
@@ -145,9 +150,9 @@ bonding_pre_start()
 		unset oiface
 		eoutdent
 		# subsume (presumably kernel auto-)configured IP
-		if [ -x "$(command -v ip)" ]; then
-			ip link set ${IFACE} up
-			ip address add ${addr} dev ${IFACE}
+		if [ -x "$(command -v ip 2>/dev/null)" ]; then
+			_netns ip link set ${IFACE} up
+			_netns ip address add ${addr} dev ${IFACE}
 		else
 			ifconfig ${IFACE} ${addr} up
 		fi
@@ -171,7 +176,7 @@ bonding_pre_start()
 	# set mtu: ifenslave, kernel
 	# set slave MAC: ifenslave, kernel
 	eoutdent
-	if [ -d /sys/class/net ]; then
+	if _netns [ -d /sys/class/net ]; then
 		sys_bonding_path=/sys/class/net/"${IFACE}"/bonding
 		local oiface
 		oiface=$IFACE
@@ -179,20 +184,20 @@ bonding_pre_start()
 			IFACE=$primary
 			_down
 			IFACE=$oiface
-			echo "+${primary}" >$sys_bonding_path/slaves
-			echo "${primary}" >$sys_bonding_path/primary
+			_netns echo "+${primary}" \> $sys_bonding_path/slaves
+			_netns echo "${primary}" \> $sys_bonding_path/primary
 		fi
 		for s in ${slaves}; do
 			[ "${s}" = "${primary}" ] && continue
-			if ! grep -q ${s} $sys_bonding_path/slaves; then
+			if ! _netns grep -q ${s} $sys_bonding_path/slaves; then
 				IFACE=$s
 				_down
 				IFACE=$oiface
-				echo "+${s}" >$sys_bonding_path/slaves
+				_netns echo "+${s}" \> $sys_bonding_path/slaves
 			fi
 		done
 	else
-		ifenslave "${IFACE}" ${slaves} >/dev/null
+		_netns ifenslave "${IFACE}" ${slaves} >/dev/null
 	fi
 	eend $?
 
@@ -206,7 +211,7 @@ bonding_stop()
 	# Wipe subsumed interface
 	if [ -n "${subsume}" ]; then
 		if [ -x "$(command -v ip)" ]; then
-			ip address flush dev ${subsume}
+			_netns ip address flush dev ${subsume}
 		else
 			ifconfig ${subsume} 0.0.0.0
 		fi
@@ -214,7 +219,7 @@ bonding_stop()
 
 	local slaves= s=
 	slaves=$( \
-		sed -n -e 's/^Slave Interface: //p' "/proc/net/bonding/${IFACE}" \
+		_netns sed -n -e 's/^Slave Interface: //p' "/proc/net/bonding/${IFACE}" \
 		| tr '\n' ' ' \
 	)
 	[ -z "${slaves}" ] && return 0
@@ -224,12 +229,12 @@ bonding_stop()
 	eindent
 	einfo "${slaves}"
 	eoutdent
-	if [ -d /sys/class/net ]; then
+	if _netns [ -d /sys/class/net ]; then
 		for s in ${slaves}; do
-			echo -"${s}" > /sys/class/net/"${IFACE}"/bonding/slaves
+			_netns echo -"${s}" \> /sys/class/net/"${IFACE}"/bonding/slaves
 		done
 	else
-		ifenslave -d "${IFACE}" ${slaves}
+		_netns ifenslave -d "${IFACE}" ${slaves}
 	fi
 
 	# reset all slaves
@@ -244,8 +249,8 @@ bonding_stop()
 
 	_down
 
-	if [ -d /sys/class/net ]; then
-		echo "-${IFACE}" > /sys/class/net/bonding_masters
+	if _netns [ -d /sys/class/net ]; then
+		_netns echo "-${IFACE}" \> /sys/class/net/bonding_masters
 	fi
 
 	eend 0