Windows 7下QEMU实例脚本化关机与USB设备添加方案咨询
Since you can't use libvirt and need a scriptable way to control your headless QEMU instance (named MyMachineName), let's break down how to handle both shutdown and USB device addition using QEMU Monitor.
First: Ensure Your QEMU Instance Has a Monitor Port Enabled
Headless QEMU instances can't use the default stdio monitor, so you need to configure a TCP monitor when launching the VM. Add this parameter to your QEMU startup command:
-monitor tcp:127.0.0.1:5555,server,nowait
tcp:127.0.0.1:5555: Binds the monitor to localhost on port 5555 (you can pick any unused port).server: Makes QEMU act as the server waiting for incoming connections.nowait: Lets the VM start immediately without waiting for a monitor connection.
Optional: Add Password Protection
To secure the monitor, add a password parameter:
-monitor tcp:127.0.0.1:5555,server,nowait,password=YourSecurePassword
We'll include how to handle this in the scripts below.
1. Scripting VM Shutdown
You can use PowerShell (built into Windows 7) or a batch script with telnet to send the system_powerdown command, which triggers an ACPI shutdown (like pressing the physical power button on a PC).
PowerShell Script (Recommended)
Save this as shutdown_qemu.ps1:
# Connect to QEMU Monitor $tcpClient = New-Object System.Net.Sockets.TcpClient $tcpClient.Connect("127.0.0.1", 5555) $stream = $tcpClient.GetStream() $writer = New-Object System.IO.StreamWriter($stream) # Uncomment below if you set a monitor password # $writer.WriteLine("password YourSecurePassword") # Send shutdown command $writer.WriteLine("system_powerdown") $writer.Flush() # Optional: Verify response $reader = New-Object System.IO.StreamReader($stream) $response = $reader.ReadLine() Write-Host "Monitor Response: $response" # Clean up connections $writer.Close() $reader.Close() $stream.Close() $tcpClient.Close()
Run it via PowerShell:
.\shutdown_qemu.ps1
Batch Script (Using Telnet)
First, enable Telnet Client in Windows 7:
- Go to Control Panel → Programs and Features → Turn Windows features on or off
- Check "Telnet Client" and click OK
Save this as shutdown_qemu.bat:
@echo off :: Uncomment below if you use a password :: echo password YourSecurePassword > temp_cmd.txt :: echo system_powerdown >> temp_cmd.txt :: type temp_cmd.txt | telnet 127.0.0.1 5555 :: del temp_cmd.txt :: Without password echo system_powerdown | telnet 127.0.0.1 5555 timeout 1 >nul
2. Scripting USB Device Addition
First, get your USB device's Vendor ID (VID) and Product ID (PID):
- Open Device Manager → Find your USB device → Right-click → Properties → Details tab
- Select "Hardware Ids" from the dropdown
- You'll see something like
USB\VID_1234&PID_5678— note down1234(VID) and5678(PID)
PowerShell Script to Add USB Device
Save this as add_usb_qemu.ps1:
# Connect to QEMU Monitor $tcpClient = New-Object System.Net.Sockets.TcpClient $tcpClient.Connect("127.0.0.1", 5555) $stream = $tcpClient.GetStream() $writer = New-Object System.IO.StreamWriter($stream) # Uncomment below if you set a monitor password # $writer.WriteLine("password YourSecurePassword") # Replace with your device's VID and PID $writer.WriteLine("usb_add host:1234:5678") $writer.Flush() # Optional: Verify response $reader = New-Object System.IO.StreamReader($stream) $response = $reader.ReadLine() Write-Host "Monitor Response: $response" # Clean up connections $writer.Close() $reader.Close() $stream.Close() $tcpClient.Close()
Run it via PowerShell:
.\add_usb_qemu.ps1
Notes for USB Devices
- If your device is USB 3.0, make sure your QEMU VM has an xHCI controller added at startup:
-device qemu-xhci,id=xhci - If QEMU can't grab the device, you may need to temporarily uninstall the device from Windows Device Manager (QEMU will take control once added).
内容的提问来源于stack exchange,提问作者Cutton Eye

