2

I'm having an instance of qemu running on Windows 7, running without an open terminal. Now I want to shutdown the machine with the name MyMachineName or add an USB-Device to it. I need a scriptable solution. Libvirt is not a solution, because it has other disadvantages for my system.

Im looking for a magic line like:

qemu-monitor -connect=MyMachineName command="shutdown"

How can I do it?

  • Unless you've redirected the QEMU monitor to a device you can access via the command line (e.g., via -monitor <dev>), you can't connect to it via a script. – dsstorefile1 Feb 26 '18 at 19:59
  • So, you saying, that I can do the following: qemu-system-... -monitor "/dev/ttyMyConsol" and than echo "My QemuMonitorCommand" > /dev/ttyMyConsol and it will make qemu execute the command on that machine? but the only way to control the machine is the qemu-monitor? – Cutton Eye Feb 27 '18 at 13:01
4

Someone might be able to chime in with a proper command for operating on TTYs, but I'll post a solution in the meantime involving the network.

There are a couple options for redirecting the QEMU monitor. One way is to have QEMU offer access to its monitor via telnet:

$ qemu-system-i386 -monitor telnet:127.0.0.1:55555,server,nowait;

Then, QEMU can be scripted by piping commands to telnet. This is fine as long as the output of commands can be discarded since the telnet session will probably close too quickly for visual feedback:

$ echo system_powerdown |telnet 127.0.0.1 55555
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Connection closed by foreign host.
$ _  # qemu sends the guest an ACPI shutdown signal

If the output of the commands executed on the monitor need to be collected, a TCP session can be used instead:

$ qemu-system-i386 -monitor tcp:127.0.0.1:55555,server,nowait;

Then, commands can be sent to the listening monitor via netcat or a similar utility:

$ echo info\ kvm |nc -N 127.0.0.1 55555
QEMU 2.11.0 monitor - type 'help' for more information
(qemu) info kvm
kvm support: enabled
(qemu) $ echo system_powerdown |nc -N 127.0.0.1 55555
QEMU 2.11.0 monitor - type 'help' for more information
(qemu) system_powerdown
(qemu) $  # hit return
$ _  # qemu sends the guest an ACPI shutdown signal

Here is a link to partial documentation of QEMU monitor commands: https://en.wikibooks.org/wiki/QEMU/Monitor

4

My preferred way to do this is connect to the QEMU "monitor" via a UNIX socket. QEMU is quite flexible like this, although the method is very poorly documented.

Start QEMU with the -monitor parameter in the following fashion:

$ qemu-system-i386 -monitor unix:qemu-monitor-socket,server,nowait

qemu-monitor-socket here is not a keyword, but a host path and filename of your choice to represent the socket on disk. You'll see this file created once QEMU starts.

The options server,nowait tell QEMU to listen for connections, but not block while waiting for them.

You can use the socat utility to connect to the socket to type commands to the QEMU monitor prompt:

$ socat - unix-connect:qemu-monitor-socket
QEMU 2.8.1 monitor - type 'help' for more information
(qemu)

What socat actually does is connect two arbitrary streams/sockets together. - is a synonym for stdio, i.e., the console keyboard and output. Parameter two says to connect to the socket file created earlier.

To issue a one-shot command to the monitor, echo it into socat, which will pipe it thru the UNIX socket to QEMU:

$ echo "info status" | socat - unix-connect:qemu-monitor-socket
QEMU 2.8.1 monitor - type 'help' for more information
(qemu) info status
VM status: running
(qemu)
$

For a cleaner display when scripting this, I also add | tail --lines=+2 | grep -v "^(qemu) " to filter the unneeded first line and the (qemu) prompt lines.

$ echo "info status" | socat - unix-connect:qemu-monitor-socket | tail --lines=+2 | grep -v "^(qemu) "
VM status: running
$

(QEMU also permits -qmp in place of -monitor, providing a JSON-based interface; that might be more robust for programmatic control, but I never tried it.)

To shut down the VM, useful monitor commands are system_powerdown, equivalent to pressing the on/off button of the imaginary machine, and quit, quitting QEMU immediately.

Note that the socket file on disk will always be empty. It doesn't store data; it's just an arbitrary handle for programs to establish communications.

  • 1
    This gives a newbe some more useful informations how qemu works with sockets! =) – Cutton Eye Oct 22 '18 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.