Created
August 5, 2015 16:54
-
-
Save thiagokronig/99ddd559c0b77f3249d7 to your computer and use it in GitHub Desktop.
Add veth pair to Docker container
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
# 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