From 78bd0cbd9865df211200a70d196b34eed1c02725 Mon Sep 17 00:00:00 2001 From: Anthony Young Date: Wed, 19 Oct 2011 02:20:04 -0700 Subject: [PATCH 1/9] initial commit of build_kvm.sh --- files/apts/general | 4 ++++ stackrc | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/files/apts/general b/files/apts/general index 32379c0..b47a60d 100644 --- a/files/apts/general +++ b/files/apts/general @@ -12,3 +12,7 @@ vim-nox locate # useful when debugging python-virtualenv python-unittest2 +iputils-ping +wget +curl +tcpdump diff --git a/stackrc b/stackrc index aaee6ec..28cdc22 100644 --- a/stackrc +++ b/stackrc @@ -12,7 +12,7 @@ KEYSTONE_BRANCH=diablo # a websockets/html5 or flash powered VNC console for vm instances NOVNC_REPO=https://github.com/cloudbuilders/noVNC.git -NOVNC_BRANCH=master +NOVNC_BRANCH=diablo # django powered web control panel for openstack DASH_REPO=https://github.com/cloudbuilders/openstack-dashboard.git @@ -28,7 +28,7 @@ OPENSTACKX_REPO=https://github.com/cloudbuilders/openstackx.git OPENSTACKX_BRANCH=diablo # Specify a comma-separated list of uec images to download and install into glance. -IMAGE_URLS=http://smoser.brickies.net/ubuntu/ttylinux-uec/ttylinux-uec-amd64-11.2_2.6.35-15_1.tar.gz +IMAGE_URLS=http://smoser.brickies.net/ubuntu/ttylinux-uec/ttylinux-uec-amd64-11.2_2.6.35-15_1.tar.gz,http://uec-images.ubuntu.com/oneiric/current/oneiric-server-cloudimg-amd64.tar.gz # allow local overrides of env variables if [ -f ./localrc ]; then From 1b7a42e07598bc53e604b387a6e747039109020f Mon Sep 17 00:00:00 2001 From: Anthony Young Date: Wed, 19 Oct 2011 02:34:06 -0700 Subject: [PATCH 2/9] add build_kvm file --- tools/build_kvm.sh | 288 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 288 insertions(+) create mode 100755 tools/build_kvm.sh diff --git a/tools/build_kvm.sh b/tools/build_kvm.sh new file mode 100755 index 0000000..ade5115 --- /dev/null +++ b/tools/build_kvm.sh @@ -0,0 +1,288 @@ +#!/usr/bin/env bash + +# Echo commands +set -o xtrace + +# Keep track of the current directory +TOOLS_DIR=$(cd $(dirname "$0") && pwd) + +ROOT_PASSWORD=${ROOT_PASSWORD:password} +PERSIST_DIR=${PERSIST_DIR:-/opt/kvmstack} +IMAGES_DIR=$PERSIST_DIR/images +mkdir -p $UEC_DIR + +# Move to top devstack dir +cd .. + +# 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 + +# Base image (oneiric by default) +IMAGE_FNAME=natty.raw +IMAGE_NAME=natty + +BASE_IMAGE=$PERSIST_DIR/images/natty.raw +BASE_IMAGE_COPY=$IMAGES_DIR/$IMAGE_NAME.raw.copy + +VM_NAME=${VM_NAME:-kvmstack} +virsh shutdown $VM_NAME +virsh destroy $VM_NAME + +VM_DIR=$PERSIST_DIR/instances/$VM_NAME + +mkdir -p $VM_DIR + +# Where to mount +COPY_DIR=$VM_DIR/copy +mkdir -p $COPY_DIR + + +if [ ! -e $IMAGES_DIR/$IMAGE_FNAME ]; then + cd $TOOLS_DIR + ./make_image.sh -m -r 5000 natty raw + mv natty.raw $BASE_IMAGE + cd $TOP_DIR +fi + +function unmount_images() { + # unmount the filesystem + while df | grep -q $COPY_DIR; do + umount $COPY_DIR || echo 'ok' + sleep 1 + done +} + +# unmount from failed runs +unmount_images + +function kill_tail() { + 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 + +# Let Ctrl-c kill tail and exit +trap kill_tail SIGINT + +# Where code will live in image +DEST=${DEST:-/opt/stack} + +# Mount the file system +mount -o loop,offset=32256 $BASE_IMAGE_COPY $COPY_DIR + +# 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 + +# unmount the filesystems +unmount_images + +rm -f $VM_DIR/kernel +rm -f $VM_DIR/disk + +cd $VM_DIR +qemu-img create -f qcow2 -b $BASE_IMAGE_COPY disk + +BRIDGE=${BRIDGE:-br0} +CONTAINER=${CONTAINER:-STACK} +CONTAINER_IP=${CONTAINER_IP:-192.168.1.50} +CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24} +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=libvirt.xml +cat > $LIBVIRT_XML < + $VM_NAME + 1524288 + + hvm + + + + + + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +EOF + +ROOTFS=$VM_DIR/root +mkdir -p $ROOTFS + +modprobe nbd max_part=63 + +umount $ROOTFS || echo 'ok' +qemu-nbd -d /dev/nbd5 || echo 'ok' + +qemu-nbd -c /dev/nbd5 disk +mount /dev/nbd5 $ROOTFS -o offset=32256 -t ext4 + +# Configure instance network +INTERFACES=$ROOTFS/etc/network/interfaces +cat > $INTERFACES <> $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 + +# Configure the runner +RUN_SH=$ROOTFS/$DEST/run.sh +cat > $RUN_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 +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 <> $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 + +chmod +x $RC_LOCAL +chroot $ROOTFS sudo update-rc.d local defaults 80 + +umount $ROOTFS +qemu-nbd -d /dev/nbd5 + +cd $VM_DIR +virsh create libvirt.xml From f994c2aac7cd88dede2adc0b68a8c58463244aa5 Mon Sep 17 00:00:00 2001 From: Anthony Young Date: Wed, 19 Oct 2011 02:34:31 -0700 Subject: [PATCH 3/9] remove extra image --- stackrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stackrc b/stackrc index 28cdc22..82dd527 100644 --- a/stackrc +++ b/stackrc @@ -28,7 +28,7 @@ OPENSTACKX_REPO=https://github.com/cloudbuilders/openstackx.git OPENSTACKX_BRANCH=diablo # Specify a comma-separated list of uec images to download and install into glance. -IMAGE_URLS=http://smoser.brickies.net/ubuntu/ttylinux-uec/ttylinux-uec-amd64-11.2_2.6.35-15_1.tar.gz,http://uec-images.ubuntu.com/oneiric/current/oneiric-server-cloudimg-amd64.tar.gz +IMAGE_URLS=http://smoser.brickies.net/ubuntu/ttylinux-uec/ttylinux-uec-amd64-11.2_2.6.35-15_1.tar.gz # allow local overrides of env variables if [ -f ./localrc ]; then From f6f52270a9cae23760875adf24f31ab969e35b26 Mon Sep 17 00:00:00 2001 From: Anthony Young Date: Wed, 19 Oct 2011 02:58:18 -0700 Subject: [PATCH 4/9] console log --- tools/build_kvm.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/build_kvm.sh b/tools/build_kvm.sh index ade5115..3dd5d79 100755 --- a/tools/build_kvm.sh +++ b/tools/build_kvm.sh @@ -146,6 +146,7 @@ cat > $LIBVIRT_XML <1524288 hvm + @@ -189,14 +216,18 @@ cat > $LIBVIRT_XML < 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 <> /$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 <> $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 diff --git a/tools/make_image.sh b/tools/make_image.sh index 0d5074b..32d59bd 100755 --- a/tools/make_image.sh +++ b/tools/make_image.sh @@ -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 From d51812d44431a2048727ce69817cfe7e3b21fc5c Mon Sep 17 00:00:00 2001 From: Anthony Young Date: Wed, 19 Oct 2011 20:09:43 -0700 Subject: [PATCH 6/9] working build_kvm --- tools/build_kvm.sh | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tools/build_kvm.sh b/tools/build_kvm.sh index 54be471..332683c 100755 --- a/tools/build_kvm.sh +++ b/tools/build_kvm.sh @@ -175,9 +175,8 @@ cat > $LIBVIRT_XML <$VM_NAME 1524288 - hvm - - + hvm + @@ -287,6 +286,7 @@ 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 +cat $DEST/run.sh.log EOF chmod 755 $RUN_SH @@ -297,13 +297,14 @@ cat > $RC_LOCAL <> $ROOTFS/$DEST/.bashrc @@ -314,6 +315,8 @@ chroot $ROOTFS chown -R stack $DEST # 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 +sudo sed -e "s/^hiddenmenu//g" -i $ROOTFS/boot/grub/menu.lst +#chroot $ROOTFS grub-install /dev/vda # Unmount umount $ROOTFS || echo 'ok' @@ -323,7 +326,7 @@ qemu-nbd -d /dev/nbd5 cd $VM_DIR && virsh create libvirt.xml # Tail the console log till we are done -WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-0} +WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1} if [ "$WAIT_TILL_LAUNCH" = "1" ]; then # Done creating the container, let's tail the log echo @@ -353,7 +356,7 @@ if [ "$WAIT_TILL_LAUNCH" = "1" ]; then trap kill_tail SIGINT echo "Waiting stack.sh to finish..." - while ! cat $VM_DIR/console.log | grep -q 'stack.sh completed' ; do + while ! cat $VM_DIR/console.log | grep -q 'All done' ; do sleep 5 done From 9c0fdd7ded7c205a5e2b629d8cb0542d8c033617 Mon Sep 17 00:00:00 2001 From: Anthony Young Date: Wed, 19 Oct 2011 20:22:32 -0700 Subject: [PATCH 7/9] parameterize nbd dev --- tools/build_kvm.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tools/build_kvm.sh b/tools/build_kvm.sh index 332683c..614c118 100755 --- a/tools/build_kvm.sh +++ b/tools/build_kvm.sh @@ -52,7 +52,6 @@ BASE_IMAGE_COPY=$IMAGES_DIR/$DIST_NAME.raw.copy VM_NAME=${VM_NAME:-kvmstack} # Mop up after previous runs -virsh shutdown $VM_NAME virsh destroy $VM_NAME # Where this vm is stored @@ -222,13 +221,16 @@ mkdir -p $ROOTFS # Make sure we have nbd-ness modprobe nbd max_part=63 +# Which NBD device to use? +NBD=${NBD:-/dev/nbd5} + # Clean up from previous runs umount $ROOTFS || echo 'ok' -qemu-nbd -d /dev/nbd5 || echo 'ok' +qemu-nbd -d $NBD || echo 'ok' # Mount the instance -qemu-nbd -c /dev/nbd5 disk -mount /dev/nbd5 $ROOTFS -o offset=32256 -t ext4 +qemu-nbd -c $NBD disk +mount $NBD $ROOTFS -o offset=32256 -t ext4 # Configure instance network INTERFACES=$ROOTFS/etc/network/interfaces @@ -304,7 +306,6 @@ su -c "$DEST/run.sh" stack EOF chmod +x $RC_LOCAL chroot $ROOTFS sudo update-rc.d local defaults 80 -#chroot $ROOTFS update-rc.d local start 80 2 . stop 80 0 1 6 # Make our ip address hostnames look nice at the command prompt echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc @@ -320,7 +321,7 @@ sudo sed -e "s/^hiddenmenu//g" -i $ROOTFS/boot/grub/menu.lst # Unmount umount $ROOTFS || echo 'ok' -qemu-nbd -d /dev/nbd5 +qemu-nbd -d $NBD # Create the instance cd $VM_DIR && virsh create libvirt.xml From 5086e71fcbe693a6b788547dcdf536aa68cd0b93 Mon Sep 17 00:00:00 2001 From: Anthony Young Date: Wed, 19 Oct 2011 20:27:23 -0700 Subject: [PATCH 8/9] point novnc at master, like trunk --- stackrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stackrc b/stackrc index 82dd527..aaee6ec 100644 --- a/stackrc +++ b/stackrc @@ -12,7 +12,7 @@ KEYSTONE_BRANCH=diablo # a websockets/html5 or flash powered VNC console for vm instances NOVNC_REPO=https://github.com/cloudbuilders/noVNC.git -NOVNC_BRANCH=diablo +NOVNC_BRANCH=master # django powered web control panel for openstack DASH_REPO=https://github.com/cloudbuilders/openstack-dashboard.git From 3ee09ec2099c908fb4c6d08455006837fb492cd6 Mon Sep 17 00:00:00 2001 From: Anthony Young Date: Wed, 19 Oct 2011 20:35:04 -0700 Subject: [PATCH 9/9] now works with natty --- tools/build_kvm.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/build_kvm.sh b/tools/build_kvm.sh index 614c118..019b1bd 100755 --- a/tools/build_kvm.sh +++ b/tools/build_kvm.sh @@ -1,9 +1,12 @@ #!/usr/bin/env bash +# Make sure that we have the proper version of ubuntu 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 + if [ ! "natty" = "$UBUNTU_VERSION" ]; then + echo "This script only works with oneiric and natty" + exit 1 + fi fi # Echo commands