Automounting all available Shares on a NAS under Mac OSX 10.7.3
I have recently been using my girlfriends Macbook with Mac OSX Lion (10.7.3) for sorting files on my Synology DS-1511+ NAS. The way OSX handles Volumes/Shares frustrates me, and having to reconnect every time the Macbook leaves standby is even more annoying.
So I started looking for ways to utilize the Mac OSX automount (autofs) tools. With a bit of looking around on the net and a few modifications, this is what I came up with:
First, I created /etc/auto_smb:
1 2 3 |
cd /etc sudo curl -O "http://juerges.net/files/mac_autofs/auto_smb" sudo chmod +x /etc/auto_smb |
Script Content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#!/bin/bash ##################################### # Save this to /etc/auto_smb # Set execution bit with chmod +x /etc/auto_smb # Add this in /etc/auto_master as: # /mountpoint auto_smb # # Create .smb_credentials file in user home dir # format: # username=[username] # password=[password] ##################################### LOCAL_USER=yourLOCALusername SMB_SERVER=yourNASipORhostname OPTIONS="-fstype=url" CACHE_TIME=900 if [ -e /Users/${LOCAL_USER}/.smb_credentials ]; then . /Users/${LOCAL_USER}/.smb_credentials SERVER=$username:$password@$SMB_SERVER else SERVER=$SMB_SERVER fi if [ $# = 0 ]; then if [ -e /tmp/.smb_cache ]; then eTime=`date +%s` eval $(stat -s -t %s /tmp/.smb_cache) if [ $(($eTime-$st_mtime)) -lt $CACHE_TIME ]; then cat /tmp/.smb_cache exit 0 fi fi rm -rf /tmp/.smb_cache sudo -u $LOCAL_USER smbutil view //$SERVER | grep Disk | grep -v homes | cut -d " " -f 1 | while read MOUNT; do if [ -n "$MOUNT" ]; then echo "\"${MOUNT}\" ${OPTIONS} cifs://${SERVER}/${MOUNT}" | tee -a /tmp/.smb_cache; chmod 0600 /tmp/.smb_cache fi done exit 0 fi echo "${OPTIONS} cifs://${SERVER}/$1" |
So far, so good: Now I have a script that handles the doing, but I have to tell the automounter where to apply this script, so I append a directive to /etc/auto_master, create the designated mountpoint and reload the automounter:
1 2 3 |
sudo echo "/Volumes/storage auto_smb" | sudo tee -a /etc/auto_master > /dev/null sudo mkdir /Volumes/storage sudo automount -vc |
At this point, the automounter is registered for /Volumes/storage/, but since I haven’t actually ACCESSED any of the shares yet, there are no subfolders under /Volumes/storage (automounter creates those after initial access).
Since I want a TRUE automount, I use a little script that can be added to the users startup routine.
This script just reads all available shares from the NAS, and cd’s to each of the corresponding automount subdirectories to create the initial stubs.
1 2 3 4 |
sudo mdkir -p /usr/local/bin cd /usr/local/bin sudo curl -O "http://juerges.net/files/mac_autofs/connect_mounts" sudo chmod +x /usr/local/bin/connect_mounts |
Script Content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#!/bin/bash # Cycles through SMB shares from server and cds into each as the specified user # Setting this script as a launch daemon on a calendar schedule can be useful LOCAL_USER=yourLOCALusername LOCAL_MOUNT=/Volumes/storage SMB_SERVER=yourNASipORhostname if [ -e /Users/${LOCAL_USER}/.smb_credentials ]; then . /Users/${LOCAL_USER}/.smb_credentials SERVER=$username:$password@$SMB_SERVER else SERVER=$SMB_SERVER fi sudo -u $LOCAL_USER smbutil view -N //$SERVER | grep Disk | grep -v homes | cut -d " " -f 1 | while read MOUNT; do if [ -n "$MOUNT" ]; then echo "Joining ${MOUNT}" sudo -u $LOCAL_USER cd "${LOCAL_MOUNT}/${MOUNT}" fi done |
Finally, something working But wait, shares mounted this way dont show up on the desktop as Volumes. Fair enough. I just linked the automounter base_dir to my desktop to have easy access:
1 |
sudo ln -s /Volumes/storage /Users/yourLOCALusername/Desktop/storage |
Tadaaa! A working Mac OSX 10.7.3 automounter, easy to use and comfortable. I hope you find this as useful as I do
~~ sebastian
Hi Sebastian,
I have the same problem with my Synology 207+NAS.
So thanks for your information en bash scripts !!!
I read your ‘ how to’ but i’m still having troubles to get it working.
When i get it working then my next step is to modify it that it’s working with AFP shares, but that is future
In the ” auto_smb” bash-script there is some coding i don’t understand, see below.
Questions:
1. need i create the .smb_cache file manually ?
2. eTime=
date +%s
generates an error3. $st_mtime -> this variable is not declared
4. Please can you explain to me what this little code exactly does?
5. Reading the ‘ sed’ is also a pain in the x for me, is it possible to explain this also ?
if [ $# = 0 ]; then
if [ -e /tmp/.smb_cache ]; then
eTime=
date +%s
eval $(stat -s -t %s /tmp/.smb_cache)
if [ $(($eTime-$st_mtime)) -lt $CACHE_TIME ]; then
cat /tmp/.smb_cache
exit 0
fi
fi
Thnx!
Greetings,
Marco
“eTime=
date +%s
”Don’t know what happened, think my mistake, but the ‘ code’ text was missing.
so now with the ‘ code’ -> start code and end code tags.
Hey, glad you liked the howto. Here are my answers:
1. need i create the .smb_cache file manually ?
-> No
2. eTime=date +%s generates an error
-> yes, it would. leave the code sections out, they were inserted by my blog software. Sorry for that. There should be backticks there.
3. $st_mtime -> this variable is not declared
-> do not create the file by hand, and you should be fine.
4. Please can you explain to me what this little code exactly does?
-> Which section do you want explained ?
5. Reading the ‘ sed’ is also a pain in the x for me, is it possible to explain this also ?
the output of smbutil view looks something like this:
-> gladly
$ smbutil view //cal@solarsystem
Password:
Share Type Comment
——————————-
netlogon disk Network Logon Service
ipc$ IPC IPC Service (Samba Server)
tmp disk Temporary file space
public disk Public Stuff
ethereal disk
root disk Home Directories
the only thing i need is the share name, so i use sed to get rid of everything else
Hi Sebastian,
Thnx for your fast reply !!!
2.
changed to:
eTime=$(date +%s)
tried this this afternoon, and was working
4. Please can you explain to me what this little code exactly does?

-> Which section do you want explained ?
if [ $# = 0 ]; then
if [ -e /tmp/.smb_cache ]; then
eTime=$(date +%s)
eval $(stat -s -t %s /tmp/.smb_cache)
if [ $(($eTime-$st_mtime)) -lt $CACHE_TIME ]; then
cat /tmp/.smb_cache
exit 0
fi
fi
What is the purpose of this, i want to now this because i want to know more from bash-scripting then just the first script named ‘ hello world’
Btw. everything is working fine !
I do just a minor change by the LOCAL_USER -> $USER
If i do so then the scripts are working for all the users on the system, not applied, need to test this.
Another question:
sudo ln -s /Volumes/storage /Users/yourLOCALusername/Desktop/storage
this creates a shortcut.
The normal way of changing the icon in OS X Lion does not work.
Do you know how to change the shortcut icon with a custom icon (png-file).
Or has it to to with the file permissions ?!
Marco
Hey Marco,
that section checks if the number of arguments is 0.
If it is, it checks if the file /tmp/.smb_cache exits.
If then sets etime variable to the current time string, and evals the file timestamp time of .smb_cache.
afterwards it subtracs the filestamp time with the etime and if that is less than the cache_time, it reads the cache file, then exits. That way the script doesnt have to recheck what shares the server offeres every time the script runs, only if the cache_time has expired. If you set that cache time high enough (seconds), the script runs much faster.
Oh, and i have no idea about the icons