blob: 3dd5d79b85f0f0e4c63e58cd652e81dfb3d55610 [file] [log] [blame]
Anthony Young1b7a42e2011-10-19 02:34:06 -07001#!/usr/bin/env bash
2
3# Echo commands
4set -o xtrace
5
6# Keep track of the current directory
7TOOLS_DIR=$(cd $(dirname "$0") && pwd)
8
9ROOT_PASSWORD=${ROOT_PASSWORD:password}
10PERSIST_DIR=${PERSIST_DIR:-/opt/kvmstack}
11IMAGES_DIR=$PERSIST_DIR/images
12mkdir -p $UEC_DIR
13
14# Move to top devstack dir
15cd ..
16
17# Abort if localrc is not set
18if [ ! -e ./localrc ]; then
19 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
20 echo "See stack.sh for required passwords."
21 exit 1
22fi
23
24# Source params
25source ./stackrc
26
27# Base image (oneiric by default)
28IMAGE_FNAME=natty.raw
29IMAGE_NAME=natty
30
31BASE_IMAGE=$PERSIST_DIR/images/natty.raw
32BASE_IMAGE_COPY=$IMAGES_DIR/$IMAGE_NAME.raw.copy
33
34VM_NAME=${VM_NAME:-kvmstack}
35virsh shutdown $VM_NAME
36virsh destroy $VM_NAME
37
38VM_DIR=$PERSIST_DIR/instances/$VM_NAME
39
40mkdir -p $VM_DIR
41
42# Where to mount
43COPY_DIR=$VM_DIR/copy
44mkdir -p $COPY_DIR
45
46
47if [ ! -e $IMAGES_DIR/$IMAGE_FNAME ]; then
48 cd $TOOLS_DIR
49 ./make_image.sh -m -r 5000 natty raw
50 mv natty.raw $BASE_IMAGE
51 cd $TOP_DIR
52fi
53
54function unmount_images() {
55 # unmount the filesystem
56 while df | grep -q $COPY_DIR; do
57 umount $COPY_DIR || echo 'ok'
58 sleep 1
59 done
60}
61
62# unmount from failed runs
63unmount_images
64
65function kill_tail() {
66 unmount_images
67 exit 1
68}
69
70if [ ! -e $BASE_IMAGE_COPY ]; then
71 cp -p $BASE_IMAGE $BASE_IMAGE_COPY
72fi
73
74# Install deps
75apt-get install -y kvm libvirt-bin kpartx
76
77# Let Ctrl-c kill tail and exit
78trap kill_tail SIGINT
79
80# Where code will live in image
81DEST=${DEST:-/opt/stack}
82
83# Mount the file system
84mount -o loop,offset=32256 $BASE_IMAGE_COPY $COPY_DIR
85
86# git clone only if directory doesn't exist already. Since ``DEST`` might not
87# be owned by the installation user, we create the directory and change the
88# ownership to the proper user.
89function git_clone {
90 if [ ! -d $2 ]; then
91 sudo mkdir $2
92 sudo chown `whoami` $2
93 git clone $1 $2
94 cd $2
95 # This checkout syntax works for both branches and tags
96 git checkout $3
97 fi
98}
99
100# Make sure that base requirements are installed
101cp /etc/resolv.conf $COPY_DIR/etc/resolv.conf
102chroot $COPY_DIR apt-get update
103chroot $COPY_DIR apt-get install -y --force-yes `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
104chroot $COPY_DIR apt-get install -y --download-only rabbitmq-server libvirt-bin mysql-server
105chroot $COPY_DIR pip install `cat files/pips/*`
106
107# Clean out code repos if directed to do so
108if [ "$CLEAN" = "1" ]; then
109 rm -rf $COPY_DIR/$DEST
110fi
111
112# Cache openstack code
113mkdir -p $COPY_DIR/$DEST
114git_clone $NOVA_REPO $COPY_DIR/$DEST/nova $NOVA_BRANCH
115git_clone $GLANCE_REPO $COPY_DIR/$DEST/glance $GLANCE_BRANCH
116git_clone $KEYSTONE_REPO $COPY_DIR/$DESTkeystone $KEYSTONE_BRANCH
117git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
118git_clone $DASH_REPO $COPY_DIR/$DEST/dash $DASH_BRANCH $DASH_TAG
119git_clone $NOVACLIENT_REPO $COPY_DIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
120git_clone $OPENSTACKX_REPO $COPY_DIR/$DEST/openstackx $OPENSTACKX_BRANCH
121git_clone $KEYSTONE_REPO $COPY_DIR/$DEST/keystone $KEYSTONE_BRANCH
122git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
123
124# unmount the filesystems
125unmount_images
126
127rm -f $VM_DIR/kernel
128rm -f $VM_DIR/disk
129
130cd $VM_DIR
131qemu-img create -f qcow2 -b $BASE_IMAGE_COPY disk
132
133BRIDGE=${BRIDGE:-br0}
134CONTAINER=${CONTAINER:-STACK}
135CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
136CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
137CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
138CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
139CONTAINER_MAC=${CONTAINER_MAC:-02:16:3e:07:70:d7}
140
141# Create configuration
142LIBVIRT_XML=libvirt.xml
143cat > $LIBVIRT_XML <<EOF
144<domain type='kvm'>
145 <name>$VM_NAME</name>
146 <memory>1524288</memory>
147 <os>
148 <type>hvm</type>
Anthony Youngf6f52272011-10-19 02:58:18 -0700149 <bootmenu enable='yes'/>
Anthony Young1b7a42e2011-10-19 02:34:06 -0700150<!--
151 <kernel>$VM_DIR/kernel</kernel>
152 <cmdline>root=/dev/vda console=ttyS0</cmdline>
153-->
154 </os>
155 <features>
156 <acpi/>
157 </features>
158 <vcpu>1</vcpu>
159 <devices>
160 <disk type='file'>
161 <driver type='qcow2'/>
162 <source file='$VM_DIR/disk'/>
163 <target dev='vda' bus='virtio'/>
164 </disk>
165
166 <interface type='bridge'>
167 <source bridge='$BRIDGE'/>
168 <mac address='$CONTAINER_MAC'/>
169 </interface>
170
171 <!-- The order is significant here. File must be defined first -->
172 <serial type="file">
173 <source path='$VM_DIR/console.log'/>
174 <target port='1'/>
175 </serial>
176
177 <console type='pty' tty='/dev/pts/2'>
178 <source path='/dev/pts/2'/>
179 <target port='0'/>
180 </console>
181
182 <serial type='pty'>
183 <source path='/dev/pts/2'/>
184 <target port='0'/>
185 </serial>
186
187 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
188 </devices>
189</domain>
190EOF
191
192ROOTFS=$VM_DIR/root
193mkdir -p $ROOTFS
194
195modprobe nbd max_part=63
196
197umount $ROOTFS || echo 'ok'
198qemu-nbd -d /dev/nbd5 || echo 'ok'
199
200qemu-nbd -c /dev/nbd5 disk
201mount /dev/nbd5 $ROOTFS -o offset=32256 -t ext4
202
203# Configure instance network
204INTERFACES=$ROOTFS/etc/network/interfaces
205cat > $INTERFACES <<EOF
206auto lo
207iface lo inet loopback
208
209auto eth0
210iface eth0 inet static
211 address $CONTAINER_IP
212 netmask $CONTAINER_NETMASK
213 gateway $CONTAINER_GATEWAY
214EOF
215
216chroot $ROOTFS groupadd libvirtd
217chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
218cp -pr $TOOLS_DIR/.. $ROOTFS/$DEST/devstack
219echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
220
221# a simple password - pass
222echo "stack:pass" | chroot $ROOTFS chpasswd
223
224# stack requires)
225echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
226
227# Gracefully cp only if source file/dir exists
228function cp_it {
229 if [ -e $1 ] || [ -d $1 ]; then
230 cp -pRL $1 $2
231 fi
232}
233
234# Copy over your ssh keys and env if desired
235COPYENV=${COPYENV:-1}
236if [ "$COPYENV" = "1" ]; then
237 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
238 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
239 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
240 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
241 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
242fi
243
244# Configure the runner
245RUN_SH=$ROOTFS/$DEST/run.sh
246cat > $RUN_SH <<EOF
247#!/usr/bin/env bash
248sleep 1
249
250# Kill any existing screens
251killall screen
252
253# Install and run stack.sh
254sudo apt-get update
255sudo apt-get -y --force-yes install git-core vim-nox sudo
256if [ ! -d "$DEST/devstack" ]; then
257 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
258fi
259cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
260echo >> /$DEST/run.sh.log
261echo >> /$DEST/run.sh.log
262echo "All done! Time to start clicking." >> /$DEST/run.sh.log
263EOF
264
265# Make the run.sh executable
266chmod 755 $RUN_SH
267
268# Make runner launch on boot
269RC_LOCAL=$ROOTFS/etc/init.d/local
270cat > $RC_LOCAL <<EOF
271#!/bin/sh -e
272su -c "$DEST/run.sh" stack
273EOF
274
275# Make our ip address hostnames look nice at the command prompt
276echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
277echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
278
279# Give stack ownership over $DEST so it may do the work needed
280chroot $ROOTFS chown -R stack $DEST
281
282chmod +x $RC_LOCAL
283chroot $ROOTFS sudo update-rc.d local defaults 80
284
Anthony Youngf6f52272011-10-19 02:58:18 -0700285sudo sed -e "s/quiet splash/splash console=ttyS0 console=ttyS1,19200n8/g" -i $ROOTFS/boot/grub/menu.lst
286
Anthony Young1b7a42e2011-10-19 02:34:06 -0700287umount $ROOTFS
288qemu-nbd -d /dev/nbd5
289
290cd $VM_DIR
291virsh create libvirt.xml