blob: 3e4a7670307ab6e7bf0367727c5de787199efae3 [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
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700169# Configure dns (use same dns as dom0)
170cp /etc/resolv.conf $STAGING_DIR/etc/resolv.conf
171
172# Copy over devstack
173rm -f /tmp/devstack.tar
174tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar $TOP_DIR/../../../devstack
175cd $STAGING_DIR/opt/stack/
176tar xf /tmp/devstack.tar
177cd $TOP_DIR
178
179# Configure OVA
180VDI_SIZE=$(($VDI_MB*1024*1024))
181PRODUCT_BRAND=${PRODUCT_BRAND:-openstack}
182PRODUCT_VERSION=${PRODUCT_VERSION:-001}
183BUILD_NUMBER=${BUILD_NUMBER:-001}
184LABEL="$PRODUCT_BRAND $PRODUCT_VERSION-$BUILD_NUMBER"
185OVA=$STAGING_DIR/tmp/ova.xml
Anthony Young3eb8f592011-10-26 23:11:52 -0700186cp $TEMPLATES_DIR/ova.xml.in $OVA
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700187sed -e "s,@VDI_SIZE@,$VDI_SIZE,g" -i $OVA
188sed -e "s,@PRODUCT_BRAND@,$PRODUCT_BRAND,g" -i $OVA
189sed -e "s,@PRODUCT_VERSION@,$PRODUCT_VERSION,g" -i $OVA
190sed -e "s,@BUILD_NUMBER@,$BUILD_NUMBER,g" -i $OVA
191
192# Directory for xvas
193XVA_DIR=$TOP_DIR/xvas
194
195# Create xva dir
196mkdir -p $XVA_DIR
197
198# Clean nova if desired
199if [ "$CLEAN" = "1" ]; then
200 rm -rf $TOP_DIR/nova
201fi
202
203# Checkout nova
204if [ ! -d $TOP_DIR/nova ]; then
205 git clone git://github.com/cloudbuilders/nova.git
206 git checkout diablo
207fi
208
209# Run devstack on launch
210cat <<EOF >$STAGING_DIR/etc/rc.local
Anthony Youngf54bc062011-10-27 00:39:30 -0700211GUEST_PASSWORD=$GUEST_PASSWORD STAGING_DIR=/ DO_TGZ=0 bash /opt/stack/devstack/tools/xen/prepare_guest.sh
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700212su -c "/opt/stack/run.sh > /opt/stack/run.sh.log" stack
213exit 0
214EOF
215
216# Install plugins
217cp -pr $TOP_DIR/nova/plugins/xenserver/xenapi/etc/xapi.d /etc/
218chmod a+x /etc/xapi.d/plugins/*
219yum --enablerepo=base install -y parted
220mkdir -p /boot/guest
221
222# Set local storage il8n
223SR_UUID=`xe sr-list --minimal name-label="Local storage"`
224xe sr-param-set uuid=$SR_UUID other-config:i18n-key=local-storage
225
226# Uninstall previous runs
227xe vm-list --minimal name-label="$LABEL" | xargs ./scripts/uninstall-os-vpx.sh
228
229# Destroy any instances that were launched
230for uuid in `xe vm-list | grep -1 instance | grep uuid | sed "s/.*\: //g"`; do
231 echo "Shutting down nova instance $uuid"
232 xe vm-shutdown uuid=$uuid
233 xe vm-destroy uuid=$uuid
234done
235
236# Path to head xva. By default keep overwriting the same one to save space
237USE_SEPARATE_XVAS=${USE_SEPARATE_XVAS:-0}
238if [ "$USE_SEPARATE_XVAS" = "0" ]; then
239 XVA=$XVA_DIR/$UBUNTU_VERSION.xva
240else
241 XVA=$XVA_DIR/$UBUNTU_VERSION.$GUEST_NAME.xva
242fi
243
244# Clean old xva. In the future may not do this every time.
245rm -f $XVA
246
Anthony Young138b2832011-10-27 13:06:39 -0700247# Configure the hostname
248echo $GUEST_NAME > $STAGING_DIR/etc/hostname
249
250# Hostname must resolve for rabbit
251cat <<EOF >$STAGING_DIR/etc/hosts
252$MGT_IP $GUEST_NAME
253127.0.0.1 localhost localhost.localdomain
254EOF
255
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700256# Configure the network
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700257INTERFACES=$STAGING_DIR/etc/network/interfaces
Anthony Young3eb8f592011-10-26 23:11:52 -0700258cp $TEMPLATES_DIR/interfaces.in $INTERFACES
Anthony Youngf6ef5692011-10-26 23:40:46 -0700259sed -e "s,@ETH1_IP@,$VM_IP,g" -i $INTERFACES
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700260sed -e "s,@ETH1_NETMASK@,$VM_NETMASK,g" -i $INTERFACES
261sed -e "s,@ETH2_IP@,$MGT_IP,g" -i $INTERFACES
262sed -e "s,@ETH2_NETMASK@,$MGT_NETMASK,g" -i $INTERFACES
263sed -e "s,@ETH3_IP@,$PUB_IP,g" -i $INTERFACES
264sed -e "s,@ETH3_NETMASK@,$PUB_NETMASK,g" -i $INTERFACES
265
Anthony Younga1a90772011-10-31 15:32:38 -0700266# Gracefully cp only if source file/dir exists
267function cp_it {
268 if [ -e $1 ] || [ -d $1 ]; then
269 cp -pRL $1 $2
270 fi
271}
272
273# Copy over your ssh keys and env if desired
274COPYENV=${COPYENV:-1}
275if [ "$COPYENV" = "1" ]; then
276 cp_it ~/.ssh $STAGING_DIR/opt/stack/.ssh
277 cp_it ~/.ssh/id_rsa.pub $STAGING_DIR/opt/stack/.ssh/authorized_keys
278 cp_it ~/.gitconfig $STAGING_DIR/opt/stack/.gitconfig
279 cp_it ~/.vimrc $STAGING_DIR/opt/stack/.vimrc
280 cp_it ~/.bashrc $STAGING_DIR/opt/stack/.bashrc
281fi
282
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700283# Configure run.sh
284cat <<EOF >$STAGING_DIR/opt/stack/run.sh
285#!/bin/bash
286cd /opt/stack/devstack
287killall screen
288UPLOAD_LEGACY_TTY=yes HOST_IP=$PUB_IP VIRT_DRIVER=xenserver FORCE=yes MULTI_HOST=1 $STACKSH_PARAMS ./stack.sh
289EOF
290chmod 755 $STAGING_DIR/opt/stack/run.sh
291
292# Create xva
293if [ ! -e $XVA ]; then
294 rm -rf /tmp/mkxva*
295 UID=0 $SCRIPT_DIR/mkxva -o $XVA -t xva -x $OVA $STAGING_DIR $VDI_MB /tmp/
296fi
297
298# Start guest
299$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 -0700300
301echo "################################################################################"
302echo ""
303echo "All Finished!"
304echo "Now, you can monitor the progress of the stack.sh installation by "
305echo "tailing /opt/stack/run.sh.log from within your domU."
306echo ""
307echo "ssh into your domU now: 'ssh stack@$PUB_IP' using your password"
308echo "and then do: 'tail -f /opt/stack/run.sh.log'"
309echo ""
310echo "When the script completes, you can then visit the OpenStack Dashboard"
311echo "at http://$PUB_IP, and contact other services at the usual ports."