0
0

control-vm.sh: Refactor logic and provide a help output

This commit is contained in:
Nils Freydank 2024-06-23 21:46:44 +02:00
parent 72013bf33d
commit bceb9a9710
Signed by: nfr
GPG Key ID: 0F1DEAB2D36AD112
2 changed files with 173 additions and 146 deletions

View File

@ -8,7 +8,7 @@
# https://git.holgersson.xyz/nfr/control-vm # https://git.holgersson.xyz/nfr/control-vm
# If that link does not work, feel free to drop me an email. # If that link does not work, feel free to drop me an email.
# Version: 2024-05-23 # For version see variable below comment section.
# Author: Nils Freydank <nils.freydank@datenschutz-ist-voll-doof.de> # Author: Nils Freydank <nils.freydank@datenschutz-ist-voll-doof.de>
# License: MIT # License: MIT
@ -65,9 +65,9 @@
# CLIPBOARD="True" # CLIPBOARD="True"
# _EOF # _EOF
# Provide a sanity check first. __VERSION__="0.0.0"
[[ -z "${1}" ]] && echo "Please provide a guest name!" && exit 1
set_params(){
# === Define default values === # === Define default values ===
CLIPBOARD="${CLIPBOARD:-}" CLIPBOARD="${CLIPBOARD:-}"
CPU_CORES="${CPU_CORES:-8}" CPU_CORES="${CPU_CORES:-8}"
@ -99,8 +99,8 @@ SPICE_SOCKET="${SPICE_SOCKET:-${BASE_PATH}/spice.socket}"
if [[ -f "${GUEST_CONFIG_FILE}" ]]; then if [[ -f "${GUEST_CONFIG_FILE}" ]]; then
source "${GUEST_CONFIG_FILE}" source "${GUEST_CONFIG_FILE}"
else else
echo "No configuration found. Aborting." echo " error: No configuration found. Aborting."
exit 2 exit 3
fi fi
# === Define a base and glue everything together. === # === Define a base and glue everything together. ===
@ -191,7 +191,7 @@ if [[ -n "${UI}" ]]; then
else else
QEMU_ARGS+=( "${QEMU_DISPLAY_NONE_ARGS[@]}" ) QEMU_ARGS+=( "${QEMU_DISPLAY_NONE_ARGS[@]}" )
fi fi
}
# === Define functions for the actual QEMU controlling. === # === Define functions for the actual QEMU controlling. ===
start_qemu(){ start_qemu(){
if [[ ! -f "${PID_FILE}" ]]; then if [[ ! -f "${PID_FILE}" ]]; then
@ -284,9 +284,36 @@ resume_qemu(){
echo "Guest ${GUEST_NAME} is not running, nothing to resume." echo "Guest ${GUEST_NAME} is not running, nothing to resume."
fi fi
} }
print_help(){
echo " ======================================================================="
echo " control-vm.sh - simple QEMU/KVM vm manager"
echo " version: ${__VERSION__}"
echo " author Nils Freydank <nils.freydank@datenschutz-ist-voll-doof.de>"
echo " license: MIT"
echo " url: https://git.holgersson.xyz/nfr/control-vm"
echo " ======================================================================="
echo ""
echo " Use this tool in the following way:"
echo " ./control-vm.sh <name of your virtual machine> <operation>"
echo ""
echo " with operation as one of:"
echo " start | shutdown | stop | connect | save | restore | pause | resume"
echo " or as one of the combinations:"
echo " start+connect | stop+restore | restore+restart"
echo ""
echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo " ! Note that stop means a forced stop - which can lead to data loss. !"
echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
}
# === Parse the user input. === # === Parse the user input. ===
# "$1 != nothing" is already checked earlier. # Provide a sanity check first.
if [[ -z "${1}" ]]; then
echo " error: Please provide a guest name!"
print_help
exit 1
else
set_params "${1}"
case "${2}" in case "${2}" in
start+connect) start_qemu && connect_to_vm;; start+connect) start_qemu && connect_to_vm;;
stop+restore) stop_qemu && restore_qemu;; stop+restore) stop_qemu && restore_qemu;;
@ -299,9 +326,10 @@ case "${2}" in
restore) restore_qemu;; restore) restore_qemu;;
pause) pause_qemu;; pause) pause_qemu;;
resume) resume_qemu;; resume) resume_qemu;;
*) echo "Unknown Operation!" *) echo " error: Unknown Operation!"
echo "Use one of: start | shutdown | stop | connect | save | restore | pause | resume" print_help
echo "or combinations: start+connect | stop+restore | restore+restart" exit 2
echo "Note that stop means a forced stop.";; ;;
esac esac
fi
# vim:fileencoding=utf-8:ts=4:syntax=bash:expandtab # vim:fileencoding=utf-8:ts=4:syntax=bash:expandtab

1
todo
View File

@ -7,4 +7,3 @@
# 7. shared dir without smb, but via FUSE # 7. shared dir without smb, but via FUSE
# 8. bash and zsh-completion # 8. bash and zsh-completion
# 9. don't rely on the PID to check if the VM is running, query socket? # 9. don't rely on the PID to check if the VM is running, query socket?
# 11. print a pretty help message and share on mastodon :)