blob: 97d2a277bfaaaacf2dfb0a765108750e36d47878 [file] [log] [blame]
Anthony Youngb62b4ca2011-10-26 22:29:08 -07001#!/bin/bash
2
3# Abort if localrc is not set
4if [ ! -e ../../localrc ]; then
5 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
6 echo "See the xen README for required passwords."
7 exit 1
8fi
9
10# Echo commands
11set -o xtrace
12
13# Name of this guest
14GUEST_NAME=${GUEST_NAME:-ALLINONE}
15
16# dom0 ip
17HOST_IP=${HOST_IP:-`ifconfig xenbr0 | grep "inet addr" | cut -d ":" -f2 | sed "s/ .*//"`}
18
19# Our nova host's network info
20MGT_IP=${MGT_IP:-172.16.100.1}
21PUB_IP=${PUB_IP:-192.168.1.55}
22
23# Public network
24PUB_BR=${PUB_BR:-xenbr0}
25PUB_NETMASK=${PUB_NETMASK:-255.255.255.0}
26
27# VM network params
28VM_NETMASK=${VM_NETMASK:-255.255.255.0}
29VM_BR=${VM_BR:-xapi1}
30VM_VLAN=${VM_VLAN:-100}
31
32# MGMT network params
33MGT_NETMASK=${MGT_NETMASK:-255.255.255.0}
34MGT_BR=${MGT_BR:-xapi2}
35MGT_VLAN=${MGT_VLAN:-101}
36
37# VM Password
38PASSWORD=${PASSWORD:-secrete}
39
40# Size of image
41VDI_MB=${VDI_MB:-2500}
42
43# This directory
44TOP_DIR=$(cd $(dirname "$0") && pwd)
45
46# Make sure we have git
47if ! which git; then
48 GITDIR=/tmp/git-1.7.7
49 cd /tmp
50 rm -rf $GITDIR*
51 wget http://git-core.googlecode.com/files/git-1.7.7.tar.gz
52 tar xfv git-1.7.7.tar.gz
53 cd $GITDIR
54 ./configure
55 make install
56 cd $TOP_DIR
57fi
58
59# Helper to create networks
60function create_network() {
61 if ! xe network-list | grep bridge | grep -q $1; then
62 echo "Creating bridge $1"
63 xe network-create name-label=$1
64 fi
65}
66
67# Create host, vm, mgmt, pub networks
68create_network xapi0
69create_network $VM_BR
70create_network $MGT_BR
71create_network $PUB_BR
72
73# Get the uuid for our physical (public) interface
74PIF=`xe pif-list --minimal device=eth0`
75
76# Create networks/bridges for vm and management
77VM_NET=`xe network-list --minimal bridge=$VM_BR`
78MGT_NET=`xe network-list --minimal bridge=$MGT_BR`
79
80# Helper to create vlans
81function create_vlan() {
82 pif=$1
83 vlan=$2
84 net=$3
85 if ! xe vlan-list | grep tag | grep -q $vlan; then
86 xe vlan-create pif-uuid=$pif vlan=$vlan network-uuid=$net
87 fi
88}
89
90# Create vlans for vm and management
91create_vlan $PIF $VM_VLAN $VM_NET
92create_vlan $PIF $MGT_VLAN $MGT_NET
93
94# Setup host-only nat rules
95HOST_NET=169.254.0.0/16
96if ! iptables -L -v -t nat | grep -q $HOST_NET; then
97 iptables -t nat -A POSTROUTING -s $HOST_NET -j SNAT --to-source $HOST_IP
98 iptables -I FORWARD 1 -s $HOST_NET -j ACCEPT
99 /etc/init.d/iptables save
100fi
101
102# Set up ip forwarding
103if ! grep -q "FORWARD_IPV4=YES" /etc/sysconfig/network; then
104 # FIXME: This doesn't work on reboot!
105 echo "FORWARD_IPV4=YES" >> /etc/sysconfig/network
106fi
107
108# Also, enable ip forwarding in rc.local, since the above trick isn't working
109if ! grep -q "echo 1 >/proc/sys/net/ipv4/ip_forward" /etc/rc.local; then
110 echo "echo 1 >/proc/sys/net/ipv4/ip_forward" >> /etc/rc.local
111fi
112
113# Enable ip forwarding at runtime as well
114echo 1 > /proc/sys/net/ipv4/ip_forward
115
116# Directory where we stage the build
117STAGING_DIR=$TOP_DIR/stage
118
119# Option to clean out old stuff
120CLEAN=${CLEAN:-0}
121if [ "$CLEAN" = "1" ]; then
122 rm -rf $STAGING_DIR
123fi
124
125# Download our base image. This image is made using prepare_guest.sh
126BASE_IMAGE_URL=${BASE_IMAGE_URL:-http://images.ansolabs.com/xen/stage.tgz}
127if [ ! -e $STAGING_DIR ]; then
128 if [ ! -e /tmp/stage.tgz ]; then
129 wget $BASE_IMAGE_URL -O /tmp/stage.tgz
130 fi
131 tar xfz /tmp/stage.tgz
132 cd $TOP_DIR
133fi
134
135# Free up precious disk space
136rm -f /tmp/stage.tgz
137
138# Make sure we have a stage
139if [ ! -d $STAGING_DIR/etc ]; then
140 echo "Stage is not properly set up!"
141 exit 1
142fi
143
144# Directory where our conf files are stored
145FILES_DIR=$TOP_DIR/files
146
147# Directory for supporting script files
148SCRIPT_DIR=$TOP_DIR/scripts
149
150# Version of ubuntu with which we are working
151UBUNTU_VERSION=`cat $STAGING_DIR/etc/lsb-release | grep "DISTRIB_CODENAME=" | sed "s/DISTRIB_CODENAME=//"`
152KERNEL_VERSION=`ls $STAGING_DIR/boot/vmlinuz* | head -1 | sed "s/.*vmlinuz-//"`
153
154# Setup fake grub
155rm -rf $STAGING_DIR/boot/grub/
156mkdir -p $STAGING_DIR/boot/grub/
157cp $FILES_DIR/menu.lst.in $STAGING_DIR/boot/grub/menu.lst
158sed -e "s,@KERNEL_VERSION@,$KERNEL_VERSION,g" -i $STAGING_DIR/boot/grub/menu.lst
159
160# Setup fstab, tty, and other system stuff
161cp $FILES_DIR/fstab $STAGING_DIR/etc/fstab
162cp $FILES_DIR/hvc0.conf $STAGING_DIR/etc/init/
163
164# Put the VPX into UTC.
165rm -f $STAGING_DIR/etc/localtime
166
167# Helper to set hostname
168function set_hostname() {
169 echo $1 > $STAGING_DIR/etc/hostname
170}
171
172# We need a resolvable host name for rabbit to launch
173if ! grep -q $GUEST_NAME $STAGING_DIR/etc/hosts; then
174 echo "$MGT_IP $GUEST_NAME" >> $STAGING_DIR/etc/hosts
175fi
176
177# Configre hosts file
178cat <<EOF >$STAGING_DIR/etc/hosts
179$MGT_IP $GUEST_NAME
180127.0.0.1 localhost localhost.localdomain
181EOF
182
183# Configure dns (use same dns as dom0)
184cp /etc/resolv.conf $STAGING_DIR/etc/resolv.conf
185
186# Copy over devstack
187rm -f /tmp/devstack.tar
188tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar $TOP_DIR/../../../devstack
189cd $STAGING_DIR/opt/stack/
190tar xf /tmp/devstack.tar
191cd $TOP_DIR
192
193# Configure OVA
194VDI_SIZE=$(($VDI_MB*1024*1024))
195PRODUCT_BRAND=${PRODUCT_BRAND:-openstack}
196PRODUCT_VERSION=${PRODUCT_VERSION:-001}
197BUILD_NUMBER=${BUILD_NUMBER:-001}
198LABEL="$PRODUCT_BRAND $PRODUCT_VERSION-$BUILD_NUMBER"
199OVA=$STAGING_DIR/tmp/ova.xml
200cp templates/ova.xml.in $OVA
201sed -e "s,@VDI_SIZE@,$VDI_SIZE,g" -i $OVA
202sed -e "s,@PRODUCT_BRAND@,$PRODUCT_BRAND,g" -i $OVA
203sed -e "s,@PRODUCT_VERSION@,$PRODUCT_VERSION,g" -i $OVA
204sed -e "s,@BUILD_NUMBER@,$BUILD_NUMBER,g" -i $OVA
205
206# Directory for xvas
207XVA_DIR=$TOP_DIR/xvas
208
209# Create xva dir
210mkdir -p $XVA_DIR
211
212# Clean nova if desired
213if [ "$CLEAN" = "1" ]; then
214 rm -rf $TOP_DIR/nova
215fi
216
217# Checkout nova
218if [ ! -d $TOP_DIR/nova ]; then
219 git clone git://github.com/cloudbuilders/nova.git
220 git checkout diablo
221fi
222
223# Run devstack on launch
224cat <<EOF >$STAGING_DIR/etc/rc.local
225STAGING_DIR=/ DO_TGZ=0 bash /opt/stack/devstack/tools/xen/prepare_guest.sh
226STAGING_DIR=/ DO_TGZ=0 bash /opt/stack/devstack/tools/xen/prepare_guest.sh
227su -c "/opt/stack/run.sh > /opt/stack/run.sh.log" stack
228exit 0
229EOF
230
231# Install plugins
232cp -pr $TOP_DIR/nova/plugins/xenserver/xenapi/etc/xapi.d /etc/
233chmod a+x /etc/xapi.d/plugins/*
234yum --enablerepo=base install -y parted
235mkdir -p /boot/guest
236
237# Set local storage il8n
238SR_UUID=`xe sr-list --minimal name-label="Local storage"`
239xe sr-param-set uuid=$SR_UUID other-config:i18n-key=local-storage
240
241# Uninstall previous runs
242xe vm-list --minimal name-label="$LABEL" | xargs ./scripts/uninstall-os-vpx.sh
243
244# Destroy any instances that were launched
245for uuid in `xe vm-list | grep -1 instance | grep uuid | sed "s/.*\: //g"`; do
246 echo "Shutting down nova instance $uuid"
247 xe vm-shutdown uuid=$uuid
248 xe vm-destroy uuid=$uuid
249done
250
251# Path to head xva. By default keep overwriting the same one to save space
252USE_SEPARATE_XVAS=${USE_SEPARATE_XVAS:-0}
253if [ "$USE_SEPARATE_XVAS" = "0" ]; then
254 XVA=$XVA_DIR/$UBUNTU_VERSION.xva
255else
256 XVA=$XVA_DIR/$UBUNTU_VERSION.$GUEST_NAME.xva
257fi
258
259# Clean old xva. In the future may not do this every time.
260rm -f $XVA
261
262# Configure the network
263set_hostname $GUEST_NAME
264INTERFACES=$STAGING_DIR/etc/network/interfaces
265cp templates/interfaces.in $INTERFACES
266sed -e "s,@ETH1_NETMASK@,$VM_NETMASK,g" -i $INTERFACES
267sed -e "s,@ETH2_IP@,$MGT_IP,g" -i $INTERFACES
268sed -e "s,@ETH2_NETMASK@,$MGT_NETMASK,g" -i $INTERFACES
269sed -e "s,@ETH3_IP@,$PUB_IP,g" -i $INTERFACES
270sed -e "s,@ETH3_NETMASK@,$PUB_NETMASK,g" -i $INTERFACES
271
272# Configure run.sh
273cat <<EOF >$STAGING_DIR/opt/stack/run.sh
274#!/bin/bash
275cd /opt/stack/devstack
276killall screen
277UPLOAD_LEGACY_TTY=yes HOST_IP=$PUB_IP VIRT_DRIVER=xenserver FORCE=yes MULTI_HOST=1 $STACKSH_PARAMS ./stack.sh
278EOF
279chmod 755 $STAGING_DIR/opt/stack/run.sh
280
281# Create xva
282if [ ! -e $XVA ]; then
283 rm -rf /tmp/mkxva*
284 UID=0 $SCRIPT_DIR/mkxva -o $XVA -t xva -x $OVA $STAGING_DIR $VDI_MB /tmp/
285fi
286
287# Start guest
288$TOP_DIR/scripts/install-os-vpx.sh -f $XVA -v $VM_BR -m $MGT_BR -p $PUB_BR