Browse code

Generalize xen network config

Allow dhcp for IP addresses.
dhclient3 bug workaround.
Refactor code to improve network creation logic.

Change-Id: Ia3e2e65bbe8b68cf4832595cb7c283c3dc84db19

root authored on 2012/01/20 06:28:21
Showing 5 changed files
... ...
@@ -200,14 +200,13 @@ LIBVIRT_TYPE=${LIBVIRT_TYPE:-kvm}
200 200
 # cases unless you are working on multi-zone mode.
201 201
 SCHEDULER=${SCHEDULER:-nova.scheduler.simple.SimpleScheduler}
202 202
 
203
+HOST_IP_IFACE=${HOST_IP_IFACE:-eth0}
203 204
 # Use the eth0 IP unless an explicit is set by ``HOST_IP`` environment variable
204
-if [ ! -n "$HOST_IP" ]; then
205
-    HOST_IP=`LC_ALL=C /sbin/ifconfig eth0 | grep -m 1 'inet addr:'| cut -d: -f2 | awk '{print $1}'`
205
+if [ -z "$HOST_IP" -o "$HOST_IP" == "dhcp" ]; then
206
+    HOST_IP=`LC_ALL=C /sbin/ifconfig ${HOST_IP_IFACE} | grep -m 1 'inet addr:'| cut -d: -f2 | awk '{print $1}'`
206 207
     if [ "$HOST_IP" = "" ]; then
207 208
         echo "Could not determine host ip address."
208
-        echo "If this is not your first run of stack.sh, it is "
209
-        echo "possible that nova moved your eth0 ip address to the FLAT_NETWORK_BRIDGE."
210
-        echo "Please specify your HOST_IP in your localrc."
209
+        echo "Either localrc specified dhcp on ${HOST_IP_IFACE} or defaulted to eth0"
211 210
         exit 1
212 211
     fi
213 212
 fi
... ...
@@ -37,51 +37,85 @@ if ! which git; then
37 37
 fi
38 38
 
39 39
 # Helper to create networks
40
+# Uses echo trickery to return network uuid
40 41
 function create_network() {
41
-    if ! xe network-list | grep bridge | grep -q $1; then
42
-        echo "Creating bridge $1"
43
-        xe network-create name-label=$1
42
+    br=$1
43
+    dev=$2
44
+    vlan=$3
45
+    netname=$4
46
+    if [ -z $br ]
47
+    then
48
+        pif=$(xe pif-list --minimal device=$dev VLAN=$vlan)
49
+        if [ -z $pif ]
50
+        then
51
+            net=$(xe network-create name-label=$netname)
52
+        else
53
+            net=$(xe network-list --minimal PIF-uuids=$pif)
54
+        fi
55
+        echo $net
56
+        return 0
57
+    fi
58
+    if [ ! $(xe network-list --minimal params=bridge | grep -w --only-matching $br) ]
59
+    then
60
+        echo "Specified bridge $br does not exist"
61
+        echo "If you wish to use defaults, please keep the bridge name empty"
62
+        exit 1
63
+    else
64
+        net=$(xe network-list --minimal bridge=$br)
65
+        echo $net
44 66
     fi
45 67
 }
46 68
 
47
-# Create host, vm, mgmt, pub networks
48
-create_network xapi0
49
-create_network $VM_BR
50
-create_network $MGT_BR
51
-create_network $PUB_BR
52
-
53
-# Get the uuid for our physical (public) interface
54
-PIF=`xe pif-list --minimal device=eth0`
69
+function errorcheck() {
70
+    rc=$?
71
+    if [ $rc -ne 0 ]
72
+    then
73
+        exit $rc
74
+    fi
75
+}
55 76
 
56
-# Create networks/bridges for vm and management
57
-VM_NET=`xe network-list --minimal bridge=$VM_BR`
58
-MGT_NET=`xe network-list --minimal bridge=$MGT_BR`
77
+# Create host, vm, mgmt, pub networks
78
+VM_NET=$(create_network "$VM_BR" "$VM_DEV" "$VM_VLAN" "vmbr")
79
+errorcheck
80
+MGT_NET=$(create_network "$MGT_BR" "$MGT_DEV" "$MGT_VLAN" "mgtbr")
81
+errorcheck
82
+PUB_NET=$(create_network "$PUB_BR" "$PUB_DEV" "$PUB_VLAN" "pubbr")
83
+errorcheck
59 84
 
60 85
 # Helper to create vlans
61 86
 function create_vlan() {
62
-    pif=$1
87
+    dev=$1
63 88
     vlan=$2
64 89
     net=$3
65
-    if ! xe vlan-list | grep tag | grep -q $vlan; then
66
-        xe vlan-create pif-uuid=$pif vlan=$vlan network-uuid=$net
90
+    # VLAN -1 refers to no VLAN (physical network)
91
+    if [ $vlan -eq -1 ]
92
+    then
93
+        return
94
+    fi
95
+    if [ -z $(xe vlan-list --minimal tag=$vlan) ]
96
+    then
97
+        pif=$(xe pif-list --minimal network-uuid=$net)
98
+        # We created a brand new network this time
99
+        if [ -z $pif ]
100
+        then
101
+            pif=$(xe pif-list --minimal device=$dev VLAN=-1)
102
+            xe vlan-create pif-uuid=$pif vlan=$vlan network-uuid=$net
103
+        else
104
+            echo "VLAN does not exist but PIF attached to this network"
105
+            echo "How did we reach here?"
106
+            exit 1
107
+        fi
67 108
     fi
68 109
 }
69 110
 
70 111
 # Create vlans for vm and management
71
-create_vlan $PIF $VM_VLAN $VM_NET
72
-create_vlan $PIF $MGT_VLAN $MGT_NET
112
+create_vlan $PUB_DEV $PUB_VLAN $PUB_NET
113
+create_vlan $VM_DEV $VM_VLAN $VM_NET
114
+create_vlan $MGT_DEV $MGT_VLAN $MGT_NET
73 115
 
74 116
 # dom0 ip
75 117
 HOST_IP=${HOST_IP:-`ifconfig xenbr0 | grep "inet addr" | cut -d ":" -f2 | sed "s/ .*//"`}
76 118
 
77
-# Setup host-only nat rules
78
-HOST_NET=169.254.0.0/16
79
-if ! iptables -L -v -t nat | grep -q $HOST_NET; then
80
-    iptables -t nat -A POSTROUTING -s $HOST_NET -j SNAT --to-source $HOST_IP
81
-    iptables -I FORWARD 1 -s $HOST_NET -j ACCEPT
82
-    /etc/init.d/iptables save
83
-fi
84
-
85 119
 # Set up ip forwarding
86 120
 if ! grep -q "FORWARD_IPV4=YES" /etc/sysconfig/network; then
87 121
     # FIXME: This doesn't work on reboot!
... ...
@@ -139,6 +173,15 @@ if [ "$DO_SHUTDOWN" = "1" ]; then
139 139
 fi
140 140
 
141 141
 # Start guest
142
+if [ -z $VM_BR ]; then
143
+    VM_BR=$(xe network-list --minimal uuid=$VM_NET params=bridge)
144
+fi
145
+if [ -z $MGT_BR ]; then
146
+    MGT_BR=$(xe network-list --minimal uuid=$MGT_NET params=bridge)
147
+fi
148
+if [ -z $PUB_BR ]; then
149
+    PUB_BR=$(xe network-list --minimal uuid=$PUB_NET params=bridge)
150
+fi
142 151
 $TOP_DIR/scripts/install-os-vpx.sh -f $XVA -v $VM_BR -m $MGT_BR -p $PUB_BR
143 152
 
144 153
 # If we have copied our ssh credentials, use ssh to monitor while the installation runs
... ...
@@ -62,7 +62,7 @@ XVA_DIR=$TOP_DIR/xvas
62 62
 mkdir -p $XVA_DIR
63 63
 
64 64
 # Path to xva
65
-XVA=$XVA_DIR/$GUEST_NAME.xva 
65
+XVA=$XVA_DIR/$GUEST_NAME.xva
66 66
 
67 67
 # Setup fake grub
68 68
 rm -rf $STAGING_DIR/boot/grub/
... ...
@@ -102,6 +102,8 @@ sed -e "s,@BUILD_NUMBER@,$BUILD_NUMBER,g" -i $OVA
102 102
 
103 103
 # Run devstack on launch
104 104
 cat <<EOF >$STAGING_DIR/etc/rc.local
105
+# network restart required for getting the right gateway
106
+/etc/init.d/networking restart
105 107
 GUEST_PASSWORD=$GUEST_PASSWORD STAGING_DIR=/ DO_TGZ=0 bash /opt/stack/devstack/tools/xen/prepare_guest.sh
106 108
 su -c "/opt/stack/run.sh > /opt/stack/run.sh.log" stack
107 109
 exit 0
... ...
@@ -122,12 +124,36 @@ EOF
122 122
 # Configure the network
123 123
 INTERFACES=$STAGING_DIR/etc/network/interfaces
124 124
 cp $TEMPLATES_DIR/interfaces.in  $INTERFACES
125
-sed -e "s,@ETH1_IP@,$VM_IP,g" -i $INTERFACES
126
-sed -e "s,@ETH1_NETMASK@,$VM_NETMASK,g" -i $INTERFACES
127
-sed -e "s,@ETH2_IP@,$MGT_IP,g" -i $INTERFACES
128
-sed -e "s,@ETH2_NETMASK@,$MGT_NETMASK,g" -i $INTERFACES
129
-sed -e "s,@ETH3_IP@,$PUB_IP,g" -i $INTERFACES
130
-sed -e "s,@ETH3_NETMASK@,$PUB_NETMASK,g" -i $INTERFACES
125
+if [ $VM_IP == "dhcp" ]; then
126
+    echo 'eth1 on dhcp'
127
+    sed -e "s,iface eth1 inet static,iface eth1 inet dhcp,g" -i $INTERFACES
128
+    sed -e '/@ETH1_/d' -i $INTERFACES
129
+else
130
+    sed -e "s,@ETH1_IP@,$VM_IP,g" -i $INTERFACES
131
+    sed -e "s,@ETH1_NETMASK@,$VM_NETMASK,g" -i $INTERFACES
132
+fi
133
+
134
+if [ $MGT_IP == "dhcp" ]; then
135
+    echo 'eth2 on dhcp'
136
+    sed -e "s,iface eth2 inet static,iface eth2 inet dhcp,g" -i $INTERFACES
137
+    sed -e '/@ETH2_/d' -i $INTERFACES
138
+else
139
+    sed -e "s,@ETH2_IP@,$MGT_IP,g" -i $INTERFACES
140
+    sed -e "s,@ETH2_NETMASK@,$MGT_NETMASK,g" -i $INTERFACES
141
+fi
142
+
143
+if [ $PUB_IP == "dhcp" ]; then
144
+    echo 'eth3 on dhcp'
145
+    sed -e "s,iface eth3 inet static,iface eth3 inet dhcp,g" -i $INTERFACES
146
+    sed -e '/@ETH3_/d' -i $INTERFACES
147
+else
148
+    sed -e "s,@ETH3_IP@,$PUB_IP,g" -i $INTERFACES
149
+    sed -e "s,@ETH3_NETMASK@,$PUB_NETMASK,g" -i $INTERFACES
150
+fi
151
+
152
+if [ -h $STAGING_DIR/sbin/dhclient3 ]; then
153
+    rm -f $STAGING_DIR/sbin/dhclient3
154
+fi
131 155
 
132 156
 # Gracefully cp only if source file/dir exists
133 157
 function cp_it {
... ...
@@ -151,7 +177,7 @@ cat <<EOF >$STAGING_DIR/opt/stack/run.sh
151 151
 #!/bin/bash
152 152
 cd /opt/stack/devstack
153 153
 killall screen
154
-UPLOAD_LEGACY_TTY=yes HOST_IP=$PUB_IP VIRT_DRIVER=xenserver FORCE=yes MULTI_HOST=1 $STACKSH_PARAMS ./stack.sh
154
+UPLOAD_LEGACY_TTY=yes HOST_IP=$PUB_IP VIRT_DRIVER=xenserver FORCE=yes MULTI_HOST=1 HOST_IP_IFACE=$HOST_IP_IFACE $STACKSH_PARAMS ./stack.sh
155 155
 EOF
156 156
 chmod 755 $STAGING_DIR/opt/stack/run.sh
157 157
 
... ...
@@ -1,8 +1,15 @@
1 1
 auto lo
2 2
 iface lo inet loopback
3 3
 
4
-auto eth0
5
-iface eth0 inet dhcp
4
+# If eth3 is static, the order should not matter
5
+# and eth0 will have the default gateway. If not,
6
+# we probably want the default gateway to be
7
+# what is on the public interface. Hence changed
8
+# the order here.
9
+auto eth3
10
+iface eth3 inet static
11
+        address @ETH3_IP@
12
+        netmask @ETH3_NETMASK@
6 13
 
7 14
 auto eth1
8 15
 iface eth1 inet static
... ...
@@ -15,7 +22,5 @@ iface eth2 inet static
15 15
         address @ETH2_IP@
16 16
         netmask @ETH2_NETMASK@
17 17
 
18
-auto eth3
19
-iface eth3 inet static
20
-        address @ETH3_IP@
21
-        netmask @ETH3_NETMASK@
18
+auto eth0
19
+iface eth0 inet dhcp
... ...
@@ -9,24 +9,31 @@ VDI_MB=${VDI_MB:-2500}
9 9
 # VM Password
10 10
 GUEST_PASSWORD=${GUEST_PASSWORD:-secrete}
11 11
 
12
-# Our nova host's network info 
12
+# Host Interface, i.e. the public facing interface on the nova vm
13
+HOST_IP_IFACE=${HOST_IP_IFACE:-eth0}
14
+
15
+# Our nova host's network info
13 16
 VM_IP=${VM_IP:-10.255.255.255} # A host-only ip that let's the interface come up, otherwise unused
14 17
 MGT_IP=${MGT_IP:-172.16.100.55}
15 18
 PUB_IP=${PUB_IP:-192.168.1.55}
16 19
 
17 20
 # Public network
18
-PUB_BR=${PUB_BR:-xenbr0}
21
+PUB_BR=${PUB_BR:-"xenbr0"}
22
+PUB_DEV=${PUB_DEV:-eth0}
23
+PUB_VLAN=${PUB_VLAN:--1}
19 24
 PUB_NETMASK=${PUB_NETMASK:-255.255.255.0}
20 25
 
21 26
 # VM network params
22 27
 VM_NETMASK=${VM_NETMASK:-255.255.255.0}
23
-VM_BR=${VM_BR:-xapi1}
28
+VM_BR=${VM_BR:-""}
24 29
 VM_VLAN=${VM_VLAN:-100}
30
+VM_DEV=${VM_DEV:-eth0}
25 31
 
26 32
 # MGMT network params
27 33
 MGT_NETMASK=${MGT_NETMASK:-255.255.255.0}
28
-MGT_BR=${MGT_BR:-xapi2}
34
+MGT_BR=${MGT_BR:-""}
29 35
 MGT_VLAN=${MGT_VLAN:-101}
36
+MGT_DEV=${MGT_DEV:-eth0}
30 37
 
31 38
 # XVA Directory
32 39
 XVA_DIR=${XVA_DIR:-xvas}