From 78d57c1b097d0692c44b4304846c607e01095f28 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Thu, 8 May 2025 00:21:31 +0200 Subject: [PATCH] add subnet create Signed-off-by: GnomeZworc --- agent.sh | 2 ++ lib/subnet.sh | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 lib/subnet.sh diff --git a/agent.sh b/agent.sh index 3716a28..3b70da3 100644 --- a/agent.sh +++ b/agent.sh @@ -8,6 +8,7 @@ [[ -f ./libs/shflags ]] && . ./libs/shflags || eval "$(curl --silent https://git.g3e.fr/H6N/tools/raw/branch/main/libs/shflags)" . ./lib/vpc.sh +. ./lib/subnet.sh . ./lib/colors.sh DRY_RUN="1" @@ -54,6 +55,7 @@ function start_vm { echo create_vpc "${FLAGS_vpc_name}" + create_subnet "${FLAGS_vpc_name}" "${FLAGS_subnet_name}" "${FLAGS_vxlan_id}" "${local_ip}" "${gateway_ip}" "${subnet}" # create volume # create qemu } diff --git a/lib/subnet.sh b/lib/subnet.sh new file mode 100644 index 0000000..67d69ff --- /dev/null +++ b/lib/subnet.sh @@ -0,0 +1,82 @@ +#!/bin/bash + +. ./lib/db.sh +. ./lib/colors.sh + +function check_subnet_exist { + vpc_name="${1}" + subnet_name="${2}" + vxlan_id="${3}" + + 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 "vxlan-${vxlan_id}" > /dev/null || return 1 + return 0 + +} + +function create_subnet { + vpc_name="${1}" + subnet_name="${2}" + vxlan_id="${3}" + local_ip="${4}" + gateway_ip="${5}" + subnet="${6}" + + check_subnet_exist "${vpc_name}" "${subnet_name}" + [ "$?" -eq "0" ] && \ + { + print_in_color "${COLOR_ORANGE}" "Would have create ${subnet_name}" + } || \ + { + print_in_color "${COLOR_GREEN}" "Create ${subnet_name}" + + print_in_color "${COLOR_GREEN}" " - add ${subnet_name} in db" + 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}" + + 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 + + print_in_color "${COLOR_GREEN}" " - add vxlan" + ip link add "vxlan-${vxlan_id}" type vxlan \ + id "${vxlan_id}" \ + dstport 4789 \ + local "${local_ip}" \ + 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}" + + + print_in_color "${COLOR_GREEN}" " - up interface" + ip link set up dev "veth-${subnet_name}-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}" + + + print_in_color "${COLOR_GREEN}" " - add subnet ip" + ip -n "${vpc_name}" a add "${gateway_ip}" dev "br-${subnet_name}" + print_in_color "${COLOR_GREEN}" " - add subnet route" + ip -n "${vpc_name}" r add "${subnet}" dev "br-${subnet_name}" 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 + + } + return 0 +} \ No newline at end of file