Invoke QMP commands using native libvirt API

Hi all, I have a problem to execute qmp commands via qemu guest agent using libvirt-Python. In libivrt and qemu tutorial, qmp commands are invoked by terminal. In my project, I want to invoke the commands natively using libvirt-Python API, not using Python subprocess module. (my test shows using subprocess to invoke the commands have some issues) I'm not sure if libvirt-Python provides such API. If the API is not avaiable, as libvirt natively use C interface to interact with QEMU, do we have to implement it by hand? Another way to invoke qmp commands is communicate the socket of qemu guest agent. I tried it which did not work. Invoking qmp commands with virsh works fine. Sorry if this post should be posted in other libvirt mail lists. Thank you very much for feedbacks. Environment - OS: Centos 8.5(x86_64) - libvirt 6.0.0 - QEMU 6.0.0 === qemu guest socket $ss | grep libvirt /var/lib/libvirt/qemu/channel/target/domain-71-run-win7/org.qemu.guest_agent.0 8568953 /run/libvirt/libvirt-sock $sudo socat unix-connect:/var/lib/libvirt/qemu/channel/target/domain-71-run-win7/org.qemu.guest_agent.0 ${"execute":"guest-info"} // nothing shows === invoke qmp commands using Python import subprocess cmd = '{"execute": "guest-info"}' p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) result = p.communicate()[0]

On 4/18/25 14:55, Blade Liu wrote:
Hi all,
I have a problem to execute qmp commands via qemu guest agent using libvirt-Python. In libivrt and qemu tutorial, qmp commands are invoked by terminal. In my project, I want to invoke the commands natively using libvirt-Python API, not using Python subprocess module. (my test shows using subprocess to invoke the commands have some issues)
I'm not sure if libvirt-Python provides such API. If the API is not avaiable, as libvirt natively use C interface to interact with QEMU, do we have to implement it by hand?
Another way to invoke qmp commands is communicate the socket of qemu guest agent. I tried it which did not work. Invoking qmp commands with virsh works fine.
Sorry if this post should be posted in other libvirt mail lists. Thank you very much for feedbacks.
Environment - OS: Centos 8.5(x86_64) - libvirt 6.0.0 - QEMU 6.0.0
=== qemu guest socket $ss | grep libvirt /var/lib/libvirt/qemu/channel/target/domain-71-run-win7/org.qemu.guest_agent.0 8568953 /run/libvirt/libvirt-sock
$sudo socat unix-connect:/var/lib/libvirt/qemu/channel/target/domain-71-run-win7/org.qemu.guest_agent.0 ${"execute":"guest-info"} // nothing shows
There's some synchronization needed first to reset agent's internal state: https://gitlab.com/libvirt/libvirt/-/blob/master/src/qemu/qemu_agent.c?ref_t...
=== invoke qmp commands using Python import subprocess
cmd = '{"execute": "guest-info"}' p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) result = p.communicate()[0]
There's libvirt_qemu module, why not use it? import libvirt import libvirt_qemu conn = libvirt.open("qemu:///system") dom = conn.lookupByName("fedora") print(libvirt_qemu.qemuAgentCommand(dom, "{\"execute\":\"guest-info\"}", libvirt_qemu.VIR_DOMAIN_QEMU_AGENT_COMMAND_DEFAULT, 0)) Or even better - majority of agent's APIs are exposed via libvirt's APIs - why not use them instead? Are you perhaps running an API that's not wrapped? Michal

Thanks! I was not aware of libvirt/libvirt_qemu can directly invoke qmp commands. From libvirt Python code, there are some APIs missing, e.g., guest-exec(). "There's some synchronization needed first to reset agent's internal state: https://gitlab.com/libvirt/libvirt/-/blob/master/src/qemu/qemu_agent.c?re..." As for guest synchronization, is there something need to to manually for synchronization? Regards
participants (2)
-
Blade Liu
-
Michal Prívozník