Orderly shutdown of Windows VMs? [SOLVED]

Vertigo 7

Explorer
Joined
May 8, 2021
Messages
78
Is there some config or something that I overlooked when setting up Windows VMs to get Truenas to properly shutdown or, even better, suspend the VMs when the host is being shut down or rebooted? All of my Windows VMs are using UEFI boot but none of them will automagically shutdown.

Can't seem to find much info on this.
 

beaster

Dabbler
Joined
May 17, 2021
Messages
27
I was looking for this as well

The only thing I could find was this freenas agent called bhyve-vm-goagent, however I am not 100% sure this actually does graceful shutdown. I am going to open another thread to find out of this agent is still used and supported in Truenas

The TRUENAS documentation talks about ACPI based signally, and this does seem to work however it's not specifically useful in cases where the VM is busy and does not respond in the time required.
 

Patrick M. Hausen

Hall of Famer
Joined
Nov 25, 2013
Messages
7,776
Use something similar to this as a shutdown task:
Code:
root@freenas[~]# cat /mnt/hdd/scripts/shutdown-bhyve.sh
#! /bin/sh

PAUSE="10"

test -t 1 && echo "Sending SIGTERM to bhyve processes."
/usr/bin/killall -TERM bhyve

test -t 1 && echo -n "Waiting for processes to exit ... "
while /usr/bin/killall -0 bhyve >/dev/null 2>&1
do
    sleep "${PAUSE}"
done
test -t 1 && echo "done."

exit 0
 

Vertigo 7

Explorer
Joined
May 8, 2021
Messages
78
hmm does that just kill the vm process or send shutdown commands to the guest OSs?
 

Patrick M. Hausen

Hall of Famer
Joined
Nov 25, 2013
Messages
7,776
That does send ACPI shutdown signals to the guest OSes, then waits until all of the VMs are powered off, before proceeding with the shutdown of the NAS itself. See my setup for example. If some VMs are still running after the timeout of 5 minutes, they are killed. Adjust as necessary.

1623693885785.png

Sorry, I thought it was obvious that this attempts to perform an orderly shutdown. Hey, there are diagnostic messages in the script! :wink:
The test -t checks if you are running it interactively and gives you some progress info. If you are running it as a task, it won't print anything.
 

Vertigo 7

Explorer
Joined
May 8, 2021
Messages
78
lol well, not so obvious to me =p I'm not familiar with those commands so not clear on what they do but I do appreciate your suggestion! I'll give er a go this afternoon!
 

Vertigo 7

Explorer
Joined
May 8, 2021
Messages
78
Works like a charm. ty so much!

Curious why this isn't default behavior for bhyve.
 

Vertigo 7

Explorer
Joined
May 8, 2021
Messages
78
Update - dunno if this is a bug in 12.0-U4 or not but "/usr/bin/killall -TERM bhyve" seems to only effect 2 VMs at a time. I tweaked the script to reissue the command after a few seconds (my VMs shut down pretty fast).

Code:
#! /bin/sh

PAUSE="15"

while pgrep -x "bhyve" >/dev/null
do
echo "Shutting down Virtual Machines..."
/usr/bin/killall -TERM bhyve
sleep "${PAUSE}"
echo "checking VM state..."
done
echo "done..."
exit 0


after much testing this is what I came up with and it seems to have done the trick and I'm getting orderly shutdowns 100% of the time on reboot or shutdown of TrueNAS.
 

centauri

Cadet
Joined
Dec 14, 2021
Messages
6
So this is also needed for scale right. The VM's are not properly shutdown.

And then something like this seems to work.

Code:
#!/bin/bash
#
# Shutdown all VM's
#
/usr/bin/clear
printf %s "VM's running:
$(virsh -c "qemu+unix:///system?socket=/run/truenas_libvirt/libvirt-sock" list | tail -n +3 | awk '{print $2}')
"

echo "Shutdown all libvirt/QEMU/KVM guests"
for i in $(virsh -c "qemu+unix:///system?socket=/run/truenas_libvirt/libvirt-sock" list | tail -n +3 | awk '{print $2}')
  do
  echo "Shutting down VM $i..."
  virsh -c "qemu+unix:///system?socket=/run/truenas_libvirt/libvirt-sock" shutdown $i
  done

exit 0
 
Top