Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # exit on error to stop unexpected errors |
| 4 | set -o errexit |
| 5 | |
| 6 | # Make sure that we have the proper version of ubuntu |
| 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 | |
Dean Troyer | 55c0273 | 2011-11-01 17:44:03 -0500 | [diff] [blame] | 15 | # Clean up any resources that may be in use |
| 16 | cleanup() { |
| 17 | set +o errexit |
| 18 | unmount_images |
| 19 | |
Dean Troyer | 8e99829 | 2011-11-07 16:41:47 -0600 | [diff] [blame] | 20 | if [ -n "$COPY_DIR" ]; then |
| 21 | umount $COPY_DIR/dev |
| 22 | umount $COPY_DIR |
| 23 | fi |
| 24 | |
Dean Troyer | 55c0273 | 2011-11-01 17:44:03 -0500 | [diff] [blame] | 25 | if [ -n "$ROOTFS" ]; then |
| 26 | umount $ROOTFS/dev |
| 27 | umount $ROOTFS |
| 28 | fi |
| 29 | |
| 30 | # Release NBD devices |
| 31 | if [ -n "$NBD" ]; then |
| 32 | qemu-nbd -d $NBD |
| 33 | fi |
| 34 | |
| 35 | # Kill ourselves to signal any calling process |
| 36 | trap 2; kill -2 $$ |
| 37 | } |
| 38 | |
Dean Troyer | 8e99829 | 2011-11-07 16:41:47 -0600 | [diff] [blame] | 39 | trap cleanup SIGHUP SIGINT SIGTERM SIGQUIT EXIT |
Dean Troyer | 55c0273 | 2011-11-01 17:44:03 -0500 | [diff] [blame] | 40 | |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 41 | # Echo commands |
| 42 | set -o xtrace |
| 43 | |
| 44 | # Keep track of the current directory |
| 45 | TOOLS_DIR=$(cd $(dirname "$0") && pwd) |
Jesse Andrews | 3198974 | 2011-10-30 18:56:05 -0700 | [diff] [blame] | 46 | TOP_DIR=`cd $TOOLS_DIR/..; pwd` |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 47 | |
| 48 | # Where to store files and instances |
Jesse Andrews | b0559b2 | 2011-10-30 19:46:54 -0700 | [diff] [blame] | 49 | WORK_DIR=${WORK_DIR:-/opt/kvmstack} |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 50 | |
| 51 | # Where to store images |
| 52 | IMAGES_DIR=$WORK_DIR/images |
| 53 | |
| 54 | # Create images dir |
| 55 | mkdir -p $IMAGES_DIR |
| 56 | |
| 57 | # Abort if localrc is not set |
| 58 | if [ ! -e $TOP_DIR/localrc ]; then |
| 59 | echo "You must have a localrc with ALL necessary passwords defined before proceeding." |
| 60 | echo "See stack.sh for required passwords." |
| 61 | exit 1 |
| 62 | fi |
| 63 | |
Jesse Andrews | 7fa5613 | 2011-10-30 18:43:54 -0700 | [diff] [blame] | 64 | cd $TOP_DIR |
| 65 | |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 66 | # Source params |
| 67 | source ./stackrc |
| 68 | |
| 69 | # Configure the root password of the vm to be the same as ``ADMIN_PASSWORD`` |
| 70 | ROOT_PASSWORD=${ADMIN_PASSWORD:-password} |
| 71 | |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 72 | # Base image (natty by default) |
| 73 | DIST_NAME=${DIST_NAME:-natty} |
| 74 | IMAGE_FNAME=$DIST_NAME.raw |
| 75 | |
| 76 | # Name of our instance, used by libvirt |
| 77 | GUEST_NAME=${GUEST_NAME:-devstack} |
| 78 | |
| 79 | # Original version of built image |
Jesse Andrews | 2b7d221 | 2011-10-30 19:37:56 -0700 | [diff] [blame] | 80 | BASE_IMAGE=$IMAGES_DIR/$DIST_NAME.raw |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 81 | |
| 82 | # Copy of base image, which we pre-install with tasty treats |
| 83 | VM_IMAGE=$IMAGES_DIR/$DIST_NAME.$GUEST_NAME.raw |
| 84 | |
| 85 | # Mop up after previous runs |
| 86 | virsh destroy $GUEST_NAME || true |
| 87 | |
| 88 | # Where this vm is stored |
| 89 | VM_DIR=$WORK_DIR/instances/$GUEST_NAME |
| 90 | |
| 91 | # Create vm dir |
| 92 | mkdir -p $VM_DIR |
| 93 | |
| 94 | # Mount point into copied base image |
| 95 | COPY_DIR=$VM_DIR/copy |
| 96 | mkdir -p $COPY_DIR |
| 97 | |
Jesse Andrews | 2b7d221 | 2011-10-30 19:37:56 -0700 | [diff] [blame] | 98 | # Get the base image if it does not yet exist |
| 99 | if [ ! -e $BASE_IMAGE ]; then |
| 100 | $TOOLS_DIR/get_uec_image.sh -f raw -r 5000 $DIST_NAME $BASE_IMAGE |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 101 | fi |
| 102 | |
| 103 | # Create a copy of the base image |
| 104 | if [ ! -e $VM_IMAGE ]; then |
| 105 | cp -p $BASE_IMAGE $VM_IMAGE |
| 106 | fi |
| 107 | |
| 108 | # Unmount the copied base image |
| 109 | function unmount_images() { |
| 110 | # unmount the filesystem |
| 111 | while df | grep -q $COPY_DIR; do |
| 112 | umount $COPY_DIR || echo 'ok' |
| 113 | sleep 1 |
| 114 | done |
| 115 | } |
| 116 | |
| 117 | # Unmount from failed runs |
| 118 | unmount_images |
| 119 | |
| 120 | # Ctrl-c catcher |
| 121 | function kill_unmount() { |
| 122 | unmount_images |
| 123 | exit 1 |
| 124 | } |
| 125 | |
| 126 | # Install deps if needed |
| 127 | dpkg -l kvm libvirt-bin kpartx || apt-get install -y --force-yes kvm libvirt-bin kpartx |
| 128 | |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 129 | # Where Openstack code will live in image |
| 130 | DEST=${DEST:-/opt/stack} |
| 131 | |
| 132 | # Mount the file system |
Jesse Andrews | 2b7d221 | 2011-10-30 19:37:56 -0700 | [diff] [blame] | 133 | # For some reason, UEC-based images want 255 heads * 63 sectors * 512 byte sectors = 8225280 |
| 134 | mount -t ext4 -o loop,offset=8225280 $VM_IMAGE $COPY_DIR |
Dean Troyer | 8e99829 | 2011-11-07 16:41:47 -0600 | [diff] [blame] | 135 | mount -o bind /dev $COPY_DIR/dev |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 136 | |
| 137 | # git clone only if directory doesn't exist already. Since ``DEST`` might not |
| 138 | # be owned by the installation user, we create the directory and change the |
| 139 | # ownership to the proper user. |
| 140 | function git_clone { |
| 141 | if [ ! -d $2 ]; then |
| 142 | sudo mkdir $2 |
| 143 | sudo chown `whoami` $2 |
| 144 | git clone $1 $2 |
| 145 | cd $2 |
| 146 | # This checkout syntax works for both branches and tags |
| 147 | git checkout $3 |
| 148 | fi |
| 149 | } |
| 150 | |
| 151 | # Make sure that base requirements are installed |
| 152 | cp /etc/resolv.conf $COPY_DIR/etc/resolv.conf |
| 153 | chroot $COPY_DIR apt-get update |
Anthony Young | ca2c047 | 2011-11-03 16:29:32 -0700 | [diff] [blame] | 154 | chroot $COPY_DIR apt-get install -y --download-only `cat files/apts/* | grep NOPRIME | cut -d\# -f1` |
| 155 | chroot $COPY_DIR apt-get install -y --force-yes `cat files/apts/* | grep -v NOPRIME | cut -d\# -f1` |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 156 | chroot $COPY_DIR pip install `cat files/pips/*` |
| 157 | |
Dean Troyer | 8e99829 | 2011-11-07 16:41:47 -0600 | [diff] [blame] | 158 | umount $COPY_DIR/dev |
| 159 | |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 160 | # Clean out code repos if directed to do so |
| 161 | if [ "$CLEAN" = "1" ]; then |
| 162 | rm -rf $COPY_DIR/$DEST |
| 163 | fi |
| 164 | |
| 165 | # Cache openstack code |
| 166 | mkdir -p $COPY_DIR/$DEST |
| 167 | git_clone $NOVA_REPO $COPY_DIR/$DEST/nova $NOVA_BRANCH |
| 168 | git_clone $GLANCE_REPO $COPY_DIR/$DEST/glance $GLANCE_BRANCH |
| 169 | git_clone $KEYSTONE_REPO $COPY_DIR/$DESTkeystone $KEYSTONE_BRANCH |
| 170 | git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH |
| 171 | git_clone $HORIZON_REPO $COPY_DIR/$DEST/horizon $HORIZON_BRANCH $HORIZON_TAG |
| 172 | git_clone $NOVACLIENT_REPO $COPY_DIR/$DEST/python-novaclient $NOVACLIENT_BRANCH |
| 173 | git_clone $OPENSTACKX_REPO $COPY_DIR/$DEST/openstackx $OPENSTACKX_BRANCH |
| 174 | git_clone $KEYSTONE_REPO $COPY_DIR/$DEST/keystone $KEYSTONE_BRANCH |
| 175 | git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH |
Dean Troyer | 264aba5 | 2011-11-04 13:22:09 -0500 | [diff] [blame] | 176 | git_clone $CITEST_REPO $COPY_DIR/$DEST/openstack-integration-tests $CITEST_BRANCH |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 177 | |
Dean Troyer | 8e99829 | 2011-11-07 16:41:47 -0600 | [diff] [blame] | 178 | if [ -z "$UPLOAD_LEGACY_TTY" =; then |
| 179 | # Pre-load an image for testing |
| 180 | UEC_NAME=$DIST_NAME-server-cloudimg-amd64 |
| 181 | CIVMDIR=${COPY_DIR}${DEST}/openstack-integration-tests/include/sample_vm |
| 182 | if [ ! -e $CIVMDIR/$UEC_NAME.tar.gz ]; then |
| 183 | mkdir -p $CIVMDIR |
| 184 | (cd $CIVMDIR && wget -N http://uec-images.ubuntu.com/$DIST_NAME/current/$UEC_NAME.tar.gz; |
| 185 | tar xzf $UEC_NAME.tar.gz;) |
| 186 | fi |
Dean Troyer | 89d1d02 | 2011-11-05 16:16:54 -0500 | [diff] [blame] | 187 | fi |
| 188 | |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 189 | # Back to devstack |
| 190 | cd $TOP_DIR |
| 191 | |
| 192 | # Unmount the filesystems |
| 193 | unmount_images |
| 194 | |
| 195 | # Network configuration variables |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 196 | GUEST_NETWORK=${GUEST_NETWORK:-1} |
Jesse Andrews | 1d1dda1 | 2011-11-01 19:46:17 -0700 | [diff] [blame] | 197 | GUEST_RECREATE_NET=${GUEST_RECREATE_NET:-yes} |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 198 | |
| 199 | GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50} |
| 200 | GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24} |
| 201 | GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0} |
| 202 | GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.$GUEST_NETWORK.1} |
| 203 | GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $GUEST_NETWORK`"} |
| 204 | GUEST_RAM=${GUEST_RAM:-1524288} |
| 205 | GUEST_CORES=${GUEST_CORES:-1} |
| 206 | |
| 207 | # libvirt.xml configuration |
| 208 | NET_XML=$VM_DIR/net.xml |
| 209 | cat > $NET_XML <<EOF |
| 210 | <network> |
| 211 | <name>devstack-$GUEST_NETWORK</name> |
| 212 | <bridge name="stackbr%d" /> |
| 213 | <forward/> |
| 214 | <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK" /> |
| 215 | </network> |
| 216 | EOF |
| 217 | |
Jesse Andrews | 1d1dda1 | 2011-11-01 19:46:17 -0700 | [diff] [blame] | 218 | if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then |
| 219 | virsh net-destroy devstack-$GUEST_NETWORK || true |
| 220 | virsh net-create $VM_DIR/net.xml |
| 221 | fi |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 222 | |
| 223 | # libvirt.xml configuration |
| 224 | LIBVIRT_XML=$VM_DIR/libvirt.xml |
| 225 | cat > $LIBVIRT_XML <<EOF |
| 226 | <domain type='kvm'> |
| 227 | <name>$GUEST_NAME</name> |
| 228 | <memory>$GUEST_RAM</memory> |
| 229 | <os> |
| 230 | <type>hvm</type> |
| 231 | <bootmenu enable='yes'/> |
| 232 | </os> |
| 233 | <features> |
| 234 | <acpi/> |
| 235 | </features> |
| 236 | <vcpu>$GUEST_CORES</vcpu> |
| 237 | <devices> |
| 238 | <disk type='file'> |
| 239 | <driver type='qcow2'/> |
| 240 | <source file='$VM_DIR/disk'/> |
| 241 | <target dev='vda' bus='virtio'/> |
| 242 | </disk> |
| 243 | |
| 244 | <interface type='network'> |
| 245 | <source network='devstack-$GUEST_NETWORK'/> |
| 246 | </interface> |
| 247 | |
| 248 | <!-- The order is significant here. File must be defined first --> |
| 249 | <serial type="file"> |
| 250 | <source path='$VM_DIR/console.log'/> |
| 251 | <target port='1'/> |
| 252 | </serial> |
| 253 | |
| 254 | <console type='pty' tty='/dev/pts/2'> |
| 255 | <source path='/dev/pts/2'/> |
| 256 | <target port='0'/> |
| 257 | </console> |
| 258 | |
| 259 | <serial type='pty'> |
| 260 | <source path='/dev/pts/2'/> |
| 261 | <target port='0'/> |
| 262 | </serial> |
| 263 | |
| 264 | <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/> |
| 265 | </devices> |
| 266 | </domain> |
| 267 | EOF |
| 268 | |
| 269 | # Mount point for instance fs |
| 270 | ROOTFS=$VM_DIR/root |
| 271 | mkdir -p $ROOTFS |
| 272 | |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 273 | # Clean up from previous runs |
| 274 | umount $ROOTFS || echo 'ok' |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 275 | |
| 276 | # Clean up old runs |
| 277 | cd $VM_DIR |
| 278 | rm -f $VM_DIR/disk |
| 279 | |
| 280 | # Create our instance fs |
| 281 | qemu-img create -f qcow2 -b $VM_IMAGE disk |
| 282 | |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 283 | # Finds the next available NBD device |
| 284 | # Exits script if error connecting or none free |
| 285 | # map_nbd image |
| 286 | # returns full nbd device path |
| 287 | function map_nbd { |
| 288 | for i in `seq 0 15`; do |
| 289 | if [ ! -e /sys/block/nbd$i/pid ]; then |
| 290 | NBD=/dev/nbd$i |
| 291 | # Connect to nbd and wait till it is ready |
| 292 | qemu-nbd -c $NBD $1 |
| 293 | if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then |
| 294 | echo "Couldn't connect $NBD" |
| 295 | exit 1 |
| 296 | fi |
| 297 | break |
| 298 | fi |
| 299 | done |
| 300 | if [ -z "$NBD" ]; then |
| 301 | echo "No free NBD slots" |
| 302 | exit 1 |
| 303 | fi |
| 304 | echo $NBD |
| 305 | } |
| 306 | |
Dean Troyer | 7a56973 | 2011-10-31 17:34:29 -0500 | [diff] [blame] | 307 | # Make sure we have nbd-ness |
| 308 | modprobe nbd max_part=63 |
| 309 | |
| 310 | # Set up nbd |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 311 | NBD=`map_nbd disk` |
Dean Troyer | 7a56973 | 2011-10-31 17:34:29 -0500 | [diff] [blame] | 312 | NBD_DEV=`basename $NBD` |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 313 | |
| 314 | # Mount the instance |
Jesse Andrews | 2b7d221 | 2011-10-30 19:37:56 -0700 | [diff] [blame] | 315 | mount ${NBD}p1 $ROOTFS |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 316 | |
| 317 | # Configure instance network |
| 318 | INTERFACES=$ROOTFS/etc/network/interfaces |
| 319 | cat > $INTERFACES <<EOF |
| 320 | auto lo |
| 321 | iface lo inet loopback |
| 322 | |
| 323 | auto eth0 |
| 324 | iface eth0 inet static |
| 325 | address $GUEST_IP |
| 326 | netmask $GUEST_NETMASK |
| 327 | gateway $GUEST_GATEWAY |
| 328 | EOF |
| 329 | |
| 330 | # User configuration for the instance |
| 331 | chroot $ROOTFS groupadd libvirtd || true |
| 332 | chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd |
Jesse Andrews | 5f894cd | 2011-10-30 19:52:50 -0700 | [diff] [blame] | 333 | cp -pr $TOP_DIR $ROOTFS/$DEST/devstack |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 334 | echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd |
| 335 | echo "stack:pass" | chroot $ROOTFS chpasswd |
| 336 | echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers |
| 337 | |
| 338 | # Gracefully cp only if source file/dir exists |
| 339 | function cp_it { |
| 340 | if [ -e $1 ] || [ -d $1 ]; then |
| 341 | cp -pRL $1 $2 |
| 342 | fi |
| 343 | } |
| 344 | |
| 345 | # Copy over your ssh keys and env if desired |
| 346 | COPYENV=${COPYENV:-1} |
| 347 | if [ "$COPYENV" = "1" ]; then |
| 348 | cp_it ~/.ssh $ROOTFS/$DEST/.ssh |
| 349 | cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys |
| 350 | cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig |
| 351 | cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc |
| 352 | cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc |
| 353 | fi |
| 354 | |
| 355 | # pre-cache uec images |
| 356 | for image_url in ${IMAGE_URLS//,/ }; do |
| 357 | IMAGE_FNAME=`basename "$image_url"` |
| 358 | if [ ! -f $IMAGES_DIR/$IMAGE_FNAME ]; then |
| 359 | wget -c $image_url -O $IMAGES_DIR/$IMAGE_FNAME |
| 360 | fi |
| 361 | cp $IMAGES_DIR/$IMAGE_FNAME $ROOTFS/$DEST/devstack/files |
| 362 | done |
| 363 | |
| 364 | # Configure the runner |
| 365 | RUN_SH=$ROOTFS/$DEST/run.sh |
| 366 | cat > $RUN_SH <<EOF |
| 367 | #!/usr/bin/env bash |
| 368 | |
| 369 | # Kill any existing screens |
| 370 | killall screen |
| 371 | |
| 372 | # Install and run stack.sh |
| 373 | sudo apt-get update |
| 374 | sudo apt-get -y --force-yes install git-core vim-nox sudo |
| 375 | if [ ! -d "$DEST/devstack" ]; then |
| 376 | git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack |
| 377 | fi |
| 378 | cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log |
| 379 | echo >> /$DEST/run.sh.log |
| 380 | echo >> /$DEST/run.sh.log |
| 381 | echo "All done! Time to start clicking." >> /$DEST/run.sh.log |
| 382 | cat $DEST/run.sh.log |
| 383 | EOF |
| 384 | chmod 755 $RUN_SH |
| 385 | |
| 386 | # Make runner launch on boot |
Jesse Andrews | 2b7d221 | 2011-10-30 19:37:56 -0700 | [diff] [blame] | 387 | RC_LOCAL=$ROOTFS/etc/init.d/zlocal |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 388 | cat > $RC_LOCAL <<EOF |
| 389 | #!/bin/sh -e |
Jesse Andrews | ddcc36d | 2011-10-30 22:41:23 -0700 | [diff] [blame] | 390 | # cloud-init overwrites the hostname with ubuntuhost |
| 391 | echo $GUEST_NAME > /etc/hostname |
| 392 | hostname $GUEST_NAME |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 393 | su -c "$DEST/run.sh" stack |
| 394 | EOF |
| 395 | chmod +x $RC_LOCAL |
Jesse Andrews | 2b7d221 | 2011-10-30 19:37:56 -0700 | [diff] [blame] | 396 | chroot $ROOTFS sudo update-rc.d zlocal defaults 99 |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 397 | |
| 398 | # Make our ip address hostnames look nice at the command prompt |
| 399 | echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc |
| 400 | echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile |
| 401 | |
| 402 | # Give stack ownership over $DEST so it may do the work needed |
| 403 | chroot $ROOTFS chown -R stack $DEST |
| 404 | |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 405 | # Set the hostname |
| 406 | echo $GUEST_NAME > $ROOTFS/etc/hostname |
| 407 | |
| 408 | # We need the hostname to resolve for rabbit to launch |
| 409 | if ! grep -q $GUEST_NAME $ROOTFS/etc/hosts; then |
| 410 | echo "$GUEST_IP $GUEST_NAME" >> $ROOTFS/etc/hosts |
| 411 | fi |
| 412 | |
Dean Troyer | 6fe687b | 2011-10-31 20:30:04 -0500 | [diff] [blame] | 413 | # GRUB 2 wants to see /dev |
| 414 | mount -o bind /dev $ROOTFS/dev |
| 415 | |
Jesse Andrews | 2b7d221 | 2011-10-30 19:37:56 -0700 | [diff] [blame] | 416 | # Change boot params so that we get a console log |
| 417 | G_DEV_UUID=`blkid -t LABEL=cloudimg-rootfs -s UUID -o value | head -1` |
| 418 | sed -e "s/GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=3/" -i $ROOTFS/etc/default/grub |
Jesse Andrews | 2c5201b | 2011-10-30 20:44:26 -0700 | [diff] [blame] | 419 | sed -e "s,GRUB_CMDLINE_LINUX_DEFAULT=.*$,GRUB_CMDLINE_LINUX_DEFAULT=\"console=ttyS0 console=tty0 ds=nocloud ubuntu-pass=pass\",g" -i $ROOTFS/etc/default/grub |
Jesse Andrews | 2b7d221 | 2011-10-30 19:37:56 -0700 | [diff] [blame] | 420 | sed -e 's/[#]*GRUB_TERMINAL=.*$/GRUB_TERMINAL="serial console"/' -i $ROOTFS/etc/default/grub |
| 421 | echo 'GRUB_SERIAL_COMMAND="serial --unit=0"' >>$ROOTFS/etc/default/grub |
| 422 | echo 'GRUB_DISABLE_OS_PROBER=true' >>$ROOTFS/etc/default/grub |
| 423 | echo "GRUB_DEVICE_UUID=$G_DEV_UUID" >>$ROOTFS/etc/default/grub |
| 424 | |
| 425 | chroot $ROOTFS update-grub |
Jesse Andrews | 2b7d221 | 2011-10-30 19:37:56 -0700 | [diff] [blame] | 426 | |
| 427 | # Pre-generate ssh host keys and allow password login |
| 428 | chroot $ROOTFS dpkg-reconfigure openssh-server |
| 429 | sed -e 's/^PasswordAuthentication.*$/PasswordAuthentication yes/' -i $ROOTFS/etc/ssh/sshd_config |
| 430 | |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 431 | # Unmount |
Dean Troyer | 8e99829 | 2011-11-07 16:41:47 -0600 | [diff] [blame] | 432 | umount $ROOTFS/dev |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 433 | umount $ROOTFS || echo 'ok' |
Dean Troyer | 55c0273 | 2011-11-01 17:44:03 -0500 | [diff] [blame] | 434 | ROOTFS="" |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 435 | qemu-nbd -d $NBD |
Dean Troyer | 55c0273 | 2011-11-01 17:44:03 -0500 | [diff] [blame] | 436 | NBD="" |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 437 | |
Dean Troyer | 8e99829 | 2011-11-07 16:41:47 -0600 | [diff] [blame] | 438 | trap - SIGHUP SIGINT SIGTERM SIGQUIT EXIT |
| 439 | |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 440 | # Create the instance |
| 441 | cd $VM_DIR && virsh create libvirt.xml |
| 442 | |
| 443 | # Tail the console log till we are done |
| 444 | WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1} |
| 445 | if [ "$WAIT_TILL_LAUNCH" = "1" ]; then |
| 446 | # Done creating the container, let's tail the log |
| 447 | echo |
| 448 | echo "=============================================================" |
| 449 | echo " -- YAY! --" |
| 450 | echo "=============================================================" |
| 451 | echo |
| 452 | echo "We're done launching the vm, about to start tailing the" |
| 453 | echo "stack.sh log. It will take a second or two to start." |
| 454 | echo |
| 455 | echo "Just CTRL-C at any time to stop tailing." |
| 456 | |
| 457 | while [ ! -e "$VM_DIR/console.log" ]; do |
| 458 | sleep 1 |
| 459 | done |
| 460 | |
Jesse Andrews | 1634196 | 2011-10-31 00:21:56 -0700 | [diff] [blame] | 461 | tail -F $VM_DIR/console.log & |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 462 | |
| 463 | TAIL_PID=$! |
| 464 | |
| 465 | function kill_tail() { |
| 466 | kill $TAIL_PID |
| 467 | exit 1 |
| 468 | } |
| 469 | |
| 470 | # Let Ctrl-c kill tail and exit |
| 471 | trap kill_tail SIGINT |
| 472 | |
Jesse Andrews | 1634196 | 2011-10-31 00:21:56 -0700 | [diff] [blame] | 473 | set +o xtrace |
| 474 | |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 475 | echo "Waiting stack.sh to finish..." |
| 476 | while ! cat $VM_DIR/console.log | grep -q 'All done' ; do |
Jesse Andrews | 1634196 | 2011-10-31 00:21:56 -0700 | [diff] [blame] | 477 | sleep 1 |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 478 | done |
| 479 | |
Jesse Andrews | 1634196 | 2011-10-31 00:21:56 -0700 | [diff] [blame] | 480 | set -o xtrace |
| 481 | |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 482 | kill $TAIL_PID |
| 483 | |
Jesse Andrews | a06ac1c | 2011-10-31 22:29:23 -0700 | [diff] [blame] | 484 | if ! grep -q "^stack.sh completed in" $VM_DIR/console.log; then |
Jesse Andrews | e97a2e7 | 2011-10-30 18:37:49 -0700 | [diff] [blame] | 485 | exit 1 |
| 486 | fi |
| 487 | echo "" |
| 488 | echo "Finished - Zip-a-dee Doo-dah!" |
| 489 | fi |