Skip to content

Instantly share code, notes, and snippets.

@s4l3h1
Forked from thiagokronig/add_veth_pair.sh
Created May 13, 2017 11:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s4l3h1/b76e8cd679aa0cdd5fb1dde6ee5541c2 to your computer and use it in GitHub Desktop.
Save s4l3h1/b76e8cd679aa0cdd5fb1dde6ee5541c2 to your computer and use it in GitHub Desktop.
Add veth pair to Docker container
# Fortemente baseado em https://github.com/jpetazzo/pipework
function add_veth_pair {
pid=$1 ; ipaddr=$2 ; container_ifname=$3
echo "Add interface $container_ifname with ip $ipaddr to container pid $pid"
local_ifname="v${container_ifname}l${pid}"
guest_ifname="v${container_ifname}g${pid}"
bridge='docker0'
mtu=$(ip link show $bridge | awk '{print $5}')
if ip link show "$local_ifname" >/dev/null 2>&1 ; then
ip link del "$local_ifname"
fi
ip link add name "$local_ifname" mtu "$mtu" type veth peer name "$guest_ifname" mtu "$mtu"
ip link set "$local_ifname" master "$bridge"
ip link set "$local_ifname" up
ip link set "$guest_ifname" netns "$pid"
ip netns exec "$pid" ip link set "$guest_ifname" name "$container_ifname"
ip netns exec "$pid" ip addr add "$ipaddr" dev "$container_ifname"
ip netns exec "$pid" ip link set "$container_ifname" up
ipaddr=$(echo "$ipaddr" | cut -d/ -f1)
ip netns exec "$pid" arping -c 1 -A -I "$container_ifname" "$ipaddr" >/dev/null 2>&1 | :
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment