#!/bin/bash # # Copyright (c) 2011 Citrix Systems, Inc. # Copyright 2011 OpenStack LLC. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. # set -eu set -o xtrace VBOX_IMG=/output/packages/vbox-img usage() { cat >&2 < -t -x -o: Colon-separated list of output filenames (one for each type). -p: Create a disk label and partition within the output image -t: Colon-separated list of types of output file. xva and ovf supported. -x: XML filenames (one for each type) EOF exit 1 } # parse cmdline OPT_USE_PARTITION= OPT_TYPES= OPT_OUTPUT_FILES= OPT_XML_FILES= while getopts o:pt:x: o do case "$o" in o) OPT_OUTPUT_FILES=$(echo "$OPTARG" | sed -e 's/\s*:\s*/ /g') ;; p) OPT_USE_PARTITION=1 ;; t) OPT_TYPES=$(echo "$OPTARG" | sed -e 's/\s*:\s*/ /g') ;; x) OPT_XML_FILES=$(echo "$OPTARG" | sed -e 's/\s*:\s*/ /g') ;; [?]) usage ;; esac done shift $((OPTIND-1)) [ $# -ne 3 ] && usage FS_STAGING="$1" FS_SIZE_MIB="$2" TMPDIR="$3" if [ "$UID" = "0" ] then SUDO= else SUDO=sudo fi if [ "$FS_SIZE_MIB" = "0" ] then # Just create a dummy file. This allows developers to bypass bits of # the build by setting the size to 0. touch $OPT_OUTPUT_FILES exit 0 fi # create temporary files and dirs FS_TMPFILE=$(mktemp "$TMPDIR/mkxva-fsimg-XXXXX") XVA_TARBALL_STAGING=$(mktemp -d "$TMPDIR/mkxva-tarball-staging-XXXXX") OVF_STAGING=$(mktemp -d "$TMPDIR/mkxva-ovf-staging-XXXXX") # Find udevsettle and udevtrigger on this installation if [ -x "/sbin/udevsettle" ] ; then UDEVSETTLE="/sbin/udevsettle --timeout=30" elif [ -x "/sbin/udevadm" ] ; then UDEVSETTLE='/sbin/udevadm settle' else UDEVSETTLE='/bin/true' fi if [ -x "/sbin/udevtrigger" ] ; then UDEVTRIGGER=/sbin/udevtrigger elif [ -x "/sbin/udevadm" ] ; then UDEVTRIGGER='/sbin/udevadm trigger' else UDEVTRIGGER= fi # CLEAN_ variables track devices and mounts that must be taken down # no matter how the script exits. Loop devices are vulnerable to # exhaustion so we make every effort to remove them CLEAN_KPARTX= CLEAN_LOSETUP= CLEAN_MOUNTPOINT= cleanup_devices () { if [ -n "$CLEAN_MOUNTPOINT" ] ; then echo "Mountpoint $CLEAN_MOUNTPOINT removed on abnormal exit" $SUDO umount "$CLEAN_MOUNTPOINT" || echo "umount failed" rmdir "$CLEAN_MOUNTPOINT" || echo "rmdir failed" fi if [ -n "$CLEAN_KPARTX" ] ; then echo "kpartx devices for $CLEAN_KPARTX removed on abnormal exit" $SUDO kpartx -d "$CLEAN_KPARTX" || echo "kpartx -d failed" fi if [ -n "$CLEAN_LOSETUP" ] ; then echo "Loop device $CLEAN_LOSETUP removed on abnormal exit" $SUDO losetup -d "$CLEAN_LOSETUP" # Allow losetup errors to propagate fi } trap "cleanup_devices" EXIT make_fs_inner () { local staging="$1" local output="$2" local options="$3" CLEAN_MOUNTPOINT=$(mktemp -d "$TMPDIR/mkfs-XXXXXX") # copy staging dir contents to fs image $SUDO mount $options "$output" "$CLEAN_MOUNTPOINT" $SUDO tar -C "$staging" -c . | tar -C "$CLEAN_MOUNTPOINT" -x $SUDO umount "$CLEAN_MOUNTPOINT" rmdir "$CLEAN_MOUNTPOINT" CLEAN_MOUNTPOINT= } # Turn a staging dir into an ext3 filesystem within a partition make_fs_in_partition () { local staging="$1" local output="$2" # create new empty disk dd if=/dev/zero of="$output" bs=1M count=$FS_SIZE_MIB # Set up a loop device on the empty disk image local loopdevice=$($SUDO losetup -f) $SUDO losetup "$loopdevice" "$output" CLEAN_LOSETUP="$loopdevice" # Create a partition table and single partition. # Start partition at sector 63 to allow space for grub cat < "$CLEAN_MOUNTPOINT/boot/grub/grub.conf" </dev/null gzip "$file" else local file="$outputdir"/$(printf "%08d" $i) dd if="$diskimg" of="$file" skip=$i bs=1M count=1 2>/dev/null local chksum=$(sha1sum -b "$file") echo -n "${chksum/ */}" > "$file.checksum" fi i=$(($i + 1)) done } if [ -n "$OPT_USE_PARTITION" ] ; then make_fs_in_partition "$FS_STAGING" "$FS_TMPFILE" else make_fs "$FS_STAGING" "$FS_TMPFILE" fi VDI_SIZE=$(stat --format=%s "$FS_TMPFILE") make_xva () { local output_file="$1" local xml_file="$2" local subdir local rio if [[ `cat $xml_file` =~ "\s*class\s*VDI\s*\s*\s*id\s*(Ref:[0-9]+)" ]] then # it's a rio style xva subdir="${BASH_REMATCH[1]}"; rio=1 else # it's a geneva style xva subdir="xvda" rio=0 fi cp "$xml_file" "$XVA_TARBALL_STAGING"/ova.xml sed -i -e "s/@VDI_SIZE@/$VDI_SIZE/" "$XVA_TARBALL_STAGING"/ova.xml mkdir "$XVA_TARBALL_STAGING/$subdir" splitvdi "$FS_TMPFILE" "$XVA_TARBALL_STAGING/$subdir" "$rio" TARFILE_MEMBERS=$(cd "$XVA_TARBALL_STAGING" && echo ova.xml $subdir/*) tar -C "$XVA_TARBALL_STAGING" --format=v7 -c $TARFILE_MEMBERS -f "$output_file.tmp" mv "$output_file.tmp" "$output_file" } make_ovf () { local output_dir="$1" local xml_file="$2" local output_base=$(basename "$output_dir") local disk="$output_dir/${output_base}.vmdk" local manifest="$output_dir/${output_base}.mf" local ovf="$output_dir/${output_base}.ovf" mkdir -p "$output_dir" rm -f "$disk" $VBOX_IMG convert --srcfilename="$FS_TMPFILE" --dstfilename="$disk" \ --srcformat RAW --dstformat VMDK --variant Stream chmod 0644 "$disk" local n_bytes=$(stat --printf=%s "$disk") cp "$xml_file" "$ovf" sed -i -e "s/@MKXVA_DISK_FULLSIZE@/$VDI_SIZE/" "$ovf" sed -i -e "s/@MKXVA_DISK_SIZE@/$n_bytes/" "$ovf" sed -i -e "s/@MKXVA_DISK_MIB_SIZE@/$FS_SIZE_MIB/" "$ovf" sed -i -e "s/@MKXVA_DISK_FILENAME@/${output_base}.vmdk/" "$ovf" for to_sign in "$ovf" "$disk" do local sha1_sum=$(sha1sum "$to_sign" | cut -d' ' -f1) echo "SHA1($(basename "$to_sign"))= $sha1_sum" >> $manifest done } output_files="$OPT_OUTPUT_FILES" xml_files="$OPT_XML_FILES" # Iterate through the type list creating the relevant VMs for create_type in $OPT_TYPES do # Shift one parameter from the front of the lists create_output_file="${output_files%% *}" output_files="${output_files#* }" create_xml_file="${xml_files%% *}" xml_files="${xml_files#* }" echo "Creating $create_type appliance $create_output_file using metadata file $create_xml_file" case "$create_type" in xva) make_xva "$create_output_file" "$create_xml_file" ;; ovf) make_ovf "$create_output_file" "$create_xml_file" ;; *) echo "Unknown VM type '$create_type'" exit 1 ;; esac done # cleanup if [ -z "${DO_NOT_CLEANUP:-}" ] ; then rm -rf "$XVA_TARBALL_STAGING" rm -f "$FS_TMPFILE" fi