Thursday, October 30, 2014

Putting an iTunes Library on a NAS, Reliably

I have tried for years to get my iTunes library to reside, reliably, on my home NAS.  After all the NAS offers a ton of storage, resilience with RAID, and allows me to run lean on an SSD in my Mac Mini.  But for years I've run into issues where the network burps, or the NAS reboots, or coronal mass ejections cause my Mini to be disconnected from the NAS.  OK, not so much the last one, but when the Mini is disconnected from the NAS, I get the inevitable sequence of events.  First I get the little "Volume Disconnected" popup on my Mini.  Then something causes iTunes to realize that the network share with the library no longer exists.  Then iTunes silently creates a new iTunes library in my home folder, under Music.  Then I have two libraries to reconcile when I finally figure it all out -- usually when 95% of my library goes missing.

So naturally I think, "There's got to be a better way.  Microsoft figured out long ago in Windows how to automatically reconnect network shares if the NAS is disconnected from the server or workstation, why not Apple?"  Well, it turns out Apple has this figured out as well.  They just don't provide the user-friendly checkbox to enable the capability.

I found a couple of very useful resources on my latest search for an answer (thanks Google).  The first was this white paper that Apple released in 2009 that details how to use autofs.  For those that want to understand the full capability, it's the place to start.  For those that want the Cliff's Notes version, there's a great post on Superuser.com that explains how to get the solution up and running quickly.  I've read both, and I chose the indirect mapping method described in the post for setting up my auto-mounted share.  And so far, so good.  I purposely held off on a software upgrade on the NAS until after I made the auto-mount change to see how resilient the solution is, and it works great.  Be sure to tune down the AUTOMOUNT_TIMEOUT value in /etc/autofs.conf.  The default value is 3600 seconds.  I've tuned it down to 300 (5 minutes), and all seems well.

One side note: when you change from traditional mounting of the share via Finder to mounting via autofs, the mount point for the share changes as well.  While Finder will automatically mount the share under the /Volumes directory, autofs pretty much lets you mount anywhere.  Best practice (Greg practice?) is to mount the NAS shares under the /Network directory.  I mount mine as /Network/NAS_Name/share_name/ just in case I ever run multiple NAS'es in my house.  The good news is that it seems applications like iTunes and Carbon Copy Cloner identify the share by its network name (e.g., afp://user@NAS/share_name/) rather than the mount point (e.g., /Volumes/share_name/).  So when I changed from Finder-based mounting to autofs, I didn't have to tell any of the applications about the change in directory structure.  They simply picked up the /Network/NAS_Name/share_name/ change automagically.

One last note: you will lose the little drive icons that Finder puts on your desktop when you mount network shares with it.  Not a big deal, but if it bothers you, you can always just create aliases on the desktop instead.

Well, happy auto-mounting!

Update:  November 25, 2015
So while all seemed well when I originally wrote this blog, I've noticed on several occasions where iTunes opens before the NAS volume is mounted.  When this happens, iTunes silently reverts the location of my iTunes library to my local SSD.  So now rather than launch iTunes directly, I wrote this handy Automator script that will test for the existence of a file on the volume, and only launch iTunes if the file exists.  The beauty is that testing for the file will automatically mount the volume if it can be mounted:

if [ -f /Network/NAS/media/00-media-is-alive ]; then
    open /Applications/iTunes.app
else
    osascript -e 'display notification "Folder media is missing. Failed to start iTunes." with title "Automator"'
fi


Then just run the command "touch /Network/NAS/media/00-media-is-alive" from a terminal window and you're good to go.  Instead of launching iTunes directly, run the script instead.  If the script can verify that the file 00-media-is-alive exists, meaning that the volume is mounted, it will start up iTunes.  If not, then you will get a message in Notification Center telling you that iTunes failed to start because the volume with the iTunes library couldn't be mounted.