ipw 13.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
#!/bin/bash

#    Copyright (C) 2013 Artyom A Anikeev <anikeev@ut.mephi.ru> 0xB5385841,
#    Dmitry Yu Okunev <xai@mephi.ru> 0x8E30679C
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

eerror() {
	echo "$@" >&2
}

newconfig_open() {
	echo -n > "$NEWCONFIG_TMPPATH"
}

newconfig_write() {
	LINE="$1"; shift;

	[[ "$NEWCONFIG" -eq 0 ]] && return

	echo "$LINE" >> "$NEWCONFIG_TMPPATH"
}

newconfig_commit() {
	rm -f "$CONFIG_PATH"
	mv "$NEWCONFIG_TMPPATH" "$CONFIG_PATH"
}

newconfig_push() {
	BUFNUM="$1";	shift;
	LINE="$1";	shift;

	BUF[$BUFNUM]="${BUF[$BUFNUM]}
$LINE"
}

newconfig_pushpopall() {
	BUFNUMTO="$1";		shift;
	BUFNUMFROM="$1";	shift;

	newconfig_push "$BUFNUMTO" "${BUF[$BUFNUMFROM]}"
	newconfig_popall "$BUFNUMFROM"
}

newconfig_popall() {
	BUFNUM="$1";	shift;

	BUF[$BUFNUM]=""
}

newconfig_popallwrite() {
	BUFNUM="$1";	shift;

	newconfig_write "${BUF[$BUFNUM]}"
	BUF[$BUFNUM]=""
}

parse_vlan_subvlan_bridge() {
	VLAN_N_BRIDGE="$1"; shift

	if [[ "${VLAN_N_BRIDGE:0:1}" = ":" ]]; then 
		VLAN=""
		VLANNAME=""
		BRNAME="${VLAN_N_BRIDGE:1}"
	else
		# Splitting by ":"
		WORDS=( ${VLAN_N_BRIDGE//[:]/ } )

		# The second word defines interface name. If it's empty, "vlan$VLAN.$SUBVLAN" is used.
		VLANNAME="${WORDS[0]}"
		BRNAME="${WORDS[1]}"

		# Splitting by "."
		WORDS=( ${VLANNAME//[.]/ } )

		# The second word defines subvlan (1q-in-1q) if it's used. It's empty if no subvlan is used.
		VLAN="${WORDS[0]}"
		SUBVLAN="${WORDS[1]}"
	fi

	# Checking if no "vlan(.subvlan)?" is set
	if ! [[ "${VLAN_N_BRIDGE: -1}" = ":" ]]; then
		# Setting default values
		[[ "$SUBVLAN" = "" ]] && BRNAME="${BRNAME:-vlan${VLAN}}" || BRNAME="${BRNAME:-vlan${VLAN}.${SUBVLAN}}"
	fi

	return;
}

CONFIG_PATH="./ipw-configsample"

source "$CONFIG_PATH"

NEWCONFIG_TMPPATH="/tmp/.~ipw"


CMD="$1"; shift;

NEWCONFIG=0
case "$CMD" in
	add1qif) #allow to add slave interface in bonding
		NEWCONFIG=1	# we need to update the config after the command

		NEW_D1Q_IFACE="$1"; shift
		NEW_SLAVE="$1"; shift

		# checking for duplicated slaves or d1q_ifaces
		D1Q_IFACE_USED=0
		for D1Q_IFACE in ${D1Q_IFACES[@]}; do
			if [[ "$D1Q_IFACE" = "$NEW_D1Q_IFACE" ]]; then
				D1Q_IFACE_USED=1
			fi
			SLAVES_VARIABLE="${D1Q_IFACE}_SLAVES"
			SLAVES_VARIABLE="${SLAVES_VARIABLE//[.]/_}[@]"
			for SLAVE in "${!SLAVES_VARIABLE}"; do
				if [[ "$SLAVE" = "$NEW_SLAVE" ]]; then
					eerror "This interface have already been enslaved"
					exit -1
				fi
			done
		done

		if [[ $D1Q_IFACE_USED -eq 0 ]]; then
			D1Q_IFACES[ ${#D1Q_IFACES} ]="$NEW_D1Q_IFACE"
		fi

		SLAVES_VARIABLE="${NEW_D1Q_IFACE}_SLAVES"
		SLAVES_VARIABLE="${SLAVES_VARIABLE//[.]/_}"
		eval "${SLAVES_VARIABLE}[\${#${SLAVES_VARIABLE}}]=\"$NEW_SLAVE\""

		;;
	del1qif)
		;;
	addbrif) #allow to add bridge/vlan in bonding
		NEWCONFIG=1	# we need to update the config after the command

		NEW_D1Q_IFACE="$1"; shift
		NEW_VLAN_SUBVLAN="$1"; shift
		NEW_BRIDGE="$1"; shift

		[[ -z $NEW_BRIDGE ]] && NEW_VLAN_N_BRIDGE="${NEW_VLAN_SUBVLAN}" || NEW_VLAN_N_BRIDGE="${NEW_VLAN_SUBVLAN}:${NEW_BRIDGE}"

		D1Q_IFACE_USED=0

		for D1Q_IFACE in ${D1Q_IFACES[@]}; do
			if [[ "$D1Q_IFACE" = "$NEW_D1Q_IFACE" ]]; then
				D1Q_IFACE_USED=1
				break;
			fi
		done

		parse_vlan_subvlan_bridge "${NEW_VLAN_N_BRIDGE}"
		NEW_VLAN=$VLAN
		NEW_SUBVLAN=$SUBVLAN

		if [[ $D1Q_IFACE_USED -eq 0 ]]; then
			eerror "There's no such interface: $NEW_D1Q_IFACE"
			exit -1
		fi

		VLAN_N_BRIDGE_VARIABLE="${NEW_D1Q_IFACE}_VLAN_N_BRIDGES"
		VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}[@]"
		for VLAN_N_BRIDGE in "${!VLAN_N_BRIDGE_VARIABLE}"; do
			parse_vlan_subvlan_bridge "${VLAN_N_BRIDGE}"
 			if [[ "$VLAN" = "$NEW_VLAN" && "$SUBVLAN" = "$NEW_SUBVLAN" ]]; then
				eerror "This vlan is alredy used"
				exit -1
			fi		
		done

		VLAN_N_BRIDGE_VARIABLE="${NEW_D1Q_IFACE}_VLAN_N_BRIDGES"
		VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}"
		eval "${VLAN_N_BRIDGE_VARIABLE}[\${#${VLAN_N_BRIDGE_VARIABLE}}]=\"$NEW_VLAN_N_BRIDGE\""

			
		#D1Q_IFACES[ ${#D1Q_IFACES}]
		
		;;
	delbrif)
		;;
	addbr) #allow to add bridge without interfaces
		NEWCONFIG=1	# we need to update the config after the command

		NEW_D1Q_IFACE="$1"; shift
		NEW_BRIDGE="$1"; shift

		VLAN_N_BRIDGE_VARIABLE="${NEW_D1Q_IFACE}_VLAN_N_BRIDGES"
		VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}"
		eval "${VLAN_N_BRIDGE_VARIABLE}[\${#${VLAN_N_BRIDGE_VARIABLE}}]=\":${NEW_BRIDGE}\""

		;;
	delbr)
		;;
	addipcmd) #allow to add ip commnds for bridges/vlans
		NEWCONFIG=1	# we need to update the config after the command
		NEW_BRIDGE="$1"; shift

		NEW_IP_CMD="$@"

		if [[ "${NEW_BRIDGE}" != "" ]]; then

			# Getting commands for "ip"
			IP_CMDS_VARIABLE="${NEW_BRIDGE//[.]/_}_IP"
			IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}"
			eval "${IP_CMDS_VARIABLE}[\${#${IP_CMDS_VARIABLE}}]=\"$NEW_IP_CMD\""

		fi

		;;
	delipcmd)
		NEWCONFIG=1	# we need to update the config after the command
		NEW_BRIDGE="$1"; shift

		NEW_IP_CMD="$@"

		if [[ "${NEW_BRIDGE}" != "" ]]; then

			IP_CMDS_VARIABLE="${NEW_BRIDGE//[.]/_}_IP"
			IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}"
			i=0
			CMD_founded=0
			for IP_CMD in "${!IP_CMDS_VARIABLE}"; do
				if [[ "$IP_CMD" = "$NEW_IP_CMD" ]]; then
					CMD_founded=1
				else
					TMP[$i]=$IP_CMD
					(( i++ ))
				fi
			done

			if [[ $CMD_founded -eq 0 ]]; then
				eerror "There's no such command"
				exit -1
	
			fi

			i=0
			for IP_CMD in "${!TMP}"; do
				IP_CMDS_VARIABLE[$i]=$IP_CMD
				(( i++ ))
			done

			#eval "${IP_CMDS_VARIABLE}[\${#${IP_CMDS_VARIABLE}}]=\"$NEW_IP_CMD\""
			#echo $IP_CMDS_VARIABLE

		fi	;;
	fix)	#fix all interfaces to config state
		#brctl addif bond0 eth2
		
		for D1Q_IFACE in ${D1Q_IFACES[@]}; do
			SLAVES_VARIABLE="${D1Q_IFACE}_SLAVES"
			SLAVES_VARIABLE="${SLAVES_VARIABLE//[.]/_}[@]"
			for SLAVE in "${!SLAVES_VARIABLE}"; do
				#if [[ ]]; then
				#???TODO:ask bircoph how to ensalve with ip
				#else
				#fi
				echo "There are a place for a bond creating. Coming soon..."
			done

			VLAN_N_BRIDGE_VARIABLE="${D1Q_IFACE}_VLAN_N_BRIDGES"
			VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}[@]"

			#REMOVING UNNECESSARY BRIDGES AND VLANS

			LINE_NUM=1;
			#LINE_NUM=3;#skip lo, etc. Not necessary due to grep master
			#LINE="`pwd | awk '{ printf $1 }'`"
			LINE="`ip link show | grep master | awk '{ if (NR=="'"$LINE_NUM"'") printf $2 $9 "\n"}' | sed -e 's/@.*:/:/g'`"
			##
			echo "LINE <$LINE>"
			echo "LINE_NUM <$LINE_NUM> ip link show | grep master | awk '{ if (NR==\"$LINE_NUM\") printf \$2 \$9 \"\\n\"}' | sed -e 's/@.*:/:/g'"
			##
			while  [[ "$LINE" != "" ]]; do
				parse_vlan_subvlan_bridge "$LINE"
				OLD_BRNAME=$BRNAME
				OLD_VLANNAME=$VLANNAME
				##
				echo "BRNAME $BRNAME"
				echo "VLANNAME $VLANNAME"
				##
				let "LINE_NUM += 1";
				LINE="`ip link show | grep master | awk '{ if (NR=="'"$LINE_NUM"'") printf $2 $9 "\n"}' | sed -e 's/@.*:/:/g'`"
				IS_SLAVE=0
				
				for D1Q_IFACE_TMP in ${D1Q_IFACES[@]}; do #we should keep slave ifaces
					SLAVES_VARIABLE_TMP="${D1Q_IFACE_TMP}_SLAVES"
					SLAVES_VARIABLE_TMP="${SLAVES_VARIABLE_TMP//[.]/_}[@]"			
					for SLAVE_TMP in "${!SLAVES_VARIABLE_TMP}"; do
						if [[ "$SLAVE_TMP" == "$VLANNAME" ]]; then
							IS_SLAVE=1
							break;
						fi
					done
				done

				if [[ "$IS_SLAVE" = 0 ]]; then
					BRIDGE_IN_USE=0
					VLAN_IN_USE=0
					VLAN_N_BRIDGE_IN_USE=0
					for VLAN_N_BRIDGE in "${!VLAN_N_BRIDGE_VARIABLE}"; do
						parse_vlan_subvlan_bridge "${VLAN_N_BRIDGE}"
						if [[ "$LINE" = "$VLAN_N_BRIDGE" ]]; then
							BRIDGE_IN_USE=1
							VLAN_IN_USE=1
							VLAN_N_BRIDGE_IN_USE=1
						elif [[ "$BRNAME" = "$OLD_BRNAME" ]]; then
							BRIDGE_IN_USE=1
						elif [[ "$VLANNAME" = "$OLD_VLANNAME" ]]; then
							VLAN_IN_USE=1
						fi
					done

					if [[ "$VLAN_N_BRIDGE_IN_USE" = 0 ]]; then
						echo "ip link set $OLD_VLANNAME nomaster"
					fi
					if [[ "$VLAN_IN_USE" = 0 ]]; then
						echo "ip link delete $OLD_VLANNAME"
					fi
					if [[ "$BRIDGE_IN_USE" = 0 ]]; then
						echo "ip link delete $OLD_BRNAME"
					fi

				fi
			done
	
			#CREATING OF NECESSARY BRIDGES AND VLANS

			for VLAN_N_BRIDGE in "${!VLAN_N_BRIDGE_VARIABLE}"; do
				##
				#echo $VLAN_N_BRIDGE
				##
				parse_vlan_subvlan_bridge "${VLAN_N_BRIDGE}"
				##
				echo BRNAME $BRNAME
				echo VLANNAME $VLANNAME
				echo VLAN $VLAN
				echo SUBVLAN $SUBVLAN
				##

				FULL_VLAN_NAME="$D1Q_IFACE"."$VLANNAME"
				##
				echo FULL_VLAN_NAME $FULL_VLAN_NAME
				##

				#VLAN STUFF
				if  [[ "$VLAN" != "" ]]; then
					if [[ "`ip link show | grep "$FULL_VLAN_NAME"`" = "" ]]; then #should we create a new vlan?
					#ip link show | grep -q "$FULL_VLAN_NAME"
					#if $?; then #should we create a new vlan?
						if [[ "$SUBVLAN" != "" ]]; then #is it subvlan?
							FULL_PARVLAN_NAME="$D1Q_IFACE"."$VLAN"
							##
							echo FULL_PARVLAN_NAME $FULL_PARVLAN_NAME
							echo D1Q_IFACE $D1Q_IFACE 
							##
							if [[ "`ip link show | grep "$FULL_PARVLAN_NAME"`" = "" ]]; then #should we create a new parent vlan?
								echo "ip link add link $D1Q_IFACE name $FULL_PARVLAN_NAME type vlan id $VLAN" #vlan in bond
								echo "ip link set dev $FULL_PARVLAN_NAME up"
							fi
							echo "ip link add link $FULL_PARVLAN_NAME name $FULL_VLAN_NAME type vlan id $SUBVLAN" #subvlan in vlan
							echo "ip link set dev $FULL_VLAN_NAME up"
						else
							echo "ip link add link $D1Q_IFACE name $FULL_VLAN_NAME type vlan id $VLANNAME" #vlan in bond
							echo "ip link set dev $FULL_VLAN_NAME up"
						fi
					fi
				fi

				#BRIDGE STUFF

				if [[ "$BRNAME" != "" ]]; then

					#if [[ "`brctl show | grep "$BRNAME"`" = "" ]]; then #do this bridge exists?
					if [[ "`ip link show | grep "$BRNAME"`" = "" ]]; then #do this bridge exists?
						#brctl addbr "$BRNAME"
						echo "ip link add "$BRNAME" type bridge"
					fi

					if [[ "$VLAN" != "" ]]; then
						#OLD_BRNAME="`brctl show | grep "$FULL_VLANNAME" | awk '{print $1}'`"
						OLD_BRNAME="`ip link show | grep master | grep "FULL_VLANNAME" | awk '{printf $9}'`"
						if [[ "$OLD_BRNAME" = "" ]]; then #is this vlan nave been already enslaved?
							#brctl addif "$BRNAME" "$FULL_VLANNAME"
							echo "ip link set "$FULL_VLAN_NAME" master "$BRNAME
							echo "ip link set dev $BRNAME up"
						else
							#brctl delif "$OLD_BRNAME" "FULL_VLANNAME"
							echo "ip link set "$FULL_VLAN_NAME" nomaster"
							#brctl addif "$BRNAME" "$FULL_VLANNAME"
							echo "ip link set "$FULL_VLAN_NAME" master "$BRNAME
							echo "ip link set dev $BRNAME up"
						fi
					fi
				fi
			done			
		done

		;;
	*)
		;;
esac

newconfig_open
newconfig_write "#"
newconfig_write ""
newconfig_write "D1Q_IFACES=("

for D1Q_IFACE in ${D1Q_IFACES[@]}; do
	newconfig_write	"	$D1Q_IFACE"

	newconfig_push 128 "${D1Q_IFACE}_SLAVES=("

	haveaslave=0
	SLAVES_VARIABLE="${D1Q_IFACE}_SLAVES"
	SLAVES_VARIABLE="${SLAVES_VARIABLE//[.]/_}[@]"
	for SLAVE in "${!SLAVES_VARIABLE}"; do
		newconfig_push 128 "	$SLAVE";
		haveaslave=1
	done

	newconfig_push 128 ")"

	# If there're no slaves then don't write anything about them
	[[ $haveaslave -ne 0 ]] && newconfig_pushpopall 1 128 || newconfig_popall 128

	VLAN_N_BRIDGE_VARIABLE="${D1Q_IFACE}_VLAN_N_BRIDGES"
	VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}[@]"
	
	newconfig_push 1 "${D1Q_IFACE}_VLAN_N_BRIGES=("
	
	for VLAN_N_BRIDGE in "${!VLAN_N_BRIDGE_VARIABLE}"; do
		newconfig_push 1 "	${VLAN_N_BRIDGE}"

		parse_vlan_subvlan_bridge "${VLAN_N_BRIDGE}"

		# Checking if no "vlan(.subvlan)?" is set
		if [[ "${BRNAME}" != "" ]]; then
			#echo $BRNAME
			# Getting commands for "ip"
			IP_CMDS_VARIABLE="${BRNAME//[.]/_}_IP"
			newconfig_push 128 "${IP_CMDS_VARIABLE}=("
			IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}[@]"
			hasanIP_CMD=0
			for IP_CMD in "${!IP_CMDS_VARIABLE}"; do
				newconfig_push 128 "	\"$IP_CMD\""
				echo ip $IP_CMD dev $BRNAME
				hasanIP_CMD=1
			done

			newconfig_push 128 ")"
			newconfig_push 128 ""

			[[ $hasanIP_CMD -ne 0 ]] && newconfig_pushpopall 2 128 || newconfig_popall 128
		else

			# Getting commands for "ip"
			#echo OK
			IP_CMDS_VARIABLE="vlan${VLANNAME//[.]/_}_IP"
			newconfig_push 128 "${IP_CMDS_VARIABLE}=("
			IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}[@]"
			hasanIP_CMD=0
			for IP_CMD in "${!IP_CMDS_VARIABLE}"; do
				newconfig_push 128 "	\"$IP_CMD\""
				echo ip $IP_CMD dev $VLANNAME
				hasanIP_CMD=1
			done

			newconfig_push 128 ")"
			newconfig_push 128 ""

			[[ $hasanIP_CMD -ne 0 ]] && newconfig_pushpopall 2 128 || newconfig_popall 128
			
		fi

		printf "br: %-10s vlan:%4s subvlan:%4s\n" "$BRNAME" "$VLAN" "$SUBVLAN"
	done
	newconfig_push 1 ")"
	

done

newconfig_write ")"

newconfig_popallwrite 1
newconfig_popallwrite 2
newconfig_popallwrite 3

echo ${D1Q_IFACES[@]}

#[[ $NEWCONFIG -ne 0 ]] && newconfig_commit

exit 0