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.