Automatically starting and stopping your VirtualBox VM

Automatically starting and stopping your VirtualBox VM

I’ve been having a lot of trouble recently with my wifi. As a technologist, there’s nothing more embarrassing than having your friends come over and laugh at you when your wifi is broken — or worse, unreliable. There’s a much longer story where I upgrade my wifi to something awesome (namely, the UniFi access points from Ubiquiti)… but this part of the story is particularly about how I fought and won a tiny battle with VirtualBox. I’m detailing it here so that others can find it and benefit.

So let’s fast forward past the part where I install the UniFi Controller (which is an excellent piece of technology), and find out that my 32-bit Debian home router doesn’t let me enable a critical feature. Let’s fast forward past the part where I decide that I should try installing a 64-bit Debian guest virtual machine in a virtual container inside my 32-bit Debian host system. Let’s even fast forward past the part where I get VirtualBox set up mostly properly (replete with all kinds of traps, pitfalls that I either narrowly avoid, or spend hours wallowing in). I get it installed and I get it running and life is good.

But there’s a hitch. Now I’m running a machine-within-a-machine and the inner machine is in charge of my home wifi network. So it’s got to be reliable, otherwise refer above to: friends laughing at me. But sometimes the host machine has to be rebooted and when that happens, I need the guest VM to shut down cleanly and start up again cleanly. That should be simple, right? You’d think that VirtualBox would have this nailed down — but it turns out that they don’t.

After much trawling around the internet, the best solution appears to be to set your guest machine to AutoStart mode and use a service called vboxautostart-service to automatically start and stop the service. But unfortunately, the script that comes with the Debian VirtualBox package is not really designed well. It’ll start the service — if you follow the direction — but it won’t cleanly stop it. So if my host machine reboots, then the guest vm shuts down uncleanly.. and then the guest machine might not restart cleanly when I reboot. It’s also grossly over-engineered for my needs and thoroughly offends my engineering sensibilities.

After much Googling of the deep web and learning of the many clever things, I found that there’s a very simple way to do this on a per VM basis. I’m sharing that here in the hopes that it helps others.

It boils down to this:

vboxautostart-service, get thee gone from my sight

If it’s already configured as a service on your system, you can either leave it alone, or you can tell the system to ignore it with:
systemctl disable vboxautostart-service

Then, create your shiny new service

Let’s say that your guest VM is called unifi and you want to create a new service that starts your VM in headless mode.

In /etc/systemd/system create a file called unify-vm.service and put this in it:

Description=Unifi VM service vboxdrv.service
ExecStart=/usr/bin/vboxheadless -s unifi
ExecStop=/usr/bin/vboxmanage controlvm unifi acpipowerbutton

(I bold-faced the parts of the config that are specific to my UniFi setup, you’ll probably want to change those bits). This basically tells systemd how to start and stop a headless VM.

Enable your service

You have to tell systemd that there’s a new service with:
systemctl daemon-reload

and that you want want your service to start at boot time with:
systemctl enable unifi-vm

Important! Change ownership of script to startup user from script:
chown runasthisuser:runasthisuser unify-vm.service
(optionaly chmod +x)

That’s it.

Your guest VM should shut down cleanly when your host shuts down. And when your host reboots, your guest should boot up with it. It’s a surprisingly easy process, and it should be fairly robust across upgrades. Share and enjoy.


Note: Stop running vm:

VBoxManage controlvm nameofvm poweroff
Automatically starting and stopping your VirtualBox VM