You are not logged in.

#1 2016-01-10 13:19:19

EdeWolf
Member
Registered: 2016-01-06
Posts: 79

[Solved] Starting unit files upon device creation

Originally this started out as a simple, slight off topic question in another thread, where I already got some help, but this issue seems to grow and I do not want to hijack an already sovled thread any more, as I don't move on.

So, until my move to arch I decrypted and mounted plugged in drives via a a RUN script in udev.rules. Worked flawlessly for years until my migration to arch/systemd. Got already an explanation for that and therefore I am now trying to migrate that to systemd. And I am afraid I am having severe understanding issues.

Basically, when an external drive has been plugged in, a link is created by udev below /dev/mnt/n, and when this happens, a unit file shall be fired off. Because "n" also equals a certain mountpoint - each drive has its dedicated own below /mnt - and I do not want to wirte a seperate unit file for each drive/stick, I want to pass the devicename "n" on to the unit file.

But before we get into the complicated part with the templates, I do not even manage to start a unit file upon insertion of the drive. Here's my first try:

# cat /etc/systemd/system/testmount.service 
[Unit]
Description=Decrypts & mounts Hardrive
Requisite=dev-mnt-test.device
After=dev-mnt-test.device

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/bash -c 'echo "Hello World" > /tmp/mounttest.txt'

But, nothing happens, the status I get is identical wether the device is present or not. Status is loaded/inactive (dead).
So it either does not get triggered by udev or is not listening permanently, wether the device in Requisite will eventually appear.

# systemctl status dev-mnt-test.device
● dev-mnt-test.device - ST3250823AS
   Follow: unit currently follows state of sys-devices-pci0000:00-0000:00:1c.0-0000:05:00.0-0000:06:05.0-0000:08:00.0-ata8-host7-target7:0:0-7:0:0:0-block-sde.device
   Loaded: loaded
   Active: active (plugged) since So 2016-01-10 14:02:55 CET; 7s ago
   Device: /sys/devices/pci0000:00/0000:00:1c.0/0000:05:00.0/0000:06:05.0/0000:08:00.0/ata8/host7/target7:0:0/7:0:0:0/block/sde

# systemctl status testmount
● testmount.service - Decrypts & mounts Hardrive
   Loaded: loaded (/etc/systemd/system/testmount.service; static; vendor preset: disabled)
   Active: inactive (dead)

What am I missing? I do not have any [Install] part, as this shall not be part of any target or service, just run unconditionally when the device has been plugged in.


[EDIT]
Adding: While writing this I came to the idea of adding:

[Install]
WantedBy=dev-mnt-test.device

and now it works, although it does not make any sense to me at all, as this unit file shall not be a dependency of the device creation process, this seems rather to be a circular dependency: This unit requires the device, but in turn the device wants this service. But, being PID1, it does not need any logic I guess. And this seems to work so far.

So back to the main issue: What do have I to do to pass on the the device name (so the test of dev-mnt-test) on to the unit file to have a base to create a template?

Last edited by EdeWolf (2016-01-11 14:27:42)

Offline

#2 2016-01-11 02:16:45

branch
Member
Registered: 2014-03-16
Posts: 209

Re: [Solved] Starting unit files upon device creation

Did you try renaming the service file to testmount@.service and replacing all instances of "dev-mnt-test.device" with "dev-mnt-%I.device"? Of course you would then need to enable each instance with

systemctl enable testmount@n.service

where "n" is the mount point.

Offline

#3 2016-01-11 09:06:30

tom.ty89
Member
Registered: 2012-11-15
Posts: 897

Re: [Solved] Starting unit files upon device creation

WantedBy merely when dev-mnt-test.device is loaded, systemd will try to start the service as well. FWIW there is also a RequiredBy, read about the difference in systemd.unit(5).

Offline

#4 2016-01-11 14:27:11

EdeWolf
Member
Registered: 2016-01-06
Posts: 79

Re: [Solved] Starting unit files upon device creation

branch wrote:

systemctl enable testmount@n.service

How embarassing. Of course, I can/have to to enable different mount services, one for earch drive. I was trying to figure out how to pass on the device name from an override of the device unit name, kind of like an argument passed on to a script, but completely missed the right use of templates, which is to instancing the follow up script.

Thanks for the eye opener! Sure not as comfortable as what I was hoping to find, as I still have to enable an instance for each drive, instead of enabling a single unit file and have the rest done automagically by udev passing on its names, but probably as good as it gets. And totally acceptable. Since I have to edit udev rules for a new device anyway, I also can create a new instance then.

And I even can pass on the instance on to a script, did not expect this at all and makes life much easier:

[Unit]
Description=Decrypts & mounts Hardrive
Requires=key.service
Requisite=%i.device
After=%i.device

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/bash -c 'echo "%i" > /tmp/mounttest.txt'

[Install]
WantedBy=%i.device
# cat /tmp/mounttest.txt 
dev-mnt-test

Offline

Board footer

Powered by FluxBB