#!/bin/sh burl="http://cloud-images.ubuntu.com/releases/xenial" goodurl="$burl/release-20161214/ubuntu-16.04-server-cloudimg-amd64-disk1.img" badurl="$burl/release-20161221/ubuntu-16.04-server-cloudimg-amd64-disk1.img" good_qcow="good-disk1.qcow" bad_qcow="bad-disk1.qcow" fail() { echo "$@" 1>&2; exit 1; } msg() { echo "$@" 1>&2; } set -f for stuff in "good $goodurl" "bad $badurl"; do set -- $stuff pre="$1"; url="$2" qcow="$pre-disk1.qcow" img="$pre-disk1.img" if [ ! -f "$img" ]; then msg "downloading $pre from $url" wget "$url" -O "$img.tmp" && mv "$img.tmp" "$img" || fail "failed downlaod $url" fi if [ ! -f "$qcow" ]; then # patch the image to have root:root login and disable cloud-init msg "converting $img to qcow (uncompressing)" qemu-img convert -O qcow2 "$img" "$qcow.tmp" && msg "disabling cloud-init, setting passwd" && sudo mount-image-callback -vv "$qcow.tmp" -- sh -xc ' touch $MOUNTPOINT/etc/cloud/cloud-init.disabled echo "root:root" | chroot "$MOUNTPOINT" chpasswd ' && mv "$qcow.tmp" "$qcow" || fail "failed convert $img to $qcow" fi if [ ! -f "$pre-kernel" -a ! -f "$pre-initrd" ]; then # copy out kernel and initramfs for easier booting sudo mount-image-callback --read-only "$qcow" -- sh -c ' kernel=$1; initrd=$2; info="$3" manif="$4" cp -v $MOUNTPOINT/boot/vmlinu?* "$kernel" cp -v $MOUNTPOINT/boot/initrd.img-* "$initrd" t=$(echo "${MOUNTPOINT}/boot/"vmlinu?*) t=${t##*/} echo "$t"> $info chroot $MOUNTPOINT dpkg-query --show > "$manif" chown $SUDO_UID:$SUDO_GID "$kernel" "$initrd" "$info" "$manif"' -- \ $pre-kernel $pre-initrd $pre-kinfo $pre-manifest || { rm -f "$pre-kernel" "$pre-initrd" fail "failed copy kernel from $qcow [$pre]" } msg "wrote $pre-kernel and $pre-initrd" fi done cat <