more progress on build_kvm

This commit is contained in:
Anthony Young 2011-10-19 11:27:02 -07:00
parent f6f52270a9
commit fa4b5eba20
2 changed files with 115 additions and 43 deletions

View File

@ -1,18 +1,32 @@
#!/usr/bin/env bash
UBUNTU_VERSION=`cat /etc/lsb-release | grep CODENAME | sed 's/.*=//g'`
if [ ! "oneiric" = "$UBUNTU_VERSION" ]; then
echo "This script only works with oneiric"
exit 1
fi
# Echo commands
set -o xtrace
# Keep track of the current directory
TOOLS_DIR=$(cd $(dirname "$0") && pwd)
TOP_DIR=$TOOLS_DIR/..
# Configure the root password of the vm
ROOT_PASSWORD=${ROOT_PASSWORD:password}
PERSIST_DIR=${PERSIST_DIR:-/opt/kvmstack}
IMAGES_DIR=$PERSIST_DIR/images
mkdir -p $UEC_DIR
# Where to store files and instances
KVMSTACK_DIR=${KVMSTACK_DIR:-/opt/kvmstack}
# Where to store images
IMAGES_DIR=$KVMSTACK_DIR/images
# Create images dir
mkdir -p $IMAGES_DIR
# Move to top devstack dir
cd ..
cd $TOP_DIR
# Abort if localrc is not set
if [ ! -e ./localrc ]; then
@ -24,26 +38,34 @@ fi
# Source params
source ./stackrc
# Base image (oneiric by default)
IMAGE_FNAME=natty.raw
IMAGE_NAME=natty
# Base image (natty by default)
DIST_NAME=${DIST_NAME:-natty}
IMAGE_FNAME=$DIST_NAME.raw
BASE_IMAGE=$PERSIST_DIR/images/natty.raw
BASE_IMAGE_COPY=$IMAGES_DIR/$IMAGE_NAME.raw.copy
# Original version of built image
BASE_IMAGE=$KVMSTACK_DIR/images/natty.raw
# Copy of base image, which we pre-install with tasty treats
BASE_IMAGE_COPY=$IMAGES_DIR/$DIST_NAME.raw.copy
# Name of our instance, used by libvirt
VM_NAME=${VM_NAME:-kvmstack}
# Mop up after previous runs
virsh shutdown $VM_NAME
virsh destroy $VM_NAME
VM_DIR=$PERSIST_DIR/instances/$VM_NAME
# Where this vm is stored
VM_DIR=$KVMSTACK_DIR/instances/$VM_NAME
# Create vm dir
mkdir -p $VM_DIR
# Where to mount
# Mount point into copied base image
COPY_DIR=$VM_DIR/copy
mkdir -p $COPY_DIR
# Create the base image if it does not yet exist
if [ ! -e $IMAGES_DIR/$IMAGE_FNAME ]; then
cd $TOOLS_DIR
./make_image.sh -m -r 5000 natty raw
@ -51,6 +73,12 @@ if [ ! -e $IMAGES_DIR/$IMAGE_FNAME ]; then
cd $TOP_DIR
fi
# Create a copy of the base image
if [ ! -e $BASE_IMAGE_COPY ]; then
cp -p $BASE_IMAGE $BASE_IMAGE_COPY
fi
# Unmount the copied base image
function unmount_images() {
# unmount the filesystem
while df | grep -q $COPY_DIR; do
@ -59,25 +87,22 @@ function unmount_images() {
done
}
# unmount from failed runs
# Unmount from failed runs
unmount_images
function kill_tail() {
# Ctrl-c catcher
function kill_unmount() {
unmount_images
exit 1
}
if [ ! -e $BASE_IMAGE_COPY ]; then
cp -p $BASE_IMAGE $BASE_IMAGE_COPY
fi
# Install deps
apt-get install -y kvm libvirt-bin kpartx
apt-get install -y --force-yes kvm libvirt-bin kpartx
# Let Ctrl-c kill tail and exit
trap kill_tail SIGINT
trap kill_unmount SIGINT
# Where code will live in image
# Where Openstack code will live in image
DEST=${DEST:-/opt/stack}
# Mount the file system
@ -121,15 +146,20 @@ git_clone $OPENSTACKX_REPO $COPY_DIR/$DEST/openstackx $OPENSTACKX_BRANCH
git_clone $KEYSTONE_REPO $COPY_DIR/$DEST/keystone $KEYSTONE_BRANCH
git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
# unmount the filesystems
# Back to devstack
cd $TOP_DIR
# Unmount the filesystems
unmount_images
rm -f $VM_DIR/kernel
# Clean up old runs
cd $VM_DIR
rm -f $VM_DIR/disk
cd $VM_DIR
# Clean up old instance data
qemu-img create -f qcow2 -b $BASE_IMAGE_COPY disk
# Network configuration variables
BRIDGE=${BRIDGE:-br0}
CONTAINER=${CONTAINER:-STACK}
CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
@ -138,7 +168,7 @@ CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
CONTAINER_MAC=${CONTAINER_MAC:-02:16:3e:07:70:d7}
# Create configuration
# libvirt.xml configuration
LIBVIRT_XML=libvirt.xml
cat > $LIBVIRT_XML <<EOF
<domain type='kvm'>
@ -146,11 +176,8 @@ cat > $LIBVIRT_XML <<EOF
<memory>1524288</memory>
<os>
<type>hvm</type>
<boot dev='hd'/>
<bootmenu enable='yes'/>
<!--
<kernel>$VM_DIR/kernel</kernel>
<cmdline>root=/dev/vda console=ttyS0</cmdline>
-->
</os>
<features>
<acpi/>
@ -189,14 +216,18 @@ cat > $LIBVIRT_XML <<EOF
</domain>
EOF
# Mount point for instance fs
ROOTFS=$VM_DIR/root
mkdir -p $ROOTFS
# Make sure we have nbd-ness
modprobe nbd max_part=63
# Clean up from previous runs
umount $ROOTFS || echo 'ok'
qemu-nbd -d /dev/nbd5 || echo 'ok'
# Mount the instance
qemu-nbd -c /dev/nbd5 disk
mount /dev/nbd5 $ROOTFS -o offset=32256 -t ext4
@ -213,15 +244,12 @@ iface eth0 inet static
gateway $CONTAINER_GATEWAY
EOF
# User configuration for the instance
chroot $ROOTFS groupadd libvirtd
chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
cp -pr $TOOLS_DIR/.. $ROOTFS/$DEST/devstack
echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
# a simple password - pass
echo "stack:pass" | chroot $ROOTFS chpasswd
# stack requires)
echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
# Gracefully cp only if source file/dir exists
@ -245,7 +273,6 @@ fi
RUN_SH=$ROOTFS/$DEST/run.sh
cat > $RUN_SH <<EOF
#!/usr/bin/env bash
sleep 1
# Kill any existing screens
killall screen
@ -261,16 +288,22 @@ echo >> /$DEST/run.sh.log
echo >> /$DEST/run.sh.log
echo "All done! Time to start clicking." >> /$DEST/run.sh.log
EOF
# Make the run.sh executable
chmod 755 $RUN_SH
# Make runner launch on boot
RC_LOCAL=$ROOTFS/etc/init.d/local
cat > $RC_LOCAL <<EOF
#!/bin/sh -e
# Reboot if this is our first run to enable console log on natty :(
if [ ! -e /root/firstlaunch ]; then
touch /root/firstlaunch
# reboot
exit 0
fi
su -c "$DEST/run.sh" stack
EOF
chmod +x $RC_LOCAL
chroot $ROOTFS sudo update-rc.d local defaults 80
# Make our ip address hostnames look nice at the command prompt
echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
@ -279,13 +312,52 @@ echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS
# Give stack ownership over $DEST so it may do the work needed
chroot $ROOTFS chown -R stack $DEST
chmod +x $RC_LOCAL
chroot $ROOTFS sudo update-rc.d local defaults 80
# Change boot params so that we get a console log
sudo sed -e "s/quiet splash/splash console=ttyS0 console=ttyS1,19200n8/g" -i $ROOTFS/boot/grub/menu.lst
umount $ROOTFS
# Unmount
umount $ROOTFS || echo 'ok'
qemu-nbd -d /dev/nbd5
cd $VM_DIR
virsh create libvirt.xml
# Create the instance
cd $VM_DIR && virsh create libvirt.xml
# Tail the console log till we are done
WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-0}
if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
# Done creating the container, let's tail the log
echo
echo "============================================================="
echo " -- YAY! --"
echo "============================================================="
echo
echo "We're done launching the vm, about to start tailing the"
echo "stack.sh log. It will take a second or two to start."
echo
echo "Just CTRL-C at any time to stop tailing."
while [ ! -e "$VM_DIR/console.log" ]; do
sleep 1
done
tail -F $VM_DIR/console.log &
TAIL_PID=$!
function kill_tail() {
kill $TAIL_PID
exit 1
}
# Let Ctrl-c kill tail and exit
trap kill_tail SIGINT
echo "Waiting stack.sh to finish..."
while ! cat $VM_DIR/console.log | grep -q 'stack.sh completed' ; do
sleep 5
done
kill $TAIL_PID
echo ""
echo "Finished - Zip-a-dee Doo-dah!"
fi

View File

@ -110,7 +110,7 @@ esac
# Install stuff if necessary
if [ -z `which vmbuilder` ]; then
sudo apt-get install ubuntu-vm-builder
sudo apt-get install -y ubuntu-vm-builder
fi
if [ -n "$CHROOTONLY" ]; then