[systemd-devel] How to add a dependency to a systemd.mount that is activated by /bin/mount?

Tom Gundersen teg at jklm.no
Fri Sep 27 07:11:13 PDT 2013


On Wed, Sep 25, 2013 at 3:11 AM, Tim Landscheidt <tim at tim-landscheidt.de> wrote:
> on Fedora 19/systemd 204, I want systemd on "mount
> /mnt/test" by a non-root user to automatically call a pro-
> gram as root (in real life cryptsetup to unlock the underly-
> ing device, for testing here echo) before the file system is
> mounted and after it is unmounted.
>
> With /etc/systemd/system/mount-wrapper.service:
>
> | [Unit]
> | Description=mount wrapper
> | Before=mnt-test.mount
> | StopWhenUnneeded=true
>
> | [Service]
> | Type=oneshot
> | ExecStart=/bin/echo Unlock device.
> | RemainAfterExit=true
> | ExecStop=/bin/echo Lock device.
>
> | [Install]
> | RequiredBy=mnt-test.mount
>
> /etc/fstab (partly):
>
> | /dev/$DEVICE /mnt/test auto noauto,user 0 0
>
> and (with or without) /etc/systemd/system/mnt-test.mount:
>
> | [Unit]
> | Description=mount test
> |
> | [Mount]
> | What=/dev/$DEVICE
> | Where=/mnt/test
> | Options=noauto,user
>
> this works (after daemon reload and enabling the service)
> for "systemctl start mnt-test.mount" and "systemctl stop
> mnt-test.mount" respectively (as root).
>
> On "mount /mnt/test", however, "systemctl status
> mnt-test.mount mount-wrapper" shows the latter service being
> "inactive (dead)", while the former is "active (mounted)".
>
> How can I (or can I not?) set up a dependency that is hon-
> oured when /bin/mount is called as well?  The status of the
> mount unit shows that "mount /mnt/test" seems to be trans-
> lated to "ExecMount=/bin/mount /dev/$DEVICE /mnt/test -t
> auto -o noauto,user", so apparently systemd gets notified.

It appears we have an issue with manual mounts (i.e., units generated
from /proc/self/mountinfo) do not correctly pick up already existing
unit files. That said, there is no way to get a service ran Before a
manual mount, as systemd is only alerted to the mount after it
happened. So for this case I suggest using "systemctl enable" instead
(but see below).

> P. S.: If there is a "more systemd" way to handle removable
>        encrypted media, I'm all ears :-).  I've seen
>        /etc/crypttab & Co., but it seems to be focused on
>        disks that are unlocked at boot and remain enabled
>        till shutdown.

Using crypttab is the way to do it, it should also work (with the
correct options) for devices not available at boot.

-t


More information about the systemd-devel mailing list