QEMU guest agent run command in Windows Guest issue

mercury131

Active Member
Aug 7, 2018
24
5
43
35
Hello everyone!

I got Windows Server 2016 guest OS with installed qemu guest agent.

Screenshots here:
https://gyazo.com/6c9c4f6030d3a7843552f072e34f29fc
https://gyazo.com/b08ea8140edfb3f1b5cf3626e224c4c4
I try to run some commands inside guest os via QEMU guest agent.

For example, if I connct to agent via socat
socat /var/run/qemu-server/109.qga -

and then i send command like this :
{"execute":"guest-exec", "arguments":{"path":"c:\windows\notepad.exe"}}

I see notepad.exe process in Windows, its good
https://gyazo.com/387d3b60194e826eb1fa27c0967f0eab

but if I try to reboot Windows via this command I got nothing
{"execute":"guest-exec", "arguments":{"path":"C:/Windows/System32/cmd.exe","arg":["/C","shutdown", "-r", "-f", "-t 00"]}}

Windows don't reboot, but I see pid in output
https://gyazo.com/bbd815059dfb7704a6d5a1e033deacd2

What I do Wrong?
 
Hello everyone!

I got Windows Server 2016 guest OS with installed qemu guest agent.

Screenshots here:
https://gyazo.com/6c9c4f6030d3a7843552f072e34f29fc
https://gyazo.com/b08ea8140edfb3f1b5cf3626e224c4c4
I try to run some commands inside guest os via QEMU guest agent.

For example, if I connct to agent via socat
socat /var/run/qemu-server/109.qga -

and then i send command like this :
{"execute":"guest-exec", "arguments":{"path":"c:\windows\notepad.exe"}}

I see notepad.exe process in Windows, its good
https://gyazo.com/387d3b60194e826eb1fa27c0967f0eab

but if I try to reboot Windows via this command I got nothing
{"execute":"guest-exec", "arguments":{"path":"C:/Windows/System32/cmd.exe","arg":["/C","shutdown", "-r", "-f", "-t 00"]}}

Windows don't reboot, but I see pid in output
https://gyazo.com/bbd815059dfb7704a6d5a1e033deacd2

What I do Wrong?

After long time I found answer by myself =)

I just install latest virtio serial driver and update qemu guest agent to latest version.

after that I enable qemu agent on VM in PVE gui.

Then, via ssh from PVE host I can execute commands inside Windows VM

Examples here:

VM ID - 101

Connect to Virtio serial via socat
Code:
socat /var/run/qemu-server/101.qga -

create new text file:
Code:
{"execute":"guest-exec", "arguments":{"path":"cmd.exe","arg":["/c","echo 123 > c:\\123.txt"]}}

reboot guest:
Code:
{"execute":"guest-exec", "arguments":{"path":"cmd.exe","arg":["/c","shutdown", "-r", "-f"]}}
or
Code:
{"execute":"guest-exec", "arguments":{"path":"shutdown.exe","arg":["-r", "-f"]}}
or powershell example
Code:
{"execute":"guest-exec", "arguments":{"path":"powershell.exe","arg":["-command","restart-computer", "-force"]}}

run sysprep on windows:
Code:
{"execute":"guest-exec", "arguments":{"path":"c:\\windows\\system32\\sysprep\\sysprep.exe","arg":["/oobe", "/generalize", "/reboot"]}}

or in one command for example:
Code:
echo '{"execute":"guest-exec", "arguments":{"path":"c:\\windows\\system32\\sysprep\\sysprep.exe","arg":["/oobe", "/generalize", "/reboot"]}}' | socat /var/run/qemu-server/101.qga -

I hope this will help somebody =)
 
Last edited:
After long time I found answer by myself =)

I just install latest virtio serial driver and update qemu guest agent to latest version.

after that I enable qemu agent on VM in PVE gui.

Then, via ssh from PVE host I can execute commands inside Windows VM

Examples here:

VM ID - 101

Connect to Virtio serial via socat
Code:
socat /var/run/qemu-server/101.qga -

create new text file:
Code:
{"execute":"guest-exec", "arguments":{"path":"cmd.exe","arg":["/c","echo 123 > c:\\123.txt"]}}

reboot guest:
Code:
{"execute":"guest-exec", "arguments":{"path":"cmd.exe","arg":["/c","shutdown", "-r", "-f"]}}
or
Code:
{"execute":"guest-exec", "arguments":{"path":"shutdown.exe","arg":["-r", "-f"]}}
or powershell example
Code:
{"execute":"guest-exec", "arguments":{"path":"powershell.exe","arg":["-command","restart-computer", "-force"]}}

run sysprep on windows:
Code:
{"execute":"guest-exec", "arguments":{"path":"c:\\windows\\system32\\sysprep\\sysprep.exe","arg":["/oobe", "/generalize", "/reboot"]}}

or in one command for example:
Code:
echo '{"execute":"guest-exec", "arguments":{"path":"c:\\windows\\system32\\sysprep\\sysprep.exe","arg":["/oobe", "/generalize", "/reboot"]}}' | socat /var/run/qemu-server/101.qga -

I hope this will help somebody =)

THIS WORKS.

Thanks you were right, it helped me.

However is there anyway to make it simpler?

like

qm agent exec ID "arguments":{"path":"shutdown.exe","arg":["-r", "-f"]}

or something, this would be great to do, tbh.
 
Code:
qm guest exec ID -- c:\\path\\to\\exe /with /arguments
should work