KVM集群环境下Windows Server 2012R2虚拟机自动重启问题求助
Hey Cook, let's tackle this annoying auto-restart issue with your Windows Server 2012R2 VM on KVM—nothing's more frustrating than being locked out of modifying resources because the VM won't stay down. Here's a step-by-step breakdown to track down the root cause and fix it:
1. Check KVM/libvirt's Auto-Start Configuration
First, let's rule out the simplest culprit: libvirt's built-in auto-start setting. Even if you destroy the VM, this setting could trigger it to restart automatically (especially if the host or libvirtd service restarts, but sometimes it can act up immediately).
- Run this command to check if auto-start is enabled for your VM:
Look for thevirsh dominfo <YOUR_VM_NAME>Autostartline—if it saysyes, disable it with:virsh autostart --disable <YOUR_VM_NAME>
2. Inspect the VM's BIOS/UEFI Power Settings
Windows VMs running on KVM often inherit firmware-level power settings that can force an auto-restart after shutdown. Here's how to fix that:
- Boot up the VM and immediately press the BIOS entry key (usually
F2orDel—virt-manager will show a prompt like "Press F2 to enter setup" during boot). - Navigate to the Power Management section (the exact name varies by firmware, but it's usually under a "System" or "Advanced" tab).
- Look for options like Restore AC Power Loss, Power On After Power Failure, or Auto Power On. Set this to
OfforStay Off(not "Last State" or "Power On"). - Save changes and exit the BIOS, then shut down the VM normally to test if it stays off.
3. Verify Windows Server's Local Power & System Settings
Sometimes the issue is on the Windows side itself, not KVM. Let's check a few key settings:
- Power Button Behavior: Open Control Panel → Power Options → Change plan settings → Change advanced power settings. Expand Power buttons and lid, ensure "Press the power button" is set to Shut down (not Restart).
- Automatic Restart on System Failure: Right-click "This PC" → Properties → Advanced system settings → Startup and Recovery. Uncheck the box next to Automatically restart under "System failure".
- Group Policy Checks: Run
gpedit.msc(if using Windows Server with the Desktop Experience) and navigate to Computer Configuration → Administrative Templates → System → Shutdown Options. Ensure "Turn off automatic termination of applications that block or cancel shutdown" is set to Not Configured or Disabled (misconfigurations here can cause unexpected restarts).
4. Dig Into the VM's Libvirt Configuration File
Occasionally, the VM's XML config file has incorrect power action settings that force a restart instead of destroying the VM on shutdown. Let's inspect and fix that:
- Export the VM's config to a file:
virsh dumpxml <YOUR_VM_NAME> > vm-config.xml - Open
vm-config.xmlwith a text editor (likenanoorvim) and search for the<on_poweroff>element. It should look like this:
If it says<on_poweroff>destroy</on_poweroff><on_poweroff>restart</on_poweroff>, change it todestroy. - Save the file and redefine the VM with the updated config:
virsh define vm-config.xml - Restart the libvirtd service to apply changes:
sudo systemctl restart libvirtd
5. Check Host-Side Logs for Trigger Clues
If none of the above work, let's look at logs to see what's actually triggering the VM restart:
- Check the libvirtd service logs for any automatic start events:
Look for lines likejournalctl -u libvirtd.service --since "1 hour ago"Starting domain <YOUR_VM_NAME>—these should tell you why the VM is starting (e.g., a timer, a hook script, or a host event). - You can also check the VM's own logs (if you can get into it) for shutdown errors or restart triggers.
If you still hit a wall, share a snippet of the libvirtd logs and your VM's XML config (redact any sensitive info) and we can dig deeper!
内容的提问来源于stack exchange,提问作者Cook

