From 3f1ac3f1080c77722abfdfe40fc764a75aaecc1b Mon Sep 17 00:00:00 2001 From: Joel Granados Date: Wed, 3 Sep 2025 10:47:50 +0200 Subject: [PATCH] cmd/run: Add a --purge argument This is usefull for when you want to remove every temporary directory and kill all processes related to a configuration file. Signed-off-by: Joel Granados --- cmd/run | 53 ++++++++++++++++++++++++++++++++++++++------------ common/rc | 26 +++++++++++++++++++++++++ lib/qemu/share | 2 ++ 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/cmd/run b/cmd/run index 7013a74..a9e292f 100644 --- a/cmd/run +++ b/cmd/run @@ -33,11 +33,13 @@ Options: -t, --trace EVENT,... enable/disable trace event (seperated by commas). Example: '-t nvme' enables all trace events with the nvme prefix. Disable events by prefixing the - event with a '-'" + event with a '-' + -P, --purge Remove all artifacts related to VM. If passed, + VM is not run." _run() { - local short="k:cd:bnfgpt:h" - local long="kernel-dir:,cloud-init,background,print,reset,gdb,perf,trace:,help" + local short="k:cd:bnfgpt:hP" + local long="kernel-dir:,cloud-init,background,print,reset,gdb,perf,trace:,help,purge" if ! tmp=$(getopt -o "$short" --long "$long" -n "$BASENAME" -- "$@"); then exit 1 @@ -84,6 +86,10 @@ _run() { _usage "$USAGE" 0 ;; + '-P' | '--purge' ) + local do_purge=1; shift + ;; + '--' ) shift; break ;; @@ -97,20 +103,12 @@ _run() { readonly RESET _load_vm + _fail_if_running if [[ -v do_gdb && -v do_background ]]; then _fatal 1 "--gdb cannot be used with --background" fi - if [[ -f "${VMRUN}/pidfile" ]]; then - if pidof "$QEMU_SYSTEM_BINARY" >/dev/null; then - _fatal 1 "'${VMRUN}/pidfile' exists; vm might already be running" - else - _log "removing the stale '${VMRUN}/pidfile'" - rm -f "${VMRUN}/pidfile" - fi - fi - local setup_fn setup_fn="$(echo "_setup_${VMNAME}" | tr - _)" if ! declare -f "$setup_fn" >/dev/null; then @@ -175,6 +173,37 @@ _run() { fi fi + # do the purge first if set + if [[ -v do_purge ]]; then + if [[ -v VMCTL_RUN_CLEAN_PROCS ]]; then + for cmd in "${VMCTL_RUN_CLEAN_PROCS[@]}"; do + if [[ -v do_print ]]; then + cmd="echo $cmd" + fi + + #shellcheck disable=SC2086 + if ! eval $cmd ; then + _fatal 1 "Evaluation of ${cmd} failed" + fi + done + fi + + # Dirs always get cleaned at the end + if [[ -v VMCTL_RUN_CLEAN_DIRS ]]; then + for cmd in "${VMCTL_RUN_CLEAN_DIRS[@]}"; do + if [[ -v do_print ]]; then + cmd="echo $cmd" + fi + #shellcheck disable=SC2086 + if ! eval $cmd ; then + _fatal 1 "Evaluation of ${cmd} failed" + fi + done + fi + + exit 0 + fi + params=("${QEMU_PARAMS[@]}") if [[ -v do_print ]]; then diff --git a/common/rc b/common/rc index 85d360f..8747588 100644 --- a/common/rc +++ b/common/rc @@ -153,10 +153,34 @@ _is_running() { return 1 } +_fail_if_running() { + if [[ -f "${VMRUN}/pidfile" ]]; then + if pidof "$QEMU_SYSTEM_BINARY" >/dev/null; then + _fatal 1 "'${VMRUN}/pidfile' exists; vm might already be running" + else + _log "removing the stale '${VMRUN}/pidfile'" + rm -f "${VMRUN}/pidfile" + fi + fi +} + _add_pre_cmd() { VMPRE+=("$1") } +_add_clean_procs_name() { + local pid="$1" + local name="$2" + if [[ -n "${pid}" && -n "${name}" ]]; then + local c="ps -p ${pid} -o comm= | grep -q ${name} && kill -9 ${pid} || true" + VMCTL_RUN_CLEAN_PROCS+=("$c") + fi +} + +_add_clean_dirs() { + VMCTL_RUN_CLEAN_DIRS+=("$1") +} + _load_vm() { declare -xg VMNAME VMROOT VMIMG VMSTATE VMRUN VMLOG @@ -178,6 +202,8 @@ _load_vm() { VMRUN="${VMROOT}/run/${VMNAME}" VMLOG="${VMROOT}/log/${VMNAME}" + _add_clean_dirs "rm -rfv ${VMSTATE} ${VMRUN} ${VMLOG}" + mkdir -p "$VMSTATE" "$VMRUN" "$VMLOG" pushd "$VMCONFIGDIR" >/dev/null || _fatal $? "could not change directory" diff --git a/lib/qemu/share b/lib/qemu/share index 5d21276..965f74e 100644 --- a/lib/qemu/share +++ b/lib/qemu/share @@ -26,6 +26,8 @@ qemu_share_add_virtiofsd() { --socket-path \"${socket_path}\" --log-level off &" _add_pre_cmd "${cmd}" + _add_clean_procs_name "$(cat "${socket_path}.pid" 2> /dev/null)" "virtiofsd" + _qemu_share_force_memshare QEMU_PARAMS+=("-chardev" "${chardev_params}")