Skip to content

Instantly share code, notes, and snippets.

@smoser
Created August 25, 2011 03:03
Show Gist options
  • Save smoser/1169889 to your computer and use it in GitHub Desktop.
Save smoser/1169889 to your computer and use it in GitHub Desktop.
nova and lxc on ubuntu oneiric
#!/bin/sh
{
## EDIT THESE DETAILS FOR YOU ##
NONROOT_USER="ubuntu" # leave this as ubuntu if you're using cloud image
LP_USER="smoser" # change this to your launchpad id for ssh-import-id
SMOSER_PREFS=1 # do you want things setup like smoser likes?
EXTRA_PKGS="vim" # whatever extra packages you'd like installed
USEFUL_PKGS="bzr"
## END NECESSARY EDITING
## some variables
INTERFACE=eth0
FLOATING_RANGE=${FLOATING_RANGE:-10.6.0.0/27}
FIXED_RANGE=${FIXED_RANGE:-10.0.0.0/24}
nova_lxc_pkgs="
cloud-utils glance nova-api
nova-common nova-compute-lxc nova-doc nova-network
nova-objectstore nova-scheduler
rabbitmq-server sqlite3 unzip
"
nova_lxc_pkgs="${nova_lxc_pkgs} qemu-kvm" # LP: #833530
nova_flags="
--network_manager=nova.network.manager.VlanManager
--default_instance_type=m1.xdev
--public_interface=$INTERFACE
--vlan_interface=$INTERFACE
--fixed_range=$FIXED_RANGE
--minimum_root_size=$((1024*1024*1024*2))
"
stop_start_services="
glance-api glance-registry
nova-api nova-compute nova-network nova-objectstore nova-scheduler
"
DL_DIR="/mnt/dl"
cloud_img_toks="oneiric|daily|amd64 " # could have other : separated tuples
other_img_toks="
http://smoser.brickies.net/ubuntu/ttylinux-uec/dev/latest.tar.gz|amd64|ttylinux-uec-images|ttylinux-uec-snapshot.img
"
OIFS="$IFS"
TEMP_D=$(mktemp -d ${TMPDIR:-/tmp}/${0##*/}.XXXXXX)
trap "rm -Rf '$TEMP_D'" EXIT
## end variables
## some functions
error() { echo "$@" 1>&2; }
debug() { error "$@"; }
user_register() {
local url="$1" arch="$2" bucket="$3" rename="$4"
sudo "TMPDIR=${DL_DIR}" -Hu "$NONROOT_USER" sh -c '
url="$1"; arch="$2"; bucket="$3" rename="$4"
[ -z "${rename}" ] && tarball="${url##*/}" ||
tarball=${rename%.img}.tar.gz
wget -c "$url" -O "$tarball" || {
echo "FAIL: wget failed in: user_register $*" 1>&2;
exit 1;
}
cloud-publish-tarball \
--kernel none --ramdisk none \
${rename:+"--rename-image=${rename}"} \
"$tarball" "$bucket" "$arch" \
> ./pubinfo.txt 2>pubinfo.err || {
echo "FAIL: publish failed: user_register $*" 1>&2;
{ echo ==== user_register $* ====;
echo ==== stdout ====; cat pubinfo.txt;
echo ==== sterr ====; cat pubinfo.err;
} >> pubinfo.failed.txt
exit 1;
}
. ./pubinfo.txt
echo "${emi} ${bucket}/${rename}"
' download_user "$url" "$arch" "$bucket" "$rename"
}
## apply smoser preferences ##
if [ "${SMOSER_PREFS:-0}" != "0" ]; then
debug "setting smoser's preferences"
sudo -Hu "${NONROOT_USER}" sh -c '
cd $HOME
echo "set -o vi" >> $HOME/.bashrc
echo export EDITOR=vi >> $HOME/.profile
byobu-ctrl-a screen
'
fi
# setup, add the kvm and libvirt groups, and add this user to them
# not necessary, but useful for debugging
for g in libvirtd kvm; do
groups $NONROOT_USER | grep -q $g && continue
addgroup --system $g && adduser $NONROOT_USER $g;
debug "added $NONROOT_USER to group $g"
done
# simple fixup for hostname not being right on openstack (LP: #820962)
if hn=$(ec2metadata --local-hostname) && [ "${hn#*_}" != "${hn}" ]; then
ohost=$(cat /etc/hostname)
nhost=$(echo "$ohost" | sed 's,_,-,g')
sed -i s,$ohost,$nhost,g /etc/hostname /etc/hosts
hostname $(cat /etc/hostname)
echo "fqdn: $(hostname -f)" >> /etc/cloud/cloud.cfg.d/94_hostname_fix.cfg
debug "fixed hostname from ${hn} -> ${nhost} (LP: #820962)"
fi
# set up /mnt if not set up (LP: #827590 on nova, LP: #784937 on ec2)
[ -e /dev/xvdf ] && eph0="/dev/xvdf"
[ -e /dev/vdb ] && eph0="/dev/vdb"
fstype="ext3";
if ! file --special-files ${eph0} | grep -q ${fstype}; then
mkfs.${fstype} "${eph0}"
debug "made $fstype filesystem on $eph0"
fi
if ! grep -q "^${eph0}" /etc/fstab; then
echo ${eph0} /mnt ${fstype} defaults 0 0 >> /etc/fstab
debug "echo added ${eph0} to /etc/fstab"
fi
grep "^${eph0}" /proc/mounts || mount -a
## Install Packages
# this gets lxc, and cgroup-bin
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get -y install lxc libvirt-bin
apt-get --assume-yes \
--option Dpkg::Options::=--force-confold dist-upgrade
# libvirt-bin needs to be re-started so it will see the cgroup-bin mount
sh -c 'stop libvirt-bin; start libvirt-bin' # (LP: #824611)
apt-get install -y ${nova_lxc_pkgs} ${EXTRA_PKGS} ${USEFUL_PKGS}
## add flags
for flag in $nova_flags; do
grep "^$flag" /etc/nova/nova.conf ||
echo "$flag" >> /etc/nova/nova.conf
debug "added flag ${flag} to nova.conf"
done
## restart services
## I have no good reason why this would be needed other
## than the adding of nova flags above
for s in ${stop_start_services}; do
stop $s; start $s;
done
## set up the networks and projects
## FIXME: don't do this stuff if its already been done
nova-manage user create xdev_admin
nova-manage project create xdev_project xdev_admin "my xdev test project"
# create a small network
nova-manage network create private $FIXED_RANGE 1 32
# create some floating ips
nova-manage floating create $FLOATING_RANGE
# I do not know of another way to attach the floating IPs above to project
echo "update floating_ips set project_id = 'xdev_project';" |
sqlite3 /var/lib/nova/nova.sqlite
TMPF="${TEMP_D}/flavors.txt"
nova-manage flavor list > "$TMPF"
highnum=$(sed 's,.*FlavorID: \([0-9]*\).*,\1,' "$TMPF" | sort -n | tail -n 1)
if ! grep -q ^m1.xdev "$TMPF"; then
# get the highest flavor-id, and add 1
[ -n "$highnum" ] || highnum=9
highnum=$(($highnum+1))
nova-manage flavor create \
--name m1.xdev --memory=256 \
--cpu=1 --local_gb=1 --flavor=$highnum \
--swap 128 --rxtx_quota=0 --rxtx_cap=0
fi
grep -q "m1.small" "$TMPF" && nova-manage flavor delete --purge --name "m1.small"
highnum=$(($highnum+1))
nova-manage flavor create \
--name=m1.small --memory=512 \
--cpu=1 --local_gb=2 --flavor=$highnum \
--swap=0
nova-manage project zipfile xdev_project xdev_admin - > creds.zip
sudo -Hu "$NONROOT_USER" sh -c '
cd $HOME &&
mkdir -p novacreds && cat > novacreds/creds.zip &&
( cd novacreds/ && unzip creds.zip ) &&
ln -sf novacreds/novarc ~/.eucarc &&
( umask 066 && euca-add-keypair mykey > novacreds/mykey.pem )
' < creds.zip
rm -f creds.zip
mkdir -p "$DL_DIR" && chown -R "$NONROOT_USER:" "$DL_DIR"
startd=$PWD
cd "$DL_DIR"
for stuff in $cloud_img_toks; do
IFS="|"; set -- $stuff; IFS=$OIFS
suite=$1; stream=$2; arch=$3
out=$(ubuntu-cloudimg-query --format "%{url} %{pubname}\n" \
"$suite" "$stream" "$arch")
set -- $out
url="$1" ; pubname="$2";
[ "$stream" = "daily" ] && bucket="cloud-images-testing" ||
bucket="cloud-images"
user_register "$url" "$arch" "$bucket" "$pubname.img" >> registered.list
done
for stuff in $other_img_toks; do
IFS="|"; set -- $stuff; IFS="$OIFS"
url="$1"; bucket="$2"; arch="$3"; rename="${4%.img}.img"
user_register "$url" "$bucket" "$arch" "$rename" >> registered.list
done
echo "=== registered images ==="
cat registered.list
cd "$startd"
} 2>&1 | tee /root/user-data.log
# vi: ts=4 noexpandtab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment