#!/bin/bash rel="vivid" arch="amd64" WPORT=${WPORT:-"32999"} IPADDR=${IPADDR:-""} PATCH_DEB=${PATCH_DEB:-""} # set this to path to deb to patch it in url="http://cloud-images.ubuntu.com/daily/server/$rel/current/$rel-server-cloudimg-amd64-disk1.img" url="http://cloud-images.ubuntu.com/daily/server/$rel/current/$rel-server-cloudimg-amd64-disk1.img" TEMP_D="" HTTP_PID="" img_dist="${url##*/}" qcow=${img_dist%.img}.qcow2 fail() { echo "$@" 1>&2; exit 1; } cleanup() { [ ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}" if [ -n "$HTTP_PID" ]; then kill $HTTP_PID fi return 1 } if [ -z "$IPADDR" ]; then ipaddr=$(ifconfig eth0 | sed -n '/inet addr/s/\([^:]*:\)\([^ ]*\)\( .*\)/\2/p') [ $? -eq 0 -a -n "$ipaddr" ] || fail "ipaddr not found, set IPADDR" fi trap cleanup EXIT if [ ! -f "${img_dist}" ]; then wget "$url" -O "${img_dist}.tmp" && mv "${img_dist}.tmp" "$img_dist" || fail fi if [ ! -f "$qcow" ]; then qemu-img convert -O qcow2 "$img_dist" "$qcow.tmp" && mv "$qcow.tmp" "$qcow" || fail fi if [ -n "$PATCH_DEB" ]; then sudo mount-image-callback "$qcow" -- \ sh -c 'cp "$1" "$MOUNTPOINT/tmp/patch.deb" && chroot "$MOUNTPOINT" dpkg -i /tmp/patch.deb && rm -f "$MOUNTPOINT/tmp/patch.deb"' -- "$PATCH_DEB" || fail "failed to install PATCH_DEB to $qcow" fi if [ ! -f kernel -o ! -f initrd ]; then sudo mount-image-callback --read-only "$qcow" -- \ sh -c 'cp $MOUNTPOINT/boot/vmlinuz-* $PWD/kernel && cp $MOUNTPOINT/boot/initrd.img-* $PWD/initrd && chown $SUDO_UID:$SUDO_GID kernel initrd' || fail "failed copy kernel/initrd out of $qcow" fi TEMP_D="$(mktemp -d ${TEMPDIR:-/tmp}/${0##*/}.XXXXXX)" || fail ## populate a seed dir mkdir "${TEMP_D}/ci-seed" printf "%s\n%s\n%s\n%s\n" "#cloud-config" "password: passw0rd" \ "chpasswd: { expire: False }" "ssh_pwauth: True" > "${TEMP_D}/ci-seed/user-data" echo "instance-id: $(uuidgen || echo i-abcdefg)" > "${TEMP_D}/ci-seed/meta-data" ## run a python web server to seed this ( cd "${TEMP_D}/ci-seed" && exec python -m SimpleHTTPServer $WPORT ) & HTTP_PID=$! url="http://$ipaddr:$WPORT/" cmdline="ds=nocloud-net;seedfrom=$url console=tty1 root=LABEL=cloudimg-rootfs overlayroot=tmpfs nomodeset console=ttyS0" cmd=( qemu-system-x86_64 -enable-kvm -m 1024 -curses \ -drive "if=virtio,file=$qcow" -device virtio-net-pci,netdev=net00 -netdev type=user,id=net00 \ -kernel kernel -initrd initrd -append "$cmdline" -serial file:serial.log ) echo "${cmd[@]}" "${cmd[@]}"