Browse Source

Added support of LXC ifaces creation

Dmitry Yu Okunev 6 years ago
parent
commit
538bc44d12
1 changed files with 52 additions and 6 deletions
  1. 52 6
      ipw

+ 52 - 6
ipw

@@ -314,7 +314,7 @@ case "$CMD" in
 		#GETTING INFORMATION ABOUT LXC INTERFACES IN BRIDGES
 
 		if [[ "$LXC_DIR" != "" ]]; then
-			LXC_BRIDGE_SLAVES=($(cat /srv/lxc/*/config | tr -d " \t" | awk -F '=' '{if($1=="lxc.network.link") {BRIDGE=$2}; if($1=="lxc.network.veth.pair") {if(BRIDGE!="") {print $2":"BRIDGE; BRIDGE=""}} }'))
+			LXC_BRIDGE_SLAVES=($(cat "$LXC_DIR"/*/config | tr -d " \t" | awk -F '=' '{if($1=="lxc.network.link") {BRIDGE=$2}; if($1=="lxc.network.veth.pair") {if(BRIDGE!="") {print $2":"BRIDGE; BRIDGE=""}} }'))
 		fi
 		
 		#REMOVING UNNECESSARY D1Q
@@ -562,11 +562,57 @@ case "$CMD" in
 			done
 		done
 
-		# ADDING LXC IFACES TO BRIDGES IF REQUIRED
-		for LXCIF_N_BRIDGE in ${LXC_BRIDGE_SLAVES[@]}; do
-			parse_vlan_subvlan_bridge "$LINE"
-			brctl addif "$BRNAME" "$VLANNAME" 2>/dev/null
-		done
+		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 }}')
+
+				# Skip the container if it's not running on this host
+
+				[[ "$CONTAINER_STATUS" != "RUNNING" ]] &&
+					continue
+
+				# Getting an already set up interfaces
+				unset CONTAINER_CURRENT_IFACES
+				declare -A CONTAINER_CURRENT_IFACES
+				#for IFACE in $(lxc-info -n "$CONTAINER" | awk '{if ($1 == "Link:") print $2 }'); do
+				for IFACE in $(lxc-attach -n "$CONTAINER" -- ls /sys/devices/virtual/net/); do
+					CONTAINER_CURRENT_IFACES["$IFACE"]="$IFACE"
+				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 }'))
+
+				LXC_PID=''
+				for CONTAINER_IF_N_BRIDGE in ${CONTAINER_CONFIG_IFACES[@]}; do
+					WORDS=( ${CONTAINER_IF_N_BRIDGE//[:]/ } )
+					HNAME="${WORDS[0]}"
+					GNAME="${WORDS[1]}"
+
+					# Skip if the interface is already exists
+					[[ "${CONTAINER_CURRENT_IFACES[$GNAME]}" != '' ]] &&
+						continue
+
+					# Getting a pid of container's init
+					[[ "$CONTAINER_PID" == '' ]] &&
+						CONTAINER_PID=$(lxc-info -p -n "$CONTAINER" | awk '{ print $2 }')
+
+					ip link add name "$HNAME" type veth peer name ".c$GNAME"
+					ip link set "$HNAME" up
+					ip link set ".c$GNAME" netns "$CONTAINER_PID"
+					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"
+				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
+		fi
 
 		;;
 	*)