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 #!/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 # Echo commands
set -o xtrace set -o xtrace
# Keep track of the current directory # Keep track of the current directory
TOOLS_DIR=$(cd $(dirname "$0") && pwd) TOOLS_DIR=$(cd $(dirname "$0") && pwd)
TOP_DIR=$TOOLS_DIR/..
# Configure the root password of the vm
ROOT_PASSWORD=${ROOT_PASSWORD:password} ROOT_PASSWORD=${ROOT_PASSWORD:password}
PERSIST_DIR=${PERSIST_DIR:-/opt/kvmstack}
IMAGES_DIR=$PERSIST_DIR/images # Where to store files and instances
mkdir -p $UEC_DIR 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 # Move to top devstack dir
cd .. cd $TOP_DIR
# Abort if localrc is not set # Abort if localrc is not set
if [ ! -e ./localrc ]; then if [ ! -e ./localrc ]; then
@ -24,26 +38,34 @@ fi
# Source params # Source params
source ./stackrc source ./stackrc
# Base image (oneiric by default) # Base image (natty by default)
IMAGE_FNAME=natty.raw DIST_NAME=${DIST_NAME:-natty}
IMAGE_NAME=natty IMAGE_FNAME=$DIST_NAME.raw
BASE_IMAGE=$PERSIST_DIR/images/natty.raw # Original version of built image
BASE_IMAGE_COPY=$IMAGES_DIR/$IMAGE_NAME.raw.copy 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} VM_NAME=${VM_NAME:-kvmstack}
# Mop up after previous runs
virsh shutdown $VM_NAME virsh shutdown $VM_NAME
virsh destroy $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 mkdir -p $VM_DIR
# Where to mount # Mount point into copied base image
COPY_DIR=$VM_DIR/copy COPY_DIR=$VM_DIR/copy
mkdir -p $COPY_DIR mkdir -p $COPY_DIR
# Create the base image if it does not yet exist
if [ ! -e $IMAGES_DIR/$IMAGE_FNAME ]; then if [ ! -e $IMAGES_DIR/$IMAGE_FNAME ]; then
cd $TOOLS_DIR cd $TOOLS_DIR
./make_image.sh -m -r 5000 natty raw ./make_image.sh -m -r 5000 natty raw
@ -51,6 +73,12 @@ if [ ! -e $IMAGES_DIR/$IMAGE_FNAME ]; then
cd $TOP_DIR cd $TOP_DIR
fi 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() { function unmount_images() {
# unmount the filesystem # unmount the filesystem
while df | grep -q $COPY_DIR; do while df | grep -q $COPY_DIR; do
@ -59,25 +87,22 @@ function unmount_images() {
done done
} }
# unmount from failed runs # Unmount from failed runs
unmount_images unmount_images
function kill_tail() { # Ctrl-c catcher
function kill_unmount() {
unmount_images unmount_images
exit 1 exit 1
} }
if [ ! -e $BASE_IMAGE_COPY ]; then
cp -p $BASE_IMAGE $BASE_IMAGE_COPY
fi
# Install deps # 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 # 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} DEST=${DEST:-/opt/stack}
# Mount the file system # 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 $KEYSTONE_REPO $COPY_DIR/$DEST/keystone $KEYSTONE_BRANCH
git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_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 unmount_images
rm -f $VM_DIR/kernel # Clean up old runs
cd $VM_DIR
rm -f $VM_DIR/disk rm -f $VM_DIR/disk
cd $VM_DIR # Clean up old instance data
qemu-img create -f qcow2 -b $BASE_IMAGE_COPY disk qemu-img create -f qcow2 -b $BASE_IMAGE_COPY disk
# Network configuration variables
BRIDGE=${BRIDGE:-br0} BRIDGE=${BRIDGE:-br0}
CONTAINER=${CONTAINER:-STACK} CONTAINER=${CONTAINER:-STACK}
CONTAINER_IP=${CONTAINER_IP:-192.168.1.50} 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_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
CONTAINER_MAC=${CONTAINER_MAC:-02:16:3e:07:70:d7} CONTAINER_MAC=${CONTAINER_MAC:-02:16:3e:07:70:d7}
# Create configuration # libvirt.xml configuration
LIBVIRT_XML=libvirt.xml LIBVIRT_XML=libvirt.xml
cat > $LIBVIRT_XML <<EOF cat > $LIBVIRT_XML <<EOF
<domain type='kvm'> <domain type='kvm'>
@ -146,11 +176,8 @@ cat > $LIBVIRT_XML <<EOF
<memory>1524288</memory> <memory>1524288</memory>
<os> <os>
<type>hvm</type> <type>hvm</type>
<boot dev='hd'/>
<bootmenu enable='yes'/> <bootmenu enable='yes'/>
<!--
<kernel>$VM_DIR/kernel</kernel>
<cmdline>root=/dev/vda console=ttyS0</cmdline>
-->
</os> </os>
<features> <features>
<acpi/> <acpi/>
@ -189,14 +216,18 @@ cat > $LIBVIRT_XML <<EOF
</domain> </domain>
EOF EOF
# Mount point for instance fs
ROOTFS=$VM_DIR/root ROOTFS=$VM_DIR/root
mkdir -p $ROOTFS mkdir -p $ROOTFS
# Make sure we have nbd-ness
modprobe nbd max_part=63 modprobe nbd max_part=63
# Clean up from previous runs
umount $ROOTFS || echo 'ok' umount $ROOTFS || echo 'ok'
qemu-nbd -d /dev/nbd5 || echo 'ok' qemu-nbd -d /dev/nbd5 || echo 'ok'
# Mount the instance
qemu-nbd -c /dev/nbd5 disk qemu-nbd -c /dev/nbd5 disk
mount /dev/nbd5 $ROOTFS -o offset=32256 -t ext4 mount /dev/nbd5 $ROOTFS -o offset=32256 -t ext4
@ -213,15 +244,12 @@ iface eth0 inet static
gateway $CONTAINER_GATEWAY gateway $CONTAINER_GATEWAY
EOF EOF
# User configuration for the instance
chroot $ROOTFS groupadd libvirtd chroot $ROOTFS groupadd libvirtd
chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
cp -pr $TOOLS_DIR/.. $ROOTFS/$DEST/devstack cp -pr $TOOLS_DIR/.. $ROOTFS/$DEST/devstack
echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
# a simple password - pass
echo "stack:pass" | chroot $ROOTFS chpasswd echo "stack:pass" | chroot $ROOTFS chpasswd
# stack requires)
echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
# Gracefully cp only if source file/dir exists # Gracefully cp only if source file/dir exists
@ -245,7 +273,6 @@ fi
RUN_SH=$ROOTFS/$DEST/run.sh RUN_SH=$ROOTFS/$DEST/run.sh
cat > $RUN_SH <<EOF cat > $RUN_SH <<EOF
#!/usr/bin/env bash #!/usr/bin/env bash
sleep 1
# Kill any existing screens # Kill any existing screens
killall screen killall screen
@ -261,16 +288,22 @@ echo >> /$DEST/run.sh.log
echo >> /$DEST/run.sh.log echo >> /$DEST/run.sh.log
echo "All done! Time to start clicking." >> /$DEST/run.sh.log echo "All done! Time to start clicking." >> /$DEST/run.sh.log
EOF EOF
# Make the run.sh executable
chmod 755 $RUN_SH chmod 755 $RUN_SH
# Make runner launch on boot # Make runner launch on boot
RC_LOCAL=$ROOTFS/etc/init.d/local RC_LOCAL=$ROOTFS/etc/init.d/local
cat > $RC_LOCAL <<EOF cat > $RC_LOCAL <<EOF
#!/bin/sh -e #!/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 su -c "$DEST/run.sh" stack
EOF 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 # Make our ip address hostnames look nice at the command prompt
echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc 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 # Give stack ownership over $DEST so it may do the work needed
chroot $ROOTFS chown -R stack $DEST chroot $ROOTFS chown -R stack $DEST
chmod +x $RC_LOCAL # Change boot params so that we get a console log
chroot $ROOTFS sudo update-rc.d local defaults 80
sudo sed -e "s/quiet splash/splash console=ttyS0 console=ttyS1,19200n8/g" -i $ROOTFS/boot/grub/menu.lst 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 qemu-nbd -d /dev/nbd5
cd $VM_DIR # Create the instance
virsh create libvirt.xml 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 # Install stuff if necessary
if [ -z `which vmbuilder` ]; then if [ -z `which vmbuilder` ]; then
sudo apt-get install ubuntu-vm-builder sudo apt-get install -y ubuntu-vm-builder
fi fi
if [ -n "$CHROOTONLY" ]; then if [ -n "$CHROOTONLY" ]; then