If you have read a couple of my posts on here before, you will know that I’ve gone to some particular trouble to procure myself a development environment so that I can start writing apps – and I have taken my first faltering steps to create the first test app that runs on my iPhone.Well, being as it has taken so much time and effort to set this environment up, there was nothing for it, but to go and buy myself a MacBook Pro anyway and have the development environment handed to me on a plate. Actually, it wasn’t quite that decadent or pointless – I had a change of employer and my new employer, a gracious gentleman of taste and sophistication, gave me the option of procuring my own laptop. Sweet.
So, now I had the tools, the time and and a completed expense form. It was really tricky coming up with new and different things to avoid knuckling down to writing the app – but I managed to come up some good ones. First there was settling in to the new job, then there was getting my TOGAF Practitioner certification, then applying for my Chartered Professional status with the British Computer Society, taking the BCS “Breadth of Knowledge” test and finally booking my PRINCE2 Practitioner certification exams. When I found myself researching the Certificate in Finance, Accounting and Business (CFAB) with the Institute of Chartered Accountants in England and Wales, I knew I had taken the whole avoidance through self improvement tactic too far and needed something to get the focus back where it should be.
There was nothing for it, but to open up that MacBook, fire up xCode and crack on! It was in a slightly chilly hotel room (the radiators had been turned off and turning them on had only improved things a little bit). The lights were that particular shade of yellow that comes from super eco energy saving bulbs that are identical to the 100 watt pearescent volt glutton ones in every way except, of course, the way that they consume energy and emit light. The MacBook high definition ultra screen seared through the murk and my retinas. You just got to love a display that is only matched in its crisp sharpness by its glossy reflectiveness. Fortunately, in this room there wasn’t much to reflect. Certainly nothing that a MacBook as cool as this one would entertain. Except of course the light bulb hanging from the ceiling in the centre of the room which, by a staggering coincidence, also aligned with the centre of my screen. Leaning forwards just a fraction placed my silhouette between the light and the screen and added just enough discomfort to my lower back to give me the satisfaction that I was suffering for my art.
That’s when I noticed it for the first time – a little pop up window saying that the network drives that I had mounted from my NAS at home for photos, video and music respectively were no longer responding and asking me politely if I would like to disconnect from them. Well, given that I was many miles away from my NAS which wasn’t hooked up to the Internet, this didn’t seem entirely unreasonable, so I disconnected them. But that started me wondering what would happen when I got back home. Would they just magically come back by themselves, would they ping back to life the next time I tried to access data on them, or would they expect me to manually remount the disks every bloody time I came home. I wasn’t sure, but I suspected that the answer had the word “bloody” in it. With a sense of moral outrage and welling satisfaction – I now had reason to avoid writing an app – there was nothing to do but to spend the rest of the evening figuring out how to fix this show stopper.
As it turns out, when I got back home, things were not quite as bad as I expected. Time Machine was quite clever and mounted the drive from the NAS it needed when it ran backups every three or four minutes. Sometimes it just runs continuously with occasional breaks when you don’t need the system to do anything quickly. But even if it’s inconvenient, it’s not going to get me out of writing an app, is it?
Surely iTunes wouldn’t let me down – I clicked the familiar blue circle and up it came without a hitch even though I knew my music was on the NAS. Darn, since when did iTunes do something the way you wanted it to? In actual fact, I realised later that the iTunes library was on the local hard disk which is why it came up – it was only my music files that were on the NAS. Had I tried to play something, to my glee it would have failed – but I didn’t because I was getting cross and wasn’t in the mood for Chas and Dave. So, disconsolately, I plugged in my iPhone to upload the photos from it and was delighted to find that Image Capture couldn’t find the NAS mounted directory to save them to. Bingo -prevarication gold!
When I started looking in to this, I was pretty sure that this must be a common issue. How many people have NAS drives? Loads! How may people have MacBooks? OK, don’t answer that one, but more than three people. And sure enough the Internet is littered with articles about how to mount your NAS when at home – along with a number of applications, both free and fee for making all the necessary adjustments. But hell, this is OSX and I started doing UNIX back in 1986 so this wasn’t going to be a problem to code up myself, even if I was cheating by searching the Internet!
The strange thing was that most of the articles seemed to very nearly do what I wanted to do, but not quite. I wanted something that would connect to my NAS when at home, and disconnect when I was away – automatically. Or at least, if it didn’t disconnect, not cause any nasty problems when the NAS wasn’t there any more.
My first thought was to use automounts. These will make the mounts to servers when you access a mounted directory and disconnect if you don’t use them for a certain time. …and then reconnect if attempt to use them again. I set this up easily enough (it involves editing /etc/auto_master and creating an /etc/auto_smb. Plenty of guidance elsewhere if you want it – but here are mine:
/etc/auto_master:## Automounter master map#+auto_master # Use directory service/net -hosts -nobrowse,hidefromfinder,nosuid/home auto_home -nobrowse,hidefromfinder/Network/Servers -fstab/- -static/- auto_smb
## Automounter master map#+auto_master # Use directory service/net -hosts -nobrowse,hidefromfinder,nosuid/home auto_home -nobrowse,hidefromfinder/Network/Servers -fstab/- -static/- auto_smb/etc/auto_smb:
/Users/<username>/Volumes/music -fstype=smbfs ://<user>:<password>@<NAS>/music
/Users/<username>/Volumes/photo -fstype=smbfs ://<user>:<password>@<NAS>/photo
/Users/<username>/Volumes/video -fstype=smbfs ://<user>:<password>@<NAS>/video
I found the problem with this, however was that if you strayed in to a directory used by automounter whilst away from home, it hung around for a very long time attempting to connect. And heaven forbid if you tried to save anything using Word, Powerpoint or Excel. They just seems to smell that there might be a mount you could conceivably attempt to use – even if you weren’t using it – and hang for even longer than Finder did.
Two things eventually came to my rescue. First was the excellent article here (http://tech.inhelsinki.nl/locationchanger/) which explains how to set up a LaunchAgent to detect when your WAN changes and a locationchanger script that to detect when your WAN is at home and when it is somewhere else. I modified the script to mount my NAS directories when at home. However, when away from home, I found that the mounts did not unmount automatically and we were back to the Finder and programs hanging when going near the mounts.
That’s when I remembered back to my days working at Rover Group in the late 90’s under the expert tutelage of Obe Wan Kenobe – otherwise known as Paul Manton. I never did make Jedi, but back in those days we had problems with clients hanging when there were brief network outages and so we changed our mounts to be “soft” mounts. Soft mounts will drop from beneath applications if the network connection is lot. Of course, this can be bad news for an application with open file handles. Soft mounts think of them more like love handles in that it can choose to ignore them rather than hang around awaiting perfection. This can result in unsaved data being lost – but if you haven’t saved something that is being written to a NAS device and then you move 76 miles away and expect it to magic the data with pixie magic then you probably deserve to lose that data anyway. What are you expecting, magic?
So here is my modified locationchanger script. With the soft mounts it pops up the dialogue asking if you want to disconnect when you move away from your WAN and automatically reconnects to the NAS when at home.
# automatically change configuration of Mac OS X based on location
# author: Onne Gorter <firstname.lastname@example.org>
# url: http://tech.inhelsinki.nl/locationchanger/
# version: 0.4
# set -x
# redirect all IO to /dev/null (comment this out if you want to debug)
exec 1>/dev/null 2>/dev/null
# get a little breather before we get data for things to settle down
# get various system information
| grep ‘ SSID:’ | cut -d ‘:’ -f 2 | tr -d ‘ ‘`
EN0IP=`ifconfig en0 | grep ‘inet ‘ | cut -d’ ‘ -f 2`
EN1IP=`ifconfig en1 | grep ‘inet ‘ | cut -d’ ‘ -f 2`
# locations (use to be used Location name here)
# detect HOME
ATHOME_EN0IP=<home network address>
ATHOME_EN1IP=<home network address>
if [ -z $LOCATION ]; then
case $SSID in
$ATHOME_SSID ) LOCATION=”$ATHOME”;;
if [ -z $LOCATION ]; then
case $EN0IP in
$ATHOME_EN0IP* ) LOCATION=”$ATHOME”;;
if [ -z $LOCATION ]; then
case $EN1IP in
$ATHOME_EN1IP* ) LOCATION=”$ATHOME”;;
if [ -z $LOCATION ]; then
# still didn’t get a location, so do automatic
case $LOCATION in
echo “At home…”
if [ -z “`mount | grep ‘/Users/<userid>/Volumes/music’`” ]
mount_smbfs -o soft //<user>:<password>@<NAS>/music /Users/<userid>/Volumes/music
if [ -z “`mount | grep ‘/Users/<userid>/Volumes/photo’`” ]
mount_smbfs -o soft //<user>:<password>@<NAS>/photo /Users/<userid>/Volumes/photo
if [ -z “`mount | grep ‘/Users/<userid>/Volumes/video’`” ]
mount_smbfs -o soft //<user>:<password>@<NAS>/video /Users/<userid>/Volumes/video
echo “Not at home…”
# do some stuff here that needs to happen after every network change
# echo `date` “Location: $LOCATION – $REASON” >> $HOME/.locationchanger.log