Browse Source

Speed up (added support of "IPW_FAST" option)

Dmitry Yu Okunev 6 years ago
parent
commit
8d65c094c6
1 changed files with 36 additions and 9 deletions
  1. 36 9
      ipw

+ 36 - 9
ipw

@@ -129,12 +129,32 @@ iface_isexists() {
 	return $?
 }
 
+IPW_FAST="${IPW_FAST:-0}"
+
+if [[ "$IPW_FAST" -ne '0' ]]; then
+	declare -A IFACE_DETAILS
+	oldIFS="$IFS"; IFS="\n"
+	for LINE in $(ip -o address show | tr -d ':' | grep -v '<'); do
+		WORDS=( ${LINE//[:]/ } )
+
+		[[ "${WORDS[2]}" = 'inet' ]]
+			IFACE_DETAILS["${WORDS[1]}-primary-ipv4address"]="${WORDS[3]}"
+	done
+fi
+IFS="$oldIFS"
 ip_cmds() {
 	_IFACE="$1"; shift
 
-	ip addr flush dev "$_IFACE"
 	IP_CMDS_VARIABLE="${_IFACE//[.]/_}_IP"
 	IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}[@]"
+
+	[[ "$IPW_FAST" -ne 0 ]] &&
+		# Don't do anything if the interface should be "empty" and it's already is
+		[[ "${!IP_CMDS_VARIABLE}" = '' ]] &&
+			[[ "${IFACE_DETAILS[${_IFACE}-primary-ipv4address]}" != '' ]]
+				return
+
+	ip addr flush dev "$_IFACE"
 	for IP_CMD in "${!IP_CMDS_VARIABLE}"; do
 		[[ "$IP_CMD" != "" ]] &&
 			ip $IP_CMD dev "$_IFACE"
@@ -565,8 +585,9 @@ case "$CMD" in
 		if [[ "$LXC_DIR" != "" ]]; then
 
 			# CREATING LXC IFACES
-			for CONTAINER in $(cat "$LXC_DIR"/*/config | tr -d " \t" | awk -F '=' '{if($1=="lxc.utsname") print $2}'); do
-				CONTAINER_STATUS=$(lxc-info -n "$CONTAINER" | awk '{if ($1 == "State:") { print $2; exit }}')
+			#for CONTAINER in $(cat "$LXC_DIR"/*/config | tr -d " \t" | awk -F '=' '{if($1=="lxc.utsname") print $2}'); do
+			for CONTAINER in $(ls "$LXC_DIR"/); do
+				CONTAINER_STATUS=$(lxc-info -n "$CONTAINER" 2>/dev/null | awk '{if ($1 == "State:") { print $2; exit }}')
 
 				# Skip the container if it's not running on this host
 
@@ -582,13 +603,14 @@ case "$CMD" in
 				done
 
 				# Getting an interfaces that _should_ be set up
-				CONTAINER_CONFIG_IFACES=($(cat "$LXC_DIR"/"$CONTAINER"/config | tr -d " \t" | awk -F '=' '{if($1=="lxc.network.veth.pair") {HNAME=$2}; if($1=="lxc.network.name") {GNAME=$2}; if ($1 == "lxc.network.type" && HNAME!="" && GNAME!="") {print HNAME":"GNAME; HNAME=""; GNAME=""} } END { if(HNAME!="" && GNAME!="") print HNAME":"GNAME }'))
+				CONTAINER_CONFIG_IFACES=($(cat "$LXC_DIR"/"$CONTAINER"/config | tr -d " \t" | awk -F '=' '{if($1=="lxc.network.veth.pair") {HNAME=$2}; if($1=="lxc.network.name") {GNAME=$2}; if ($1 == "lxc.network.link") {BRIDGE=$2}; if ($1 == "lxc.network.type" && HNAME!="" && GNAME!="") {print HNAME":"GNAME":"BRIDGE; HNAME=""; GNAME=""; BRIDGE=""} } END { if(HNAME!="" && GNAME!="") print HNAME":"GNAME":"BRIDGE }'))
 
 				LXC_PID=''
 				for CONTAINER_IF_N_BRIDGE in ${CONTAINER_CONFIG_IFACES[@]}; do
 					WORDS=( ${CONTAINER_IF_N_BRIDGE//[:]/ } )
 					HNAME="${WORDS[0]}"
 					GNAME="${WORDS[1]}"
+					BRIDGE="${WORDS[2]}"
 
 					# Skip if the interface is already exists
 					[[ "${CONTAINER_CURRENT_IFACES[$GNAME]}" != '' ]] &&
@@ -604,14 +626,19 @@ case "$CMD" in
 					lxc-attach -n "$CONTAINER" ip link set ".c$GNAME" name "$GNAME"
 					lxc-attach -n "$CONTAINER" ip link set "$GNAME" up
 					lxc-attach -n "$CONTAINER" ifup "$GNAME"
+
+					[[ "$BRIDGE" != '' ]] &&
+						[[ "$IPW_FAST" -ne 0 ]] &&
+							brctl addif "$BRIDGE" "$HNAME"
 				done
 			done
 
-			# ADDING LXC IFACES TO BRIDGES IF REQUIRED
-			for LXCIF_N_BRIDGE in ${LXC_BRIDGE_SLAVES[@]}; do
-				parse_vlan_subvlan_bridge "$LXCIF_N_BRIDGE"
-				brctl addif "$BRNAME" "$VLANNAME" 2>/dev/null
-			done
+			[[ "$IPW_FAST" -eq 0 ]] &&
+				# ADDING LXC IFACES TO BRIDGES IF REQUIRED
+				for LXCIF_N_BRIDGE in ${LXC_BRIDGE_SLAVES[@]}; do
+					parse_vlan_subvlan_bridge "$LXCIF_N_BRIDGE"
+					brctl addif "$BRNAME" "$VLANNAME" 2>/dev/null
+				done
 		fi
 
 		;;