If you’re like me and use NFS to share files between multiple servers, you may occasionally run into a connection issue when you reboot - if the client comes up before the server, it won’t mount the NFS filesystems automatically. If you’re around, you can just remount them manually, but if it happens when you’re away you could be out of luck. One way to make sure this doesn’t happen is to use autofs for your NFS mounts. It’s super easy to set up, and once it’s configured the filesystems will mount and remount automatically whenever they are accessed, as long as the remote system is available.
Before we begin, I should note that autofs will NOT fix stale file handle
errors. That’s a separate issue that is usually caused by hard links on union filesystems. If you get these stale file errors in unraid, you should disable hard links. You can find this setting under Settings
> Global Shares Settings
. Look for Tunable (support Hard Links):
and set it to No
. Once you’ve made this change, reboot both your unraid system and your ubuntu system and you should be back in business.
Prerequisites
- If you haven’t already done so, make sure you disable automatic emptying of the trash in Plex. Go to
Settings
>Library
, and uncheck the optionEmpty trash automatically after every scan
, then scroll down and save your changes. This will prevent Plex from deleting any custom posters or metadata you may have in the event that something goes wrong and Plex can’t find your files.
0. Install autofs
-
Open a terminal and run the following command to install autofs:
sudo apt install -y autofs
- Enter your password if prompted. You will see a bunch of scrolling text, and you may see a warning saying
start and stop actions are no longer supported
, which is fine. In the end, you should see a couple lines starting withProcessing triggers for...
. If you see a warning other than the start/stop warning, or you don’t see the processing triggers lines, take a screenshot of the output and post here or in the tech-support channel on Discord before proceeding.
- Enter your password if prompted. You will see a bunch of scrolling text, and you may see a warning saying
1. Configure autofs
-
You need to edit a couple of configuration files using your favorite text editor. I recommend using nano if you’re new to linux, so these steps will use nano.
-
First, you need to edit the
auto.master
file.- Enter
sudo nano /etc/auto.master
. Once nano opens, use the Page Down or arrow keys on your keyboard to scroll to the bottom. Then, add the following text below the last line in the file:
/mnt/nfs /etc/auto.nfsdb --timeout=0 --browse
-
Adjust the folder (
/mnt/nfs
) to point to the parent folder where you want your NFS shares to mount. In my example, I entered/mnt/nfs
so my NFS volumes will mount inside that, like/mnt/nfs/tv
or/mnt/nfs/movies
. Please note, that you won’t be able to create any folders inside of this folder on your own, so you need to use a dedicated sub-folder like/mnt/nfs
instead of just using the/mnt
folder. -
If you want the filesystem to unmount when it’s not used for a while, you can set the timeout to a value greater than zero, in which case it will auto unmount if there has been no activity for that many seconds. Leaving the timeout at zero disables the automatic unmount.
-
Save the file. In nano, press
Ctrl+O
(Letter O), and then pressEnter
to save. Then, pressCtrl+X
to close nano
- Enter
-
Next, you need to create the file
/etc/auto.nfsdb
.-
Run
sudo nano /etc/auto.nfsdb
-
You should get a blank nano screen where you can just start entering text. This file is where you are going to enter all of the different NFS mounts.
-
For each mount, you will need to add a line similar to the following:
tv -fstype=nfs,ro,timeo=100,noatime 192.168.10.10:/mnt/user/tv
-
There are a couple things you’ll need to adjust here. The first parameter
tv
is the name of the folder to create on your local system. This folder will be created inside the parent folder you defined above. So, in my example, thetv
folder will be mounted inside the/mnt/nfs
folder I entered in step 1. -
The parameters in the middle should be left alone most of the time. The one exception is if you need to write to the folder. If you’re mounting the folder solely for Plex, generally this is not needed. Plex will play videos just fine with read-only access. However, if you are using the DVR functions in Plex, or if you’re mounting a folder for another purpose, you will want read-write access. Enabling read-write access requires you to change the
ro
torw
, example-fstype=nfs,rw,timeo=100,noatime
. But changingro
torw
here isn’t the only thing you need to do. The NFS server needs to export the folder as read-write as well. If you’re using unraid, the easiest method is to export the share as public. Public exports are writeable by anyone who mounts them. If you don’t want this, you’ll need to do a bit more work. Set the export toPrivate
and click Apply. When you do, a new text area will show up for the rule. You need to enter the IPs of the systems you want to have write access to the folder, followed by(rw)
, and separated by spaces, and finally add*(ro)
at the end. For example,192.168.10.10(rw) 192.168.10.20(rw) *(ro)
would give read-write access to 192.168.10.10 and 192.168.10.20, but anyone else who connects will get read-only access. If you’re using another OS as the NFS server, you may need to edit your exports file manually. -
The last parameter (
192.168.10.10:/mnt/user/tv
) is the IP and folder of the remote NFS filesystem. Adjust it as necessary, following the pattern<ip_address>
:<folder/to/folder>
(note the colon separating IP and folder is required). NOTE - folder names in linux are case-sensitive, so make sure you enter the name correctly. TV is not the same as tv, nor is Movies the same as movies. I recommend using all lower-case letters for your share names, but if you’ve already been using upper-case, it can work as long as you match the case exactly. -
Add additional lines for every NFS folder you want auto-mounted. Once you’ve added all the folders, save the file and close nano.
-
-
-
2. Disable and remove the previous mounts
-
Now that the auto-mounting is configured, you’ll need to disable the standard mounting of the folders in
fstab
, if you are re-using the folders for the autofs mounts.-
First, stop any running programs that are accessing files inside these folders. Most likely it will just be Plex. Run
sudo systemctl stop plexmediaserver
to close it. -
Next, unmount the folders. Run
sudo umount /mnt/tv
(replacingtv
with whatever your folder name is), and repeat this for every other folder you have mounted.- If you get an error when you try unmounting any folder, verify that all the programs that were accessing it have been closed. If you’re not sure what’s accessing the folder, enter
sudo lsof | grep /folder/name
to see a list of any processes that have open files in that folder.
- If you get an error when you try unmounting any folder, verify that all the programs that were accessing it have been closed. If you’re not sure what’s accessing the folder, enter
-
Next, comment out the lines you added to
fstab
for the folders.- Run
sudo nano /etc/fstab
and scroll down to the lines you added - Add a
#
symbol in front of each line that you added - Save the file and exit nano
- Run
-
3. Restart the autofs service
-
Now that the folders are unmounted and the configuration files have been updated, you need to restart the autofs service so it will read the new configuration. Run the following command to start the service:
sudo systemctl restart autofs.service
-
Next, verify the service started correctly by running:
systemctl status autofs.service
- It will show a few lines of text, and as long as the
Active:
line showsactive (running)
then you can continue. If it showsinactive
or anything else, double-check theauto.master
andauto.nfsdb
files to verify that they don’t contain any spelling errors in the lines you added.
- It will show a few lines of text, and as long as the
-
Change to one of the folders you want mounted and make sure you can see files in the folder. Run
ls /mnt/nfs/tv
for my example. This may take a few seconds to load the first time, but as long as it shows your files, then you’re nearly done. If it doesn’t show anything, double-check the configuration files above to make sure the folders and parameters were all spelled correctly, then restart the autofs service again and repeat this step.
3.5 Special Note for Plex Libraries
- If you already have your Plex libraries configured with your old folders from the
fstab
file, you will need to do one of two things:- Edit your libraries in Plex for the new folders (follow these same steps for every library you have)
- Add the new folders where your media is located. DO NOT delete the old folder from the library yet.
- Rescan the library if it doesn’t start scanning automatically.
- Once finished scanning, edit the library again and remove the old folder.
- Empty the Plex trash to remove the entries from the old folder
- Create a symbolic link pointing the old folder to the new folder (follow these steps for every library you have)
- In the terminal, enter the following command
ln -s /<new_media_folder> /<old_media_folder>
- For example, if my old folder was
/mnt/tv
and my new folder is/mnt/nfs/tv
I would typeln -s /mnt/nfs/tv /mnt/tv
- For example, if my old folder was
- In the terminal, enter the following command
- Edit your libraries in Plex for the new folders (follow these same steps for every library you have)
4. Reboot and verify your work
- As long as you’ve made it this far, everything should work smoothly on subsequent reboots. While not technically required, I would recommend rebooting the system and checking that the folder mounted properly after the reboot.