Building a Home FreeNAS Server - Automatic CIFS SMB Connection over Wireless

The Dolphin client connection to the FreeNAS server highlighted in my earlier tutorial at Building-a-Home-FreeNAS-Server--Configuration is good for a quick-and-dirty connection to check everything looks ok. However, there are two shortcomings with this approach. Firstly, a manual intervention is required to effect the connection and awkward parameters and syntax need remembering, and secondly files will be copied locally before being executed.

A much better approach would be to connect automatically to the FreeNAS server during my laptop's boot up process, and furthermore, connect as a properly mounted drive which will mean files such as video and audio can be streamed.

It's normal to use the /etc/fstab file to do this (won't work if you are using WiFi like I am), or the command line utility mount.cifs. The normal syntax would be of the format:
mount.cifs //{cifs share}/{cifs directory} {mount point} -o file_mode=0664,username=xxxxxx,password=xxxxxx,noperm
Note: I have included the option noperm. I found that without this only my laptop's root user had write access to the CIFS drive. By specifying noperm I am opening up the permissions to everyone on my laptop, but since this will only ever be me, there is no security implication. So, using the command in my example, it would be:

linux-ldan:/mnt # mount.cifs //192.168.0.9/personal /mnt/freenas -o file_mode=0664,username=xxxxxx,password=xxxxxx,noperm

Now we have a command that works on the command line, we need to put it in a script to automatically start when my wireless connection is started. There is a directory called if-up.d that runs scripts upon a successful network connection. Its location depends upon the Linux distribution you are using. So find it first with:

linux-ldan:~ # locate if-up.d
/etc/sysconfig/network/if-up.d
/etc/sysconfig/network/if-up.d/21-cifs
/etc/sysconfig/network/if-up.d/21-dhcpcd-hook-samba
/etc/sysconfig/network/if-up.d/55-samba-winbindd
/etc/sysconfig/network/if-up.d/ndp-proxy
/etc/sysconfig/network/if-up.d/SuSEfirewall2
linux-ldan:~ #

Now copy the following script. You will need to edit it to match your circumstances.
/etc/sysconfig/network/if-up.d/99-cifs-wifi-start

#!/bin/bash
if iwconfig|grep -c xxxxxx
then
    sleep 10s
    mount.cifs //192.168.0.9/personal /mnt/freenas -o file_mode=0664,username=xxxxxx,password=xxxxxx,noperm   
fi

On the second line you need to check for the SSID of your WiFi connection, so put that value in where I have the xxxxxx. You will also have to change the mount.cifs parameters as discussed earlier. Now make sure it is executable.

linux-ldan:/etc/sysconfig/network/if-up.d # chmod u+x 99-cifs-wifi-start
linux-ldan:/etc/sysconfig/network/if-up.d #

Next a shutdown script is required in the network's shutdown directory.
/etc/sysconfig/network/if-down.d/99-cifs-wifi-stop

if cat /etc/mtab|grep -c /mnt/freenas
then
    umount /mnt/freenas
fi

And change the permissions.
linux-ldan:/etc/sysconfig/network/if-down.d # chmod u+x 99-cifs-wifi-stop
linux-ldan:/etc/sysconfig/network/if-down.d #

Ready to reboot and automatically connect Laughing out loud