Last active
April 26, 2025 20:21
-
-
Save cloudnull/3294dae9dd3fbd6d2b1d29ccbb8d59de to your computer and use it in GitHub Desktop.
Create a vxlan mesh on multiple hosts for multiple bridged interfaces to create isolated user networks. The primary use-case here is tenant Isolation with OpenStack Ironic.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # This package needs to be installed before bridging will work. | |
| apt-get update | |
| apt-get install -y bridge-utils | |
| # Define the primary network interface. This is determined by the Gateway interface | |
| # should it not be defined elsewhere. In many cases this interface should NOT be | |
| # the gateway device. If you're building this in an environment like the Rackspace | |
| # Public cloud you likley want this to be the internal network interface on SNET or | |
| # a tenant specific network. | |
| PRIMARY_INTERFACE="${PRIMARY_INTERFACE:=$(ip -o r g 1 | awk '{print $5}')}" | |
| function get_ip { | |
| # Generate a random IP | |
| proposed_addr=$1.$(( ( RANDOM % 254 ) + 1 )) | |
| # See if the IP address is consumed and print if it's not. | |
| ping -c 1 -w 1 ${proposed_addr} 2>&1 > /dev/null && get_ip $1 || echo "${proposed_addr}" | |
| } | |
| function vxlan_mesh_create { | |
| # Create the tunnel interface. | |
| ip link add $1-mesh type vxlan id $(( $2 + 5149 )) group 239.51.50.1 ttl 4 dev $PRIMARY_INTERFACE | |
| # Up the tunnel interface. | |
| ip link set $1-mesh up || true | |
| sysctl -w net.ipv4.conf.$1-mesh.arp_notify=1 | |
| # create the bridge. | |
| brctl addbr br-$1 | |
| brctl stp br-$1 off | |
| # Add the tunnel interface to the bridge. | |
| brctl addif br-$1 $1-mesh | |
| # Set an IP address on the bridge. | |
| ip address add $(get_ip 172.16.$2)/24 dev br-$1 | |
| sysctl -w net.ipv4.conf.br-$1.arp_notify=1 | |
| ip link set br-$1 up | |
| # Broadcast a notification for the interface on the bridge. | |
| ip link set br-$1 address "$(cat /sys/class/net/br-$1/address)" | |
| } | |
| # Ensure the bonds are using an MTU of 9000 | |
| for i in $(cat /sys/class/net/bond0/bonding/slaves); do | |
| ip link set $i mtu 9000 | |
| done | |
| ip link set bond0 mtu 9000 | |
| # Create the mesh. This will create various bridges and space the address | |
| # subnets out in increments of 4. | |
| COUNT=0 | |
| for i in "mgmt" "vlan" "vxlan" "storage"; do | |
| COUNT=$(( COUNT + 4 )) | |
| vxlan_mesh_create $i $COUNT | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment