#!/usr/bin/env bash set -Eeuo pipefail root=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) if [[ "${1:-}" == -h || "${1:-}" == --help ]]; then printf 'Usage: %s [INPUT.iso] [OUTPUT.ova]\n' "$0" printf 'Defaults: %s/ep-eraser-amd64.hybrid.iso and %s/ep-eraser-amd64.ova\n' "$root" "$root" exit 0 fi if (( $# > 2 )); then printf 'Usage: %s [INPUT.iso] [OUTPUT.ova]\n' "$0" >&2 exit 2 fi input=${1:-"$root/ep-eraser-amd64.hybrid.iso"} output=${2:-"$root/ep-eraser-amd64.ova"} for command_name in qemu-img sha256sum tar stat realpath; do command -v "$command_name" >/dev/null 2>&1 || { printf 'ERROR: %s is required.\n' "$command_name" >&2 exit 1 } done [[ -f "$input" ]] || { printf 'ERROR: ISO not found: %s\n' "$input" >&2 exit 1 } input=$(realpath -- "$input") output=$(realpath -m -- "$output") checksum="$output.sha256" for path in "$output" "$checksum"; do if [[ -e "$path" || -L "$path" ]]; then printf 'ERROR: refusing to overwrite existing file: %s\n' "$path" >&2 exit 1 fi done input_bytes=$(stat -c %s -- "$input") if (( input_bytes % 1048576 != 0 )); then printf 'ERROR: ISO size must be a whole number of MiB for OVF disk capacity.\n' >&2 exit 1 fi disk_capacity_mib=$((input_bytes / 1048576)) mkdir -p -- "$(dirname -- "$output")" temp_dir=$(mktemp -d) temp_ova="$output.tmp.$$" temp_checksum="$checksum.tmp.$$" trap 'rm -rf -- "$temp_dir"; rm -f -- "$temp_ova" "$temp_checksum"' EXIT ovf_name=ep-eraser-amd64.ovf disk_name=ep-eraser-amd64-disk1.vmdk manifest_name=ep-eraser-amd64.mf disk_path="$temp_dir/$disk_name" qemu-img convert -p -f raw -O vmdk -o subformat=streamOptimized "$input" "$disk_path" disk_file_bytes=$(stat -c %s -- "$disk_path") cat > "$temp_dir/$ovf_name" < Virtual disk information Logical networks Network connection for EP Eraser EP Eraser live environment EP Eraser 64-bit Linux Debian Live Virtual hardware requirements Virtual Hardware Family 0 EP Eraser vmx-10 hertz * 10^6 Number of virtual CPUs 2 virtual CPUs 1 3 2 byte * 2^20 Memory Size 2048 MB of memory 2 4 2048 0 SCSI Controller SCSI Controller 0 3 lsilogic 6 0 EP Eraser live disk ovf:/disk/disk1 4 3 17 0 true VM Network Network Adapter 1 5 E1000 10 EOF ovf_hash=$(sha256sum "$temp_dir/$ovf_name" | awk '{print $1}') disk_hash=$(sha256sum "$disk_path" | awk '{print $1}') printf 'SHA256(%s)= %s\nSHA256(%s)= %s\n' "$ovf_name" "$ovf_hash" "$disk_name" "$disk_hash" > "$temp_dir/$manifest_name" tar --format=ustar -C "$temp_dir" -cf "$temp_ova" "$ovf_name" "$disk_name" "$manifest_name" hash=$(sha256sum < "$temp_ova") printf '%s %s\n' "${hash%% *}" "$(basename -- "$output")" > "$temp_checksum" mv -- "$temp_ova" "$output" mv -- "$temp_checksum" "$checksum" trap - EXIT printf 'Created OVA: %s\nSHA-256: %s\n' "$output" "$checksum"