[Guide] Auto-Mounting Filesystems in Linux

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.

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 it should end with a line similar to Processing triggers for libc-bin. If you get a different message, post the message in this thread and do not proceed.
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
      
      • Adjust the path (/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 I would highly recommend using a dedicated suffolder like /mnt/nfs insead 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 press Enter to save. Then, press Ctrl+X to close nano

    • 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.1.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, the tv folder will be mounted inside the /mnt/nfs folder I entered in step 1.

        • The parameters in the middle should be left alone

        • The last parameter (192.168.1.10:/mnt/user/tv) is the IP and path of the remote NFS filesystem. Adjust it as necessary, following the pattern <ip_address>:<path/to/folder> (note the colon separating IP and path is required).

        • Add additional lines for every NFS path you want auto-mounted. Once you’ve added all the paths, 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 paths 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 (replacing tv 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.
    • 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
3. Retart 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 shows active (running) then you can continue. If it shows inactive or anything else, double-check the auto.master and auto.nfsdb files to verify that they don’t contain any spelling errors in the lines you added.
  • 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 paths and parameters were all spelled correctly, then restart the autofs service again and repeat this step.

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.

  • One quick note, if you browse the parent folder (/mnt/nfs in my example) immediately after reboot, you may notice that the tv folder doesn’t show up. The autofs folders are created automatically as soon as they are accessed for the first time, but they don’t get created until that first access.

5 Likes

Just went through this guide successfully and there are two things I want to emphasize:

  1. It is critical that what ever path you put in /etc/auto.master for the parent folder ends in a / is it does not this will not work.
  2. It is critical that if you are currently using an fstab set up that you unmount them per the instructions otherwise they will still be running on fstab even if you reboot.
  3. It is critical that if you are currently using an fstab set up that you use a different folder inside of /mnt to do your new mounts

These things are more or less stated above but I wanted to be double sure everyone avoids my trials and tribulations

Here’s what I’m getting on step zero:

All of those operations succeeded properly. It looks like you had the support libraries installed already, so it just had to install autofs itself. You’re good to proceed with step 1.

1 Like

Thank you. I followed your “Migrating Plex Metadata from Unraid to Ubuntu” guide, so are there any tweaks I need to make from the guide above?

I have my stuff in “/mnt/user/media” in Unraid and mounted as “/media” in Ubuntu for Plex.

If you migrated your metadata, you’re going to need to do a couple extra steps. Autofs needs exclusive access to whatever root folder you point it at. This is fine if you’re pointing it at a subfolder, like the /mnt/nfs folder I use above, but that won’t work if Plex is expecting the media to be in folders directly under the root folder /. The good news is there’s an easy workaround in symbolic links. Follow the guide above as normal, throwing your autofs mounts in /mnt/nfs or whatever subfolder you want under /mnt. Test the mounts to make sure they work, and then, before you reboot, perform the following step for each path you added to autofs:

sudo ln -s /mnt/nfs/tv /tv (Substitute your paths as necessary)

Check that they work by typing ls /tv and verify that your TV folders are listed properly. After you reboot, run the same command again as a final check.

having a new issue and need a hand from the masters… i set this up last year and all was working perfectly until maybe a month ago.

i have sonarr/radarr/lidarr on my anniversary build server, and plex on a separate box running ubuntu, as desrcibed by jdm in the transcode post. shares are mounted within ubuntu as per this thread. all was working perfectly but the other week i noticed that the TV episodes stopped pulling into plex automatically. only the tv shows. movies and music worked fine. but on reboot, tv shows would import correctly into plex.

sonarr is working perfectly fine, as is plex – i’ve posting about them in their respective forums and the logs look good. i’ve traced the problem (i think) to the automounting file system.

This is weird: tv shows stream fine and I’m able to view them without any issue at all. But when I check all the shares in /mnt on ubuntu, the /tv share isn’t there! But then I run an ls /mnt command and all three media directories are present but the /tv share is either missing or i cannot cd /mnt/tv into it.

Looks like when I reboot /mnt/tv is there and an ls command works fine. But after a while I lose it.

No clue how or why this is happening, since all 3 directories are located Within the same unraid share and they’re all streaming fine.

any thoughts?

i’ve retraced all my steps using this post and checked that all settings in fstab and autofs are correct, which they are. any help would be greatly appreciated.

I haven’t experience your issue, so I don’t have a quick answer for you, but I do have a couple suggestions. First, disable the automatic library scan in Plex, and configure Sonarr, Radarr and Lidarr to use Plex Connect to notify Plex when new media is added. To enable Plex Connect, follow these steps in Sonarr, Radarr and Lidarr:

  1. Go to Settings > Connect, and click the add button.
  2. Select Plex Media Server from the available options.
  3. Enter a name for your server at the top
  4. Enter the IP or hostname for your Plex server in the hostname field. Leave all other settings at their defaults.
  5. Click the Authenticate with Plex.tv button and log in to your Plex account.
  6. Click the Test button
  7. If the test was successful, save the connection.

After you’ve enabled the connections, go into your Plex server settings, and disable the options to Scan my library automatically and Run a partial scan when changes are detected. You can leave the periodic scan enabled, and set it to run daily.

Run your system like this for a few days and see if the issue comes up again. If it does, then I’d recommend enabling debug logging for autofs temporarily so the logs can hopefully be used to pinpoint the issue. To enable logging, open the file /etc/autofs.conf in your text editor, scroll down until you see # logging = none, and change it to logging = debug. Save the file, then restart the autofs daemon sudo systemctl restart autofs. Once debug logging is enabled, everything autofs does will be logged in /var/log/syslog, hopefully including whatever is causing the issue. If it comes to this point, please note that the syslog file isn’t used exclusively by autofs, so the relevant information may be scattered among log lines from other programs. I wouldn’t recommend posting the log file anywhere without filtering it first to display only the automount logs.

thanks mthrboard! will make those changes and report back!

If you are like me and named your unraid Share “TV Shows” you will need to take an extra step in your auto fs command. that hinders you from automounting it.

Replace tv with below:
“TV\ Shows”

I have migrated successfully to autofs on my system. Interestingly enough, I originally wanted to do this because I was getting “stale file handle” when using fstab. Unfortunately, I am still having the same issue.

In checking the logs, I see the following entries repeatedly:

Apr 9 12:49:51 dionysus kernel: [684639.585302] fsid 0:73: expected fileid 0x901000025857ba7, got 0x27000011a31bce
Apr 9 12:49:51 dionysus kernel: [684639.585797] NFS: server 192.168.86.2 error: fileid changed
Apr 9 12:49:51 dionysus kernel: [684639.585797] fsid 0:73: expected fileid 0x901000025857ba7, got 0x27000011a31bce

I currently trying to figure this out but wondering if this has to do with using cache and then moving to the array? Does anyone have any experience with this?

If you run into stale stale file handlers or similar things, read up on the unraid forums about this hot mess.

The TL;DR; fix: Settings > Global Shares Settings: Shut down array, switch Tunables Hard Links to No.

I’m testing a resolution which is to disable Hard Links on NFS Shares in Unraid 6.8. In the next minor release, there is a slight promise of seeing Hard Links per NFS share.

2 Likes