Browse code

add build_kvm file

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