2013-10-11 12:39:16 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -eu
|
2014-04-03 02:24:15 +00:00
|
|
|
set -o pipefail
|
2013-10-11 12:39:16 +00:00
|
|
|
|
2014-04-22 21:44:37 +00:00
|
|
|
function find_free_nbd_device () {
|
|
|
|
# Return first free nbd device
|
|
|
|
return_device=''
|
|
|
|
for chk_device in /sys/class/block/nbd*; do
|
|
|
|
# find free nbd device by checking size field
|
|
|
|
size_chk=`cat $chk_device/size`
|
|
|
|
if [ "$size_chk" = "0" ]; then
|
|
|
|
# device looks free. lets use it.
|
|
|
|
return_device=$chk_device
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo $return_device
|
|
|
|
}
|
2013-10-11 12:39:16 +00:00
|
|
|
[ -n "$ARCH" ]
|
|
|
|
[ -n "$TARGET_ROOT" ]
|
|
|
|
|
|
|
|
if [ 'amd64' = "$ARCH" ] ; then
|
|
|
|
ARCH="x86_64"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ 'i386' = "$ARCH" ] ; then
|
|
|
|
ARCH="i586"
|
|
|
|
fi
|
|
|
|
|
|
|
|
DIB_CLOUD_IMAGES=${DIB_CLOUD_IMAGES:-http://download.opensuse.org/repositories/Cloud:/Images/images/}
|
|
|
|
DIB_RELEASE=${DIB_RELEASE:-13.1}
|
|
|
|
# FIXME: Hard coded build numbers, versions, etc.
|
2014-04-22 21:44:37 +00:00
|
|
|
# NOTE: Actual file name seems unstable, and has changed several times.
|
|
|
|
BASE_IMAGE_NAME=${BASE_IMAGE_NAME:-openSUSE-$DIB_RELEASE-OS-guest.$ARCH-0.0.2-Build3.1}
|
2013-10-11 12:39:16 +00:00
|
|
|
|
|
|
|
if [ -z "$DIB_OFFLINE" ] ; then
|
|
|
|
# XXX: Try to extract the filename of the current built from the index
|
|
|
|
# page. As there is one built available in the repo at a given time, this
|
|
|
|
# should work. This is a tempoary workaround until the fix for
|
|
|
|
# https://bugzilla.novell.com/show_bug.cgi?id=853882 is deployed
|
|
|
|
echo "Looking up current built of Base Image ($BASE_IMAGE_NAME):"
|
|
|
|
BASE_IMAGE_FILE=${BASE_IMAGE_FILE:-$(curl $DIB_CLOUD_IMAGES | \
|
2014-04-22 21:44:37 +00:00
|
|
|
sed -n "s/^.*\<a\ href\=\"\($BASE_IMAGE_NAME.*\.tar\.bz2\)\".*$/\1/p")}
|
2013-10-11 12:39:16 +00:00
|
|
|
if [ -n "$BASE_IMAGE_FILE" ]; then
|
|
|
|
echo "Using base image: $DIB_CLOUD_IMAGES/$BASE_IMAGE_FILE"
|
|
|
|
else
|
|
|
|
echo -e "Failed to extract image file name from $DIB_CLOUD_IMAGES" \
|
2014-05-02 17:02:38 +00:00
|
|
|
"\nPlease set BASE_IMAGE_FILE manually and retry."
|
2013-10-11 12:39:16 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
2014-04-22 21:44:37 +00:00
|
|
|
BASE_IMAGE_FILE=${BASE_IMAGE_FILE:-$BASE_IMAGE_NAME-raw.tar.bz2}
|
2013-10-11 12:39:16 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# FIXME: either check the checksums into git or verify the signature
|
|
|
|
# on the checksums. We should not be trusting checksums we download
|
|
|
|
# over HTTP
|
|
|
|
SHA256SUMS=${SHA256SUMS:-$DIB_CLOUD_IMAGES/$BASE_IMAGE_FILE.sha256}
|
|
|
|
CACHED_FILE=$DIB_IMAGE_CACHE/$BASE_IMAGE_FILE
|
|
|
|
|
|
|
|
if [ -n "$DIB_OFFLINE" -a -f "$CACHED_FILE" ] ; then
|
|
|
|
echo "Not checking freshness of cached $CACHED_FILE."
|
|
|
|
else
|
|
|
|
echo "Fetching Base Image"
|
|
|
|
$TMP_HOOKS_PATH/bin/cache-url $SHA256SUMS $DIB_IMAGE_CACHE/SHA256SUMS.openSUSE.$DIB_RELEASE.$ARCH
|
|
|
|
$TMP_HOOKS_PATH/bin/cache-url $DIB_CLOUD_IMAGES/$BASE_IMAGE_FILE $CACHED_FILE
|
|
|
|
pushd $DIB_IMAGE_CACHE
|
|
|
|
grep "$BASE_IMAGE_FILE" SHA256SUMS.openSUSE.$DIB_RELEASE.$ARCH | sha256sum --check -
|
|
|
|
popd
|
|
|
|
fi
|
2014-04-22 21:44:37 +00:00
|
|
|
|
|
|
|
# Extract the qcow2 image and qcow2.sha256 files.
|
|
|
|
# to make life easier create a temp directory to unzip the files.
|
|
|
|
# TODO(NobodyCam): validate qcow2 with sha256 file
|
|
|
|
UNTAR_TO_LOCATION=$TMP_MOUNT_PATH/openSUSE/Untar
|
|
|
|
mkdir -p $UNTAR_TO_LOCATION
|
|
|
|
sudo tar -C $UNTAR_TO_LOCATION -xjf $DIB_IMAGE_CACHE/$BASE_IMAGE_FILE
|
|
|
|
# mount qcow
|
|
|
|
nbd_device=$(find_free_nbd_device)
|
|
|
|
mount_device=/dev/`basename $nbd_device`
|
|
|
|
sudo qemu-nbd -c $mount_device $UNTAR_TO_LOCATION/$BASE_IMAGE_NAME.qcow2
|
|
|
|
# Ensure that $mount_device is a block device.
|
|
|
|
if [ -b $mount_device ]; then
|
|
|
|
org_dir=`pwd`
|
|
|
|
# make a mount point for the image
|
|
|
|
mkdir $UNTAR_TO_LOCATION/mnt
|
|
|
|
# mount the images root partition
|
|
|
|
sudo mount ${mount_device}p1 $UNTAR_TO_LOCATION/mnt
|
|
|
|
# copy contents to the build area
|
|
|
|
cd $UNTAR_TO_LOCATION/mnt
|
|
|
|
sudo find . -print | sudo cpio -padmu $TARGET_ROOT
|
|
|
|
# ensure we are not in the directory about to be unmounted
|
|
|
|
cd $org_dir
|
|
|
|
echo "Copied $UNTAR_TO_LOCATION/mnt to $TARGET_ROOT"
|
|
|
|
# unmount and remove the device used for the copy
|
|
|
|
sudo umount $UNTAR_TO_LOCATION/mnt
|
|
|
|
sudo qemu-nbd -d $mount_device
|
|
|
|
else
|
|
|
|
echo "Unable to extract openSuSe Image."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
# remove the temp directory
|
|
|
|
sudo rm -rf $UNTAR_TO_LOCATION
|