How do I have a network share mount in Mac OS X persist between reboots
I currently connect to a Windows (smb) share via the Finder -> Go -> Connect To Server. I have to type in the IP (although I have saved the credentials in my keychain). If I reboot or logout, I have to do this again.
I would like the SMB share I am connecting to always be available as soon as I login. How can I accomplish this?
Make the share point a login item, and it'll be automatically "opened" (i.e. connected) every time you log on. Go to System Preferences -> Users & Groups -> select your account in the sidebar -> Login Items tab, then drag the mounted share point (you can get it from the Finder's computer view, available under its Go menu).
(Historical note: the preference pane was named "Accounts" in OS X 10.6, but changed to "Users & Groups" in 10.7 and later.)
Overview:
Create a launchd agent (a bash script in this case) to mount us whatever shares we like every couple of minutes. It is not perfect but it works. This is for Samba shares, but you can modify it to do other types.
Made with help from Dave Nicoll's about sharing windows/x iTunes libraries.
I'm using my laptop's wireless card to determine if I am connected to my home SSID. You can of course use anything you'd like for conditional execution.
If you're going to use SSID as a condition for mounting as I have you may find it benificial to alias the Airport utility that ships with OS X to your /usr/bin/. If not skip ahead to the next section.
Open Terminal and run:
sudo ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport
Remember the output of this:
airport -I | grep '.* SSID:' | sed 's/^.* SSID: //'
The Script:
My conditionals check for
- The share is not already mounted (line 1-4)
- The SSID of our Airport card network is not null (line 5-8)
- That our SSID must be equal to a pre-defined SSID, in my case "2600leagues" (line 9)
Open your favorite text editor and start a new file, I called mine MountShares.sh
if [ -d '/Users/kyle/Music/iTunes/Podcasts/' ]; then
#echo Nothing to do, share is mounted
exit
else
if [ -z `airport -I | grep '.* SSID:' | sed 's/^.* SSID: //'` ]; then
#echo SSID is Null, we're not connected with the Airport to any Network.
exit
else
if [ `airport -I | grep '.* SSID:' | sed 's/^.* SSID: //'` == "2600leagues" ]; then
#echo SSID is 2600leagues!
/sbin/mount -o nodev,nosuid -t smbfs '//Kyle:[email protected]/media/Music' '/Users/kyle/Music/'
fi
fi
fi
Next we need to edit line 11 to represent your specific needs. You only need to edit the bold parts:
/sbin/mount -o nodev,nosuid -t smbfs '//User:Password@ServerIP_or_Name/Share' '/Where/You/Want/it_to_mount/'
Example:
/sbin/mount -o nodev,nosuid -t smbfs '//Kyle:[email protected]/media/Music' '/Users/kyle/Music/'
Now that we have our edited mount command, try running it in Terminal. If it is successful, your share should be available at the mount location. (in my example /Users/kyle/Music/
)
Edit the line 1 of the bash script to reflect a file or directory inside the mounted share. If you are going to use the SSID as a condition as I have, you'll want to change line 5 by replacing 2600leagues with your SSID.
Save the bash script in a convenient location. (I use ~/Library/Scripts/NetworkMounts/MountShares.sh
)
You now need to make the bash script executable, we use chmod to do this.
In Terminal:
chmod 777 ~/Library/Scripts/NetworkMounts/MountShares.sh
If the network share is still mounted, make sure you unmount/eject it now. (Finder works)
Try executing the script via Terminal:
~/Library/Scripts/NetworkMounts/MountShares.sh
If all goes well your share should be mounted. Now all that is left to do is make it so the script runs every so often. Normally you would use crontab for this sort of thing, but 10.6 has deprecated it. Apple would rather you use their launchd service.
Making the launchd Agent
To help you make a launchd agent get Lingon from sourceforge.
- Open Lingon
- Create a new User Agent (My Agent)
- Give it a name such as com.kyle.MountShares
- Choose or type the path to the bash
script
~/Library/Scripts/NetworkMounts/MountShares.sh
- Specify when you would like it to run. (I have Run when it is loaded by the system and *Run it every 10 minutes)
- Save it
- Exit Lingon
That's it
Let me know if this helps; I typed all this out as fast as possible.
With auto_fs, auto_master, etc, try having a read of Autofs: Automatically Mounting Network File Shares in Mac OS X (PDF). It's a little dated now (2009) but, using the examples in the doc, I got all my NFS & SMB shares organised and auto mounting easily.
Have you looked at auto_master(5)
? At a glance it looks possible, but it looks like it could be a lot of work to get set up.