Skip to contentSkip to conversation

Getting VM state from outside QEMU?

Qemu-devel Erik Rull 4 participants 8 messages Apr 7, 2015 – Apr 9, 2015

lore
Share & tools
lore

Erik Rull

author
#0 lore raw

Hi all,

I need a pretty simple way to get the current state of the VM running in QEMU - I only need the VM state (e.g. running, paused,...). Since my environment does not have any perl, python or other high level scripting capabilities, a simple way e.g. via a shell script would be nice. QEMU is running daemonized, so interacting with the qemu console is not possible.
Are there any usable entries in /proc, /dev or /sys that could be used?

Thanks.

Best regards,

Erik

#1 lore raw
Quoted 9 lines · Erik Rull

Hi all,

I need a pretty simple way to get the current state of the VM running in QEMU - I only need the VM state (e.g. running, paused,...). Since my environment does not have any perl, python or other high level scripting capabilities, a simple way e.g. via a shell script would be nice. QEMU is running daemonized, so interacting with the qemu console is not possible.
Are there any usable entries in /proc, /dev or /sys that could be used?

Hello Erik,

My suggestion is to create a script that sends the QMP command "query-status" an then parse the result. The syntax and output is:

-> { "execute": "query-status" }
<- { "return": { "running": true, "singlestep": false, "status":
"running" } }

Quoted 6 lines

Thanks.

Best regards,

Erik

Signature · 2 lines
Paulo Ricardo Paz Vital <[email protected]>
ProfitBricks GmbH
author
#2 lore raw

Hello Paulo,

Quoted 18 lines

On April 7, 2015 at 3:42 PM Paulo Ricardo Paz Vital <[email protected]> wrote:

Quoted 12 lines · Erik Rull

Hi all,

I need a pretty simple way to get the current state of the VM running in QEMU -
I only need the VM state (e.g. running, paused,...). Since my environment does
not have any perl, python or other high level scripting capabilities, a simple
way e.g. via a shell script would be nice. QEMU is running daemonized, so interacting with the qemu console is not possible.
Are there any usable entries in /proc, /dev or /sys that could be used?

Quoted 9 lines

Hello Erik,

My suggestion is to create a script that sends the QMP command "query-status" an then parse the result. The syntax and output is:

-> { "execute": "query-status" }
<- { "return": { "running": true, "singlestep": false, "status":
"running" } }

Sounds good - I tried that - but all attempts return that the command has not been found. I added the following command line snippet and the results are:
[...] -qmp tcp:localhost:4444,server,nowait [...]

172.17.48.45 ~ # telnet 127.0.0.1 4444
{"QMP": {"version": {"qemu": {"micro": 0, "minor": 1, "major": 2}, "package":
""}, "capabilities": []}}

{ "execute": "query-status" }
{"error": {"class": "CommandNotFound", "desc": "The command query-status has not been found"}}
{ "execute": "info status" }
{"error": {"class": "CommandNotFound", "desc": "The command info status has not been found"}}
{ "execute": "info", "arguments": { "status": "" } }
{"error": {"class": "CommandNotFound", "desc": "The command info has not been found"}}
{ "execute": "query-kvm" }
{"error": {"class": "CommandNotFound", "desc": "The command query-kvm has not been found"}}

Any ideas what's wrong here?

Thanks.

Best regards,

Erik

#3 lore raw
Quoted 9 lines · Erik Rull
Quoted 8 lines

My suggestion is to create a script that sends the QMP command "query-status" an then parse the result. The syntax and output is:

-> { "execute": "query-status" }
<- { "return": { "running": true, "singlestep": false, "status":
"running" } }

Quoted 7 lines

Sounds good - I tried that - but all attempts return that the command has not been found. I added the following command line snippet and the results are:
[...] -qmp tcp:localhost:4444,server,nowait [...]

172.17.48.45 ~ # telnet 127.0.0.1 4444
{"QMP": {"version": {"qemu": {"micro": 0, "minor": 1, "major": 2}, "package":
""}, "capabilities": []}}

You HAVE to use {"execute":"qmp_capabilities"} (possibly with an "id":...) as your first command on the monitor, before you can issue any other command. I really wish we could improve the error message:

Quoted 4 lines

{ "execute": "query-status" }
{"error": {"class": "CommandNotFound", "desc": "The command query-status has not been found"}}

it would be a LOT nicer if we reported 'still in negotiation phase; "qmp_capabilities" expected' than a bland "CommandNotFound". Of course, patches are welcome to improve the experience there!

Similarly, once you are NOT in capabilities negotiation, any subsequent use of "qmp_capabilities" fails. That's also something where the error message could be improved.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#4 lore raw

Hi Eric,

Quoted 33 lines · Eric Blake
Quoted 14 lines · Erik Rull
Quoted 8 lines

My suggestion is to create a script that sends the QMP command "query-status" an then parse the result. The syntax and output is:

-> { "execute": "query-status" }
<- { "return": { "running": true, "singlestep": false, "status":
"running" } }

Sounds good - I tried that - but all attempts return that the command has not
been found. I added the following command line snippet and the results are:
[...] -qmp tcp:localhost:4444,server,nowait [...]

Quoted 4 lines

172.17.48.45 ~ # telnet 127.0.0.1 4444
{"QMP": {"version": {"qemu": {"micro": 0, "minor": 1, "major": 2}, "package":
""}, "capabilities": []}}

You HAVE to use {"execute":"qmp_capabilities"} (possibly with an "id":...) as your first command on the monitor, before you can issue any other command. I really wish we could improve the error message:

Quoted 5 lines

{ "execute": "query-status" }
{"error": {"class": "CommandNotFound", "desc": "The command query-status has not
been found"}}

Quoted 11 lines

it would be a LOT nicer if we reported 'still in negotiation phase; "qmp_capabilities" expected' than a bland "CommandNotFound". Of course, patches are welcome to improve the experience there!

Similarly, once you are NOT in capabilities negotiation, any subsequent use of "qmp_capabilities" fails. That's also something where the error message could be improved.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Thanks! That fixed it - even if I don't know why this was actually necessary :-)

{"execute":"qmp_capabilities"}
{"return": {}}
{ "execute": "query-status" }
{"return": {"status": "running", "singlestep": false, "running": true}}

Best regards,

Erik

#5 lore raw
Quoted 10 lines · Erik Rull
Quoted 8 lines
Quoted 4 lines

172.17.48.45 ~ # telnet 127.0.0.1 4444
{"QMP": {"version": {"qemu": {"micro": 0, "minor": 1, "major": 2}, "package":
""}, "capabilities": []}}

You HAVE to use {"execute":"qmp_capabilities"} (possibly with an "id":...) as your first command on the monitor, before you can issue any other command. I really wish we could improve the error message:

Thanks! That fixed it - even if I don't know why this was actually necessary :-)

docs/qmp/qmp-spec.txt

It's part of the protocol, to allow us to make non-backwards-compatible changes to how QMP is parsed while still keeping sane interactions between mismatches in server vs. client (to date, we have wisely not needed to add any capabilities).

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#6 lore raw
In reply to Eric Blake

Sorry guys only checked the emails this morning :-D

Quoted 23 lines · Eric Blake
Quoted 9 lines · Erik Rull
Quoted 8 lines

My suggestion is to create a script that sends the QMP command "query-status" an then parse the result. The syntax and output is:

-> { "execute": "query-status" }
<- { "return": { "running": true, "singlestep": false, "status":
"running" } }

Quoted 7 lines

Sounds good - I tried that - but all attempts return that the command has not been found. I added the following command line snippet and the results are:
[...] -qmp tcp:localhost:4444,server,nowait [...]

172.17.48.45 ~ # telnet 127.0.0.1 4444
{"QMP": {"version": {"qemu": {"micro": 0, "minor": 1, "major": 2}, "package":
""}, "capabilities": []}}

You HAVE to use {"execute":"qmp_capabilities"} (possibly with an "id":...) as your first command on the monitor, before you can issue any other command. I really wish we could improve the error message:

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

{ "execute": "query-status" }
{"error": {"class": "CommandNotFound", "desc": "The command query-status has not been found"}}

it would be a LOT nicer if we reported 'still in negotiation phase; "qmp_capabilities" expected' than a bland "CommandNotFound". Of course, patches are welcome to improve the experience there!

That's an interesting point to see. I'm going to take a look on this and submit a patch until next week, probably good to 2.4 release.

Quoted 5 lines

Similarly, once you are NOT in capabilities negotiation, any subsequent use of "qmp_capabilities" fails. That's also something where the error message could be improved.

Signature · 2 lines
Paulo Ricardo Paz Vital <[email protected]>
ProfitBricks GmbH
#7 lore raw
In reply to Erik Rull
Quoted 9 lines · Erik Rull

Hi all,

I need a pretty simple way to get the current state of the VM running in QEMU - I only need the VM state (e.g. running, paused,...). Since my environment does not have any perl, python or other high level scripting capabilities, a simple way e.g. via a shell script would be nice. QEMU is running daemonized, so interacting with the qemu console is not possible.
Are there any usable entries in /proc, /dev or /sys that could be used?

Quoted 6 lines

Thanks.

Best regards,

Erik

Assuming this is some sort of embedded system under which python is not an option:

You'll need to use the monitor interface (-monitor), and set it to a pipe. you can echo commands into the pipe ("info status") and read lines out of it.

try:

mkfifo monitor.out
mkfifo monitor.in
./qemu-system-blah -monitor pipe:monitor &
echo "info status" > monitor.in
read $line < monitor.out # 'info status'
read $line < monitor.out # 'VM status: running'

And now you've got e.g. "VM status: running" in $line. Use whichever shell tricks to distill this down as needed.

Warning: this is using the human monitor protocol, which as the name implies, is not meant for you to be scraping it with tools. It could change arbitrarily in the future and shouldn't be used in production.

Really recommend the QMP interface and something capable of sending/reading JSON for programmatic scripting of QEMU. Check out scripts/qmp.py for a nice library for sending/receiving these sorts of things.

Alternatively you can use a similar fifo trick for -qmp and try to parse the answers you get with sed/awk/bash regex etc.