Using autofs to fix a number of issues with using AFP network shares in Mac OS X

My wife and I share a Macbook Air with Mac OS X Lion using fast user switching.  We both work with files stored on an AFP network share from our family Mac Mini using the MBA.  We were running into the following issues with this setup:

  • The network share ("family-network-share") from the Mac Mini would frequently get dropped and have to be remounted manually. Often the original mount point stealthily persisted in /Volumes (not shown in Finder, but shows up when you do an ls in Terminal) so that when you remounted the share it got the name family-network-share-1 (as shown with ls in Terminal), which broke any applications or scripts that used the share.
  • If one of us mounted the network share it would show up under Volumes (and on the desktop) for the other user, but it would be unaccessible to the second user (as indicated by a red circle badge on the mounted share in Finder) because permissions were restricted to the person who originally mounted the share.
  • When the second person mounted the share it would show up in Finder and the desktop as a second instance of the share with the same name, but in Terminal it got the name family-network-share-1 because the /Volumes mount point was already taken by the first user.  This made it impossible to have any scripts or applications use the network share because you couldn't be sure what its path would be for a particular user.
After a lot of googling around, I came up with the solution of using autofs to set up separate permanent mounts of the network share for each user in a location other than /Volumes.  What autofs in Mac OS X lets you do is set up virtual folders that automatically connect to the specified network share whenever an application tries to use the folder.  Because the connection is on demand it (so far) never drops.  Because I set up separate autofs mounts for each user the name of the share mount point is reliable and you can point applications and scripts to the share mount point with confidence.

The documentation for autofs can be found here.  It is a little obscure, so here is a quick and dirty tutorial.

Open up the existing system auto_master file using sudo:
sudo nano /etc/auto_master
Then add an entry at the end with the name of your (soon to be created) mount map file
/-        auto_my_mount_map_file
You can apparently name this mount map file anything you want, so don't feel you have to copy the arbitrary name used here.

Then create your mount map file in the /etc folder using sudo nano
sudo nano /etc/auto_my_mount_map_file
And in that file add a line for each network share you want to automount giving the mount point first, followed by the network share path and credentials, like so:
/Users/andy/family_share_mount_point -fstype=afp  afp://username:password@share_local_IP_address/family-network-share
This is all on one line; ignore how Blogger wraps it.

Then load the new mount map file by reloading autofs like so:
sudo automount -vc
The first thing you are going to do is look for the new mount in Finder, and when you do that you will think it didn't work because it won't show up in Finder.  However, it is working, as you can demonstrate by navigating to the new folder in Terminal:
cd /Users/andy/family-network-share 
After you do that, go back to Finder and voila, now it shows up.  My theory is that autofs doesn't bother to mount the network share until some program tries to use it.  To avoid the problem of the folder not being visible in Finder after a reboot I wrote a two line BASH script to cd to the share folder and then list its contents using ls, and then put that script in each user's login items under System Preferences - Users and Groups.

UPDATE 2016-01-13:

After coming back from a long trip and setting up our network again from scratch, I was having trouble getting this to work.  The specific issue was that one user, or both users, would be locked out of the mount point for the network share because the owner of the mount point was randomly (at least I couldn't detect a consistent pattern) being changed to root by the system. This problem appeared to go away after I took the following steps:
 - I put the mount points one more level down in the directory structure, i.e. instead of /Users/andy/my_mount_point I made it /Users/andy/Famly_shares/my_mount_point.
 - I did not create the mount point manually, I just put it in the auto_my_map_point file.
 - I very excluded the parent folders of both users' mount points from Spotlight (System Preferences -> Spotlight -> Privacy).  It can be confusing because privacy exclusions set by both users seem to show up in both user's Spotlight settings, and it shows only the final folder of the excluded path, but if you hover over the folder name for a few seconds a tooltip shows up with the full path.  My reason for trying this was that autofs creates a mount point when a user tries to access the mount point, so maybe Spotlight indexing activity was causing the share to be mounted as root and locking users out.
 - I reactivated my old login script that changed directory into the mount point (see above) even though under El Capitan the mount point showed up without it.  Once again, my theory was that the first user to access the mount point wins, so the login script to go into the folder hopefully prevents some root process from getting there first.

1 comment:

Peter Boni said...

Thanks Andy, this really helped me!