Gracefully dismount unconnected AFP disk

Have you ever picked up you laptop and gone from one place to another, one net connection to another, one router to another and forgotten to unmount that AFP mounted hard disk?

I have. I’ve got to say, for as much as I think OS X handles everything gracefully it really sucks at unmounting a disk that is no longer present. If you push on the eject button and the disk is no longer connected you’ll end up sequentially locking up your whole machine for 15 - 20 minutes. It will eventually come back, but if you’re impatient the only way out is to physically hold down the power button. It ought to work better than that.

I do this all the time. I’ve got a network disk that I use to back up my laptop to and I’m constantly forgetting to dismount it prior to leaving the office. I did a bit of Googling, I think that’s a real word now, and came to this article. It gives me an answer but I need an automatic solution.

I fiddled with writing a shell script that would check the SSID of my wireless network and if I wasn’t at the office it would dismount any AFP mounted disks. That worked OK except that if I was on another network I couldn’t keep an AFP mounted disk attached. :-(

I was prodded into fixed my script when Matt told of his misadventures. But Matt’s using ethernet, not WiFi, so I had to find another solution. I think I did. I’m still testing and so is Matt.

What I do is grab stuff from netstat -r, which gives me routing tables. It seems that the LAN IP address is labeled default, from here I grab the router’s LAN MAC address and see if it matches the previously stored router’s LAN MAC address. If not then script executes. So far it seems to work. I run the script from a launchd process every minute. You could use a cron job too.

Here’s the archive, unmount.zip. It contains the script, a short readme and a launchd process file that you’ll have to edit, but only a little.

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

This entry was posted in Uncategorized. Bookmark the permalink.

Comments are closed.