Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 3 | # Ubuntu distro to install |
| 4 | DIST_NAME=${DIST_NAME:-oneiric} |
| 5 | |
| 6 | # Make sure that we have the proper version of ubuntu (only works on natty/oneiric) |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 7 | UBUNTU_VERSION=`cat /etc/lsb-release | grep CODENAME | sed 's/.*=//g'` |
| 8 | if [ ! "oneiric" = "$UBUNTU_VERSION" ]; then |
| 9 | if [ ! "natty" = "$UBUNTU_VERSION" ]; then |
| 10 | echo "This script only works with oneiric and natty" |
| 11 | exit 1 |
| 12 | fi |
| 13 | fi |
| 14 | |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 15 | # Keep track of the current directory |
| 16 | TOOLS_DIR=$(cd $(dirname "$0") && pwd) |
| 17 | TOP_DIR=`cd $TOOLS_DIR/..; pwd` |
| 18 | |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 19 | # exit on error to stop unexpected errors |
| 20 | set -o errexit |
| 21 | set -o xtrace |
| 22 | |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 23 | # Abort if localrc is not set |
| 24 | if [ ! -e $TOP_DIR/localrc ]; then |
| 25 | echo "You must have a localrc with ALL necessary passwords defined before proceeding." |
| 26 | echo "See stack.sh for required passwords." |
| 27 | exit 1 |
| 28 | fi |
| 29 | |
| 30 | # Install deps if needed |
| 31 | dpkg -l kvm libvirt-bin kpartx || apt-get install -y --force-yes kvm libvirt-bin kpartx |
| 32 | |
| 33 | # Where to store files and instances |
| 34 | WORK_DIR=${WORK_DIR:-/opt/kvmstack} |
| 35 | |
| 36 | # Where to store images |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 37 | image_dir=$WORK_DIR/images/$DIST_NAME |
| 38 | mkdir -p $image_dir |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 39 | |
| 40 | # Original version of built image |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 41 | uec_url=http://uec-images.ubuntu.com/$DIST_NAME/current/$DIST_NAME-server-cloudimg-amd64.tar.gz |
Jesse Andrews | 96dffbc | 2011-11-05 17:37:33 -0700 | [diff] [blame] | 42 | tarball=$image_dir/$(basename $uec_url) |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 43 | |
| 44 | # download the base uec image if we haven't already |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 45 | if [ ! -f $tarball ]; then |
| 46 | curl $uec_url -o $tarball |
Jesse Andrews | 3b76858 | 2011-11-05 17:40:20 -0700 | [diff] [blame] | 47 | (cd $image_dir && tar -Sxvzf $tarball) |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 48 | cp $image_dir/*.img $image_dir/disk |
| 49 | cp $image_dir/*-vmlinuz-virtual $image_dir/kernel |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 50 | fi |
| 51 | |
| 52 | cd $TOP_DIR |
| 53 | |
| 54 | # Source params |
| 55 | source ./stackrc |
| 56 | |
| 57 | # Configure the root password of the vm to be the same as ``ADMIN_PASSWORD`` |
| 58 | ROOT_PASSWORD=${ADMIN_PASSWORD:-password} |
| 59 | |
| 60 | # Name of our instance, used by libvirt |
| 61 | GUEST_NAME=${GUEST_NAME:-devstack} |
| 62 | |
| 63 | # Mop up after previous runs |
| 64 | virsh destroy $GUEST_NAME || true |
| 65 | |
| 66 | # Where this vm is stored |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 67 | vm_dir=$WORK_DIR/instances/$GUEST_NAME |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 68 | |
| 69 | # Create vm dir and remove old disk |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 70 | mkdir -p $vm_dir |
| 71 | rm -f $vm_dir/disk |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 72 | |
| 73 | # Create a copy of the base image |
Jesse Andrews | f5a7691 | 2011-11-05 17:47:50 -0700 | [diff] [blame^] | 74 | qemu-img create -f qcow2 -b $image_dir/disk $vm_dir/disk |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 75 | |
| 76 | # Back to devstack |
| 77 | cd $TOP_DIR |
| 78 | |
| 79 | GUEST_NETWORK=${GUEST_NETWORK:-1} |
| 80 | GUEST_RECREATE_NET=${GUEST_RECREATE_NET:-yes} |
| 81 | GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50} |
| 82 | GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24} |
| 83 | GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0} |
| 84 | GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.$GUEST_NETWORK.1} |
| 85 | GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $GUEST_NETWORK`"} |
| 86 | GUEST_RAM=${GUEST_RAM:-1524288} |
| 87 | GUEST_CORES=${GUEST_CORES:-1} |
| 88 | |
| 89 | # libvirt.xml configuration |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 90 | NET_XML=$vm_dir/net.xml |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 91 | cat > $NET_XML <<EOF |
| 92 | <network> |
| 93 | <name>devstack-$GUEST_NETWORK</name> |
| 94 | <bridge name="stackbr%d" /> |
| 95 | <forward/> |
| 96 | <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK" /> |
| 97 | </network> |
| 98 | EOF |
| 99 | |
| 100 | if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then |
| 101 | virsh net-destroy devstack-$GUEST_NETWORK || true |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 102 | virsh net-create $vm_dir/net.xml |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 103 | fi |
| 104 | |
| 105 | # libvirt.xml configuration |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 106 | LIBVIRT_XML=$vm_dir/libvirt.xml |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 107 | cat > $LIBVIRT_XML <<EOF |
| 108 | <domain type='kvm'> |
| 109 | <name>$GUEST_NAME</name> |
| 110 | <memory>$GUEST_RAM</memory> |
| 111 | <os> |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 112 | <type>hvm</type> |
Jesse Andrews | f5a7691 | 2011-11-05 17:47:50 -0700 | [diff] [blame^] | 113 | <kernel>$image_dir/kernel</kernel> |
| 114 | <cmdline>root=/dev/vda ro console=ttyS0 init=/usr/lib/cloud-init/uncloud-init ds=nocloud ubuntu-pass=ubuntu</cmdline> |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 115 | </os> |
| 116 | <features> |
| 117 | <acpi/> |
| 118 | </features> |
| 119 | <clock offset='utc'/> |
| 120 | <vcpu>$GUEST_CORES</vcpu> |
| 121 | <devices> |
| 122 | <disk type='file'> |
| 123 | <driver type='qcow2'/> |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 124 | <source file='$vm_dir/disk'/> |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 125 | <target dev='vda' bus='virtio'/> |
| 126 | </disk> |
| 127 | |
| 128 | <interface type='network'> |
| 129 | <source network='devstack-$GUEST_NETWORK'/> |
| 130 | </interface> |
| 131 | |
| 132 | <!-- The order is significant here. File must be defined first --> |
| 133 | <serial type="file"> |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 134 | <source path='$vm_dir/console.log'/> |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 135 | <target port='1'/> |
| 136 | </serial> |
| 137 | |
| 138 | <console type='pty' tty='/dev/pts/2'> |
| 139 | <source path='/dev/pts/2'/> |
| 140 | <target port='0'/> |
| 141 | </console> |
| 142 | |
| 143 | <serial type='pty'> |
| 144 | <source path='/dev/pts/2'/> |
| 145 | <target port='0'/> |
| 146 | </serial> |
| 147 | |
| 148 | <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/> |
| 149 | </devices> |
| 150 | </domain> |
| 151 | EOF |
| 152 | |
| 153 | # Create the instance |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 154 | cd $vm_dir && virsh create libvirt.xml |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 155 | |
| 156 | # Tail the console log till we are done |
| 157 | WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1} |
| 158 | if [ "$WAIT_TILL_LAUNCH" = "1" ]; then |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 159 | set +o xtrace |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 160 | # Done creating the container, let's tail the log |
| 161 | echo |
| 162 | echo "=============================================================" |
| 163 | echo " -- YAY! --" |
| 164 | echo "=============================================================" |
| 165 | echo |
| 166 | echo "We're done launching the vm, about to start tailing the" |
| 167 | echo "stack.sh log. It will take a second or two to start." |
| 168 | echo |
| 169 | echo "Just CTRL-C at any time to stop tailing." |
| 170 | |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 171 | while [ ! -e "$vm_dir/console.log" ]; do |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 172 | sleep 1 |
| 173 | done |
| 174 | |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 175 | tail -F $vm_dir/console.log & |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 176 | |
| 177 | TAIL_PID=$! |
| 178 | |
| 179 | function kill_tail() { |
| 180 | kill $TAIL_PID |
| 181 | exit 1 |
| 182 | } |
| 183 | |
| 184 | # Let Ctrl-c kill tail and exit |
| 185 | trap kill_tail SIGINT |
| 186 | |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 187 | echo "Waiting stack.sh to finish..." |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 188 | while ! cat $vm_dir/console.log | grep -q 'All done' ; do |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 189 | sleep 1 |
| 190 | done |
| 191 | |
| 192 | set -o xtrace |
| 193 | |
| 194 | kill $TAIL_PID |
| 195 | |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 196 | if ! grep -q "^stack.sh completed in" $vm_dir/console.log; then |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 197 | exit 1 |
| 198 | fi |
| 199 | echo "" |
| 200 | echo "Finished - Zip-a-dee Doo-dah!" |
| 201 | fi |