diskimage-builder/elements/fedora/root.d/10-fedora-cloud-image

46 lines
1.4 KiB
Plaintext
Raw Normal View History

#!/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