wit-network-config/files/qemu-ifup

53 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
set -Eeuo pipefail
IFACE=$1
if [ ${#IFACE} -gt 14 ]; then
echo "interface name too long, mac 14 char: $IFACE"
exit 10
fi
if [ -e /etc/libvirt/hooks/$IFACE ]
then
source /etc/libvirt/hooks/$IFACE
else
eval $(
curl --fail --connect-timeout 5 -s http://stackapi.usw3.admin.stage.wit.com:4000/network_interface?interface_name=$IFACE && \
curl --fail --connect-timeout 5 -s http://stackapi.usw3.admin.stage.wit.com:4001/network_interface?interface_name=$IFACE
)
CLUSTER=${VNI:-""}
fi
if ! [[ $CLUSTER =~ ^[0-9]+$ ]]; then
echo "CLUSTER '$CLUSTER' seems not to be valid"
exit 10
fi
LOOPBACKIP=$(ip -4 addr show dev lo | grep -Po 'inet \K[\d.]+' | grep -v "^127.0.0.1$")
BRIDGE=br${CLUSTER}
VIF=vxlan${CLUSTER}
[ -z ${PARENTIF:=""} ] || VIF=vlan${CLUSTER} ### if we have a parentif set we're just going to handle it as a legacy old school vlan
ip link set up ${IFACE}
if ! ip link show dev ${VIF} &>/dev/null; then
if [ -z ${PARENTIF:=""} ]
then
ip link add ${VIF} type vxlan id ${CLUSTER} dstport 4789 local ${LOOPBACKIP} nolearning
else
ip link add link ${PARENTIF} name ${VIF} type vlan protocol 802.1q id ${CLUSTER}
fi
ip link set up ${VIF}
fi
if ! ip link show dev ${BRIDGE} &>/dev/null; then
brctl addbr ${BRIDGE}
brctl stp ${BRIDGE} off
brctl addif ${BRIDGE} ${VIF}
ip link set up dev ${BRIDGE}
bridge vlan del dev ${BRIDGE} vid 1 self
echo 1 >/sys/class/net/${BRIDGE}/bridge/vlan_filtering
fi
brctl addif ${BRIDGE} ${IFACE}