Using the Mac OS X automounter

A quick tip on how to use the Mac OS X automount command to automatically mount a remote filesystem. You will need to be comfortable with the OS X command-line for this but once you know how it is fairly simple.

Why automount may be useful

I have a number of remote filesystems that are stored on Network Attached Storage (NAS) devices connected to my local network wireless router. For example, I use a share on a NAS device to host all of my media for use with the Plex Media Center. Anytime I run Plex on a computer I need to mount that share in order to be able to play the media. Likewise I have a number of cron scripts that run nightly to sync local filesystems on my Mac with a share on the NAS device.

(As an aside I should mention that remote syncing is not intended as a replacement for backups or for a good distributed version control system for your Xcode projects).

Now it would be a pain if I had to manually mount those remote filesystem shares everytime I wanted to run Plex or some other application or script. One possible option is to have them permanently mounted by adding them as login items to my OS X account (mount the share and then drag the disk icon from your desktop into the Login Items tab of your account settings in the System Preferences application). However these are filesystems that I do not generally want to see on the desktop and which sometimes are not even reachable.

A better approach in this case is to use the automount service that is part of Mac OS X. A few very simple configuration steps will allow the automount service to automatically manage access to a remote filesystem mounting it only when it is accessed and unmounting it later when it is no longer being used.

Create a local mount point

To get started you need to create a local directory that will be managed by the automount service. All remote filesystems will appear under this directory which acts as a trigger to the automount service. I keep these remote filesystems under the directory /mnt/Resources. The Resources directory is created and managed by the automount service but you will need to create the /mnt directory if it does not exist. You will need to use sudo to create the directory:

$ sudo mkdir /mnt

17-Nov-2013 Update for OS X Mavericks (10.9)

The OS X Mavericks upgrade seems to have introduced some new behaviour to the automounter that caused the original mountpoint /Volumes/Resources I was using to be deleted. Following a suggestion in the comments I have moved the mount point from /Volumes which is used by OS X to /mnt.

Modify the master map

The automount process reads the file /etc/auto_master to retrieve a list of directories that it should control. Since we are going to use our mountpoint (/mnt/Resources) to manage a number of remote maps we will place these maps in a separate map file (/etc/auto_resources). This is known as an indirect map as opposed to direct maps which are stored directly in the auto_master file. The one line modification to add /mnt/Resources to the auto_master file is as follows:

## Automounter master map
/mnt/Resources          auto_resources
+auto_master            # Use directory service
/net                    -hosts    -nobrowse,hidefromfinder,nosuid
/home                   auto_home -nobrowse,hidefromfinder
/Network/Servers        -fstab
/-                      -static

So any time an attempt is made to access a directory under /mnt/Resources the automount process will check the auto_resources map file to determine if it needs to mount a remote filesystem.

Creating the resources map

Let’s assume we have a remote filesystem “/media” on a NAS device named “nas001”. Also we will assume the filesystem is formatted as a Microsoft Windows (SMB) filesystem as it is also accessed by Windows devices (true in my case). To automatically mount that filesystem anytime we attempt to access it under /mnt/Resources/media we need to add a map to the /etc/auto_resources file as follows:

media   -fstype=smbfs ://nas001/media


Activate the changes

Any time you modify the map files you need to inform the automount service in order for them to become active:

$ sudo automount -vc

Optional configuration changes

A number of options can be changed in the /etc/autofs.conf configuration file. The only option that you are likely to ever want to change is the timeout period. This is time that the automount service will wait before unmounting a filesystem that has not been accessed. This is set to 1 hour (3600 seconds) in the /etc/autofs.conf that ships with OS X:


Finder integration

It should be noted that filesystems that are automounted in this way will not show up automatically in the sidebar of the OS X Finder application. Nor will they automatically appear as disk icons on the desktop. If that is important you may want to simply add the share as a login item and have it mounted whenever you login.

For filesystems that are accessed by scripts or applications I prefer having them automatically mounted and unmounted in the background on demand. You can of course always go to the directory in the Finder using the Go > Go To Folder… command and entering /mnt/Resources.

Never miss a post!

iOS Size Classes Cheat Sheet

Subscribe and get my free iOS Size Classes Cheat Sheet

Success! Now check your email to confirm your subscription and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time.
No time to watch WWDC videos?

Sign up to get my iOS posts direct to your inbox and I will send you a free PDF of my iOS Size Classes Cheat Sheet.

OK! Check your inbox (or spam folder) for an email to confirm your details and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time.
Archives Categories