style: make snapshot actions consistent with other actions

This commit is contained in:
Martin Wimpress 2024-05-13 10:41:30 +01:00 committed by Martin Wimpress
parent b3a1300144
commit ef711a0bbc
1 changed files with 52 additions and 44 deletions

View File

@ -90,60 +90,64 @@ function kill_vm() {
} }
function snapshot_apply() { function snapshot_apply() {
echo "Snapshot apply to ${disk_img}"
local TAG="${1}" local TAG="${1}"
if [ -z "${TAG}" ]; then if [ -z "${TAG}" ]; then
echo "ERROR! No snapshot tag provided." echo " - ERROR! No snapshot tag provided."
exit exit
fi fi
if [ -e "${disk_img}" ]; then if [ -e "${disk_img}" ]; then
if ${QEMU_IMG} snapshot -q -a "${TAG}" "${disk_img}"; then if ${QEMU_IMG} snapshot -q -a "${TAG}" "${disk_img}"; then
echo "SUCCESS! Applied snapshot ${TAG} to ${disk_img}" echo " - Applied snapshot '${TAG}' to ${disk_img}"
else else
echo "ERROR! Failed to apply snapshot ${TAG} to ${disk_img}" echo " - ERROR! Failed to apply snapshot '${TAG}' to ${disk_img}"
fi fi
else else
echo "NOTE! ${disk_img} not found. Doing nothing." echo " - NOTE! ${disk_img} not found. Doing nothing."
fi fi
} }
function snapshot_create() { function snapshot_create() {
echo "Snapshotting ${disk_img}"
local TAG="${1}" local TAG="${1}"
if [ -z "${TAG}" ]; then if [ -z "${TAG}" ]; then
echo "ERROR! No snapshot tag provided." echo "- ERROR! No snapshot tag provided."
exit exit
fi fi
if [ -e "${disk_img}" ]; then if [ -e "${disk_img}" ]; then
if ${QEMU_IMG} snapshot -q -c "${TAG}" "${disk_img}"; then if ${QEMU_IMG} snapshot -q -c "${TAG}" "${disk_img}"; then
echo "SUCCESS! Created snapshot ${TAG} of ${disk_img}" echo " - Created snapshot '${TAG}' for ${disk_img}"
else else
echo "ERROR! Failed to create snapshot ${TAG} of ${disk_img}" echo " - ERROR! Failed to create snapshot '${TAG}' for ${disk_img}"
fi fi
else else
echo "NOTE! ${disk_img} not found. Doing nothing." echo " - NOTE! ${disk_img} not found. Doing nothing."
fi fi
} }
function snapshot_delete() { function snapshot_delete() {
echo "Snapshot removal ${disk_img}"
local TAG="${1}" local TAG="${1}"
if [ -z "${TAG}" ]; then if [ -z "${TAG}" ]; then
echo "ERROR! No snapshot tag provided." echo " - ERROR! No snapshot tag provided."
exit exit
fi fi
if [ -e "${disk_img}" ]; then if [ -e "${disk_img}" ]; then
if ${QEMU_IMG} snapshot -q -d "${TAG}" "${disk_img}"; then if ${QEMU_IMG} snapshot -q -d "${TAG}" "${disk_img}"; then
echo "SUCCESS! Deleted snapshot ${TAG} of ${disk_img}" echo " - Deleted snapshot '${TAG}' from ${disk_img}"
else else
echo "ERROR! Failed to delete snapshot ${TAG} of ${disk_img}" echo " - ERROR! Failed to delete snapshot '${TAG}' from ${disk_img}"
fi fi
else else
echo "NOTE! ${disk_img} not found. Doing nothing." echo " - NOTE! ${disk_img} not found. Doing nothing."
fi fi
} }
function snapshot_info() { function snapshot_info() {
echo
if [ -e "${disk_img}" ]; then if [ -e "${disk_img}" ]; then
${QEMU_IMG} info "${disk_img}" ${QEMU_IMG} info "${disk_img}"
fi fi
@ -1628,18 +1632,21 @@ else
network="none" network="none"
shift;; shift;;
-snapshot|--snapshot) -snapshot|--snapshot)
if [ -z "${2}" ]; then
echo "ERROR! '--snapshot' needs an action to perform."
exit 1
fi
SNAPSHOT_ACTION="${2}" SNAPSHOT_ACTION="${2}"
if [ -z "${SNAPSHOT_ACTION}" ]; then if [ -z "${3}" ] && [ "${SNAPSHOT_ACTION}" != "info" ]; then
echo "ERROR! No snapshot action provided." echo "ERROR! '--snapshot ${SNAPSHOT_ACTION}' needs a tag."
exit 1 exit 1
fi fi
shift SNAPSHOT_TAG="${3}"
SNAPSHOT_TAG="${2}" if [ "${SNAPSHOT_ACTION}" == "info" ]; then
if [ -z "${SNAPSHOT_TAG}" ] && [ "${SNAPSHOT_ACTION}" != "info" ]; then shift 2
echo "ERROR! No snapshot tag provided." else
exit 1 shift 3
fi fi;;
shift 2;;
-status-quo|--status-quo) -status-quo|--status-quo)
STATUS_QUO="-snapshot" STATUS_QUO="-snapshot"
shift;; shift;;
@ -1741,6 +1748,30 @@ if [ -n "${VM}" ] && [ -e "${VM}" ]; then
exit exit
fi fi
if [ -n "${SNAPSHOT_ACTION}" ]; then
case ${SNAPSHOT_ACTION} in
apply)
snapshot_apply "${SNAPSHOT_TAG}"
snapshot_info
exit;;
create)
snapshot_create "${SNAPSHOT_TAG}"
snapshot_info
exit;;
delete)
snapshot_delete "${SNAPSHOT_TAG}"
snapshot_info
exit;;
info)
echo "Snapshot information ${disk_img}"
snapshot_info
exit;;
*)
echo "ERROR! \"${SNAPSHOT_ACTION}\" is not a supported snapshot action."
usage;;
esac
fi
# Backwards compatibility for ${driver_iso} # Backwards compatibility for ${driver_iso}
if [ -n "${driver_iso}" ] && [ -z "${fixed_iso}" ]; then if [ -n "${driver_iso}" ] && [ -z "${fixed_iso}" ]; then
fixed_iso="${driver_iso}" fixed_iso="${driver_iso}"
@ -1831,29 +1862,6 @@ else
usage usage
fi fi
if [ -n "${SNAPSHOT_ACTION}" ]; then
case ${SNAPSHOT_ACTION} in
apply)
snapshot_apply "${SNAPSHOT_TAG}"
snapshot_info
exit;;
create)
snapshot_create "${SNAPSHOT_TAG}"
snapshot_info
exit;;
delete)
snapshot_delete "${SNAPSHOT_TAG}"
snapshot_info
exit;;
info)
snapshot_info
exit;;
*)
echo "ERROR! \"${SNAPSHOT_ACTION}\" is not a supported snapshot action."
usage;;
esac
fi
if [ -z "${VM_PID}" ]; then if [ -z "${VM_PID}" ]; then
#TODO: double quote the args array to prevent word splitting and this can be removed #TODO: double quote the args array to prevent word splitting and this can be removed
# Fix failing to start VM with spaces in the path # Fix failing to start VM with spaces in the path