devstack_custom/tools/build_kvm.sh

396 lines
10 KiB
Bash
Raw Normal View History

2011-10-19 09:34:06 +00:00
#!/usr/bin/env bash
2011-10-24 19:42:43 +00:00
# exit on error to stop unexpected errors
set -o errexit
2011-10-20 03:35:04 +00:00
# Make sure that we have the proper version of ubuntu
2011-10-19 18:27:02 +00:00
UBUNTU_VERSION=`cat /etc/lsb-release | grep CODENAME | sed 's/.*=//g'`
if [ ! "oneiric" = "$UBUNTU_VERSION" ]; then
2011-10-20 03:35:04 +00:00
if [ ! "natty" = "$UBUNTU_VERSION" ]; then
echo "This script only works with oneiric and natty"
exit 1
fi
2011-10-19 18:27:02 +00:00
fi
2011-10-19 09:34:06 +00:00
# Echo commands
set -o xtrace
# Keep track of the current directory
TOOLS_DIR=$(cd $(dirname "$0") && pwd)
2011-10-19 18:27:02 +00:00
TOP_DIR=$TOOLS_DIR/..
2011-10-19 09:34:06 +00:00
2011-10-19 18:27:02 +00:00
# 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
2011-10-19 09:34:06 +00:00
# Move to top devstack dir
2011-10-19 18:27:02 +00:00
cd $TOP_DIR
2011-10-19 09:34:06 +00:00
# Abort if localrc is not set
if [ ! -e ./localrc ]; then
echo "You must have a localrc with ALL necessary passwords defined before proceeding."
echo "See stack.sh for required passwords."
exit 1
fi
# Source params
source ./stackrc
# Configure the root password of the vm to be the same as ``ADMIN_PASSWORD``
ROOT_PASSWORD=${ADMIN_PASSWORD:-password}
2011-10-19 18:27:02 +00:00
# Base image (natty by default)
DIST_NAME=${DIST_NAME:-natty}
IMAGE_FNAME=$DIST_NAME.raw
# Name of our instance, used by libvirt
GUEST_NAME=${GUEST_NAME:-kvmstack}
2011-10-19 18:27:02 +00:00
# Original version of built image
2011-10-21 19:29:30 +00:00
BASE_IMAGE=$KVMSTACK_DIR/images/$DIST_NAME.raw
2011-10-19 09:34:06 +00:00
2011-10-19 18:27:02 +00:00
# Copy of base image, which we pre-install with tasty treats
VM_IMAGE=$IMAGES_DIR/$DIST_NAME.$GUEST_NAME.raw
2011-10-19 18:27:02 +00:00
# Mop up after previous runs
virsh destroy $GUEST_NAME || true
2011-10-19 09:34:06 +00:00
2011-10-19 18:27:02 +00:00
# Where this vm is stored
VM_DIR=$KVMSTACK_DIR/instances/$GUEST_NAME
2011-10-19 09:34:06 +00:00
2011-10-19 18:27:02 +00:00
# Create vm dir
2011-10-19 09:34:06 +00:00
mkdir -p $VM_DIR
2011-10-19 18:27:02 +00:00
# Mount point into copied base image
2011-10-19 09:34:06 +00:00
COPY_DIR=$VM_DIR/copy
mkdir -p $COPY_DIR
2011-10-19 18:27:02 +00:00
# Create the base image if it does not yet exist
2011-10-19 09:34:06 +00:00
if [ ! -e $IMAGES_DIR/$IMAGE_FNAME ]; then
cd $TOOLS_DIR
2011-10-21 19:29:30 +00:00
./make_image.sh -m -r 5000 $DIST_NAME raw
mv $DIST_NAME.raw $BASE_IMAGE
2011-10-19 09:34:06 +00:00
cd $TOP_DIR
fi
2011-10-19 18:27:02 +00:00
# Create a copy of the base image
2011-10-21 19:58:44 +00:00
if [ ! -e $VM_IMAGE ]; then
cp -p $BASE_IMAGE $VM_IMAGE
2011-10-19 18:27:02 +00:00
fi
# Unmount the copied base image
2011-10-19 09:34:06 +00:00
function unmount_images() {
# unmount the filesystem
while df | grep -q $COPY_DIR; do
umount $COPY_DIR || echo 'ok'
sleep 1
done
}
2011-10-19 18:27:02 +00:00
# Unmount from failed runs
2011-10-19 09:34:06 +00:00
unmount_images
2011-10-19 18:27:02 +00:00
# Ctrl-c catcher
function kill_unmount() {
2011-10-19 09:34:06 +00:00
unmount_images
exit 1
}
# Install deps if needed
dpkg -l kvm libvirt-bin kpartx || apt-get install -y --force-yes kvm libvirt-bin kpartx
2011-10-19 09:34:06 +00:00
# Let Ctrl-c kill tail and exit
2011-10-19 18:27:02 +00:00
trap kill_unmount SIGINT
2011-10-19 09:34:06 +00:00
2011-10-19 18:27:02 +00:00
# Where Openstack code will live in image
2011-10-19 09:34:06 +00:00
DEST=${DEST:-/opt/stack}
# Mount the file system
2011-10-21 19:58:44 +00:00
mount -o loop,offset=32256 $VM_IMAGE $COPY_DIR
2011-10-19 09:34:06 +00:00
# git clone only if directory doesn't exist already. Since ``DEST`` might not
# be owned by the installation user, we create the directory and change the
# ownership to the proper user.
function git_clone {
if [ ! -d $2 ]; then
sudo mkdir $2
sudo chown `whoami` $2
git clone $1 $2
cd $2
# This checkout syntax works for both branches and tags
git checkout $3
fi
}
# Make sure that base requirements are installed
cp /etc/resolv.conf $COPY_DIR/etc/resolv.conf
chroot $COPY_DIR apt-get update
chroot $COPY_DIR apt-get install -y --force-yes `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
chroot $COPY_DIR apt-get install -y --download-only rabbitmq-server libvirt-bin mysql-server
chroot $COPY_DIR pip install `cat files/pips/*`
# Clean out code repos if directed to do so
if [ "$CLEAN" = "1" ]; then
rm -rf $COPY_DIR/$DEST
fi
# Cache openstack code
mkdir -p $COPY_DIR/$DEST
git_clone $NOVA_REPO $COPY_DIR/$DEST/nova $NOVA_BRANCH
git_clone $GLANCE_REPO $COPY_DIR/$DEST/glance $GLANCE_BRANCH
git_clone $KEYSTONE_REPO $COPY_DIR/$DESTkeystone $KEYSTONE_BRANCH
git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
git_clone $DASH_REPO $COPY_DIR/$DEST/dash $DASH_BRANCH $DASH_TAG
git_clone $NOVACLIENT_REPO $COPY_DIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
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
2011-10-20 19:32:58 +00:00
# Back to devstack
cd $TOP_DIR
2011-10-19 09:34:06 +00:00
2011-10-24 19:38:13 +00:00
# Unmount the filesystems
unmount_images
2011-10-19 18:27:02 +00:00
# Network configuration variables
2011-10-19 09:34:06 +00:00
BRIDGE=${BRIDGE:-br0}
GUEST_IP=${GUEST_IP:-192.168.1.50}
GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.1.1}
GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $(echo $GUEST_IP | sed "s/.*\.//")`"}
GUEST_RAM=${GUEST_RAM:-1524288}
GUEST_CORES=${GUEST_CORES:-1}
2011-10-19 09:34:06 +00:00
2011-10-19 18:27:02 +00:00
# libvirt.xml configuration
LIBVIRT_XML=$VM_DIR/libvirt.xml
2011-10-19 09:34:06 +00:00
cat > $LIBVIRT_XML <<EOF
<domain type='kvm'>
<name>$GUEST_NAME</name>
<memory>$GUEST_RAM</memory>
2011-10-19 09:34:06 +00:00
<os>
2011-10-20 03:09:43 +00:00
<type>hvm</type>
<bootmenu enable='yes'/>
2011-10-19 09:34:06 +00:00
</os>
<features>
<acpi/>
</features>
<vcpu>$GUEST_CORES</vcpu>
2011-10-19 09:34:06 +00:00
<devices>
<disk type='file'>
<driver type='qcow2'/>
<source file='$VM_DIR/disk'/>
<target dev='vda' bus='virtio'/>
</disk>
<interface type='bridge'>
<source bridge='$BRIDGE'/>
<mac address='$GUEST_MAC'/>
2011-10-19 09:34:06 +00:00
</interface>
<!-- The order is significant here. File must be defined first -->
<serial type="file">
<source path='$VM_DIR/console.log'/>
<target port='1'/>
</serial>
<console type='pty' tty='/dev/pts/2'>
<source path='/dev/pts/2'/>
<target port='0'/>
</console>
<serial type='pty'>
<source path='/dev/pts/2'/>
<target port='0'/>
</serial>
<graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
</devices>
</domain>
EOF
2011-10-19 18:27:02 +00:00
# Mount point for instance fs
2011-10-19 09:34:06 +00:00
ROOTFS=$VM_DIR/root
mkdir -p $ROOTFS
2011-10-19 18:27:02 +00:00
# Make sure we have nbd-ness
2011-10-19 09:34:06 +00:00
modprobe nbd max_part=63
2011-10-20 03:22:32 +00:00
# Which NBD device to use?
NBD=${NBD:-/dev/nbd5}
2011-10-19 18:27:02 +00:00
# Clean up from previous runs
2011-10-19 09:34:06 +00:00
umount $ROOTFS || echo 'ok'
2011-10-20 03:22:32 +00:00
qemu-nbd -d $NBD || echo 'ok'
2011-10-19 09:34:06 +00:00
2011-10-20 19:32:58 +00:00
# Clean up old runs
cd $VM_DIR
rm -f $VM_DIR/disk
# Create our instance fs
2011-10-21 19:58:44 +00:00
qemu-img create -f qcow2 -b $VM_IMAGE disk
2011-10-20 19:32:58 +00:00
# Connect our nbd and wait till it is mountable
2011-10-20 03:22:32 +00:00
qemu-nbd -c $NBD disk
NBD_DEV=`basename $NBD`
if ! timeout 60 sh -c "while ! [ -e /sys/block/$NBD_DEV/pid ]; do sleep 1; done"; then
echo "Couldn't connect $NBD"
exit 1
fi
# Mount the instance
2011-10-20 03:22:32 +00:00
mount $NBD $ROOTFS -o offset=32256 -t ext4
2011-10-19 09:34:06 +00:00
# Configure instance network
INTERFACES=$ROOTFS/etc/network/interfaces
cat > $INTERFACES <<EOF
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address $GUEST_IP
netmask $GUEST_NETMASK
gateway $GUEST_GATEWAY
2011-10-19 09:34:06 +00:00
EOF
2011-10-19 18:27:02 +00:00
# User configuration for the instance
chroot $ROOTFS groupadd libvirtd || true
2011-10-19 09:34:06 +00:00
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
echo "stack:pass" | chroot $ROOTFS chpasswd
echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
# Gracefully cp only if source file/dir exists
function cp_it {
if [ -e $1 ] || [ -d $1 ]; then
cp -pRL $1 $2
fi
}
# Copy over your ssh keys and env if desired
COPYENV=${COPYENV:-1}
if [ "$COPYENV" = "1" ]; then
cp_it ~/.ssh $ROOTFS/$DEST/.ssh
cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
fi
2011-10-26 06:41:59 +00:00
# pre-cache uec images
for image_url in ${IMAGE_URLS//,/ }; do
IMAGE_FNAME=`basename "$image_url"`
if [ ! -f $IMAGES_DIR/$IMAGE_FNAME ]; then
wget -c $image_url -O $IMAGES_DIR/$IMAGE_FNAME
fi
2011-10-26 06:51:43 +00:00
cp $IMAGES_DIR/$IMAGE_FNAME $ROOTFS/$DEST/devstack/files
2011-10-26 06:43:03 +00:00
done
2011-10-26 06:41:59 +00:00
2011-10-19 09:34:06 +00:00
# Configure the runner
RUN_SH=$ROOTFS/$DEST/run.sh
cat > $RUN_SH <<EOF
#!/usr/bin/env bash
# Kill any existing screens
killall screen
# Install and run stack.sh
sudo apt-get update
sudo apt-get -y --force-yes install git-core vim-nox sudo
if [ ! -d "$DEST/devstack" ]; then
git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
fi
cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$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
2011-10-20 03:09:43 +00:00
cat $DEST/run.sh.log
2011-10-19 09:34:06 +00:00
EOF
chmod 755 $RUN_SH
# Make runner launch on boot
RC_LOCAL=$ROOTFS/etc/init.d/local
cat > $RC_LOCAL <<EOF
#!/bin/sh -e
2011-10-21 19:29:30 +00:00
# Reboot if this is our first run to enable console log on $DIST_NAME :(
2011-10-19 18:27:02 +00:00
if [ ! -e /root/firstlaunch ]; then
touch /root/firstlaunch
2011-10-20 03:09:43 +00:00
reboot -f
2011-10-19 18:27:02 +00:00
exit 0
fi
2011-10-19 09:34:06 +00:00
su -c "$DEST/run.sh" stack
EOF
2011-10-19 18:27:02 +00:00
chmod +x $RC_LOCAL
chroot $ROOTFS sudo update-rc.d local defaults 80
2011-10-19 09:34:06 +00:00
# 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/etc/profile
# Give stack ownership over $DEST so it may do the work needed
chroot $ROOTFS chown -R stack $DEST
2011-10-19 18:27:02 +00:00
# Change boot params so that we get a console log
2011-10-19 09:58:18 +00:00
sudo sed -e "s/quiet splash/splash console=ttyS0 console=ttyS1,19200n8/g" -i $ROOTFS/boot/grub/menu.lst
2011-10-20 03:09:43 +00:00
sudo sed -e "s/^hiddenmenu//g" -i $ROOTFS/boot/grub/menu.lst
#chroot $ROOTFS grub-install /dev/vda
2011-10-19 09:58:18 +00:00
2011-10-19 18:27:02 +00:00
# Unmount
umount $ROOTFS || echo 'ok'
2011-10-20 03:22:32 +00:00
qemu-nbd -d $NBD
2011-10-19 09:34:06 +00:00
2011-10-19 18:27:02 +00:00
# Create the instance
cd $VM_DIR && virsh create libvirt.xml
# Tail the console log till we are done
2011-10-20 03:09:43 +00:00
WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
2011-10-19 18:27:02 +00:00
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
}
2011-10-20 17:07:10 +00:00
2011-10-19 18:27:02 +00:00
# Let Ctrl-c kill tail and exit
trap kill_tail SIGINT
echo "Waiting stack.sh to finish..."
2011-10-20 03:09:43 +00:00
while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
2011-10-19 18:27:02 +00:00
sleep 5
done
kill $TAIL_PID
2011-10-25 05:25:16 +00:00
if grep -q "stack.sh failed" $VM_DIR/console.log; then
exit 1
fi
2011-10-19 18:27:02 +00:00
echo ""
echo "Finished - Zip-a-dee Doo-dah!"
fi