Start a systemd service after a device has been mounted

6

1

I'm writing a systemd's .service file for minidlnad (DLNA server) under CentOS 7. Since my media file collection is hosted on a RAID array, I need that array to be mounted BEFORE the minidlnad server is started. Both the RAID array and minidlnad should be started on boot. Is there a way to do this in systemd?

Marcello

Posted 2015-06-22T01:12:41.867

Reputation: 173

Answers

2

You need to adjust dependencies in your .service's Unit section:

On centos:

Requires=mdmonitor.service local-fs.target
After=mdmonitor.service local-fs.target

On other distrib, it could be:

Requires=mdadm.service local-fs.target
After=mdadm.service local-fs.target

The raid service file should execute /sbin/mdadm --monitor --scan

source:http://www.freedesktop.org/software/systemd/man/systemd.unit.html

maxxvw

Posted 2015-06-22T01:12:41.867

Reputation: 381

Is this enough to be sure that the raid device is also mounted (as specified in /etc/fstab) and not just assembled? – Marcello – 2015-06-22T12:09:39.863

edited to be sure that local-fs.target is already activated – maxxvw – 2015-06-22T12:26:39.310

Apparently I don't have a mdadm.service file in /usr/lib/systemd/system, just mdadm-last-resort@.service, mdadm-grow-continue@.service and mdadm-last-resort@.timer. Should I still put the mdadm.service entry in requires and after? – Marcello – 2015-06-22T12:52:52.870

Sorry don't have a centos installed to try, but it seems that you may have a /usr/lib/systemd/system/mdmonitor.service which is the one you should use, i'll edit my answer – maxxvw – 2015-06-22T13:07:16.510

Yes, I have mdmonitor.service and it does execute that command. Thank you very much! – Marcello – 2015-06-22T13:43:12.450

It should be noted that although this is the selected answer, this is not the preferred systemd way of achieving the objective. For an easier and more robust solution, see my answer.

– Run CMD – 2016-05-01T09:11:30.933

9

systemd has a specific directive for this case, called RequiredMountsFor; see man systemd.directives.

The usage would be RequiresMountsFor=[mountpoint], e.g. RequiresMountsFor=/var.

Run CMD

Posted 2015-06-22T01:12:41.867

Reputation: 508

1I'd disagree with your comment above that "this is not the preferred systemd way". RequiresMountsFor= is ignored if the mount point is marked noauto, so sometimes the explicit Requires target is not only recommended but required. – Auspex – 2016-07-09T20:53:08.610

@Auspex The OP clearly stated that the question relates to system boot. In this regard, the title of the Q may be seen as incomplete, while my answer addressed this scenario. Do you see a case where a noauto option would be used during boot? – Run CMD – 2016-07-12T18:03:16.793

I don't think it's relevant whether "noauto" would be used during boot. You argued with the accepted answer because you claimed it was not the preferred systemd way. I see nothing in the documentation to say that, and reasons why you might want to avoid your method. – Auspex – 2016-07-13T10:52:49.400

1@Auspex I also don't think it's relevant when noauto is used. It's just that the OP's use case suggests that he's not going to use noauto. That's got nothing to do with the documentation, but rather with the scenario of the question. – Run CMD – 2016-07-20T16:05:57.953