28

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?

1
  • 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
    Commented Feb 27, 2018 at 13:01

2 Answers 2

48

My preferred way to do this is to connect to the QEMU "monitor" via a UNIX socket. It works well, although the method is very poorly documented.

When you start QEMU, add the -monitor parameter as follows:

$ qemu-system-i386 [..other params..] -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 when QEMU starts.

The options server,nowait tell QEMU to listen for connections, but start the VM without waiting for a connection.

You can use the socat utility (available from all good repositories) to connect to the socket to type commands to the QEMU monitor prompt:

$ socat -,echo=0,icanon=0 unix-connect:qemu-monitor-socket
QEMU 2.8.1 monitor - type 'help' for more information
(qemu) _

What socat does is connect two arbitrary streams/sockets together. The first parameter - is a synonym for stdio, i.e., the console keyboard and output. The couple of options echo=0,icanon=0 make keyboard interaction nicer here by preventing re-echoing of entered commands and enabling Tab completion and arrow keys for history. The final parameter says to connect to the socket file created earlier.

To send a one-shot command to QEMU, echo it thru socat to the UNIX socket:

$ 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 cleaner output in a script, I also add | tail --lines=+2 | grep -v '^(qemu)' to filter out the 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
$ _

To shut down the VM as you wanted, useful monitor commands are system_powerdown, which presses the on/off button of the imaginary machine so it can gracefully shut down, or quit, which quits QEMU immediately.

A couple of notes:

  • 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.

  • A socket file on disk is always empty. It doesn't store data; it's just an arbitrary handle for programs to open communications.

9
  • 2
    This gives a newbe some more useful informations how qemu works with sockets! =)
    – Cutton Eye
    Commented Oct 22, 2018 at 7:59
  • 1
    This solution is very nice because we can set appropriate permissions on the socket (if qemu doesn't by default) so that nobody else than root can send commands to the VM; the accepted answer does not care about that. However, an additional question: Can we somehow have a "shell" where we enter qemu commands (in contrast to echoing them into a pipe at the normal command line)?
    – Binarus
    Commented Sep 15, 2020 at 8:40
  • 2
    @Binarus The first example with socat above gives you the interactive shell.
    – Boann
    Commented Sep 15, 2020 at 13:13
  • 2
    Got “Inappropriate ioctl for device” following these examples? You might have run into a socat bug. Try swapping its arguments. bugs.launchpad.net/ubuntu/+source/socat/+bug/1883957
    – Garth Kidd
    Commented Aug 27, 2021 at 6:16
  • 1
    re: security: in addition to having permissions, it also seems to be a lot harder to accidentally expose a unix socket to the internet than a port bound to localhost (@Binarus) this is also a question where more than one answer deserves the "correct" tick, but apparently that's not possible and deemed very very bad
    – chichak
    Commented Jul 14, 2023 at 20:34
21

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

1
  • This method is insecure, and actually, the QEMU website discourages it for production environments (just saw it yesterday, but don't have the link any more). Instead, the QEMU folks recommend to talk to QEMU via unix domain sockets (as in @boann's answer) or to authenticate and encrypt network connections via TLS.
    – Binarus
    Commented Aug 3, 2023 at 5:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.