Search This Blog

Thursday, February 4, 2010

Mount network share at startup in MacOSx

I run a Buffalo TeraStation NAS server within my environment. To use it within a unix based machine like my IMac I just need to open a Finder and select the share on the server. That'll do the trick for instant access or manual connect. If you want to have your shares mapped before you logon do the following:

1. Create Script, which mounts your shares (see my sample below)


2. Add the script to the LoginHook
     sudo defaults write com.apple.loginwindow LoginHook /path/to/script
    (I store my script on MacOSX in /Library/Scripts)
. More infos on the LoginHook

Note: If you mount a share keep in mind that mounting the share with root privileges will only allow root to use the share. Because of that I used su $1 -c to run the mount as the current logged in user. 



Sample mount script for a NAS server with afp protocol for Mac OSX (if your NAS has no afp just change the mount parameter accordingly):
#!/bin/bash
 

# Information about the NAS access
    username=MY_NAS_USER
    password=MY_NAS_PASS
    hostname=MY_NAS_HOST

# The $1 is within login scripts the current user, which enters MacOsx   
loginuser=$1

# Create a mount dir if necessary
save_mkdir() {
    if [ ! -d $1 ];
    then
        su $loginuser -c "mkdir $1"
    fi
}

# Create a mount as the current logged in user
do_mount() {
    if /sbin/mount|grep -q $1; then
    # echo "Mounted"
        /sbin/umount $1
    fi
    save_mkdir $1
    su $loginuser -c "/sbin/mount -t afp afp://$username:$password@$hostname/$2 $1"
}

echo "******************************************************"
echo "*** Unmount all network drives from boshuda"
echo "******************************************************"
/sbin/umount /Volumes/pictures
/sbin/umount /Volumes/music

echo "******************************************************"
echo "*** Mount all network drives from boshuda"
echo "******************************************************"
do_mount /Volumes/pictures pictures
do_mount /Volumes/music my_music

No comments:

Post a Comment