diskimage-builder/elements/fedora/root.d/10-fedora-cloud-image
Robert Collins 9afddcf266 New element that uses a fedora cloud image as the base.
Change-Id: I7d83bb2b359e7a8c3858eca04c96e35cf4e1fe9e
2013-02-15 09:33:29 +13:00

45 lines
1.4 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
CLOUD_IMAGES=${CLOUD_IMAGES:-http://mattdm.fedorapeople.org/cloud-images/}
RELEASE=${RELEASE:-Fedora18}
BASE_IMAGE_FILE=${BASE_IMAGE_FILE:-$RELEASE-Cloud-$ARCH-latest.raw.tar.xz}
BASE_IMAGE_TAR=$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 $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"
tar -xJC $WORKING -f $IMG_PATH/$BASE_IMAGE_FILE
LOOPDEV=$(sudo losetup --show -r -f $WORKING/*.raw)
EACTION="sudo losetup -d $LOOPDEV;$EACTION"
trap "$EACTION" EXIT
sudo partprobe $LOOPDEV
mkdir $WORKING/mnt
sudo mount ${LOOPDEV}p2 $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