Skip to content

Instantly share code, notes, and snippets.

@mjf
Last active February 7, 2024 14:06
Show Gist options
  • Save mjf/45435cd86e73ef7bb463d80071b4f8b2 to your computer and use it in GitHub Desktop.
Save mjf/45435cd86e73ef7bb463d80071b4f8b2 to your computer and use it in GitHub Desktop.
KVM/QEMU/LibVirt HMP/QMP shell functions
#! /bin/sh
# KVM/QEMU/LibVirt HMP/QMP shell functions
# Copyright (C) 2024 Matous Jan Fialka, <https://mjf.cz/>
# Released under the terms of the "MIT" license
# Usage: qmp [DOMAIN [COMMAND]]
qmp() {
case "$#" in
('0')
virsh list
;;
('1')
virsh qemu-monitor-command $1 '{
"execute": "query-version"
}' |
jq --indent 4
;;
(*)
local DOMAIN="$1"
shift
local QUERY="$(
sed '{
s|^|{"|
s|$|"}|
s|[ \t]\+|,|g
s|:|":"|g
}' <<< "$@"
)"
virsh qemu-monitor-command "$DOMAIN" "$QUERY" |
jq --indent 4
esac
}
# Usage: hmp [DOMAIN [COMMAND]]
hmp() {
case "$#" in
('0')
virsh list
;;
('1')
virsh qemu-monitor-command "$1" --hmp 'info version'
;;
(*)
local DOMAIN="$1"
shift
virsh qemu-monitor-command $DOMAIN --hmp "$@"
esac |
cat -s
}
# vi:ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment