Mounting a MacOS X Disk Image at login

Work says that we need to store sensitive data like email and customer files on some encrypted media. This is a good thing. My laptop has my home directory secured, but I don’t want to encrypt everything on my desktop. The solution to this is to create an encrypted Disk Image (using Disk Utility) and make Mail.app store my email there. This all works great!

The problem surfaced this week when I decided it would be good to shutdown my desktop to save energy (which I usually did, but I got sloppy). **What happened?

When Mac OS X shuts down, and there is still some application doing something with your opened Disk Image, it will not remove the mount-point, e.g. in my case /Volumes/FileVaultBlack. Later, if you open it again, Mac OS will create the directory /Volumes/FileVaultBlack 1 and use that as mount-point. The issue here is that I configured the AccountPath in Mail.app to a location in /Volumes/FileVaultBlock.. In mortals speech: “Email is broken”.

The solution is to mount the Disk Image when logging in using a predefined mounting point. This involves making a directory and creating an AppleScript application. So lets get busy!

First, eject the Disk Image** using the Finder application. It will complain when applications still need it, so you have to make sure that nothing is accessing it.

Create the mountpoint. The idea is to create a directory in /Volumes/ (it doesn’t really matter where you do it, but I like to keep them in one place). I suggest opening Terminal for this and do the following:

$ mkdir /Volumes/FileVaultBlack

You want to name it differently. I’m using the name of the Disk Image, just like MacOS would do when you double click the .dmg-file.

Open AppleScript Editor* and save the following script into a file called, for example, FileVaultBlack_mountatlogin.

do shell script "hdiutil attach /Users/geert/FileVaultBlack.dmg -mountpoint /Volumes/FileVaultBlack"

The above script will execute the hdiutil command-line utility telling it to mount the given Disk Image (.dmg-file) on the give mount-point (the one we created earlier).

Test this script by pushing the Run-button in the AppleScript Editor and see whether your Disk Image is visible in Finder. If not, check the locations of both the Disk Image and the mount-point you gave in the script.

When you saved it, save it again as an application. Still within the AppleScript Editor do the following:

  1. Choose Save As from the File menu
  2. Save As: save it using slighly different name, for example MountFileVaultBlack
  3. File Format: Application
  4. You don’t have to check Run Only

We save it 2 times, once as a script, once as an application. This way, you can easily edit the script later, and save it again as an application. (I’m using different names because it’s less confusing when writing this post.)

Lets now add it to our Login Items:

  1. Open System Preferences and go to Accounts
  2. Make sure you choose My Account, i.e. your account, and go to Login Items
  3. Add an item to the list, clicking the +-sign
  4. Locate the application you created above, in our exampled named MountFileVaultBlack, and press Add.
  5. Check also the Hide option next to the new item.

That should be it. Log out and in and check if the Disk Image is mounting. There should be an icon happily jumping in the Dock while the mounting is on-going. Open Finder and see if your files are accessible.