How to pass commands noninteractively to running QEMU from the guest. QMP via telnet/SSH?

1

I'm running a virtualized Win8 (gaming) platform under Linux host. I use Synergy for keyboard and mouse sharing but I need to pass the usb devices occasionally. GPU is passed to the guest and I'm unable to access the QEMU monitor from the guest.

Issue is that I cannot release said devices without shutting down the guest. I'd like to pass usb_add host:bus.addr and usb_del bus.addr commands on the fly so I can attach and detach the devices as I need them. Beauty would be that I can keep the Synergy running all the time and only lock the devices to guest when I need them. I should add that Synergy's build-in locking via lockCursorToScreen(toggle) doesn't work reliably enough.

I'm not overly familiar with QMP but I can set up a telnet server to host with -qmp tcp:192.168.0.10:4444,server,nowait command line option. I don't know how to pass three commands via telnet and quit under windows.

Another option could be to set up a socket for the QEMU monitor with -qmp unix:/path/to/socket,server. Then I suppose I'd SSH to the host to interact with the monitor. I'm not sure how to automate that either. Run a shellscript on host from SSH command line?

SSH is more secure but I trust machines on this private network so that's not a huge issue.

TL;DR: I need a way to pass commands to running QEMU from guest. Telnet and socket (accessed possibly via SSH) are possible but don't know how to automate. Other sollutions welcome.

WhimsicalWombat

Posted 2015-06-21T09:27:02.363

Reputation: 181

Answers

1

I solved the problem and post the answer here for posterity if someone else has the same issue. My host is linux and guest windows.

QEMU is launched with -qmp tcp:192.168.0.10:4444,server,nowait to enable telnet access to monitor. To bind the devices you want at lauch, add -device usb-host,productid=12850,id="FancyKeyboard" -device usb-host,productid=64,id="FancyMouse" to the commandline. Note that -device,productid= wants decimal id so you have to convert the hex you get from lsusb or similar to decimal first. the ,id= part is to enable easy unbinding and can be any unique string.

To bind them at runtime from the host, I run a script I named _kvm_bind with

echo '{ "execute": "qmp_capabilities" }'
echo '{ "execute": "device_add", "arguments": { "driver": "usb-host","productid": "12850","id": "FancyKeyboard" }}'
echo '{ "execute": "device_add", "arguments": { "driver": "usb-host","productid": "64","id": "FancyMouse" }}'

And pipe the output to the monitor with nmap's netcat: _kvm_bind|ncat 192.168.0.10 4444 I'm running it from another script. ncat is smart and exits the telnet session automagically after EOF.

Binding from guest is similar and I installed nmap's commandline packet also to the guest. File _kvm_bind.bat looks as follows:

@echo off
echo { "execute": "qmp_capabilities" } & echo.
choice /T 1 /D y > nul
echo { "execute": "device_add", "arguments": { "driver": "usb-host","productid": "12850","id": "FancyKeyboard" }} & echo.
choice /T 1 /D y > nul
echo { "execute": "device_add", "arguments": { "driver": "usb-host","productid": "64","id": "FancyMouse" }} & echo.

choice is there to facilitate a second delay between the commands just in case. I'm unsure if & echo. is actually necessary, I didn't try that without it. It just outputs a blank lne ibetween the commands. It's likely not needed.

Piping is similar to linux version:

@echo off
_kvm_bind.bat|ncat 192.168.0.10 4444

Unbinding is similar to the binding, just exchange the second and third line to device_del and argument with the id you supplied when you bound it earlier:

echo '{ "execute": "qmp_capabilities" }'
echo '{ "execute": "device_del", "arguments": { "id":"FancyMouse" }}'
echo '{ "execute": "device_del", "arguments": { "id":"FancyKeyboard" }}'

Like I said in the question, I run Synergy to share the mouse and keyboard between guest and host and both of them have a monitor of their own. I don't need to shut down Synergy on binding and it works fine after unbinding. All in all, I'm happy with this for now.

WhimsicalWombat

Posted 2015-06-21T09:27:02.363

Reputation: 181