bhyve Making /sbin/shutdown actually wait for a full shutdown of running VMs

When I close down the system by executing /sbin/shutdown, it tries to perform a graceful shutdown of all running virtual machines. However I am not sure how the process works, at the moment I assume it just sends ACPI shutdown signal to them once /sbin/shutdown has been executed (so the same as running # vm stop <vm-name>). But as far as I'm concerned it does not wait for the VM shutdown process to fully finish, as when I boot the system again I may see my VM being in a LOCKED state, I presume it happens because the VM was not able to properly shutdown in time before a full system shutdown, and the vm command didn't properly finish its execution and didn't clear the run.lock file.

I have a virtual machine starting on boot, so while I could just manually run # vm stop <vm-name> before a full system-wide shutdown, I'd like to actually properly automate the process, since the functionality of automatic VM shutdown is already present, but in my opinion it is not handled as gracefully as needed. Ungraceful shutdowns can (and if they can - they will) lead to virtual disks' corruption, which already happened to me once.
 
I'd like to actually properly automate the process
Apart from cron, which can only handle things at the start and while the system runs, and other ways,
the first choice for automating things under unix[like] were shell scripts.
For example you could do two things:
1. Add to the end of /etc/rc.shutdown what needs to be done, when the machine shuts down:
sh:
[...]
# Insert other shutdown procedures here

exit 0
2. You can write a script myshutdown.sh
sh:
#!/bin/sh

# ...
# all the things need to be done first, before
# ...

shutdown -h now
exit 0
Then execute this instead of shutdown.

And there are other ways, too.
 
I can just add a basic sleep here. But is this really the way?
I don't know.
By your OP I understood you need to do some things first, before you shutdown your machine, to bring some things (VMs) into another order/state than it happens when you use the default shutdown. I don't know what it is, what all needs to be done exactly, and I also don't know if that's done with just a sleep only.
I just wanted to answer your question about how to automate things at shutdown in general.

Anyway, sometimes "sleep" is sufficient enough for some things. But it's seldom a really clean job, because there is no control. Plus you need to know how long to sleep. And when you change something then you have to adjust the value for sleep too. Otherwise it sleeps too long (annoying) or too short (crap happen again.)

So the first, most simple idea jumps to mind to get to a more sophisticated way, would be to just place all the commands you type anyway manually in the shell before you execute the shutdown into a simple script, and execute it first. This was just a first step which later can be improved to get more control and become more general.

Another way was to look for some tool or option that handles your VMs, which may do it more elegant. Depending on the VMs you're running there are such, because you are not the only one running a bunch of those, and don't fancy it to handle them all manually everytime you shutdown. :cool:

Or, the other way around, since you complain your VMs are in the "wrong state" when you reboot your machine, may not bother what to do when you shutdown, but to do things (automated [cron/script]) at the start to bring them in the state you want them to be.
 
No problems here.
Code:
# vm list
NAME               DATASTORE  LOADER     CPU  MEMORY  VNC           AUTO      STATE
case               default    bhyveload  4    4096M   -             Yes [4]   Running (3962)
jenkins            default    bhyveload  4    4096M   -             Yes [6]   Running (9832)
kdc                default    uefi       2    2048M   0.0.0.0:5900  Yes [1]   Running (3195)
lady3jane          default    bhyveload  4    16384M  -             Yes [3]   Running (3666)
errol              stor10k    bhyveload  2    4096M   -             Yes [8]   Running (5186)
fbsd-test          stor10k    bhyveload  2    4096M   -             Yes [12]  Running (6038)
fbsd-test-pkgbase  stor10k    bhyveload  4    4096M   -             No        Stopped
gl-runner-1        stor10k    bhyveload  4    4096M   -             Yes [11]  Running (5991)
haos               stor10k    uefi       4    4096M   -             No        Stopped
kibana             stor10k    bhyveload  4    8192M   -             No        Stopped
riviera            stor10k    bhyveload  2    4096M   -             Yes [10]  Running (5696)
sdgame01           stor10k    uefi       4    8192M   0.0.0.0:5901  Yes [7]   Running (4723)
tessierashpool     stor10k    bhyveload  2    4096M   -             Yes [5]   Running (4417)
wintermute         stor10k    bhyveload  4    4096M   -             Yes [9]   Running (5649)
Everything stops correctly before shutting down. But if you really want to make sure, you could do; vm stopall && shutdown -r now
 
Everything stops correctly before shutting down.
Then why sometimes I see my VM being in a "locked" state (as opposed to a "stopped" state) when I boot my system after performing shutdown with a virtual machine running? There's no realy a pattern. Sometimes I do, sometimes I don't.
 
What's running in the VM? Maybe it's taking too long to shutdown, then it'll get killed fairly hard after a certain amount of time.
 
Back
Top