zeroG wrote:launchd sends a SIGTERM to whatever process is spawned from the ProgramArguments spec, right?
I have yet to test this, but what I'll likely end up doing is trapping SIGTERM with a graceful shutdown of the VM. From the
launchd.plist manpage, a daemon should catch SIGTERM at least.
zeroG wrote:does it care if the script spawns additional processes or forks? should i be running vboxheadless in a subshell? does it matter?
This does matter. Launchd doesn't want you to fork. That said, it's OK if you spawn other processes
as long as your original process stays open. If, for instance, you were to call VBoxHeadless and run it in the background, your script would finish and launchd would restard it (if that's what it's designed to do). Here's a relevant portion of the manpage:
EXPECTATIONS
Daemons or agents managed by launchd are expected to behave certain ways.
A daemon or agent launched by launchd MUST NOT do the following in the process directly launched by launchd:
o Call daemon(3).
o Do the moral equivalent of daemon(3) by calling fork(2) and have the parent process exit(3) or _exit(2).
A daemon or agent launched by launchd SHOULD NOT do the following as a part of their startup initialization:
o Setup the user ID or group ID.
o Setup the working directory.
o chroot(2)
o setsid(2)
o Close "stray" file descriptors.
o Change stdio(3) to /dev/null.
o Setup resource limits with setrusage(2).
o Setup priority with setpriority(2).
o Ignore the SIGTERM signal.
A daemon or agent launched by launchd SHOULD:
o Launch on demand given criteria specified in the XML property list. More information can be found later in this man page.
o Catch the SIGTERM signal.
So how does this impact running VirtualBox? Here's a quick draft of a script to use:
- Code: Select all Expand viewCollapse view
#!/bin/bash
trap shutdown SIGTERM
function shutdown()
{
/usr/bin/VBoxManage controlvm ${VMNAME} savestate
exit 0
}
/usr/bin/VBoxHeadless --startvm ${VMNAME}
I haven't had time to test it but I'll probably do that this afternoon. I'll be sure to post my entire script, since I'm going to be doing much, much more. Our script will handle creation of VMs as well, so that we can push it out to all of our Macs and have them build Windows instead of having to push out a 30GB image. Instead, we'll push out a disk image that will boot them up and install Windows from a network share. So, our script will be smart enough to create a VM if it isn't there, create a hard drive if need be, and finally start the machine. It will stay open while VBoxHeadless runs, and stop the VM if it gets a SIGTERM.