add subnet create

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2025-05-08 00:21:31 +02:00
commit 78d57c1b09
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
2 changed files with 84 additions and 0 deletions

View file

@ -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
}

82
lib/subnet.sh Normal file
View file

@ -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
}