#!/bin/bash function exec_command { eval "${1}" } function up_bridge { local vpc="${1}" local bridge="br-${2}" local exec="" if [[ "${vpc}" != "" ]] then exec="ip netns exec ${vpc}" fi exec_command "${exec} brctl addbr ${bridge}" exec_command "${exec} brctl stp ${bridge} off" exec_command "${exec} ip link set up dev ${bridge}" } function up_vxlan { local id="${1}" local local_ip="${2}" local bridge="br-${3}" ip link add "vxlan-${id}" type vxlan \ id "${id}" \ dstport 4789 \ local "${local_ip}" \ nolearning brctl addif "${bridge}" "vxlan-${id}" ip link set up dev "vxlan-${id}" } function up_netns { local netns="${1}" local subnet="${2}" local veth="veth-${subnet}" ip netns add "${netns}" ip link add "${veth}-ext" type veth peer name "${veth}-int" netns ${netns} ip link set up dev "${veth}-ext" ip -n "${netns}" link set up dev "${veth}-int" } INTERFACE="eno1" LOCAL_IP=$(ip a | grep -E "^ .*${INTERFACE}$" | sed 's/ */ /g' | cut -d\ -f 3|cut -d\/ -f1) VXLAN_ID="${2}" SUBNET_NAME="${3}" VPC_NAME="${4}" TAP_NAME="${5}" echo "Create vm subnet" echo " -> interface name : ${INTERFACE}" echo " -> interface ip : ${LOCAL_IP}" echo " -> vxlan id : ${VXLAN_ID}" echo " -> subnet name : ${SUBNET_NAME}" echo " -> vpn : ${VPC_NAME}" echo " -> tapid : ${TAP_NAME}" up_netns "${VPC_NAME}" "${SUBNET_NAME}" up_bridge "" "${SUBNET_NAME}" up_bridge "${VPC_NAME}" "${SUBNET_NAME}" up_vxlan "${VXLAN_ID}" "${LOCAL_IP}" "${SUBNET_NAME}" brctl addif "br-${SUBNET_NAME}" "veth-${SUBNET_NAME}-ext" ip netns exec "${VPC_NAME}" brctl addif "br-${SUBNET_NAME}" "veth-${SUBNET_NAME}-int" ip -n "${VPC_NAME}" tuntap add dev "tap${TAP_NAME}" mode tap ip -n "${VPC_NAME}" link set up dev "tap${TAP_NAME}" ip netns exec "${VPC_NAME}" brctl addif "br-${SUBNET_NAME}" "tap${TAP_NAME}"