Automount nas cifs share

I have several CIFS shares on my NAS. One of those is my Documents share. I had been mounting it to the Documents directory in my home directory via the fstab. Works fine, however, I decided that I would rather use automount to mount the share on demand rather than it always being mounted.

I installed autofs, setup auto.master and auto.cifs. All looks good, I enable and start autofs and boom, the GUI dies. BTW, I use KDE Plasma. I spend a long time verifying syntax and making sure everything is configured correctly, but every time I try to automounting my cifs shared Documents to my home Documents dir, KDE crashes.

I created a directory in / called cifs. I then point the auto.cifs to mount Documents there, and it works just fine. I can see my data without issue.

Is there some reason why I can’t mount the CIFS share to my home Documents dir? As previously stated, it worked fine when I was doing it via fstab.

Thanks

I don’t use automount instead I wrote my own script to do on demand mounts and umounts and created launchers on the DE dashboard to call each.


On client
Create group sharemount and add users allowed to mount shares.

Create file 01_sharemount in /etc/sudoers.d/ with perms 440 and this
content:
%sharemount ALL=NOPASSWD: /usr/sbin/mount.cifs, /bin/umount

Create credentials file in users home directory
/home/<username>/.samba/.<username> with directory perms 700 file perms
600


On server

Create corresponding users with passwords but no login access.
Create user mountable directories and entries in smb.conf

Now back on clients create the mount scripts in the users bin
directory with directory and file perms of 750

The mount script "sharemount":

#!/bin/bash
# sharemount <sharename 1> <sharename 2>
# This script will test for <servername> on the network and then mount shares or
# issue an error message

share="$@"

if ping  -w 2 -c 1 <servername> && smbclient -NL <servername>; then
   echo "<servername> is present"
for share in "$@" ; do
   mkdir ~/$share

   sudo mount.cifs -o username=$USERNAME,uid=$USERNAME,gid=$USERNAME,credentials=/home/$USERNAME/.samba/.$USERNAME //<servername>/$share ~/$share

done

else
    echo "The server <servername> could not be reached. Is it on?"

fi

#################end of script###############

Now the umount script "umountsmb"

#!/bin/bash
# umount.cifs <share 1> <share 2>
# this program un-mounts network shares and then removes the mount point

share="$@"

for share in "$@" ; do

    if [ -d ~/$share ]; then

	sudo	/bin/umount /home/$USERNAME/$share
	wait=2
	rmdir /home/$USERNAME/$share
	wait=1
    fi
done

################end of ummountsmb########################

Substitute $USER for $USERNAME in the noted scripts above as is something I defined long ago that is not necessary.
Since the shares are variables on the command line they can be changed as needed w/o changing the script.

sorry this isn’t a solution to automount. I tried it once and I think I had similar issues. It is probably due to the fact that root is mounting in the users home and this breaks things. If you can somehow run it as the user it may work.

What do your auto.master and auto.cifs look like?

I suspect that the automounter is mounting the CIFS share in a different directory to the one you expect and that’s what is interfering with KDE.

Thanks for this script. I’ll look at it and give it a try.

This is how my auto.master looked when trying to mount to my home dir.

#
# Include central master map if it can be found using
# nsswitch sources.
#
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#
+auto.master

/home/<user>    /etc/auto.cifs

This is how my auto.cifs looks:

Documents -fstype=cifs,rw,noperm,credentials=/home/<user>/.cifscreds,vers=3.0 ://nas01/Documents

Changing auto.master to this works

#
# Include central master map if it can be found using
# nsswitch sources.
#
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#
+auto.master

/cifs/<user> /etc/auto.cifs --ghost

Thanks

What is happening is that you have given the automounter complete control over /home/<user>. This means that subdirectories of /home/<user> other than Documents are no longer visible.

I think the solution is to use a direct map, instead of an indirect map, so your line in auto.master is

/-             /etc/auto.direct

Remove the line with auto.cifs from auto.master.

Then in /etc/auto.direct, you have something like this:

/home/<user>/Documents   -fstype=cifs,rw,noperm,credentials=/home/<user>/.cifscreds,vers=3.0 ://nas01/Doc

The alternative solution is to mount Documents in /cifs/<user> and make your Documents directory a symbolic link to /cifs/<user>/Documents

2 Likes

One does not need to edit the auto.master at all. Mine is edited a bit though, but the statements that I have are in the default too:

$ cat /etc/auto.master
# ansible #
+dir:/etc/auto.master.d
+auto.master

The dir makes autofs read all /etc/auto.master.d/*.autofs files.
I can thus have:

$ cat /etc/auto.master.d/local.autofs 
# ansible #
/-       /etc/auto.master.d/auto.direct  --timeout=300
/home    /etc/auto.master.d/auto.home    --timeout=300 nobrowse
/data    /etc/auto.master.d/auto.data    --timeout=300 browse

In other words, all my automount rules, “maps”, are in one place: /etc/auto.master.d/


I do agree with @Whoever that direct mount or mount to elsewhere makes much more sense for Documents folder.


man auto.master writes:

[no]browse
This is an autofs specific option that is a pseudo mount option and so is given without a leading dash. Use of the browse option pre-creates mount point directories for indirect mount maps so the map keys can be seen in a directory listing without being mounted. Use of this option can cause performance problem if the indirect map is large so it should be used with caution. The internal program default is to enable browse mode for indirect mounts but the default installed configuration overrides this by setting BROWSE_MODE to “no” because of the potential performance problem. This option does the same as the deprecated --ghost option, the browse option is preferred because it is used by other autofs implementations.

Thank you for these great options. I’ll give them a try.

Perhaps I should expand my rationale for that:

The Documents is a directory that usually contains files and directories; it can have files.
It makes no difference whether Documents is a key under mount-point ${HOME} in indirect map or a directly mounted directory.

The /home being a mount-point and each ${HOME} a key under it is also fine, because there will be no files directly in /home/. (One mounts directories, not files.)

However, the ${HOME} definitely does contain files, like the .cifscreds in OP example, so it cannot be a mount-point. (I might have seen an autofs mount-point that had some non-mounted content, but it was a mess that did require a hard reboot to get out of. KISS principle holds.)

I want to try to give your script a go. I’m not very familiar with the smb.conf file. What entries should I put in there?

Thanks

What entries should I put in there?

There usually is an example smb.conf shipped with the samba packages. That usually explains the basics. I set my samba server up 20 years ago and have only made modifications as things didn’t work or after reading the documentation I saw that I had incorrect options or there was a better way. My script that I shared only knows the share names defined in the smb.conf. Other than that it has no other knowledge of the smb.conf settings. You really have to read the documentation. Googling for simple samba sharing set up will probably get you some examples. Back when there were bookstores I used a book called “Samba for Dummies”.

Yes I was searching for some examples. Reading smb.conf, I see a section labeled " USERSHARES". I guessing you have something setup along those lines?

Thanks for sharing your script. I appreciate it.

Here is an example share:

[bits]				
	path		= /data/bits
	browseable	= no
	writeable	= yes
	valid users	= @k-r
        force group     = k-r

I created a group “k-r” and added the users allowed to access that share. The sharename in square brackets does not have to match the target directory in the path statement but it is what you would add to the sharemount command line w/o bracket.
I didn’t mention in my outline above that you need to populate the tdb.sam user database by adding the allowed users with the smbpasswd command. The name and password should match those created on the server.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.