Mounting network shares with autofs (alternative to fstab)

This how-to describes how to mount your network shares via autofs. Autofs should be as performant as fstab based mounting but has the advantage that your shares are mounted on demand which also should give you more reliability in case of network interuptions.

First install the autofs package

sudo apt-get install autofs

If you already have mounted your shares already via fstab comment out that line in fstab and reboot.

For this how-to we are going to use a directory for each server, if you want to replicate your existing folder structure that can also be done.

autofs will create any mountpoints required, so you do not need to create them

Important notes to read before proceeding

  • When you see an item surrounded by < > do not include the < > in your file. For example if your server is MyServer, then /mnt/<server> would become /mnt/MyServer
  • If you have spaces in the name, then you need a \ before the space. For example /mnt/My Files should be /mnt/My\ Files
  • If you are migrating from using fstab, you were used to replacing spaces with \040. This will not work with autofs. Just a plain \ before the space is all that’s needed.

For SMB shares

sudo nano /etc/auto.master

and add to the end of the file:

 /- /etc/auto.smb.shares --timeout 15 browse

[EDIT 09/24/2020: Make sure that it’s browse, not --browse!]

(Don’t forget to save the file: CTRL-O ENTER CTRL-X)

Next you need to create a map to the share:

sudo nano /etc/auto.smb.shares

and add

/mnt/<server1>/<share1> -fstype=cifs,rw,credentials=/home/osmc/.smbcredentials,iocharset=utf8,uid=osmc,gid=osmc ://<IP of server1>/<share1>
/mnt/<server2>/<share2> -fstype=cifs,rw,credentials=/home/osmc/.smbcredentials,iocharset=utf8,uid=osmc,gid=osmc ://<IP of server2>/<share2>

[EDIT 09/24/2020: Make sure that the mount point (/mnt/… does not end with a / )
For example, /mnt/server/share1/ will cause problems with autofs. The mounts will not automatically be unmounted if the mount point ends with a /. This is true for all of the following examples also.]

You may have to add an additional parameter to your /etc/auto.smb.shares , namely vers=3.0 to enforce the usage of SMB version 3 especially if you have connection issues with Windows 10. Some setups may require vers=3.1.1 to be used instead (possibly Synology users).
We suggest the usage of a “credentials” file for security reasons.

This is what a credentials file should look like. (a good name for the file would be .smbcredentials) Create this file in the osmc home, so using nano:

nano ~/.smbcredentials

And add:

username=<username> 
password=<password>

Note that the credentials in this file are read from the equals sign to a line feed. If you have any trailing spaces or a carriage return because you created it in, or copied it from a Windows PC then the file will not work. If you have special characters in your password you may need to place your username line after your password line.

If you don’t want to use a credentials file use this instead:

/mnt/<server1>/<share1> -fstype=cifs,rw,username=<username>,password=<password>,iocharset=utf8,uid=osmc,gid=osmc ://<IP of server1>/<share1>
/mnt/<server2>/<share2> -fstype=cifs,rw,username=<username>,password=<password>,iocharset=utf8,uid=osmc,gid=osmc ://<IP of server2>/<share2>

If your username or password includes a space then use quotes around it like this:

/mnt/<server1>/<share1> -fstype=cifs,rw,username="User Name",password=<password>,iocharset=utf8,uid=osmc,gid=osmc ://<IP of server1>/<share1>

You don’t need quotes in a credentials file to deal with spaces. You can’t use escape characters in either file.

For NFS:

As with smb, edit auto.master

sudo nano /etc/auto.master

and add

 /- /etc/auto.nfs.shares --timeout 15 browse

to the end of the file. (Don’t forget to save the file: CTRL-O ENTER CTRL-X)

And for the map file we will be mounting several shares from 2 servers:

sudo nano /etc/auto.nfs.shares

and add:

/mnt/<server1>/<share1> <IP of server1>:</path/to/share1>
/mnt/<server2>/<share2> <IP of server2>:</path/to/share2>

If you’d like, you can always just mount them directly under /mnt (or anywhere else):

/mnt/<share1> <IP of server1>:</path/to/share1>
/mnt/<share2> <IP of server2>:</path/to/share2>

Testing the config:
After you have made the changes restart autofs:

sudo systemctl restart autofs

To test if your mounts works execute:

ls -lah /mnt/<server1>/<share1>

you should see the files from your network share. If it didn’t work run

sudo systemctl stop autofs
sudo automount -f -v -d

to debug what’s wrong. While this is running, it will appear to keep trying to mount. That’s normal. While automount is running, open another SSH connection and try:

 ls -lah /mnt/<server1>/<share1>

again while watching the output from the automount command.

To exit if running automount, just do a CTRL-C. To restart the service once everything is working, either reboot or:

sudo systemctl start autofs

Using Path Substitution in case you want to keep old library

If you are migrating from Kodi build in Network access you might want to check out path substitution if you don’t want to rescan your library.

17 Likes

I just had success using autofs to mount a remote sshfs share without root server access. I’ve bumped this thread in case others might find the solution useful.

Can you specify the meaning of /mnt/<server1>/<share1> as I think that means /mnt/<IP of my Windows PC>/<Name_of_shared_dir>.

I’ve used to do mounting via fstab using

//192.168.1.101/Movies /mnt/Movies cifs x-systemd.automount,noauto,rw,iocharset=utf8,username=First\040Name,password=password123,uid=osmc,gid=osmc,file_mode=0770,dir_mode=0770,vers=3.0 0 0

and all worked well. Now I tried to switch autofs as I tend to shutdown and restart my SMB server PC every now and then. I #uncommented the fstab as needed and proceeded in accordance of these instructions. I’ve edited /etc/auto.master as followed

# Sample auto.master file
# This is a 'master' automounter map and it has the following format:
# mount-point [map-type[,format]:]map [options]
# For details of the format look at auto.master(5).
#
#/misc  /etc/auto.misc
#
# NOTE: mounts done from a hosts map will be mounted with the
#       "nosuid" and "nodev" options unless the "suid" and "dev"
#       options are explicitly given.
#
#/net   -hosts
#
# Include /etc/auto.master.d/*.autofs
# The included files must conform to the format of this file.
#
+dir:/etc/auto.master.d
#
# 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
/- /etc/auto.smb.shares --timeout 15 browse

/- /etc/auto.smb.shares --timeout 15 browse is now added to the bottom of the file, just below +auto.master. Then I created /etc/auto.smb.shares

/mnt/192.168.1.101/Movies -fstype=cifs,rw,credentials=/home/osmc/.smbcredentials,iocharset=utf8,uid=osmc,gid=osmc,vers=3.0 ://192.168.1.101/Movies/

Notice that I’ve added the parameter vers=3.0. Then

osmc@osmc:/$ cd home
osmc@osmc:/home$ cd osmc

At /home/osmc I used nano ~/.smbcredentials to create

username=First\ Name
password=password123

Note that there is a \ in the username followed by space as my username in my Windows 10 PC is First Name. After all this I type

osmc@osmc:~$ sudo systemctl restart autofs
osmc@osmc:~$ ls -lah /mnt/192.168.1.101/Movies
ls: cannot open directory '/mnt/192.168.1.101/Movies': No such file or directory

although there is dir called Movies in /mnt/192.168.1.101/

osmc@osmc:~$ cd /mnt/192.168.1.101/
osmc@osmc:/mnt/192.168.1.101$ dir
Movies

If I type

osmc@osmc:/$ ls -lah /mnt/192.168.1.101/

I Get

total 8.0K
dr-xr-xr-x 3 root root 4.0K Sep 18 12:20 .
drwxr-xr-x 5 root root 4.0K Sep 18 11:28 ..
drwxr-xr-x 2 root root    0 Sep 18 12:20 Movies

It’s like the dir Movies is in there but then again it isn’t. I type

sudo systemctl stop autofs
sudo automount -f -v -d

and get this kind of info:

Starting automounter version 5.1.2, master map /etc/auto.master
using kernel protocol version 5.02
lookup_nss_read_master: reading master file /etc/auto.master
do_init: parse(sun): init gathered global options: (null)
lookup_read_master: lookup(file): read entry +dir:/etc/auto.master.d
lookup_nss_read_master: reading master dir /etc/auto.master.d
lookup(dir): dir map /etc/auto.master.d missing or not readable
lookup(file): failed to read included master map dir:/etc/auto.master.d
lookup_read_master: lookup(file): read entry +auto.master
lookup_nss_read_master: reading master files auto.master
do_init: parse(sun): init gathered global options: (null)
lookup(file): failed to read included master map auto.master
lookup_read_master: lookup(file): read entry /-
master_do_mount: mounting /-
automount_path_to_fifo: fifo name /var/run/autofs.fifo--
lookup_nss_read_map: reading map file /etc/auto.smb.shares
do_init: parse(sun): init gathered global options: (null)
mounted direct on /mnt/192.168.1.101/Movies with timeout 15, freq 4 seconds
do_mount_autofs_direct: mounted trigger /mnt/192.168.1.101/Movies
st_ready: st_ready(): state = 0 path /-
statemachine:1500: got unexpected signal 28!
st_expire: state 1 path /-
expire_proc: exp_proc = 4104123472 path /-
expire_cleanup: got thid 4104123472 path /- stat 0
expire_cleanup: sigchld: exp 4104123472 finished, switching from 2 to 1
st_ready: st_ready(): state = 2 path /-
st_expire: state 1 path /-
expire_proc: exp_proc = 4104123472 path /-
expire_cleanup: got thid 4104123472 path /- stat 0
expire_cleanup: sigchld: exp 4104123472 finished, switching from 2 to 1
st_ready: st_ready(): state = 2 path /-
st_expire: state 1 path /-
expire_proc: exp_proc = 4104123472 path /-
expire_cleanup: got thid 4104123472 path /- stat 0
expire_cleanup: sigchld: exp 4104123472 finished, switching from 2 to 1
st_ready: st_ready(): state = 2 path /-
st_expire: state 1 path /-
expire_proc: exp_proc = 4104123472 path /-
expire_cleanup: got thid 4104123472 path /- stat 0
expire_cleanup: sigchld: exp 4104123472 finished, switching from 2 to 1
st_ready: st_ready(): state = 2 path /-
st_expire: state 1 path /-

Any ideas?

1 Like

Correct

Can you try without vers=3.0 to check if different error

Can you try with a user name without the space?

I removed vers=3.0 and now it is

/mnt/192.168.1.101/Movies -fstype=cifs,rw,credentials=/home/osmc/.smbcredentials,iocharset=utf8,uid=osmc,gid=osmc ://192.168.1.101/Movies/

osmc@osmc:~$ sudo nano /etc/auto.smb.shares
osmc@osmc:~$ sudo systemctl restart autofs
osmc@osmc:~$ ls -lah /mnt/192.168.1.101/Movies
ls: cannot open directory '/mnt/192.168.1.101/Movies': No such file or directory

Now I try

osmc@osmc:~$ sudo systemctl stop autofs
osmc@osmc:~$ sudo automount -f -v -d
Starting automounter version 5.1.2, master map /etc/auto.master
using kernel protocol version 5.02
lookup_nss_read_master: reading master file /etc/auto.master
do_init: parse(sun): init gathered global options: (null)
lookup_read_master: lookup(file): read entry +dir:/etc/auto.master.d
lookup_nss_read_master: reading master dir /etc/auto.master.d
lookup(dir): dir map /etc/auto.master.d missing or not readable
lookup(file): failed to read included master map dir:/etc/auto.master.d
lookup_read_master: lookup(file): read entry +auto.master
lookup_nss_read_master: reading master files auto.master
do_init: parse(sun): init gathered global options: (null)
lookup(file): failed to read included master map auto.master
lookup_read_master: lookup(file): read entry /-
master_do_mount: mounting /-
automount_path_to_fifo: fifo name /var/run/autofs.fifo--
lookup_nss_read_map: reading map file /etc/auto.smb.shares
do_init: parse(sun): init gathered global options: (null)
mounted direct on /mnt/192.168.1.101/Movies with timeout 15, freq 4 seconds
do_mount_autofs_direct: mounted trigger /mnt/192.168.1.101/Movies
st_ready: st_ready(): state = 0 path /-
statemachine:1500: got unexpected signal 28!
st_expire: state 1 path /-
expire_proc: exp_proc = 4104123472 path /-
expire_cleanup: got thid 4104123472 path /- stat 0
expire_cleanup: sigchld: exp 4104123472 finished, switching from 2 to 1
st_ready: st_ready(): state = 2 path /-
st_expire: state 1 path /-
expire_proc: exp_proc = 4104123472 path /-
expire_cleanup: got thid 4104123472 path /- stat 0
expire_cleanup: sigchld: exp 4104123472 finished, switching from 2 to 1
st_ready: st_ready(): state = 2 path /-

and this goes on and on and on…

I also tried to remove the spacebar in my credentials file like this

username=First\Name
password=password123

No luck. I do not have another account in this PC with users without a spacebar in the name. Although I could replace the First Name username with the username written in my C:/Users dir and try with that…

EDIT: Tried with my C:/Users dir username (kaspe) and no luck.

Can’t you just create a user without space for accessing that share?
statemachine:1500: got unexpected signal 28!
last time I saw that indicated issues with the format and the only think that sticks out for me is the space in username.

I try to create user with a name testuser and see how it goes. Stand by for further info.

EDIT: No luck even I created a new user testuser into my PC. All the sharing settings were OK and as they should but no luck.

At /etc/auto.master file there was 2 empty row’s at the bottom of the page. I removed then and now the debug gives me this:

osmc@osmc:~$ sudo automount -f -v -d
Starting automounter version 5.1.2, master map /etc/auto.master
using kernel protocol version 5.02
lookup_nss_read_master: reading master file /etc/auto.master
do_init: parse(sun): init gathered global options: (null)
lookup_read_master: lookup(file): read entry +dir:/etc/auto.master.d
lookup_nss_read_master: reading master dir /etc/auto.master.d
lookup(dir): dir map /etc/auto.master.d missing or not readable
lookup(file): failed to read included master map dir:/etc/auto.master.d
lookup_read_master: lookup(file): read entry +auto.master
lookup_nss_read_master: reading master files auto.master
do_init: parse(sun): init gathered global options: (null)
lookup(file): failed to read included master map auto.master
lookup_read_master: lookup(file): read entry /-
master_do_mount: mounting /-
automount_path_to_fifo: fifo name /var/run/autofs.fifo--
lookup_nss_read_map: reading map file /etc/auto.smb.shares
do_init: parse(sun): init gathered global options: (null)
mounted direct on /mnt/192.168.1.101/Movies with timeout 15, freq 4 seconds
do_mount_autofs_direct: mounted trigger /mnt/192.168.1.101/Movies
st_ready: st_ready(): state = 0 path /-
st_expire: state 1 path /-
expire_proc: exp_proc = 4102026320 path /-
expire_cleanup: got thid 4102026320 path /- stat 0
expire_cleanup: sigchld: exp 4102026320 finished, switching from 2 to 1
st_ready: st_ready(): state = 2 path /-

ls -lah /mnt/192.168.1.101 gives this

image

but adding Movies to ls -lah /mnt/192.168.1.101/Movies gives error

image

The funni thing is that when I stop autofs, I can do this:

image

Too bad that this does not work at the moment. I revert the fstab way as it works.

Strange that it is not working. Did you add the space in the Username again?
Let me know when you want to continue testing.

I can continue testing tomorrow, if it’s okay for you. I’ve added the space to the username again. As I use fstab again, My W10 account’s name First Name works in the file as First\040Name.

I don’t think there should issues with fstab if your just rebooting your server every once and a while and not doing that when your actually playing something at the same time. If I understand it correctly the problems generally come when you are letting your remote share sleep and the time for it to wake up is excessive.

The problem comes when I try watch a movie and I don’t realize that my PC has secretly rebooted due to automatic updates. Now, thanks to VeraCrypt, my Windows does not boot until I enter the master password. It seems like OSMC thinks that the media is no longer available. Reboot of all of my devices doesn’t help. The only thing left to do is to remove my media mounts from settings > media > videos and do fstab mounting again. All this has probably something to do with my PC.

Never the less, I’d like to debug this autofs thing. I will try the username with \040 as a space bar. I will also try again the user without spaces in the name. Then I try to debug it with sudo automount -f -v -d.

It could actually be the name of the server, or anything else you want. We suggest the name of the server just to make things easy. A simple example if you only have one share with movies would be instead of /mnt/192.168.1.101/Movies just use /mnt/Movies. If you have more than one server with shares you could do /mnt/myfirstserver/Movies

I’ve got it working now. The solution was to use the non ~/.smbcredentials method, where I put my credentials into the /etc/auto.smb.shares itself like this:

/mnt/Movies -fstype=cifs,rw,username=First\ Name,password=password123,iocharset=utf8,uid=osmc,gid=osmc,vers=3.0 ://192.168.1.101/Movies/

So my guess is that this has something to do with the name, type or location of the ~/.smbcredentials file. I recall that the name of it in the home/osmc was .smbcredentials.

Now the funny thing was that when I got it work, I added my TV_Series dir also to osmc. When opening the /etc/auto.smb.shares I noticed that my first mount Movieswas written with dual comma ,, after the password (password123) like this:

/mnt/Movies -fstype=cifs,rw,username=First\ Name,password=password123,,iocharset=utf8,uid=osmc,gid=osmc,vers=3.0 ://192.168.1.101/TV_Series/

I tried the “dual comma” with my TV series folder TV_Series also so that the /etc/auto.smb.shares was

/mnt/Movies -fstype=cifs,rw,username=First\ Name,password=password123,,iocharset=utf8,uid=osmc,gid=osmc,vers=3.0 ://192.168.1.101/Movies/
/mnt/TV_Series -fstype=cifs,rw,username=First\ Name,password=password123,,iocharset=utf8,uid=osmc,gid=osmc,vers=3.0 ://192.168.1.101/TV_Series/

Worked like a charm. Then I removed excessive commas and it worked also after that. Tested with interrupted connection / restart PC. Works in all scenarios imaginable.

As I side mention I’d like to point out that while it works now, I’ve left my /etc/fstab as it was with the exception of commenting the lines like this:

# rootfs is not mounted in fstab as we do it via initramfs. Uncomment for remount (slower boot)
#/dev/vero-nand/root  /    ext4      defaults,noatime    0   0
#//192.168.1.101/TV_Series /mnt/TV_Series cifs x-systemd.automount,noauto,rw,iocharset=utf8,username=First\040Name,password=password123,uid=osmc,gid=osmc,file_mode=0770,dir_mode=0770,vers=3.0 0 0
#//192.168.1.101/Movies /mnt/Movies cifs x-systemd.automount,noauto,rw,iocharset=utf8,username=First\040Name,password=password123,uid=osmc,gid=osmc,file_mode=0770,dir_mode=0770,vers=3.0 0 0

I’ve also selected “Wait for network” option from the OSMC options.

1 Like

Thank you for this excellent explanation. It helped me „out of the box „ to created my smb shares.

May be I got something wrong (I am not a Linux guy): In Kodi under „media „“ video „“auto mount „, I don’t see my shares. I have to go to „add videos“ „root „“, „mnt „.
All my shares are listed here and not as I expected under the section „auto mount „.

Also I find in the mnt folder several entries I have created testing the feature. How can I delete this entries?

Thanks

Chang

In the second file you made the first part of the line is the location where you are mounting to. If you followed the guide the /mnt/<server1>/<share1> is the ‘root’ /mnt folder. If you want to mount it somewhere else you just need to change that location in the auto.nfs.shares file.

sudo rmdir /mnt/*
(this will remove ONLY empty folders in your /mnt directory)

“auto mount” is another OSMC internal function to automount local devices (e.g. USB Disks).
The deivces/shares you mount via autofs will show up in the directly you define in the config file as indicated by @darwindesign

Thank you for your kind help and your fast response.

Great stuff! I was using fstab to mount a HDD connect to a router running AdvancedTomato (and SMB 1). It was working fine until it suddenly stopped working (and I get “No such file or directory” error message when I try to access the serve share).

So I tried autofsinstead, and it works. Thanks.