Archive for Februar, 2012
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