refactor: clean up qemu monitor variables

This commit is contained in:
Martin Wimpress 2024-05-13 01:02:28 +01:00 committed by Martin Wimpress
parent a628ee687d
commit 50111513be
1 changed files with 25 additions and 50 deletions

View File

@ -1201,43 +1201,33 @@ function vm_boot() {
-device tpm-tis,tpmdev=tpm0) -device tpm-tis,tpmdev=tpm0)
fi fi
if [ -z "${MONITOR}" ]; then if [ -n "${monitor_telnet_port}" ] && ! is_numeric "${monitor_telnet_port}"; then
MONITOR="${monitor:-none}" echo "ERROR: telnet port must be a number!"
fi
if [ -z "${MONITOR_TELNET_HOST}" ]; then
MONITOR_TELNET_HOST="${monitor_telnet_host:-localhost}"
fi
if [ -z "${MONITOR_TELNET_PORT}" ]; then
MONITOR_TELNET_PORT="${monitor_telnet_port}"
fi
if [ -n "${MONITOR_TELNET_PORT}" ] && ! is_numeric "${MONITOR_TELNET_PORT}"; then
echo "ERROR: telnet-port must be a number!"
exit 1 exit 1
fi fi
if [ "${MONITOR}" == "none" ]; then if [ "${monitor}" == "none" ]; then
args+=(-monitor none) args+=(-monitor none)
echo " - Monitor: (off)" echo " - Monitor: (off)"
elif [ "${MONITOR}" == "telnet" ]; then elif [ "${monitor}" == "telnet" ]; then
# Find a free port to expose monitor-telnet to the guest # Find a free port to expose monitor-telnet to the guest
TEMP_PORT="$(get_port "${MONITOR_TELNET_PORT}" 9)" TEMP_PORT="$(get_port "${monitor_telnet_port}" 9)"
if [ -z "${TEMP_PORT}" ]; then if [ -z "${TEMP_PORT}" ]; then
echo " - Monitor: All Monitor-Telnet ports have been exhausted." echo " - Monitor: All Monitor-Telnet ports have been exhausted."
else else
MONITOR_TELNET_PORT="${TEMP_PORT}" monitor_telnet_port="${TEMP_PORT}"
# shellcheck disable=SC2054 # shellcheck disable=SC2054
args+=(-monitor telnet:"${MONITOR_TELNET_HOST}:${MONITOR_TELNET_PORT}",server,nowait) args+=(-monitor telnet:"${monitor_telnet_host}:${monitor_telnet_port}",server,nowait)
echo " - Monitor: On host: telnet ${MONITOR_TELNET_HOST} ${MONITOR_TELNET_PORT}" echo " - Monitor: On host: telnet ${monitor_telnet_host} ${monitor_telnet_port}"
echo "monitor-telnet,${MONITOR_TELNET_PORT},${MONITOR_TELNET_HOST}" >> "${VMDIR}/${VMNAME}.ports" echo "monitor-telnet,${monitor_telnet_port},${monitor_telnet_host}" >> "${VMDIR}/${VMNAME}.ports"
fi fi
elif [ "${MONITOR}" == "socket" ]; then elif [ "${monitor}" == "socket" ]; then
# shellcheck disable=SC2054,SC2206 # shellcheck disable=SC2054,SC2206
args+=(-monitor unix:${VM_MONITOR_SOCKETPATH},server,nowait) args+=(-monitor unix:${VM_MONITOR_SOCKETPATH},server,nowait)
echo " - Monitor: On host: nc -U \"${VM_MONITOR_SOCKETPATH}\"" echo " - Monitor: On host: nc -U \"${VM_MONITOR_SOCKETPATH}\""
echo " or : socat -,echo=0,icanon=0 unix-connect:${VM_MONITOR_SOCKETPATH}" echo " or : socat -,echo=0,icanon=0 unix-connect:${VM_MONITOR_SOCKETPATH}"
else else
echo "ERROR! \"${MONITOR}\" is an unknown monitor option." echo "ERROR! \"${monitor}\" is an unknown monitor option."
exit 1 exit 1
fi fi
@ -1471,8 +1461,8 @@ function parse_ports_from_file {
elif [ "${port_name}" == "spice" ]; then elif [ "${port_name}" == "spice" ]; then
spice_port="${port_number}" spice_port="${port_number}"
elif [ "${port_name}" == "monitor-telnet" ]; then elif [ "${port_name}" == "monitor-telnet" ]; then
MONITOR_TELNET_PORT="${port_number}" monitor_telnet_port="${port_number}"
MONITOR_TELNET_HOST="${host_name}" monitor_telnet_host="${host_name}"
elif [ "${port_name}" == "serial-telnet" ]; then elif [ "${port_name}" == "serial-telnet" ]; then
SERIAL_TELNET_PORT="${port_number}" SERIAL_TELNET_PORT="${port_number}"
SERIAL_TELNET_HOST="${host_name}" SERIAL_TELNET_HOST="${host_name}"
@ -1492,28 +1482,16 @@ function monitor_send_cmd {
return 1 return 1
fi fi
# Determine monitor channel case "${monitor}" in
local monitor_channel=""
if [ -S "${VMDIR}/${VMNAME}-monitor.socket" ]; then
monitor_channel="socket"
elif [ -n "${MONITOR_TELNET_PORT}" ] && [ -n "${MONITOR_TELNET_HOST}" ]; then
monitor_channel="telnet"
else
echo "WARNING! No qemu-monitor channel available - Couldn't send message to monitor!"
return
fi
case "${monitor_channel}" in
socket) socket)
echo -e " - Sending: ${MSG}" echo -e " - Sending: via socket ${MSG}"
echo -e "${MSG}" | socat -,shut-down unix-connect:"${VM_MONITOR_SOCKETPATH}" > /dev/null 2>&1;; echo -e "${MSG}" | socat -,shut-down unix-connect:"${VM_MONITOR_SOCKETPATH}" > /dev/null 2>&1;;
telnet) telnet)
echo -e " - Sending: ${MSG}" echo -e " - Sending: via telnet ${MSG}"
echo -e "${MSG}" | socat - tcp:"${MONITOR_TELNET_HOST}":"${MONITOR_TELNET_PORT}" > /dev/null 2>&1;; echo -e "${MSG}" | socat - tcp:"${monitor_telnet_host}":"${monitor_telnet_port}" > /dev/null 2>&1;;
*) *)
echo "ERROR! This should never happen!" echo "WARNING! No qemu-monitor channel available - Couldn't send message to monitor!"
exit 1;; return 1;;
esac esac
return 0 return 0
@ -1549,9 +1527,9 @@ height="${height:-}"
ssh_port="${ssh_port:-}" ssh_port="${ssh_port:-}"
spice_port="${spice_port:-}" spice_port="${spice_port:-}"
public_dir="" public_dir=""
monitor="socket" monitor="${monitor:-socket}"
monitor_telnet_port="4440" monitor_telnet_port="${monitor_telnet_port:-4440}"
monitor_telnet_host="localhost" monitor_telnet_host="${monitor_telnet_host:-localhost}"
serial="socket" serial="socket"
serial_telnet_port="6660" serial_telnet_port="6660"
serial_telnet_host="localhost" serial_telnet_host="localhost"
@ -1583,9 +1561,6 @@ VM=""
VMDIR="" VMDIR=""
VMNAME="" VMNAME=""
VMPATH="" VMPATH=""
MONITOR=""
MONITOR_TELNET_PORT=""
MONITOR_TELNET_HOST=""
MONITOR_CMD="" MONITOR_CMD=""
VM_MONITOR_SOCKETPATH="" VM_MONITOR_SOCKETPATH=""
VM_SERIAL_SOCKETPATH="" VM_SERIAL_SOCKETPATH=""
@ -1688,16 +1663,16 @@ else
PUBLIC="${2}" PUBLIC="${2}"
shift 2;; shift 2;;
-monitor|--monitor) -monitor|--monitor)
MONITOR="${2}" monitor="${2}"
shift 2;; shift 2;;
-monitor-cmd|--monitor-cmd) -monitor-cmd|--monitor-cmd)
MONITOR_CMD="${2}" MONITOR_CMD="${2}"
shift 2;; shift 2;;
-monitor-telnet-host|--monitor-telnet-host) -monitor-telnet-host|--monitor-telnet-host)
MONITOR_TELNET_HOST="${2}" monitor_telnet_host="${2}"
shift 2;; shift 2;;
-monitor-telnet-port|--monitor-telnet-port) -monitor-telnet-port|--monitor-telnet-port)
MONITOR_TELNET_PORT="${2}" monitor_telnet_port="${2}"
shift 2;; shift 2;;
-serial|--serial) -serial|--serial)
SERIAL="${2}" SERIAL="${2}"