From 9153ef101179c6ecc5cccbaed012d9fe92bff6bf Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Thu, 8 May 2025 00:49:36 +0200 Subject: [PATCH] fix subnet_name Signed-off-by: GnomeZworc --- lib/subnet.sh | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/lib/subnet.sh b/lib/subnet.sh index c5f3924..8f3c3e3 100644 --- a/lib/subnet.sh +++ b/lib/subnet.sh @@ -7,15 +7,16 @@ function check_subnet_exist { local vpc_name="${1}" local subnet_name="${2}" local vxlan_id="${3}" + local subnet_id="${4}" print_in_color "${COLOR_GREY}" "Check in db if ${subnet_name} exist" check_in_db subnet "${subnet_name};${vpc_name}" [ "$?" -eq "0" ] || return 1 print_in_color "${COLOR_GREY}" "Check in linux if ${subnet_name} exist" - ip link show | grep -E '^[0-9]*:'|sed -e 's/ //g' | cut -d: -f 2 | grep "br-${subnet_name}" > /dev/null || return 1 - ip -n "${vpc_name}" link show | grep -E '^[0-9]*:'|sed -e 's/ //g' | cut -d: -f 2 | grep "br-${subnet_name}" > /dev/null || return 1 - ip link show | grep -E '^[0-9]*:'|sed -e 's/ //g' | cut -d: -f 2 | grep "veth-${subnet_name}-ext" > /dev/null || return 1 - ip -n "${vpc_name}" link show | grep -E '^[0-9]*:'|sed -e 's/ //g' | cut -d: -f 2 | grep "veth-${subnet_name}-int" > /dev/null || return 1 + ip link show | grep -E '^[0-9]*:'|sed -e 's/ //g' | cut -d: -f 2 | grep "br-${subnet_id}" > /dev/null || return 1 + ip -n "${vpc_name}" link show | grep -E '^[0-9]*:'|sed -e 's/ //g' | cut -d: -f 2 | grep "br-${subnet_id}" > /dev/null || return 1 + ip link show | grep -E '^[0-9]*:'|sed -e 's/ //g' | cut -d: -f 2 | grep "veth-${subnet_id}-ext" > /dev/null || return 1 + ip -n "${vpc_name}" link show | grep -E '^[0-9]*:'|sed -e 's/ //g' | cut -d: -f 2 | grep "veth-${subnet_id}-int" > /dev/null || return 1 ip link show | grep -E '^[0-9]*:'|sed -e 's/ //g' | cut -d: -f 2 | grep "vxlan-${vxlan_id}" > /dev/null || return 1 return 0 @@ -28,8 +29,9 @@ function create_subnet { local local_ip="${4}" local gateway_ip="${5}" local subnet="${6}" + local subnet_id="$(echo "${subnet_name}" | cut -d\- -f2)" - check_subnet_exist "${vpc_name}" "${subnet_name}" + check_subnet_exist "${vpc_name}" "${subnet_name}" "${vxlan_id}" "${subnet_id}" [ "$?" -eq "0" ] && \ { print_in_color "${COLOR_ORANGE}" "Would have create ${subnet_name}" @@ -41,13 +43,13 @@ function create_subnet { add_in_db "subnet" "${subnet_name}" "${vpc_name}" "${vxlan_id}" "${local_ip}" "${gateway_ip}" "${subnet}" print_in_color "${COLOR_GREEN}" " - create veth" - ip link add "veth-${subnet_name}-ext" type veth peer name "veth-${subnet_name}-int" netns "${vpc_name}" + ip link add "veth-${subnet_id}-ext" type veth peer name "veth-${subnet_id}-int" netns "${vpc_name}" print_in_color "${COLOR_GREEN}" " - add bridges" - brctl addbr "br-${subnet_name}" - brctl stp "br-${subnet_name}" off - ip netns exec "${vpc_name}" brctl addbr "br-${subnet_name}" - ip netns exec "${vpc_name}" brctl stp "br-${subnet_name}" off + brctl addbr "br-${subnet_id}" + brctl stp "br-${subnet_id}" off + ip netns exec "${vpc_name}" brctl addbr "br-${subnet_id}" + ip netns exec "${vpc_name}" brctl stp "br-${subnet_id}" off print_in_color "${COLOR_GREEN}" " - add vxlan" ip link add "vxlan-${vxlan_id}" type vxlan \ @@ -57,25 +59,25 @@ function create_subnet { nolearning print_in_color "${COLOR_GREEN}" " - add interface in bridge" - brctl addif "br-${subnet_name}" "veth-${subnet_name}-ext" - ip netns exec "${vpc_name}" brctl addif "br-${subnet_name}" "veth-${subnet_name}-int" - brctl addif "br-${subnet_name}" "vxlan-${vxlan_id}" + brctl addif "br-${subnet_id}" "veth-${subnet_id}-ext" + ip netns exec "${vpc_name}" brctl addif "br-${subnet_id}" "veth-${subnet_id}-int" + brctl addif "br-${subnet_id}" "vxlan-${vxlan_id}" print_in_color "${COLOR_GREEN}" " - up interface" - ip link set up dev "veth-${subnet_name}-ext" + ip link set up dev "veth-${subnet_id}-ext" ip link set up dev "vxlan-${vxlan_id}" - ip link set up dev "br-${subnet_name}" - ip -n "${vpc_name}" link set up dev "veth-${subnet_name}-int" - ip -n "${vpc_name}" link set up dev "br-${subnet_name}" + ip link set up dev "br-${subnet_id}" + ip -n "${vpc_name}" link set up dev "veth-${subnet_id}-int" + ip -n "${vpc_name}" link set up dev "br-${subnet_id}" print_in_color "${COLOR_GREEN}" " - add subnet ip" - ip -n "${vpc_name}" a add "${gateway_ip}" dev "br-${subnet_name}" + ip -n "${vpc_name}" a add "${gateway_ip}" dev "br-${subnet_id}" print_in_color "${COLOR_GREEN}" " - add subnet route" - ip -n "${vpc_name}" r add "${subnet}" dev "br-${subnet_name}" scope link + ip -n "${vpc_name}" r add "${subnet}" dev "br-${subnet_id}" scope link print_in_color "${COLOR_GREEN}" " - add subnet firewall" - ebtables -A FORWARD -p arp --arp-op Request --arp-ip-dst "${gateway_ip}" --out-interface "br-${subnet_name}" -j DROP + ebtables -A FORWARD -p arp --arp-op Request --arp-ip-dst "${gateway_ip}" --out-interface "br-${subnet_id}" -j DROP } return 0