From d66d9c3c1bba97f418f12280709a743d0ea08cb8 Mon Sep 17 00:00:00 2001 From: Dino Korah <691011+codemedic@users.noreply.github.com> Date: Sun, 22 Mar 2026 17:50:14 +0000 Subject: [PATCH] feat: stop virtiofsd gracefully when VM is killed or fails to start Write virtiofsd PID to a file alongside other VM runtime files. Add stop_virtiofsd() which sends SIGTERM and waits up to 1s for a clean exit before falling back to SIGKILL. Call it from kill_vm() and on QEMU startup failure. --- quickemu | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/quickemu b/quickemu index 7d3141a..27701d4 100755 --- a/quickemu +++ b/quickemu @@ -167,12 +167,14 @@ function kill_vm() { rm -f "${VMDIR}/${VMNAME}.pid" rm -f "${VMDIR}/${VMNAME}.spice" rm -f "${VMDIR}/${VMNAME}.sock" + stop_virtiofsd elif [ -n "${VM_PID}" ]; then if kill -9 "${VM_PID}" > /dev/null 2>&1; then echo " - ${VMNAME} (${VM_PID}) killed." rm -f "${VMDIR}/${VMNAME}.pid" rm -f "${VMDIR}/${VMNAME}.spice" rm -f "${VMDIR}/${VMNAME}.sock" + stop_virtiofsd else echo " - ${VMNAME} (${VM_PID}) was not killed." fi @@ -1662,9 +1664,44 @@ function start_virtiofsd() { cat "${virtiofsd_stderr}" >> "${VMDIR}/${VMNAME}.log" rm -f "${virtiofsd_stderr}" + echo "${VIRTIOFSD_PID}" > "${VMDIR}/${VMNAME}.virtiofsd-pid" echo " - virtiofsd: ${VIRTIOFSD_SOCKET} (${VIRTIOFSD_PID})" } +function stop_virtiofsd() { + local pid_file="${VMDIR}/${VMNAME}.virtiofsd-pid" + local pid="" + + if [ -n "${VIRTIOFSD_PID}" ]; then + pid="${VIRTIOFSD_PID}" + elif [ -r "${pid_file}" ]; then + pid=$(cat "${pid_file}") + fi + + if [ -z "${pid}" ]; then + return + fi + + if kill -0 "${pid}" 2>/dev/null; then + # Ask virtiofsd to shut down gracefully first; it will close the + # vhost-user socket and flush any pending I/O before exiting. + kill -TERM "${pid}" 2>/dev/null + local i + for i in 1 2 3 4 5; do + kill -0 "${pid}" 2>/dev/null || break + sleep 0.2 + done + # Force-kill only if it is still alive after the grace period. + if kill -0 "${pid}" 2>/dev/null; then + kill -KILL "${pid}" 2>/dev/null + fi + fi + + rm -f "${pid_file}" "${VMDIR}/${VMNAME}.virtiofsd-sock" + VIRTIOFSD_PID="" + VIRTIOFSD_SOCKET="" +} + function vm_boot() { AUDIO_DEV="" BALLOON="-device virtio-balloon" @@ -2226,6 +2263,7 @@ function vm_boot() { rm -f "${VMDIR}/${VMNAME}.pid" rm -f "${VMDIR}/${VMNAME}.spice" rm -f "${VMDIR}/${VMNAME}.sock" + stop_virtiofsd echo && cat "${VMDIR}/${VMNAME}.log" exit 1 fi