File:  [ELWIX - Embedded LightWeight unIX -] / elwix / config / etc / default / rc.d / 009.aggr_dev.stop
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Tue May 7 20:46:48 2013 UTC (11 years, 2 months ago) by misho
Branches: MAIN
CVS tags: elwix2_8, elwix2_7, elwix2_6, elwix2_3, elwix2_2, elwix2_1, elwix2_0, elwix1_9_mips, elwix1_9, HEAD, ELWIX2_7, ELWIX2_6, ELWIX2_5, ELWIX2_2p0, ELWIX2_1, ELWIX2_0, ELWIX1_9, ELWIX1_8
elwix 1.8

#!/bin/sh
#
# (C)`13 ELWIX network aggregation device appliance
#	by Michael Pounov <misho@elwix.org>
#
# $Id: 009.aggr_dev.stop,v 1.5 2013/05/07 20:46:48 misho Exp $

IFACES=""
LANPORT=""
BRIDGE=""
LAGG=""
LAGGPORTS=""

CLEANUP_FILE="/tmp/cleanup.agg"

select_eth()
{
	local RETIF=""

	IFACES=$(ifconfig -l)

	for i in $IFACES; do
		ISETH=$(ifconfig $i | grep "ether")
		ISVLAN=$(ifconfig $i | grep "vlan")
		if [ -n "$ISETH" -a -z "$ISVLAN" ]; then
			RETIF="$RETIF $i"
		fi
	done

	IFACES=$RETIF
}

cleanup()
{
	echo ">> Emergency destroy clones ..."
	ifconfig $BRIDGE destroy >/dev/null 2>&1
	ifconfig $LAGG destroy >/dev/null 2>&1
	rm -f $CLEANUP_FILE
}

up_lan()
{
	echo "+ Up lan port $1"
	if ! `ifconfig $1 description "LAN port" up`; then
		exit 1
	else
		LANPORT=$1
	fi
}

up_others()
{
	if [ $# -lt 2 ]; then
		echo "Error:: Not enough ethernet interfaces for aggregation!"
		exit 2
	else
		shift
		LAGGPORTS=$*
	fi

	for i in $LAGGPORTS; do
		echo "+ Up aggregation port $i"
		if ! `ifconfig $i description "Aggregation port" up`; then
			exit 2
		fi
	done
}

bring_clones()
{
	rm -f $CLEANUP_FILE

	BRIDGE=$(ifconfig bridge create up)
	if [ -z "$BRIDGE" ]; then
		exit 3
	else
		echo "+ Create link-bridge interface $BRIDGE"
		echo $BRIDGE >>/tmp/cleanup.agg
	fi

	LAGG=$(ifconfig lagg create laggproto roundrobin up)
	if [ -z "$LAGG" ]; then
		ifconfig $BRIDGE destroy >/dev/null 2>&1
		rm -f $CLEANUP_FILE
		exit 3
	else
		echo "+ Create link-aggregation interface $LAGG"
		echo $LAGG >>/tmp/cleanup.agg
	fi
}

join2lagg()
{
	echo "+ Join to aggregation interface $LAGG ports $LAGGPORTS"

	for i in $LAGGPORTS; do
		if ! `ifconfig $LAGG laggport $i`; then
			cleanup
			exit 4
		fi
	done
}

join2bridge()
{
	echo "+ Join to bridge interface $BRIDGE ports $LANPORT $LAGG"

	if ! `ifconfig $BRIDGE addm $LANPORT addm $LAGG`; then
		cleanup
		exit 5
	fi
}

# main()
if [ -n "$1" ]; then
	echo ">> Cleanup cloned interfaces ..."

	if [ -r $CLEANUP_FILE ]; then
		while read line; do
			if ! `ifconfig $line >/dev/null 2>&1`; then
				break
			else
				ifconfig $line destroy >/dev/null 2>&1
			fi
		done <$CLEANUP_FILE

		rm -f $CLEANUP_FILE
	fi
else
	echo ">> Setup aggregation device ..."
	select_eth

	up_lan $IFACES
	up_others $IFACES

	bring_clones
	join2lagg
	join2bridge
fi

echo ">> Done."

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>