#!/bin/bash set -e [ -n "$ARCH" ] [ -n "$TARGET_ROOT" ] if [ 'amd64' = "$ARCH" ] ; then ARCH="x86_64" fi IMG_PATH=~/.cache/image-create DIB_CLOUD_IMAGES=${DIB_CLOUD_IMAGES:-http://mattdm.fedorapeople.org/cloud-images/} DIB_RELEASE=${DIB_RELEASE:-Fedora18} BASE_IMAGE_FILE=${BASE_IMAGE_FILE:-$DIB_RELEASE-Cloud-$ARCH-latest.qcow2} BASE_IMAGE_TAR=$DIB_RELEASE-Cloud-$ARCH-latest.tgz mkdir -p $IMG_PATH # TODO: don't cache forever. if [ ! -f $IMG_PATH/$BASE_IMAGE_FILE ] ; then echo "Fetching Base Image" wget $DIB_CLOUD_IMAGES/$BASE_IMAGE_FILE -O $IMG_PATH/$BASE_IMAGE_FILE.tmp mv $IMG_PATH/$BASE_IMAGE_FILE.tmp $IMG_PATH/$BASE_IMAGE_FILE fi if [ ! -f $IMG_PATH/$BASE_IMAGE_TAR ] ; then echo "Repacking base image as tarball." WORKING=$(mktemp -d) EACTION="rm -r $WORKING" trap "$EACTION" EXIT echo "Working in $WORKING" RAW_FILE=$(basename $BASE_IMAGE_FILE) RAW_FILE=${RAW_FILE#.qcow2}.raw qemu-img convert -f qcow2 -O raw $IMG_PATH/$BASE_IMAGE_FILE $WORKING/$RAW_FILE # WARNING: The mattdm image has the root filesystem on the second # partition (p2). If he changes the image the MAGIC_BIT # might also need to change. MAGIC_BIT=p2 # XXX: Parsing stdout is dangerous, would like a better way to discover # the device used for the image. # NOTE: On F17 (parted-3.0-10.fc17.x86_64), partprobe of # /dev/loop0 does not create /dev/loop0p2, while kpartx at # least creates /dev/mapper/loop0p2. LOOPDEV=$(sudo kpartx -avr $WORKING/$RAW_FILE | awk "/loop[0-9]+$MAGIC_BIT/ {print \$3}") EACTION="sudo kpartx -d $WORKING/$RAW_FILE;$EACTION" trap "$EACTION" EXIT mkdir $WORKING/mnt sudo mount /dev/mapper/$LOOPDEV $WORKING/mnt EACTION="sudo umount -f $WORKING/mnt;$EACTION" trap "$EACTION" EXIT # Chroot in so that we get the correct uid/gid sudo chroot $WORKING/mnt bin/tar -cz . > $WORKING/tmp.tar mv $WORKING/tmp.tar $IMG_PATH/$BASE_IMAGE_TAR fi # Extract the base image sudo tar -C $TARGET_ROOT -xzf $IMG_PATH/$BASE_IMAGE_TAR sudo rmdir $TARGET_ROOT/lost+found