#!/bin/bash set -o pipefail shopt -s expand_aliases alias die='EXIT=$? LINE=$LINENO error_exit' trap die ERR function error_exit() { REASON=$1 MSG="\e[91mERROR: \e[93m$EXIT@" if [ -z "$REASON" ]; then MSG="$MSG$LINE:" REASON="Unknown failure occured." else MSG="$MSG`echo $(( $LINE - 1 ))`:" fi echo -e "$MSG \e[97m$REASON\e[39m\e[49m" exit $EXIT } function cleanup() { popd >/dev/null rm -rf $TMP } trap cleanup EXIT TMP=`mktemp -d` pushd $TMP >/dev/null STORAGE=${1:-local-lvm} pvesm list $STORAGE >& /dev/null || die "'$STORAGE' is not a valid storage ID." pvesm status -content images -storage $STORAGE >&/dev/null || die "'$STORAGE' does not allow 'Disk image' to be stored." STORAGE_TYPE=`pvesm status -storage $STORAGE | awk 'NR>1 {print $2}'` VMID=$(cat<&/dev/null qm importdisk $VMID ${FILE%".gz"} $STORAGE $IMPORT_OPT 1>&/dev/null qm set $VMID -bootdisk sata0 -efidisk0 ${DISK0_REF},size=128K \ -sata0 ${DISK1_REF},size=6G > /dev/null echo -e "\n\n\n" \ "********************************\n" \ "* Completed Successfully *\n" \ "* New VM ID is \e[1m$VMID\e[0m *\n" \ "********************************\n"