When I start a qemu (Windows Server 2003) virtual machine with
-qmp tcp:127.0.0.1:4444,server,nowait
I can make it shutdown with the following script
#!/bin/bash
telnet 127.0.0.1 4444 <<JSON
{ "execute": "qmp_capabilities" }
{ "execute": "system_powerdown" }
JSON
But there are two problems:
the script exits with $? = 1 and says "Connection closed by foreign host". Can I make it exit in a clean way?
The shutdown process happens asynchronously. So, the script returns at once, not waiting for the vm to finish the shutdown. How do I know when it's done (without grepping ps for qemu or something like this)?
The background is I want to host a Windows Server 2003 installation inside a VM on a linux host and I want to use an UPS. In case of a blackout, I want to shutdown the VM and then the host – and of course, the host should start the shutdown not until the VM is done.