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
30 changes: 22 additions & 8 deletions cmd/kill
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ USAGE="usage: ${BASENAME} -c CONFIG kill
The \`kill\` command shuts down the running virtual machine defined by the
given \`CONFIG\` using a \`quit\` QEMU monitor command."

source "${BASEDIR}/common/msgs"
_run_sys_kill() {
local curr_pid=""
if [[ ! -f "${VMRUN}/pidfile" ]]; then
return 1
fi
curr_pid="$(cat "${VMRUN}/pidfile")"
if kill -0 "${curr_pid}" 2> /dev/null; then
kill -9 "${curr_pid}"
fi
rm -f "${VMRUN}/pidfile"
return 0
}

_kill() {
_require_program socat

Expand Down Expand Up @@ -42,15 +56,15 @@ _kill() {
_fatal 1 "$VMNAME does not appear to be running"
fi

if ! _is_daemonized; then
_fatal 1 "$VMNAME is running interactively; attach to monitor from console using 'Ctrl-a c' and kill using 'quit'"
if [[ -S "${VMRUN}/monitor" ]]; then
local args=( "-" "UNIX-CONNECT:${VMRUN}/monitor")
if echo quit | socat "${args[@]}" >> "${VMLOG}/full" 2>&1; then
return 0
fi
fi

local args=(
"-" "UNIX-CONNECT:${VMRUN}/monitor"
)

if ! echo quit | socat "${args[@]}" >> "${VMLOG}/full" 2>&1; then
_fatal 1 "could not power off vm; see ${VMLOG}/full"
if ! _run_sys_kill >> "${VMLOG}/full" ; then
_fatal 1 "could not power off vm; see ${VMLOG}/full"
fi

}
5 changes: 3 additions & 2 deletions cmd/monitor
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The \`monitor\` command is used to run commands in the QEMU monitor of a
running virtual machine defined by the given \`CONFIG\`. If no \`COMMAND\` is
given, attach interactively to the monitor."

source "${BASEDIR}/common/msgs"
_monitor() {
_require_program socat

Expand Down Expand Up @@ -43,8 +44,8 @@ _monitor() {
_fatal 1 "$VMNAME does not appear to be running"
fi

if ! _is_daemonized; then
_fatal 1 "$VMNAME is running interactively; attach to monitor from console using 'Ctrl-a c'"
if [[ ! -S "${VMRUN}/monitor" ]]; then
_fatal 1 "${VMNAME}: ${MSG_CHECK_DAEMONIZED/THE COMMAND/any command}"
fi

local args=(
Expand Down
10 changes: 8 additions & 2 deletions cmd/poweroff
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ USAGE="usage: ${BASENAME} -c CONFIG poweroff
The \`poweroff\` command gracefully shuts down the running virtual machine
defined by the given \`CONFIG\` if supported by the machine."

source "${BASEDIR}/common/msgs"
_poweroff() {
_require_program socat

Expand Down Expand Up @@ -42,8 +43,8 @@ _poweroff() {
_fatal 1 "$VMNAME does not appear to be running"
fi

if ! _is_daemonized; then
_fatal 1 "$VMNAME is running interactively; attach to monitor from console using 'Ctrl-a c' and type 'system_powerdown'"
if [[ ! -S "${VMRUN}/monitor" ]]; then
_fatal 1 "${VMNAME}: ${MSG_CHECK_DAEMONIZED/THE COMMAND/system_powerdown}"
fi

local args=(
Expand All @@ -54,4 +55,9 @@ _poweroff() {
if ! echo system_powerdown | socat "${args[@]}" >> "${VMLOG}/full" 2>&1; then
_fatal 1 "could not power off vm; see ${VMLOG}/full"
fi

if echo "info status" | socat "${args[@]}" >> "${VMLOG}/full" 2>&1; then
_fatal 1 "poweroff returned success but did not execute a power off." \
"Try the kill command"
fi
}
12 changes: 10 additions & 2 deletions cmd/run
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ _run() {
QEMU_PARAMS+=("-daemonize")

# serial and monitor
QEMU_PARAMS+=("-serial" "unix:${VMROOT}/run/${VMNAME}/console,server,nowait")
QEMU_PARAMS+=("-monitor" "unix:${VMROOT}/run/${VMNAME}/monitor,server,nowait")
QEMU_PARAMS+=("-serial" "unix:${VMRUN}/console,server,nowait")
QEMU_PARAMS+=("-monitor" "unix:${VMRUN}/monitor,server,nowait")
else
_add_pre_cmd "rm -f ${VMRUN}/console ${VMRUN}/monitor"
QEMU_PARAMS+=("-serial" "mon:stdio")
fi

Expand Down Expand Up @@ -179,6 +180,10 @@ _run() {
if [[ -v do_print ]]; then
if [[ -v VMPRE ]]; then
echo "Before QEMU:"
if command -v _pre > /dev/null; then
declare -f _pre
fi
echo
for cmd in "${VMPRE[@]}"; do
echo -n " $cmd"
echo
Expand All @@ -203,6 +208,9 @@ _run() {
done

echo
if command -v _post > /dev/null; then
typeset -f _post
fi

exit 0
fi
Expand Down
13 changes: 4 additions & 9 deletions cmd/ssh
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ Options:
-h, --help display this help message and exit
-w, --wait wait until an ssh connection is established
-p, --port port to connect to (default: \"$GUEST_SSH_PORT\")
-l, --login the user to log in as (default: \"$GUEST_SSH_USER\")
-c, --command Execute a command and disconnect"
-l, --login the user to log in as (default: \"$GUEST_SSH_USER\")"

_ssh() {
_require_program ssh

local short="+w,p:,l:,c:,h"
local long="wait,port:,login:,command:,help"
local short="+w,p:,l:,h"
local long="wait,port:,login:,help"

if ! tmp=$(getopt -o "$short" --long "$long" -n "$BASENAME" -- "$@"); then
exit 1
Expand All @@ -43,9 +42,6 @@ _ssh() {
'-h' | '--help' )
_usage "$USAGE" 0
;;
'-c' | '--command' )
cmd="$2"; shift 2
;;
'--' )
shift; break
;;
Expand All @@ -59,7 +55,6 @@ _ssh() {

: "${port:="${GUEST_SSH_PORT}"}"
: "${user:="${GUEST_SSH_USER}"}"
: "${cmd:=""}"

if ! _is_running; then
_fatal 1 "$VMNAME does not appear to be running"
Expand All @@ -82,5 +77,5 @@ _ssh() {
fi

#shellcheck disable=SC2029
ssh "${args[@]}" localhost "$@" "${cmd}"
ssh "${args[@]}" localhost "$@"
}
13 changes: 13 additions & 0 deletions common/msgs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved.
#
# Written by Joel Granados <joel.granados@kernel.org>

source "${BASEDIR}/common/shellcheck"

MSG_CHECK_DAEMONIZED="The vm is most likely running interactively. \
Try to attach monitor using 'Ctrl-a c' and type THE COMMAND. \
Or add '-d' to vmctl command to run daemonized"


8 changes: 0 additions & 8 deletions common/rc
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,6 @@ _is_running() {
return 1
}

_is_daemonized() {
if [[ -S "${VMRUN}/monitor" ]]; then
return 0
fi

return 1
}

_add_pre_cmd() {
VMPRE+=("$1")
}
Expand Down
3 changes: 2 additions & 1 deletion common/shellcheck
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ _silence_sc2034() {
$QEMU_TRACE_EVENTS \
${GUEST_KERNEL_CMDLINE[*]} \
$RESET \
$GUEST_KERNEL_CUSTOM_DIR " > /dev/null
$MSG_CHECK_DAEMONIZED \
$GUEST_KERNEL_CUSTOM_DIR" > /dev/null
}
2 changes: 1 addition & 1 deletion examples/vm/nvme-aarch64.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

QEMU_SYSTEM_AARCH64=$(/usr/bin/which qemu-system-aarch64)
QEMU_SYSTEM_AARCH64=$(/usr/bin/env which qemu-system-aarch64)
GUEST_BOOT_BASE="img/debian-13-genericcloud-arm64.qcow2"

source "aarch64-virt-base.conf"
Expand Down
2 changes: 1 addition & 1 deletion examples/vm/nvme.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

QEMU_SYSTEM_X86_64=$(/usr/bin/which qemu-system-x86_64)
QEMU_SYSTEM_X86_64=$(/usr/bin/env which qemu-system-x86_64)

source "x86_64-q35-base.conf"
#source "x86_64-q35-noimgnix-base.conf"
Expand Down
3 changes: 1 addition & 2 deletions examples/vm/x86_64-q35-base.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ if [[ -f "common.conf" ]]; then
source "common.conf"
fi

QEMU_SYSTEM_BINARY=${QEMU_SYSTEM_X86_64}

: "${QEMU_SYSTEM_BINARY:="$(/usr/bin/env which qemu-system-x86_64)"}"
: "${GUEST_DISPLAY:="0"}"
: "${GUEST_VIOMMU:="1"}"
: "${GUEST_VIOMMU_ARGS:="intel-iommu,intremap=on"}"
Expand Down
10 changes: 4 additions & 6 deletions lib/qemu/rc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ qemu_drive_add() {
while true; do
case "$1" in
'--file' )
local file="/$2"; shift 2
local file="$2"; shift 2
;;

'--format' )
Expand Down Expand Up @@ -139,11 +139,9 @@ qemu_drive_add() {
local id="$1"

if [[ ! -v file ]]; then
local file="/state/${VMNAME}/${id}.img"
fi

if [[ ! -v DOCKER_IMAGE ]]; then
file="${VMROOT}${file}"
local file="${VMROOT}/state/${VMNAME}/${id}.img"
elif [[ "${file}" != /* ]]; then
file="${VMROOT}/${file}"
fi

if [[ -v do_create ]]; then
Expand Down