autofs everything

By | 14 Jul 2024

I usually have a few SMB shares from my NAS mounted on my Raspberry Pis. However, I rarely need to actually use them and scripts only need them once a day (backup). So I figured: Why let the OS bother with keeping those mounts active all day long when there’s a better way?

Cue autofs.

A simple sudo apt install autofs is all it needs to get it installed. It already comes with this line in /etc/auto.master:

+dir:/etc/auto.master.d

This means it will load additional configs from /etc/auto.master.d – so that’s where I had to add my own. However, this is not the actual definition of mounts but the main entry pointing to the config file containing the actual mounts.

My /etc/auto.master.d/mbirth.autofs looks like this:

/-    /etc/auto.mbirth      browse,--timeout 30

The /- in the beginning has autofs NOT create an indirect mount point. Normally, you’d have this indirect mount point, e.g. /smb, and autofs would populate all mounts below that. This is great for larger setups or where the available mounts change often. But not for me.

The second parameter points to the map file and after that there are additional options. browse has autofs create the mount point folders. (In older versions, this option was called ghost.) Normally, those wouldn’t be visible in the file system unless you try to explicitly access them. I guess this is to prevent a mounting frenzy if there’s a tool scanning the whole filesystem.

And now to the important file: /etc/auto.mbirth:

/mnt/mbirth    -fstype=smb3,credentials=/etc/cred/mynas    ://nas/mbirth

This will make a folder mbirth show up in /mnt and once you enter it, it will mount //nas/mbirth using smb3 with the credentials in the given file. You can add more parameters to mount.smb3, if you like. After 30 seconds of no activity, the share will be unmounted again in the background.

Just be aware that mounting the share upon the first request takes a little time. So if you have some task that regularly needs something from the storage, either adjust the timeout or maybe this isn’t a good solution for your use case.

Leave a Reply