diff options
author | Marek Gradzki <mgradzki@cisco.com> | 2017-11-24 14:24:53 +0100 |
---|---|---|
committer | Marek Gradzki <mgradzki@cisco.com> | 2017-11-27 07:22:42 +0100 |
commit | fc838c481e6f588906f6282ee1891b4b98ab1779 (patch) | |
tree | c6f4e6854c500692ad08d85f6b9c259d2e8736df /docker/utils.sh | |
parent | 8f481756073847efaa6f54c6482b3850b3cd7535 (diff) |
Various scripts for creating and connecting hc2vpp docker containers
Change-Id: I755a23ba279a84600edfa2ddef89c2fe645c7945
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'docker/utils.sh')
-rwxr-xr-x | docker/utils.sh | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/docker/utils.sh b/docker/utils.sh new file mode 100755 index 000000000..7b4674752 --- /dev/null +++ b/docker/utils.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +# Creates named container from given image. +# +# $1 - container name +# $2 - image name +# +function create_container { + container_name=$1 + image_name=$2 + echo "Creating $container_name from $image_name" + docker run -dt --privileged --name=$container_name $image_name +} + +# Starts container +# and adds container's network namespace +# to the linux runtime data (/var/run). +# +# $1 - container name +# +# See: +# https://platform9.com/blog/container-namespaces-deep-dive-container-networking/ +# +function start_container { + container_name=$1 + echo "Starting container $container_name" + + # Remove namespace if it was present + ip netns del $container_name + + # Start container + docker start $container_name + + # Make container's network namespaces accessible using ip netns + pid=$(docker inspect -f '{{.State.Pid}}' $container_name) + ln -s /proc/$pid/ns/net /var/run/netns/$container_name + echo "Container $container_name started sucessfully (pid=$pid)" +} + +# Links two containers using a veth pair. +# +# $1 - name of the container A +# $2 - name of the veth endpoint that belongs to A +# $3 - name of the veth endpoint that belongs to B +# $4 - name of the container B +# +function create_link { + container1=$1 + if1=$2 + if2=$3 + container2=$4 + echo "Creating link from $container1($if1) to $container2($if2)" + ip link add $if1 type veth peer name $if2 + + # Move veth endpoints to corresponding namespaces + ip link set $if1 netns $container1 + ip link set $if2 netns $container2 + + # Bring interfaces up + ip netns exec $container1 ip link set $if1 up + ip netns exec $container2 ip link set $if2 up + echo "Link created successfully" +} |