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