-
Notifications
You must be signed in to change notification settings - Fork 146
Using PMS on systemd with mounted filesystems
If you're on a system using systemd, it's easy to add a dependency to ensure that any mounted filesystems that PMS will need to access are available before PMS actually starts. This is especially useful if you have a remote NFS/SMB filesystem for your media collection that sometimes isn't available, and you don't want PMS to start up without it.
In this example, we'll assume that your media files for Plex are mounted at /mounts/foo
on your system. To start, we need to identify which mount
to refer to. Just run systemctl status /mounts/foo
and look in the first line in the output:
$ systemctl status /mounts/foo
● mounts-foo.mount - /mounts/foo
Loaded: loaded (/etc/fstab; generated)
Active: active (mounted) since Sat 2021-07-31 00:32:34 EDT; 14h ago
Where: /mounts/foo
What: mynfsserver:/volume1/foomedia
Docs: man:fstab(5)
man:systemd-fstab-generator(8)
Tasks: 0 (limit: 9505)
Memory: 160.0K
CGroup: /system.slice/mounts-foo.mount
In this case it's mounts-foo.mount
.
Once you have the correct mount(s), it's time to update the plexmediaserver.service
unit with them:
sudo systemctl edit plexmediaserver.service
will open an editor with a blank "override" file that allows you to make your own local changes without affecting the packaged version.
Simply add the following:
[Unit]
After=mounts-foo.mount
Requires=mounts-foo.mount
If you have multiple required mounts, you can list them separated with spaces. For example, to use both /mounts/foo
and /media/bar
, you could enter:
[Unit]
After=mounts-foo.mount media-bar.mount
Requires=mounts-foo.mount media-bar.mount
That's it! Your system is now configured to require any listed mount points before PMS can start.
If you're interested in how this works, we're using a couple of very useful systemd features here: mounts and overrides.
Systemd manages all of your mounts by creating corresponding .mount
units. In a traditional system, part of the system startup process is to run mount -a
at startup, which will read through all of the entries in /etc/fstab
and decide which ones to try mounting.
On a modern systemd system, systemd uses systemd-fstab-generator
to automatically generate mount units for all listed filesystems, and then perform the mounts based on those definitions. To look at the root mount, for example, you can run systemctl cat -- -.mount
(the --
is necessary because the name contains a -
). Fortunately, you don't have to change anything in your workflow since systemd is smart enough to recreate the units if you make any changes in your fstab
.
Since these units are already available, we can now use them to establish a dependency between PMS and the mount by using an override. An override is simply a local unit file which gets added on top of the system unit file and either adds or overrides things that were already defined. For example, if you have a systemd unit which specifies that a service will run as the user foo
, but instead you want it to run as user bar
, you can just create an override for the unit which contains:
[Service]
User=bar
The original system definition will still have User=foo
, but your override takes precedence. It's possible to manually create override files and just place them in /etc/systemd/system/<unit name>.d/
, so if you have multiple systems you may just want to copy that file to each system and drop it in the appropriate directory. In the example above we used systemctl edit <unit name>
because it will automatically create the necessary directory and place the file in it (along with some other sanity checks).
In our case, the override provides two additional directives to plexmediaserver.service
, After
and Requires
. These essentially do what it sounds like. The After
directive simply tells systemd that plexmediaserver.service
should only be started After
mounts-foo.mount
in cases where both are being started (like at system boot).
The Requires
directive just tells systemd that PMS won't work without the required units. If possible, systemd will start the required units first and then start PMS. If the required unit doesn't start up for whatever reason then PMS won't be started.