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:
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
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
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.
You have to tell systemd that there’s a new service with:
and that you want want your service to start at boot time with:
systemctl enable unifi-vm
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.