utils.sh revision c6e27c7f
1#!/bin/bash
2
3# Creates named container from given image.
4#
5# $1 - container name
6# $2 - image name
7#
8function create_container {
9  container_name=$1
10  image_name=$2
11  echo "Creating $container_name from $image_name"
12  docker run -dt --privileged --name=$container_name $image_name
13}
14
15# Starts container
16# and adds container's network namespace
17# to the linux runtime data (/var/run).
18#
19# $1 - container name
20#
21# See:
22# https://platform9.com/blog/container-namespaces-deep-dive-container-networking/
23#
24function start_container {
25  container_name=$1
26  echo "Starting container $container_name"
27
28  # Remove namespace if it was present
29  ip netns del $container_name
30
31  # Start container
32  docker start $container_name
33
34  # Make container's network namespaces accessible using ip netns
35  pid=$(docker inspect -f '{{.State.Pid}}' $container_name)
36  ln -s /proc/$pid/ns/net /var/run/netns/$container_name
37  echo "Container $container_name started sucessfully (pid=$pid)"
38}
39
40# Links two containers using a veth pair.
41#
42# $1 - name of the container A
43# $2 - name of the veth endpoint that belongs to A
44# $3 - name of the veth endpoint that belongs to B
45# $4 - name of the container B
46#
47function create_link {
48  container1=$1
49  if1=$2
50  if2=$3
51  container2=$4
52  echo "Creating link from $container1($if1) to $container2($if2)"
53  ip link add $if1 type veth peer name $if2
54
55  # Move veth endpoints to corresponding namespaces
56  ip link set $if1 netns $container1
57  ip link set $if2 netns $container2
58
59  # Bring interfaces up
60  ip netns exec $container1 ip link set $if1 up
61  ip netns exec $container2 ip link set $if2 up
62
63  # Set interfaces mtu to 9000 to match default VPP mtu values
64  ip netns exec ${container1} ifconfig ${if1} mtu 9000
65  ip netns exec ${container2} ifconfig ${if2} mtu 9000
66  echo "Link created successfully"
67}
68