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
  • Save s4l3h1/b76e8cd679aa0cdd5fb1dde6ee5541c2 to your computer and use it in GitHub Desktop.
Save s4l3h1/b76e8cd679aa0cdd5fb1dde6ee5541c2 to your computer and use it in GitHub Desktop.

Revisions

  1. @thiagokronig thiagokronig created this gist Aug 5, 2015.
    21 changes: 21 additions & 0 deletions add_veth_pair.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # 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 | :
    }