Friday, 4 March 2011

How To Automatically Connect To Network Shares On Login To A Mac

EDIT: Please see my new article for a better way to do this:
http://www.vuzzlevuzz.org/2011/10/how-to-automatically-connect-to-network.html

Been getting a bit hands on with Macs lately...

One of my clients employs quite a few freelancers on short term bases and they, understandably, need access to network shares.  Now on windows it's easy to configure this in a login script but there does not seem to be anyway to do this for Mac clients (on a Windows Active Directory domain at least, Open Directory can do it I understand).

At first research seemed to indicate that using a login hook to run a shell script to mount the share was the way to go, but when I tried it I found that the drive gets mapped as root rather than the logged in user - not what we want at all.

The solution I finally hit upon was to use a launchd agent.

Launchd is a system for running various things when certain events occur and is the Mac replacement for the common Unix startup scripts, rc.d, init.d, etc.  On cursory inspection it seems quite flexible in what it can do, but the bit that I'm interested in allows a script to be run whenever any user logs in - this lets me mount the network share at login even for a user that has never logged into the machine, and with the correct credentials to boot.

In order to make this easy to modify at a later date I set up a shell script to be run by launchd that, instead of mounting the shares directly, instead mounts the Windows server's NETLOGON share and executes a Mac specific logon script which then mounts the end user shares.  This allows me to manage what shares are mounted centrally, without having to modify every machine if I want to change something, and is analogous to the windows logon script and so easy for other engineers to understand and support.


To set this up on a Mac you need 3 files:

org.vuzzlevuzz.mapfolders.plist
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//ENhttp://www.apple.com/DTDs/PropertyList-1.0.dtd>
 3 <plist version="1.0">
 4 <dict>
 5 <key>Label</key>
 6 <string>org.vuzzlevuzz.mapfolders</string>
 7 <key>Program</key>
 8 <string>/Library/Scripts/mapfolders.sh</string>
 9 <key>RunAtLoad</key>
10 <true/>
11 </dict>
12 </plist>
mapfolders.sh
 1 #!/bin/bash
 2 mkdir /Volumes/NETLOGON
 3 /sbin/mount -t smbfs //
servername/NETLOGON /Volumes/NETLOGON
 4 /Volumes/NETLOGON/OSXLogon.sh
 5 /sbin/umount /Volumes/NETLOGON
OSXLogon.sh
 1 /bin/mkdir /Volumes/sharename
 2 /sbin/mount -t smbfs //servername/sharename /Volumes/sharename
NOTE:  The line numbers are just to make it clear when lines have been wrapped

Copy org.vuzzlevuzz.mapfolders.plist to /Library/LaunchAgents and make it executable:
sudo chmod +x /Library/LaunchAgents/org.vuzzlevuzz.mapfolders.plist 
Copy mapfolders.sh to /Library/Scripts
Configure OSXLogon.sh as appropriate for your environment and place in the server NETLOGON share


There are a couple of caveats with this - I'm working to resolve them but at the moment they are not important for the place I am using this.  Will post a follow up if I get them sorted out, but for now be aware of the following:

  • The drives do not unmount when the user logs off
  • If another user logs in they will not have their drives mapped
  • If one user restarts the machine and then someone else logs in, it will work correctly


Mac OS X Reference Library: Creating launchd Daemons and Agents

No comments:

Post a Comment