Wednesday, March 21, 2012

Auto-mounting network drives in Linux/Ubuntu when server is available

After switching the network connection of my Ubuntu desktop box from LAN to Wifi/WLAN, my pam_mount configuration ceased to work. The plain reason: The Wifi connection is established by the Gnome Network Manager only after I login. pam_mount fails to mount them right before the connection is established. While I'm sure that there's some way to work that out, I chose a quick but reliable solution inspired by this thread.

In detail, here's my script that is run after login:

#!/bin/bash

SERVER=
while [ -z "$SERVER" ] ; do
SERVER=`host server | grep address`
done

gvfs-mount smb://server/share

It surely isn't perfect, but solves the need. If you want to use it, replace the two occurences of server with the name of your Windows/Samba server and share with the name of the network share you want to mount. You can of course place more gvfs-mount lines at the bottom.

It might also be a good starting point for some if-up / ip-up scripts, or even in laptop situations where the drives are only mounted when connected to the 'right' network (identified by the host lookup).

Any comments?

No comments: