netns.sh revision 764d4be7
1#!/usr/bin/env bash
2
3# Copyright (c) 2016 Cisco and/or its affiliates.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at:
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16if [ $USER != "root" ] ; then
17    #echo "Restarting script with sudo..."
18    sudo $0 ${*}
19    exit
20fi
21
22cone_IP=$1
23cone_GW=$2
24ctwo_IP=$3
25ctwo_GW=$4
26
27containers=( cone ctwo )
28idx=1
29
30# Restart VPP so there is no config
31service vpp restart
32
33for cntr in "${containers[@]}"
34do
35    #stop the container
36    echo "stopping container $cntr"
37    sudo lxc-stop -n $cntr
38done
39
40# LXC gives backend interfaces horrible names, give them a better name.
41function rename_veth_interface() {
42
43    local cntr="$1"
44    local nifname="$2"
45
46    ifr_index=`sudo lxc-attach -n $cntr -- ip -o link | tail -n 1 | awk -F : '{print $1}'`
47    ifr_index=$((ifr_index+1))
48
49    for dir in /sys/class/net/*/
50    do
51        ifindex=`cat $dir/ifindex`
52        if [ $ifindex == $ifr_index ]
53            then ifname=`basename $dir`
54        fi
55    done
56
57    sudo ip link set $ifname down
58    sudo ip link set $ifname name $nifname
59    sudo ip link set $nifname up
60}
61
62for cntr in "${containers[@]}"
63do
64    cip=$cntr\_IP
65    cgw=$cntr\_GW
66
67    #start the container
68    sudo lxc-start -n $cntr -d
69
70    #give the backend of our veth a sensible name.
71    rename_veth_interface $cntr "link"$idx
72
73    #start vpp in the container.
74    #sudo lxc-attach -n $cntr service vpp start
75
76    #setup the inteface in the container 
77    sudo lxc-attach -n $cntr -- \
78        bash -c "
79        ip link set dev lo up
80        ip addr add ${!cip} dev veth_link1
81        ip link set dev veth_link1 up
82        ip route add default via ${!cgw} dev veth_link1"
83
84    idx=$((idx+1))
85done
86