4.0 KiB
Automounting network drives with NFS
This is the first part of a series of posts about setting up a music server using a NAS and Raspberry Pi. The next part is here.
I have a NAS which supports NFS, which I use to store all of my photos, music and other media on my local network. This gives me OS-independent to all of these files, and frees up drive space on my laptops and desktop - most of which are dual-booted. On Windows it's fairly straightforward to establish a network drive, but on Linux-based systems - at least on the Debian- and Arch- based distros I find myself using - the process is a little more involved.
Here I'll use systemd
to automatically mount a shared folder when they're accessed by a client machine.
There are other ways to do this, but as my machines predimonantly run Debian- or Arch-derived Linux distributions, systemd
is a choice that works for both.
This post is largely based on the description on the ArchWiki.
My NAS' hostname is sleeper-service
, and I'll be mounting the Music
shared folder.
You'll need the appropriate package to mount NFS filesytems.
On Arch Linux, nfs-utils
is what you'll be after.
On Debian, the client pckage is nfs-common
, which may already be installed.
You may also need to configure security on your NAS to allow NFS connections from your local machine's IP.
Initial mount
Before doing anything automatically, we first need to create a systemd
unit to mount the remote filesystem at a path in our local filesystem.
I'll mount the remote folder onto the local path /sleeper-service/Music
.
When creating this file, pay attention to its name, as it's important for it to correspond to the path of the mountpoint.
The correct name can be determined using systemd-escape
- pay attention to escape characters in the output, this caught me out several times.
$ systemd-escape /sleeper-service/Music
-sleeper\x2dservice-Music
$ sudo touch /etc/systemd/system/sleeper\\x2dservice-Music.mount
Don't ask me why systemd
is like this - I think it's silly too.
After creating the unit file, we then need to edit it and fill out some information, specifying where the remote filesystem is and also when we need to initialise it.
Here I used a name instead of an address for the What=
part - I have an entry for sleeper-service
configured in /etc/hosts
, but you can equally use an IP address just as well.
[Unit]
Description=Mount music at boot
[Mount]
What=sleeper-service:/volume1/Music
Where=/sleeper-service/Music
Options=vers=3
Type=nfs
TimeoutSec=30
[Install]
WantedBy=multi-user.target
Once we've created this, we can try to manually mount the shared folder by starting the unit:
$ sudo systemctl start sleeper\\x2dservice-Music.mount
$ ls /sleeper-service/Music
At this stage you ought to see the contents of your shared folder. Next, we want to set up the automount, so that this remote folder is mounted automatically when we try to access it. To do that, we need to first stop/disable the unit we just created:
$ sudo systemctl disable sleeper\\x2dservice-Music.mount
Then, let's create an .automount
unit with the same name as the .mount
file we already have.
The automount unit expects the mount unit to exist alongside it - it doesn't replace it.
$ sudo touch /etc/systemd/system/sleeper\\x2dservice-Music.automount
[Unit]
Description=Automount NAS music
[Automount]
Where=/sleeper-service/Music
[Install]
WantedBy=multi-user.target
Then, enable the new .automount
unit to have it run automatically:
$ sudo systemctl enable sleeper\\x2dservice-Music.automount
The folder should now be automatically mounted at the target location when trying to access it.
As always, thanks for reading and I hope this was helpful. If I got something wrong, or there's an easier way to do it, or you just want to say hi, please don't hesitate to get in touch!