#!/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 <<EOF
$0 -o <output filenames> -t <types> -x <xml files> <fs-staging-dir> <fs-size-MiB> <tmpdir>
  -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 <<EOF
Errors from sfdisk below are expected because the new disk is uninitialised
  Expecting: sfdisk: ERROR: sector 0 does not have an msdos signature
  Expecting: /dev/loop0: unrecognized partition table type
EOF
    $SUDO sfdisk -uS "$CLEAN_LOSETUP" <<EOF
63 - - *
EOF

    # kpartx creates a device for the new partition
    # in the form /dev/mapper/loop1p1
    $SUDO kpartx -av "$CLEAN_LOSETUP"
    CLEAN_KPARTX="$CLEAN_LOSETUP"
    # Wait for the device to appear
    $UDEVTRIGGER
    $UDEVSETTLE  || echo "udev settle command return code non-zero"
    # Infer the name of the partition device
    local partition="${CLEAN_LOSETUP/dev/dev/mapper}p1"
    # Set permissive privileges on the device
    $SUDO chmod 0777 "$partition"
    # Make an ext3 filesystem on the partition
    /sbin/mkfs.ext3 -I 128 -m0 -F "$partition"
    /sbin/e2label "$partition" vpxroot
    make_fs_inner "$staging" "$partition" ""

    # Now run grub on the image we've created
    CLEAN_MOUNTPOINT=$(mktemp -d "$TMPDIR/mkfs-XXXXXX")

    # copy Set up[ grub files prior to installing grub within the image
    $SUDO mount "$partition" "$CLEAN_MOUNTPOINT"
    $SUDO cp $CLEAN_MOUNTPOINT/usr/share/grub/i386-redhat/* "$CLEAN_MOUNTPOINT/boot/grub"
    kernel_version=$($SUDO chroot "$CLEAN_MOUNTPOINT" rpm -qv kernel | sed -e 's/kernel-//')
    kernel_version_xen=$($SUDO chroot "$CLEAN_MOUNTPOINT" rpm -qv kernel-xen | sed -e 's/kernel-xen-//')
    $SUDO cat > "$CLEAN_MOUNTPOINT/boot/grub/grub.conf" <<EOF
default 0
timeout 2

title vmlinuz-$kernel_version (HVM)
        root (hd0,0)
        kernel /boot/vmlinuz-$kernel_version ro root=LABEL=vpxroot
        initrd /boot/initrd-$kernel_version.img

title vmlinuz-${kernel_version_xen}xen (PV)
        root (hd0,0)
        kernel /boot/vmlinuz-${kernel_version_xen}xen ro root=LABEL=vpxroot console=xvc0
        initrd /boot/initrd-${kernel_version_xen}xen.img
EOF

    $SUDO umount "$CLEAN_MOUNTPOINT"
    CLEAN_MOUNTPOINT=

    # Grub expects a disk with name /dev/xxxx with a first partition
    # named /dev/xxxx1, so we give it what it wants using symlinks
    # Note: /dev is linked to the real /dev of the build machine, so
    # must be cleaned up
    local disk_name="/dev/osxva$$bld"
    local disk_part1_name="${disk_name}1"
    rm -f "$disk_name"
    rm -f "$disk_part1_name"
    ln -s "$CLEAN_LOSETUP" "$disk_name"
    ln -s "$partition" "$disk_part1_name"

    # Feed commands into the grub shell to setup the disk
    grub --no-curses --device-map=/dev/null <<EOF
device (hd0) $disk_name
setup (hd0) (hd0,0)
quit
EOF

    # Cleanup
    rm -f "$disk_name"
    rm -f "$disk_part1_name"
    $SUDO kpartx -dv "$CLEAN_KPARTX"
    CLEAN_KPARTX=
    $SUDO losetup -d "$CLEAN_LOSETUP"
    CLEAN_LOSETUP=
}

# turn a staging dir into an ext3 filesystem image
make_fs () {
    local staging="$1"
    local output="$2"

    # create new empty fs
    dd if=/dev/zero of="$output" bs=1M count=0 seek=$FS_SIZE_MIB
    /sbin/mkfs.ext3 -m0 -F "$output"
    /sbin/e2label "$output" vpxroot
    make_fs_inner "$staging" "$output" "-oloop"
}


# split a virtual disk image into the format expected inside an xva file
splitvdi () {
    local diskimg="$1"
    local outputdir="$2"
    local rio="$3"

    local n_bytes=$(stat --printf=%s "$diskimg")
    local n_meg=$((($n_bytes+$((1024*1024 -1)))/$((1024*1024))))
    local i=0
    while [ $i -lt $n_meg ] ; do
	if [ $rio -eq 0 ] ; then
		local file="$outputdir"/chunk-$(printf "%08d" $i)
		dd if="$diskimg" of="$file" skip=$i bs=1M count=1 2>/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` =~ "<member>\s*<name>class</name>\s*<value>VDI</value>\s*</member>\s*<member>\s*<name>id</name>\s*<value>(Ref:[0-9]+)</value>" ]]
    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