I want systemd on mount /mnt/test
to automatically call a program (in real life cryptsetup
to unlock the underlying device, for testing here echo
) before the file system is mounted and after it is unmounted.
With /etc/systemd/system/stickbak-encryption.service
:
[Unit]
Description=stickbak encryption
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
and /etc/fstab
(partly):
/dev/$DEVICE /mnt/test auto noauto 0 0
this works (after daemon reload and enabling the service) for systemctl start mnt-test.mount
and respectively systemctl stop mnt-test.mount
(as root).
On mount /mnt/test
, however, systemctl status mnt-test.mount stickbak-encryption
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 honoured when /bin/mount
is called as well? The status of the mount unit shows that mount /mnt/test
seems to be translated to ExecMount=/bin/mount /dev/$DEVICE /mnt/test -t auto -o noauto
, so apparently systemd gets notified.
Best Answer
I very recently asked myself the same question, but I quickly came to the realisation that it doesn't work that way.
When you use the
mount
command-line program, systemd is not involved:mount
reads/etc/fstab
(or takes options from the command-line) and mounts the device. When you start a systemd mount unit, it's parsed by systemd which internally uses mount system calls to perform the mount.So there is no way to systemd to get involved when you use
mount
.As an aside, there is an interesting difference between using
mount
and systemd in what they accept as valid in/etc/fstab
. Systemd parses the file and creates mount units that it then uses. When it does so, it accepts fewer parameters thanmount
requires.If you use systemd then you only have to supply the device and mount point, however
mount
requires further options such as the file system and options.