Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions cmd/run
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -84,6 +86,10 @@ _run() {
_usage "$USAGE" 0
;;

'-P' | '--purge' )
local do_purge=1; shift
;;

'--' )
shift; break
;;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions common/rc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions lib/qemu/share
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down