Catalina has broken auto start/stop scripts with launchd?

Discussions related to using VirtualBox on Mac OS X hosts.
Post Reply
mynick
Posts: 20
Joined: 8. Feb 2014, 11:42

Catalina has broken auto start/stop scripts with launchd?

Post by mynick »

Hi all,

I am trying to automate startup/shutdown with VirtualBox 6.0.14 on Catalina host system. Let me clarify first:
  • I know how launchd works (keep script running and trap SIGTERM to invoke stop-service code).
  • I'm aware of both the "standard"

    Code: Select all

     /Applications/VirtualBox.app/Contents/MacOS/org.virtualbox.vboxautostart.plist
    which uses the "standard" VBoxAutostartDarwin.sh script distributed by VirtualBox itself, and of course know how to configure it (e.g. as per https://gist.github.com/str8edgedave/b0 ... 778079ee7b).
  • I'm also familiar with custom scripts such as https://github.com/freedev/macosx-vbox- ... er/vbox.sh which use VBoxHeadless and trap SIGTERM to run "VBoxManage controlvm VM savestate" (or acpipowerbutton) when launchd signals shutdown.
However, none of the above is working for me with Catalina. Can anyone confirm whether org.virtualbox.vboxautostart.plist still works properly for them on Catalina, or if they have come up with a custom script they can share to use with launchd?

As it stands, both approaches fail to cleanly shut down for me on Catalina. They run fine on startup. They also perform a clean shutdown of guest VMs if I manually unload the service using launchctl, like this:

Code: Select all

launchctl unload /Library/LaunchDaemons/org.virtualbox.vboxautostart.plist # standard VBoxAutostartDarwin.sh
launchctl unload /Library/LaunchDaemons/home.vbox.vmsvc.plist # custom script written by me
However, when you actually shut down or restart Catalina, the "stop" part of the script fails. Looking at the logs it seems as if Catalina has changed something in the shutdown process (maybe stops some service earlier to speed up shutdown?) which causes VirtualBox to become unresponsive. I added logging to my custom script and found that running "VBoxManage controlvm <vm> savestate"' fails and just prints "Killed: 9"

Any help on this will be greatly appreciated.
socratis
Site Moderator
Posts: 27329
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Catalina has broken auto start/stop scripts with launchd?

Post by socratis »

User 'granada29' has put together a script for automating the creation of a Launch Agent and I helped with the testing part, but not on 10.15. See if that might help you: https://www.whatroute.net/installerapp2iso.html#login
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
mynick
Posts: 20
Joined: 8. Feb 2014, 11:42

Re: Catalina has broken auto start/stop scripts with launchd?

Post by mynick »

Hi socratis,

The solution you point to is using a LaunchAgent, not a LaunchDaemon. Agents (as opposed to daemons) are tied to graphical login/logout and run in that specific user's context. I really need a launch daemon so that the VMs can run even without any user logged in (and also to run for with a user that is not the same as the user that is logged in). So while I expect the solution you pointed to still works in Catalina (because all launch daemons should start shutting down AFTER the agents are done) it will not help in my case.

Does anyone know how VBoxManage interacts with running VMs? What does "VBoxManage controlvm SOME_VM acpipowerbutton" really do? What sort of IPC is used in Mac OS to signal the guest VM to stop running? Knowing implementation details might help with diagnosing what has changed in Mac OS that causes this to no longer work (or find an alternative).

Also, is there some signal you could send to a running VBoxHeadless VM to signal it to "stop" cleanly? I tried TERM and HUP but both seem to just kill the client VM...
stoli412
Posts: 1
Joined: 8. Jul 2020, 08:27

Re: Catalina has broken auto start/stop scripts with launchd?

Post by stoli412 »

mynick wrote: I am trying to automate startup/shutdown with VirtualBox 6.0.14 on Catalina host system.
HI mynick, Did you ever find a solution to this issue? I'm facing the same problem where the launchd daemon starts VMs correctly, but I can't get it to exit cleanly. Launchd runs the VBoxAutostartDarwin.sh script at boot, which starts VMAutostart and VMs start correctly, but then it looks like the daemon closes and isn't listening to catch any signals at shutdown/reboot.

I'd love to know how to get this working as intended on Catalina.
mynick
Posts: 20
Joined: 8. Feb 2014, 11:42

Re: Catalina has broken auto start/stop scripts with launchd?

Post by mynick »

It is impossible to get this working cleanly in a simple way.

I did manage to get it to start/stop cleanly with a VERY custom script that uses password-less ssh to control the guest VM from the host.

The way it works is like this: I added one extra virtual NIC to my guest VMs with host-only communication. In the guest VMs I created a "vmcontrol" user and generated an SSH key-pair that the "vmcontrol" user trusts. I created a script that I added as a LaunchAgent at startup which would work as follows: it would ssh from the host VM into the guest and leave an ssh session running constantly. I was redirecting input/output from a nameless pipe. The script would trap the signal signal and upon receiving it I would echo (via the unamed pipe) the "shutdown" command over the SSH session to the guest which would start shutting down. The host script would wait a few seconds for the machine to shut down by looping and checking with VBoxManage.

It was a hack, it was ugly, but it was the only way I could get this to work and it made me dislike MacOS even more.

I've since switched to mini-PCs with Linux to run my VMs. Most recently got a Ryzen-based one which is amazing!

Mac OS is really not worth the trouble. I now only use it for development of native MacOS applications when I absolutely have to. Fortunately for most of my work I don't really need it...
Post Reply