| 1 |
from typing import Any, Callable, IO, Optional |
| 2 |
|
| 3 |
|
| 4 |
def _dispatchQemuMonitorEventCallback(conn: libvirt.virConnect, dom: libvirt.virDomain, event: str, seconds: int, micros: int, details: str, cbData: dict[str, Any]) -> int: |
| 5 |
"""Dispatches events to python user qemu monitor event callbacks |
| 6 |
""" |
| 7 |
cb = cbData["cb"] |
| 8 |
opaque = cbData["opaque"] |
| 9 |
|
| 10 |
cb(conn, libvirt.virDomain(conn, _obj=dom), event, seconds, micros, details, opaque) |
| 11 |
return 0 |
| 12 |
|
| 13 |
|
| 14 |
def qemuMonitorEventDeregister(conn: libvirt.virConnect, callbackID: int) -> None: |
| 15 |
"""Removes a qemu monitor event callback. De-registering for a callback |
| 16 |
will disable delivery of this event type""" |
| 17 |
try: |
| 18 |
ret = libvirtmod_qemu.virConnectDomainQemuMonitorEventDeregister(conn._o, callbackID) |
| 19 |
if ret == -1: |
| 20 |
raise libvirt.libvirtError('virConnectDomainQemuMonitorEventDeregister() failed') |
| 21 |
del conn.qemuMonitorEventCallbackID[callbackID] |
| 22 |
except AttributeError: |
| 23 |
pass |
| 24 |
|
| 25 |
|
| 26 |
def qemuMonitorEventRegister(conn: libvirt.virConnect, dom: libvirt.virDomain, event: str, cb: Callable[[libvirt.virConnect, libvirt.virDomain, str, int, int, str, libvirt._T], None], opaque: libvirt._T, flags: Optional[int] = 0) -> int: |
| 27 |
"""Adds a qemu monitor event callback. Registering for a monitor |
| 28 |
callback will enable delivery of the events""" |
| 29 |
if not hasattr(conn, 'qemuMonitorEventCallbackID'): |
| 30 |
conn.qemuMonitorEventCallbackID = {} |
| 31 |
cbData = {"cb": cb, "conn": conn, "opaque": opaque} |
| 32 |
if dom is None: |
| 33 |
ret = libvirtmod_qemu.virConnectDomainQemuMonitorEventRegister(conn._o, None, event, cbData, flags) |
| 34 |
else: |
| 35 |
ret = libvirtmod_qemu.virConnectDomainQemuMonitorEventRegister(conn._o, dom._o, event, cbData, flags) |
| 36 |
if ret == -1: |
| 37 |
raise libvirt.libvirtError('virConnectDomainQemuMonitorEventRegister() failed') |
| 38 |
conn.qemuMonitorEventCallbackID[ret] = opaque |
| 39 |
return ret |
| 40 |
|
| 41 |
def qemuMonitorCommandWithFiles(domain: libvirt.virDomain, cmd: str, files: list[int] = [], flags: Optional[int] = 0) -> tuple[str, list[IO]]: |
| 42 |
"""This API is QEMU specific, so it will only work with hypervisor |
| 43 |
connections to the QEMU driver with local connections using the unix |
| 44 |
socket. |
| 45 |
|
| 46 |
Send an arbitrary monitor command @cmd with file descriptors @files to |
| 47 |
domain through the QEMU monitor and optionally return a list of files |
| 48 |
in the returned tuple. There are several requirements to safely |
| 49 |
and successfully use this API: |
| 50 |
|
| 51 |
- A @cmd that queries state without making any modifications is safe |
| 52 |
- A @cmd that alters state that is also tracked by libvirt is unsafe, |
| 53 |
and may cause libvirtd to crash |
| 54 |
- A @cmd that alters state not tracked by the current version of |
| 55 |
libvirt is possible as a means to test new qemu features before |
| 56 |
they have support in libvirt, but no guarantees are made to safety |
| 57 |
|
| 58 |
If VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP is set, the command is considered to |
| 59 |
be a human monitor command and libvirt will automatically convert it into |
| 60 |
QMP if needed. In that case the @result will also be converted back from |
| 61 |
QMP. |
| 62 |
|
| 63 |
Returns a tuple consisting of the string output from @cmd and a list of |
| 64 |
files respectively.""" |
| 65 |
ret = libvirtmod_qemu.virDomainQemuMonitorCommandWithFiles(domain._o, cmd, files, flags) |
| 66 |
if ret is None: |
| 67 |
raise libvirt.libvirtError('virDomainQemuMonitorCommandWithFiles() failed') |
| 68 |
return ret |