Samba is probably the most common implementation of Service Message Block (SMB) on Linux. I use it to provide SMB shares to all my various computers, most of which run Linux.
Interaction between NetworkManager and SMB mounts is less than ideal. You can mount it easily enough manually or in a script that NetworkManager runs. More on the latter option later.
Unmounting an SMB share isn't really an issue on stationary machines. The only reason to do so is if the server goes down.
On laptops, however, unmounting it can get hairy. I usually forget to do so when shutting down my laptop for a trip. The result is I will accidentally try to go into the share while I am away and have no connection to the server. Oops. This results in a hung program. SMB implements some pretty long timeouts, so that program can stay hung for a long time.
Fortunately, with a bit of scripting we can do something about that.
Network Manager provides a place to put a script. The time to unmount a share is before the network link goes down. So the place to put a script is in /etc/NetworkManager/dispatcher.d/pre-down.d/
. We'll call the script 50cifs.sh.
First we set up useful information, like the location of the log file, and feed some useful data to the log file. Doing so logs the fact that the script was called, even if the script ends up doing nothing.
Then we test to see if the share is mounted. That's easy, as I have it mount in the same place on all my machines. Otherwise a bit of parsing with grep
and cut
would be in order.
If the share is mounted, we log that fact. Then we check for the right tool. Note: it may be elsewhere on your computer. Edit accordingly.
Then the script unmounts the share, and that's that!
#!/bin/bash
# Pre-down script to shut down a Samba/cifs/smb connection if one is
# running.
LOG=/var/log/NetworkManager
# Our home samba mount point.
smbMount='/home/charles/samba'
/bin/echo >> $LOG
/bin/date >> $LOG
/bin/echo $0 >> $LOG
/bin/echo $# arguments: $* >> $LOG
if [ $(mount | grep '/home/charles/samba type cifs' >> /dev/null ; echo $?) = '0' ] ; then
echo 'cifs mount is mounted.' >> $LOG
if [ -x /usr/sbin/mount.cifs ] ; then
echo Unmounting the Samba mount... >> $LOG
umount $smbMount >> $LOG
fi
fi