blob: 543a89078c3f7552411d76166250482d0aa0d947 [file] [log] [blame]
#!/bin/bash
#
# Copyright 2021 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# make_ova.sh -d [vmdk file] -o [ova file] -p[product-name] \
# -n[image-name] -t ${TEMPLATE_OVF}
#
# This scripts creates .ova file from given disk image and OVA template.
#
set -o xtrace
set -o errexit
set -o nounset
SCRIPT_ROOT=$(dirname $(readlink -f "$0"))
TEMPLATE_PATH=${SCRIPT_ROOT}/template.ovf
WORKSPACE=${SCRIPT_ROOT%\/src\/scripts\/cos}
BOARD=anthos-amd64-vsphere
PRODUCT_NAME="Anthos OnPrem on COS"
IMAGE_NAME="COS"
IMAGE_TYPE="test"
IMAGE_ROOT=${WORKSPACE}/src/build/images/${BOARD}/latest
DISK_FILE=${IMAGE_ROOT}/chromiumos_${IMAGE_TYPE}_image.vmdk
OUTPUT_FILE=${IMAGE_ROOT}/chromiumos_${IMAGE_TYPE}_image.ova
usage() {
echo "Usage: $0 -b board -d disk.vmdk \
-p product-name -n image-name \
-o output-file [-t template.ovf]"
}
while getopts ":b:d:p:n:t:o:h" arg; do
case $arg in
b) BOARDD=$OPTARG ;;
d) DISK_FILE=$OPTARG ;;
p) PRODUCT_NAME=$OPTARG ;;
n) IMAGE_NAME=$OPTARG ;;
t) TEMPLATE_PATH=$OPTARG ;;
o) OUTPUT_FILE=$OPTARG ;;
h)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
: "${BOARD?Missing -d BOARD value}"
: "${DISK_FILE?Missing -d DISK_FILE value}"
: "${PRODUCT_NAME?Missing -p PRODUCT_NAME value}"
: "${IMAGE_NAME?Missing -n IMAGE_NAME value}"
: "${TEMPLATE_PATH?Missing -t TEMPLATE_PATH value}"
: "${OUTPUT_FILE?Missing -o OUTPUT_FILE value}"
if [[ ! -f ${TEMPLATE_PATH} ]]; then
echo "Cannot find template at ${TEMPLATE_PATH}"
exit 1
fi
XML_NS=(
-N 'x=http://schemas.dmtf.org/ovf/envelope/1'
-N 'ovf=http://schemas.dmtf.org/ovf/envelope/1'
-N 'vssd=http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData'
)
WORK_DIR=$(mktemp -d)
trap 'rm -rf "${WORK_DIR}"' EXIT
# xmlstar does not support multiple updates at once, and we need to provide
# namespaces to every invocation, so disable quoting warning.
# shellcheck disable=SC2086
xmlstarlet ed ${XML_NS[*]} \
--update '//x:VirtualSystem/@ovf:id' --value "${IMAGE_NAME}" \
"${TEMPLATE_PATH}" \
| xmlstarlet ed ${XML_NS[*]} \
--update '//x:VirtualSystem/x:Name' --value "${IMAGE_NAME}" \
| xmlstarlet ed ${XML_NS[*]} \
--update '//vssd:VirtualSystemIdentifier' --value "${IMAGE_NAME}" \
> "${WORK_DIR}/tmp.ovf"
# Add a disk image to temporary .ovf
cot --force add-disk "${DISK_FILE}" "${WORK_DIR}/tmp.ovf" \
-o "${WORK_DIR}/image.ovf" \
-f vmdisk1 -t harddisk -c scsi
# Add product information and convert .ovf to .ova
cot --force edit-product "${WORK_DIR}/image.ovf" \
-o "${OUTPUT_FILE}" \
--product "${PRODUCT_NAME}"