Instantly share code, notes, and snippets.

Embed
What would you like to do?
set up vxlan lab environment
#/usr/bin/env sh
set -e
if [ $# -ne 3 ]; then
echo "Usage: $0 bridgename containername containerip"
exit 1
fi
BRIDGE_NAME=$1
CONTAINER_NAME=$2
CONTAINER_IP=$3
VETH_OUT=veth-${RANDOM}${RANDOM}
VETH_IN=veth-${RANDOM}${RANDOM}
IFACE_NAME=eth0
# create a network namespace with specified name
ip netns add "$CONTAINER_NAME"
# create veth peer
ip link add "${VETH_OUT}" type veth peer name "${VETH_IN}"
ip link set dev "$VETH_OUT" master "$BRIDGE_NAME"
ip link set dev "${VETH_OUT}" up
# config container network
ip link set dev ${VETH_IN} netns ${CONTAINER_NAME}
ip netns exec ${CONTAINER_NAME} ip link set lo up
ip netns exec ${CONTAINER_NAME} ip link set ${VETH_IN} name ${IFACE_NAME}
ip netns exec ${CONTAINER_NAME} ip addr add "$CONTAINER_IP" dev ${IFACE_NAME}
ip netns exec ${CONTAINER_NAME} ip link set ${IFACE_NAME} up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment