NFS mount via systemd
Sometimes you feel like fstab, sometimes you don't.
If you're running modern Linux and are wanting to invest all of your brain power in a systemd thing-a-me and you're wishing there's was systemd way to mount a file-system(s), you're in luck!
In this post I'm going to quickly cover how to mount an NFS export using nothing more than systemd mount unit on Ubuntu 16.04.
Do you want to mount an FS?
The first thing to do is get the NFS mount exported and the relevant options required to make it all go noted. In my case, I've got NFS4 running on my server and I've exported one path such that only my little Brix is able to attach to it. I'm not covering setting up an NFS server within this post.
Here's what my export looks like on my NFS server.
As you can see, one server is permitted to attach to the path
/mnt/things using a single IP address. For more information on the options being used please refer to the NFS documentation found here.
On the client host
install some packages.
# apt-get install nfs-client
nfs kernel module.
# modprobe nfs
I'm also going to make loading the
nfs kernel module persistent across reboots.
# echo NFS | tee -a /etc/modules
Create a systemd
mount unit file
This unit file MUST contain the path within the name using hyphens instead of slashes. In my case, I will be mounting the NFS export at
/mnt/things so my file name will be
mnt-things.mount. The mount file itself will be stored at
/etc/systemd/system/ here's the full path to the file
Within the the mount unit file, add the following sections/entries:
[Unit] Description=Things devices After=network.target [Mount] What=172.16.24.192:/mnt/things Where=/mnt/things Type=nfs Options=_netdev,auto [Install] WantedBy=multi-user.target
These few lines give the unit file a description, ensure that it's only started after networking is available, mounts the NFS export
/mnt/things using an
nfs type, and makes sure the mount is ready before starting any other machines (VMs, Containers, etc).
Start your mount (service?)!
Reload the systemd daemon.
# systemctl daemon-reload
Start the mount (yes that sounds odd).
# systemctl start mnt-media.mount
Validate that it all works.
If everything is working you should be able to begin using the mount at
/mnt/things and browsing the filesystem. You can also check the status of the mount using the following:
# systemctl status mnt-things.mount ● mnt-things.mount - /mnt/things Loaded: loaded Active: active (mounted) since Sun 2017-05-21 20:56:44 CDT; 22h ago Where: /mnt/things What: 172.16.24.92:/mnt/things May 21 20:56:44 carterhousekodi systemd: Mounting Things devices...
You'll notice that I omitted the directory creation command (
mkdir -p /mnt/things) for the mount point on the client node, this was intentional. There's no need to create the path when mounting filesystems using systemd, if the
Where path defined in the mount unit file does not exist systemd will create it Automagically.
That's all folks
Just a quick aside on the power of systemd, the init system, and it's ability to replace the simplist of things in an administators everyday life, like mounting a filesystem.