Browse Source

Adopted to malfurion

Dmitry Yu Okunev 7 years ago
parent
commit
c6d6dfa59d
1 changed files with 54 additions and 33 deletions
  1. 54 33
      ipw

+ 54 - 33
ipw

@@ -76,6 +76,7 @@ parse_vlan_subvlan_bridge() {
 
 	if [[ "${VLAN_N_BRIDGE:0:1}" = ":" ]]; then 
 		VLAN=""
+		SUBVLAN=""
 		VLANNAME=""
 		BRNAME="${VLAN_N_BRIDGE:1}"
 	else
@@ -103,10 +104,15 @@ parse_vlan_subvlan_bridge() {
 	return;
 }
 
+declare -A get_brname_byslave_assoc
+for VLAN_N_BRIDGE in $(find /sys/devices/virtual/net -wholename '*/brif/*' | awk -F '/' '{print $8":"$6}'); do
+	oldIFS="$IFS"; IFS=':'; words=($VLAN_N_BRIDGE); IFS="$oldIFS"
+	get_brname_byslave_assoc[${words[0]}]=${words[1]}
+done
 get_brname_byslave() {
 	SLAVE="$1"; shift
 
-	find /sys/devices/virtual/net -wholename '*/brif/'"$SLAVE" | awk -F '/' '{print $6}'
+	echo ${get_brname_byslave_assoc[$SLAVE]}
 }
 
 get_vlan_n_bridges() {
@@ -115,6 +121,12 @@ get_vlan_n_bridges() {
 	find /sys/devices/virtual/net -wholename '*/brif/*'      | awk -F '/' '{print $8":"$6}'
 }
 
+iface_isexists() {
+	IFACE="$1"; shift;
+	[ -e /sys/devices/virtual/net/"$IFACE" ]
+	return $?
+}
+
 case "$CMD" in
 	down)
 		IPW_CONFIG_PATH=/dev/null
@@ -326,6 +338,29 @@ case "$CMD" in
 
 		#REMOVING UNNECESSARY BRIDGES AND VLANS
 
+		declare -A REQUIRED_FULLNAME_HASH
+		declare -A REQUIRED_BRNAME_HASH
+		declare -A REQUIRED_IFACENAME_HASH
+
+		for D1Q_IFACE_TMP in ${D1Q_IFACES[@]}; do #we should keep slave ifaces
+			VLAN_N_BRIDGE_VARIABLE="${D1Q_IFACE_TMP}_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}"
+				REQUIRED_FULLNAME_HASH["$D1Q_IFACE_TMP"."$VLAN_N_BRIDGE"]="$D1Q_IFACE_TMP"."$VLAN_N_BRIDGE"
+				if [[ "$BRNAME" != "" ]]; then
+					REQUIRED_BRNAME_HASH["$BRNAME"]="$BRNAME"
+				fi
+				if [[ "$VLANNAME" != "" ]]; then
+					REQUIRED_IFACENAME_HASH["$VLANNAME"]="$VLANNAME"
+					REQUIRED_IFACENAME_HASH["$D1Q_IFACE_TMP"."$VLAN"]="$D1Q_IFACE_TMP"."$VLAN" 
+				fi
+			done
+		done
+		for LXCIF_N_BRIDGE in ${LXC_BRIDGE_SLAVES[@]}; do
+			REQUIRED_FULLNAME_HASH["$LXCIF_N_BRIDGE"]="$LXCIF_N_BRIDGE"
+		done
+
 		OLD_VLAN_N_BRIDGES=(`get_vlan_n_bridges`)
 		for LINE in ${OLD_VLAN_N_BRIDGES[@]}; do
 			#echo ITERATION
@@ -341,34 +376,21 @@ case "$CMD" in
 			BRIDGE_IN_USE=0
 			IFACE_IN_USE=0
 			VLAN_N_BRIDGE_IN_USE=0
-			for D1Q_IFACE_TMP in ${D1Q_IFACES[@]}; do #we should keep slave ifaces
-				VLAN_N_BRIDGE_VARIABLE="${D1Q_IFACE_TMP}_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 [[ "$LINE" = "$D1Q_IFACE_TMP"."$VLAN_N_BRIDGE" ]]; then
-						BRIDGE_IN_USE=1
-						IFACE_IN_USE=1
-						VLAN_N_BRIDGE_IN_USE=1
-						break 2
-					fi
-					if [[ "$OLD_BRNAME" = "$BRNAME" ]]; then
+			if [[ "${REQUIRED_FULLNAME_HASH[$LINE]}" != "" ]]; then
+				BRIDGE_IN_USE=1
+				IFACE_IN_USE=1
+				VLAN_N_BRIDGE_IN_USE=1
+			else
+				[[ "$OLD_BRNAME" != "" ]] &&
+					if [[ "${REQUIRED_BRNAME_HASH[$OLD_BRNAME]}" != "" ]]; then
 						BRIDGE_IN_USE=1
 					fi
-					if [[ "$OLD_VLANNAME" = "$VLANNAME" ]]; then
-						IFACE_IN_USE=1
-					fi
-					if [[ "$OLD_VLANNAME" = "$D1Q_IFACE_TMP"."$VLAN" ]]; then
+				[[ "$OLD_VLANNAME" != "" ]] &&
+					if [[ "${REQUIRED_IFACENAME_HASH[$OLD_VLANNAME]}" != "" ]]; then
 						IFACE_IN_USE=1
 					fi
-				done
-			done
-			for LXCIF_N_BRIDGE in ${LXC_BRIDGE_SLAVES[@]}; do
-				if [[ "$LINE" = "$LXCIF_N_BRIDGE" ]]; then
-					IFACE_IN_USE=1
-					break
-				fi
-			done
+			fi
+				
 			if [[ "$BRIDGE_IN_USE" = 0 ]]; then
 				[[ "$OLD_BRNAME" != '' ]] &&
 					ip link delete "$OLD_BRNAME"
@@ -451,7 +473,7 @@ case "$CMD" in
 			VLAN_N_BRIDGE_VARIABLE="${D1Q_IFACE}_VLAN_N_BRIDGES"
 			VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}[@]"
 	
-			#CREATING OF NECESSARY BRIDGES AND VLANS
+			#CREATING NECESSARY BRIDGES AND VLANS
 
 			for VLAN_N_BRIDGE in "${!VLAN_N_BRIDGE_VARIABLE}"; do
 				##
@@ -472,7 +494,7 @@ case "$CMD" in
 
 				#VLAN STUFF
 				if  [[ "$VLAN" != "" ]]; then
-					if ! ip link show "$FULL_VLAN_NAME" >/dev/null 2>/dev/null; then #should we create a new vlan?
+					if ! iface_isexists "$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?
@@ -481,7 +503,7 @@ case "$CMD" in
 							#echo FULL_PARVLAN_NAME $FULL_PARVLAN_NAME
 							#echo D1Q_IFACE $D1Q_IFACE 
 							##
-							if ! ip link show "$FULL_PARVLAN_NAME" >/dev/null 2>/dev/null; then #should we create a new parent vlan?
+							if ! iface_isexists "$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
 								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"
@@ -509,7 +531,7 @@ case "$CMD" in
 				if [[ "$BRNAME" != "" ]]; then
 
 					#if [[ "`brctl show | grep "$BRNAME"`" = "" ]]; then #do this bridge exists?
-					if ! ip link show "$BRNAME" >/dev/null 2>/dev/null; then #do this bridge exists?
+					if ! iface_isexists "$BRNAME"; then #do this bridge exists?
 						#brctl addbr "$BRNAME"
 						#echo "ip link add "$BRNAME" type bridge"
 						ip link add "$BRNAME" type bridge
@@ -544,7 +566,8 @@ case "$CMD" in
 					IP_CMDS_VARIABLE="${BRNAME//[.]/_}_IP"
 					IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}[@]"
 					for IP_CMD in "${!IP_CMDS_VARIABLE}"; do
-						ip $IP_CMD dev "$BRNAME"
+						[[ "$IP_CMD" != "" ]] &&
+							ip $IP_CMD dev "$BRNAME"
 					done
 				else
 					ip addr flush dev "$VLANNAME"
@@ -640,7 +663,7 @@ for D1Q_IFACE in ${D1Q_IFACES[@]}; do
 			
 		fi
 
-		printf "br: %-10s vlan:%4s subvlan:%4s\n" "$BRNAME" "$VLAN" "$SUBVLAN"
+		printf "br: %-16s vlan:%5s subvlan:%5s\n" "$BRNAME" "$VLAN" "$SUBVLAN"
 	done
 	newconfig_push 1 ")"
 
@@ -652,8 +675,6 @@ newconfig_popallwrite 1
 newconfig_popallwrite 2
 newconfig_popallwrite 3
 
-echo ${D1Q_IFACES[@]}
-
 #[[ $NEWCONFIG -ne 0 ]] && newconfig_commit
 
 exit 0