How to start service in systemd right after cryptsetup

systemd

I have an ArchLinux installation with systemd. I am trying to run a hook immediately after my encrypted data device is mounted in order to put flashcache on top of it. I have scripts that work, however I cannot make them load in right order. Here's what I did:

file: /usr/lib/systemd/system/flashcache.service

[Unit]
Description=FlashCache
Requires=systemd-cryptsetup@cdata.service

[Service]
Type=oneshot
RemainAfterExit=no
ExecStart=/usr/local/bin/flashcache_startup_script

[Install]
WantedBy=dev-mapper-data_cached.device

After creating file, I ran systemctl enable flashcache.service.

However, on boot it does not start, the dev-mapper-data_cached times out, after which I can get into a root shell and run flashcache_startup_script manually.

I examined output of journalctl -b, and determined that FlashCache service does not load before timeout occurs.

Another option I tried without success is Requires=dev-mapper-vg1-data.device

What am I doing wrong?

Best Answer

I tried all sorts of things, mainly using following resource. I am not exactly sure why it worked, but following unit file solved the issue:

[Unit]
Description=FlashCache
BindsTo=dev-vg1-data.device
After=dev-vg1-data.device
DefaultDependencies=no

[Service]
Type=oneshot
RemainAfterExit=no
ExecStart=/usr/local/bin/flashcache_startup_script

[Install]
WantedBy=sysinit.target