vznetinit-elab.sh 3.47 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/sh
#
# vzinitnet-elab handles all the tasks associated with configuring runtime state
# of Emulab openvz containers.  We unfortunately can't do all this from 
# bootvnodes because the root context "half" of the veth device pair is not
# persistent across starts and stops of the container... so we have to use the
# openvz way to try to get the veths get configured in good time, and to make
# sure that if a user reboots the container, all this stuff still happens 
# even though bootvnodes is not in the path.
#

# first arg can be "init"
OP=$1
# second arg can be "veth"
DEVTYPE=$2
# third arg is the CT0 name of the veth
DEV=$3

CONFIGFILE=/etc/vz/conf/$VEID.conf
if [ ! -f $CONFIGFILE ]; then
    echo "Could not find $CONFIGFILE!"
    exit 1
fi
. $CONFIGFILE

IFCONFIG=/sbin/ifconfig
ROUTE=/sbin/route
28
IP=/sbin/ip
29
BRCTL=/usr/sbin/brctl
30
OVSCTL=/usr/local/bin/ovs-vsctl
31
32
33
34
35
36
37
38
39
40
41
42
43
44

# ELABIFS="veth100.1,br0;veth100.2,brp3"
# ELABBRS="br0:noencap,short;brp3:encap"
# ELABCTRLIP="172.16.1.10"
# ELABCTRLDEV="veth100.999"

if [ -z "$ELABCUSTOM" -o "$ELABCUSTOM" != "yes" ]; then
    exit 0
fi

#
# Control net is a special case:
#
if [ $ELABCTRLDEV = $DEV ]; then
David Johnson's avatar
David Johnson committed
45
    echo "Emulab configuring network for CT$VEID: control net ($ELABCTRLDEV)"
46
    if [ "x$ELABCTRLBR" != "x" ]; then
47
48
49
50
51
	if [ -e /usr/local/var/run/openvswitch/ovsdb-server.pid ]; then
	    $OVSCTL add-port $ELABCTRLBR $ELABCTRLDEV
	else
	    $BRCTL addif $ELABCTRLBR $ELABCTRLDEV
	fi
52
    fi
53
54
55
56
57
58
59
60
    $IFCONFIG $ELABCTRLDEV 2&>1 > /dev/null
    while [ $? -ne 0 ]; do
	echo "Waiting for $ELABCTRLDEV to appear"
	sleep 1
	$IFCONFIG $ELABCTRLDEV 2&>1 > /dev/null
    done
    $IFCONFIG $ELABCTRLDEV 0 up
    echo 1 > /proc/sys/net/ipv4/conf/$ELABCTRLDEV/forwarding
61
62
63
64
65
66
    if [ "x$ELABCTRLBR" = "x" ]; then
	echo 1 > /proc/sys/net/ipv4/conf/$ELABCTRLDEV/proxy_arp
	$ROUTE add -host $ELABCTRLIP dev $ELABCTRLDEV
    else
        $ROUTE add -host $ELABCTRLIP dev $ELABCTRLBR
    fi
67
68
69
    # Hmm, weird race. Sometimes the container starts with its
    # control network not working right. This seems to help. 
    sleep 2
70
71
72
73
74
75
76
77
78
79
    # no point continuing.
    exit 0
fi

#
# Make sure veths are in bridges, and up, fwding, etc
#
echo "$ELABIFS" | sed -e 's/;/\n/g' | \
    while read iface; \
    do \
80
81
82
        _if=`echo "$iface" | sed -r -e 's/([^,]*),[^,]*,[^,]*/\1/'`
        _br=`echo "$iface" | sed -r -e 's/[^,]*,([^,]*),[^,]*/\1/'`
        _sc=`echo "$iface" | sed -r -e 's/[^,]*,[^,]*,([^,]*)/\1/'`
83
84

	if [ $_if = $DEV ]; then
David Johnson's avatar
David Johnson committed
85
	    echo "Emulab configuring network for CT$VEID: exp net ($_if)"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
86
    	    if [ "x$_br" != "x" ]; then
87
88
89
90
91
		if [ -e /usr/local/var/run/openvswitch/ovsdb-server.pid ]; then
		    $OVSCTL add-port $_br $_if
		else
	            $BRCTL addif $_br $_if
		fi
Leigh B. Stoller's avatar
Leigh B. Stoller committed
92
	    fi
93
94
95
96
97
98
99
100
101
102
	    $IFCONFIG $_if 2&>1 > /dev/null
	    while [ $? -ne 0 ]; do
		echo "Waiting for $_if to appear"
		sleep 1
		$IFCONFIG $_if 2&>1 > /dev/null
	    done

	    $IFCONFIG $_if 0 up
	    echo 1 > /proc/sys/net/ipv4/conf/$_if/forwarding
	    echo 1 > /proc/sys/net/ipv4/conf/$_if/proxy_arp
103
	    $_sc
104
105
106
	fi
    done

Leigh B. Stoller's avatar
Leigh B. Stoller committed
107
108
109
#
# Get the routes, as for tunnels. This is not a workable approach.
#
110
111
112
113
114
115
if [ "x$ELABROUTES" != "x" ]; then
    echo "$ELABROUTES" | sed -e 's/;/\n/g' | \
	while read route; \
	do \
            _if=`echo "$route" | sed -r -e 's/([^,]*),[^,]*,[^,]*/\1/'`
            _rt=`echo "$route" | sed -r -e 's/[^,]*,([^,]*),[^,]*/\1/'`
Leigh B. Stoller's avatar
Leigh B. Stoller committed
116

117
118
	    if [ $_if = $DEV ]; then
		echo "Emulab configuring route for CT$VEID: exp net ($_if)"
119
		$IP route replace default via $_rt dev $_if onlink table $ROUTETABLE
120
	    fi
Leigh B. Stoller's avatar
Leigh B. Stoller committed
121
    done
122
fi
Leigh B. Stoller's avatar
Leigh B. Stoller committed
123

124
exit 0