feat: add command line argument to kill VMs. close #1195

This commit is contained in:
Martin Wimpress 2024-05-10 19:35:53 +01:00 committed by Martin Wimpress
parent 7b915d691c
commit 135a08c7b0
1 changed files with 37 additions and 13 deletions

View File

@ -1420,6 +1420,7 @@ function usage() {
echo " --display : Select display backend. 'sdl' (default), 'gtk', 'none', 'spice' or 'spice-app'" echo " --display : Select display backend. 'sdl' (default), 'gtk', 'none', 'spice' or 'spice-app'"
echo " --fullscreen : Starts VM in full screen mode (Ctl+Alt+f to exit)" echo " --fullscreen : Starts VM in full screen mode (Ctl+Alt+f to exit)"
echo " --ignore-msrs-always : Configure KVM to always ignore unhandled machine-specific registers" echo " --ignore-msrs-always : Configure KVM to always ignore unhandled machine-specific registers"
echo " --kill : Kill the VM process if it is running"
echo " --screen <screen> : Use specified screen to determine the window size." echo " --screen <screen> : Use specified screen to determine the window size."
echo " --screenpct <percent> : Percent of fullscreen for VM if --fullscreen is not specified." echo " --screenpct <percent> : Percent of fullscreen for VM if --fullscreen is not specified."
echo " --shortcut : Create a desktop shortcut" echo " --shortcut : Create a desktop shortcut"
@ -1598,6 +1599,7 @@ DELETE_DISK=0
DELETE_VM=0 DELETE_VM=0
FULLSCREEN="" FULLSCREEN=""
FULLSPICY="" FULLSPICY=""
KILL_VM=0
OUTPUT="" OUTPUT=""
PUBLIC="" PUBLIC=""
PUBLIC_PERMS="" PUBLIC_PERMS=""
@ -1685,6 +1687,9 @@ else
-ignore-msrs-always|--ignore-msrs-always) -ignore-msrs-always|--ignore-msrs-always)
ignore_msrs_always ignore_msrs_always
exit;; exit;;
-kill|--kill)
KILL_VM=1
shift;;
-screen|--screen) -screen|--screen)
SCREEN="${2}" SCREEN="${2}"
shift shift
@ -1818,6 +1823,30 @@ fi
if [ -n "${VM}" ] && [ -e "${VM}" ]; then if [ -n "${VM}" ] && [ -e "${VM}" ]; then
# shellcheck source=/dev/null # shellcheck source=/dev/null
source "${VM}" source "${VM}"
VMDIR=$(dirname "${disk_img}")
VMNAME=$(basename "${VM}" .conf)
VMPATH=$(realpath "$(dirname "${VM}")")
VM_MONITOR_SOCKETPATH="${VMDIR}/${VMNAME}-monitor.socket"
VM_SERIAL_SOCKETPATH="${VMDIR}/${VMNAME}-serial.socket"
if [ ${KILL_VM} -eq 1 ]; then
echo "Killing ${VMNAME}"
if [ -r "${VMDIR}/${VMNAME}.pid" ]; then
VM_PID=$(head -c50 "${VMDIR}/${VMNAME}.pid")
if kill -9 "${VM_PID}" > /dev/null 2>&1; then
echo " - ${VMNAME} (${VM_PID}) killed."
exit 0
else
echo " - ERROR! ${VMNAME} with PID ${VM_PID} was not killed."
exit 1
fi
else
echo " - ERROR! Could not find a PID for ${VMNAME}."
exit 1
fi
fi
if [ -z "${disk_img}" ]; then if [ -z "${disk_img}" ]; then
echo "ERROR! No disk_img defined." echo "ERROR! No disk_img defined."
exit 1 exit 1
@ -1832,11 +1861,6 @@ if [ -n "${VM}" ] && [ -e "${VM}" ]; then
fi fi
fi fi
VMDIR=$(dirname "${disk_img}")
VMNAME=$(basename "${VM}" .conf)
VMPATH=$(realpath "$(dirname "${VM}")")
VM_MONITOR_SOCKETPATH="${VMDIR}/${VMNAME}-monitor.socket"
VM_SERIAL_SOCKETPATH="${VMDIR}/${VMNAME}-serial.socket"
if [ ! -f "${disk_img}" ]; then if [ ! -f "${disk_img}" ]; then
pushd "${VMPATH}" || exit pushd "${VMPATH}" || exit
fi fi