As I mentioned yesterday, I have a couple of disk images in my Mac OS X machine that hold NetBSD's and pkgsrc's source code. I also have some virtual machines in Parallels that need to use these project's files.

In order to keep disk usage to the minimum, I share the project's disk images with the virtual machines by means of NFS. (See Mac OS X as an NFS Server for more details.) But in doing so, a problem appears: the NFS daemon is started as part of the system's boot process, long before I can manually mount the disk images. As a result, the NFS daemon — more specifically, mountd — cannot see the exported directories and assumes that their corresponding export entries are invalid. This effectively means that, after mounting the images, I have to manually send a HUP signal to mountd to refresh its export list.

A little research will tell you that it is trivial to mount disk volumes on login by dragging their icon to the Login items section of the Accounts preference panel:


But... that doesn't solve the problem. If you do that, the images will be mounted when you log in, and that happens long after the system has spawned the NFS daemons.

Ideally, one should be able to list the disk images in /etc/fstab, just as is done with any other file system, but that does not work (or I don't know the appropriate syntax). So how do you resolve the problem? Or better said, how did I resolve it (because I doubt it's the only solution)?

It turns out it was not trivial: you need to manually write a new startup script that mounts the images for you on system startup. In order to do that, start by creating the /Library/StartupItems/DiskImages directory; this will hold the startup script as well as the necessary meta-data to tell the system what to do with it.

Then create the real script within that directory and name it DiskImages:
#! /bin/sh

#
# DiskImages startup script
#

. /etc/rc.common

basePath="/Library/StartupItems/DiskImages"

StartService() {
hdiutil attach -nobrowse
/Users/jmmv/Projects/NetBSD.dmg
hdiutil attach -nobrowse
/Users/jmmv/Projects/pkgsrc.dmg
}

StopService() {
true
}

RestartService() {
true
}

RunService "$1"
Don't forget to grant the executable permission to that script with chmod +x DiskImages.

At last, create the StartupParameters.plist file, also in that directory, and put the following in it:
{
Description = "Automatic attachment of disk images";
OrderPreference = "First";
Uses = ("Disks");
}
And that's it! Reboot and those exported directories contained within images will be properly recognized by the NFS daemon.

I'm wondering if there is a better way to resolve the issue, but so far this seems to work. Now... mmm... a UI to create and manage this script could be sweet.