Allow for multiple image outputs from raw source

When uploading images to multiple clouds it is possible that the same
image will be needed in multiple formats to accomodate hypervisors
across clouds. Update disk-image-create's -t flag to take a list of
desired output image formats so that a single disk-image-create can
output all of the desired image formats.

Change-Id: If121b2342ae888855ba435aa3189f039e985b812
This commit is contained in:
Clark Boylan 2014-10-01 12:34:21 -07:00
parent f06493bcd6
commit ae928057bd
4 changed files with 30 additions and 16 deletions

View file

@ -45,7 +45,8 @@ function show_options () {
echo "Options:"
echo " -a i386|amd64|armhf -- set the architecture of the image(default amd64)"
echo " -o imagename -- set the imagename of the output image file(default image)"
echo " -t qcow2|tar -- set the imagetype of the output image file(default qcow2)"
echo " -t qcow2,tar -- set the image types of the output image files (default qcow2)"
echo " File types should be comma separated"
echo " -x -- turn on tracing"
echo " -u -- uncompressed; do not compress the image - larger but faster"
echo " -c -- clear environment before starting work"
@ -85,6 +86,7 @@ function show_options () {
}
INSTALL_PACKAGES=""
IMAGE_TYPES=("qcow2")
COMPRESS_IMAGE="true"
DIB_ROOT_LABEL=""
TEMP=`getopt -o a:ho:t:xucnp: -l no-tmpfs,offline,help,min-tmpfs:,image-size:,image-cache:,max-online-resize:,qemu-img-options:,root-label: -n $SCRIPTNAME -- "$@"`
@ -97,7 +99,7 @@ while true ; do
case "$1" in
-a) export ARCH=$2; shift 2 ;;
-o) export IMAGE_NAME=$2; shift 2 ;;
-t) export IMAGE_TYPE=$2; shift 2 ;;
-t) IFS="," read -a IMAGE_TYPES <<< "$2"; export IMAGE_TYPES ; shift 2 ;;
-h|--help) show_options; exit 0;;
-x) shift; set -x;;
-u) shift; export COMPRESS_IMAGE="";;
@ -137,7 +139,10 @@ fi
arg_to_elements "$@"
export IMAGE_NAME=${IMAGE_NAME%%\.${IMAGE_TYPE}}
if [ "${#IMAGE_TYPES[@]}" = "1" ]; then
export IMAGE_NAME=${IMAGE_NAME%%\.${IMAGE_TYPES[0]}}
fi
# FS_TYPE isn't available until after we source img-defaults
if [ -z "$DIB_ROOT_LABEL" ]; then
# NOTE(bnemec): XFS has a limit of 12 characters for filesystem labels
@ -207,18 +212,27 @@ mount_proc_dev_sys
run_d_in_target finalise
finalise_base
if [ "$IMAGE_TYPE" == "tar" ]; then
sudo tar -C ${TMP_BUILD_DIR}/mnt -cf $IMAGE_NAME.tar --exclude ./sys \
--exclude ./proc --xattrs --xattrs-include=\* .
sudo chown $USER: $IMAGE_NAME.tar
fi
for X in ${!IMAGE_TYPES[@]} ; do
if [ "${IMAGE_TYPES[$X]}" == "tar" ]; then
sudo tar -C ${TMP_BUILD_DIR}/mnt -cf $IMAGE_NAME.tar --exclude ./sys \
--exclude ./proc --xattrs --xattrs-include=\* .
sudo chown $USER: $IMAGE_NAME.tar
unset IMAGE_TYPES[$X]
fi
done
unmount_image
if [ "$IS_RAMDISK" == "0" -a "$IMAGE_TYPE" != "tar" ]; then
compress_and_save_image $IMAGE_NAME.$IMAGE_TYPE
if [ "$IS_RAMDISK" == "0" ]; then
for IMAGE_TYPE in ${IMAGE_TYPES[@]} ; do
compress_and_save_image $IMAGE_NAME.$IMAGE_TYPE
done
rm $TMP_IMAGE_PATH
cleanup_dirs
else
# This is a ramdisk build, we have already extracted the kernel and ramdisk
# by this point.
rm $TMP_IMAGE_PATH
fi
# All done!
trap EXIT

View file

@ -39,5 +39,8 @@ populate_udev
SCRIPT_HOME=/tmp/in_target.d/bin TMP_HOOKS_PATH=/tmp/in_target.d run_d ramdisk-install
finalise_image
save_image /tmp/ramdisk
# In the past save_image did this for us. If EXIT handler is not
# reset ramdisk image builds fail.
trap EXIT
cp /boot/vmlinuz-${KERNEL_VERSION} /tmp/kernel
chmod o+r /tmp/kernel

View file

@ -39,6 +39,7 @@ function mk_build_dir () {
trap trap_cleanup EXIT
echo Building in $TMP_BUILD_DIR
export TMP_IMAGE_PATH=$TMP_IMAGE_DIR/image.raw
export OUT_IMAGE_PATH=$TMP_IMAGE_PATH
export TMP_HOOKS_PATH=$TMP_BUILD_DIR/hooks
}
@ -49,10 +50,7 @@ function finish_image () {
mv "$1" "$old_image"
fi
mv $TMP_IMAGE_PATH $1
cleanup_dirs
# All done!
trap EXIT
mv $OUT_IMAGE_PATH $1
echo "Image file $1 created..."
}

View file

@ -121,9 +121,8 @@ function compress_and_save_image () {
EXTRA_OPTIONS=""
fi
qemu-img convert ${COMPRESS_IMAGE:+-c} -f raw $TMP_IMAGE_PATH -O $IMAGE_TYPE $EXTRA_OPTIONS $1-new
rm $TMP_IMAGE_PATH
TMP_IMAGE_PATH=$1-new
OUT_IMAGE_PATH=$1-new
finish_image $1
}