Automount CIFS on SLE12SP4

Mounting CIFS on Linux is always a source of problems. Unlike Windows clients, Linux can not reconnect a disconnected mount, so it hangs if something happens to the Windows server (for example, reboot). Therefore, permanent mounting for Windows share is not recommended.

You should use dynamic on-demand mounting and unmounting of unused directories. The following is an example of setting autofs for CIFS. First let's install the software used here (most likely it is already installed):

# zypper install autofs cifs-utils

Create a credential file, make it readable only for root:

# ll /etc/cifs.creds 
-rw------- 1 root root 43 Oct 17 12:13 /etc/cifs.creds
# cat /etc/cifs.creds
username=value
password=value
domain=value

Try to mount manually using this file:

# mount -t cifs -o credentials=/etc/cifs.creds //192.168.122.1/tmp /mnt/net
# df /mnt/net
Filesystem           Size  Used Avail Use% Mounted on
//192.168.122.1/tmp  3.8G   20M  3.8G   1% /mnt/net
# umount /mnt/net

Where 192.168.122.1 is my Windows Server and tmp is a Share Name on it.

Once mount works manually, you can continue to make it automatically. Create /etc/auto.cifs like this (all slashes and backslashes are important):

# cat /etc/auto.cifs 
backup  -fstype=cifs,credentials=/etc/cifs.creds        \//192.168.122.1/tmp

Then edit /etc/auto.master file. Add reference to our configuration file before +auto.master line:

# cat /etc/auto.master 
/cifs   /etc/auto.cifs
+auto.master

Then start the service and enable it on OS boot:

# /etc/init.d/autofs start
# chkconfig autofs on

Check it works:

# ls /cifs/backup
 ..
# df /cifs/backup
Filesystem           Size  Used Avail Use% Mounted on
//192.168.122.1/tmp  3.8G   20M  3.8G   1% /cifs/backup

Check the mount after the timeout (10 minutes by default). This value can be tuned in /etc/sysconfig/autofs configuration file. You can force autofs to unmount inactive mounts by command:

# kill -USR1 $(cat /var/run/automount.pid)
# df

Updated on Tue Oct 17 13:27:14 IDT 2017 by Oleg Volkov More documentations here