diskimage-builder/elements/fedora/root.d/10-fedora-cloud-image
Mark McLoughlin 6c997fda97 Re-use cache_url() in fedora element.
The fedora element downloads images too, so we should re-use the caching
code from the ubuntu element.

There doesn't seem to be other examples of code shared between root.d
scripts. In the fedora and dpkg elements we copy install-packages into
the chroot, but that model doesn't apply when we're running scripts
outside of the chroot. Seems sane to just run it directly from the bin/
dir in the temporary hooks directory.

Change-Id: Iaa6aca660042fea323cab4271633a4bdbbc271b8
2013-06-20 00:04:59 +01:00

53 lines
2 KiB
Bash
Executable file

#!/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
echo "Fetching Base Image"
$TMP_HOOKS_PATH/bin/cache-url $DIB_CLOUD_IMAGES/$BASE_IMAGE_FILE $IMG_PATH/$BASE_IMAGE_FILE
if [ ! -f $IMG_PATH/$BASE_IMAGE_TAR -o \
$IMG_PATH/$BASE_IMAGE_FILE -nt $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