From 32b4ed1987c8dabc864e4fe3ef6fe976ca5aed2b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Old=C5=99ich=20Jedli=C4=8Dka?= <oldium.pro@gmail.com>
Date: Sat, 9 Nov 2024 10:20:43 +0100
Subject: [PATCH] dracut: fix running with pre-v103 Dracut
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Dracut v103+ moved SystemD cryptsetup code into a separate module, so
detect it and when found, use it.

Clevis units do not depend on `cryptsetup.target`, so do not install it.
The target `cryptsetup.target` is installed by both
[90systemd-cryptsetup][1] in v103+ and [01systemd-cryptsetup][2] in v105+,
so there is no need to install it by Clevis.

[1]: https://github.com/dracut-ng/dracut-ng/blob/3fce598fb45aa5618cdf885eb48cf327104ffcb8/modules.d/90systemd-cryptsetup/module-setup.sh#L53
[2]: https://github.com/dracut-ng/dracut-ng/blob/a2669c447bd5e0406f55efcc8c9c58be89458b08/modules.d/01systemd-cryptsetup/module-setup.sh#L54

Signed-off-by: Oldřich Jedlička <oldium.pro@gmail.com>
---
 src/luks/dracut/clevis/module-setup.sh.in | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/luks/dracut/clevis/module-setup.sh.in b/src/luks/dracut/clevis/module-setup.sh.in
index 55f32818..ef8770da 100755
--- a/src/luks/dracut/clevis/module-setup.sh.in
+++ b/src/luks/dracut/clevis/module-setup.sh.in
@@ -21,7 +21,13 @@
 depends() {
     local __depends=crypt
     if dracut_module_included "systemd"; then
-        __depends=$(printf '%s systemd-cryptsetup' "${__depends}")
+        # Dracut v103 introduced a separate systemd-cryptsetup module
+        systemd_cryptsetup_dir=$(dracut_module_path "systemd-cryptsetup")
+        if [ -d "$systemd_cryptsetup_dir" ]; then
+            __depends=$(printf '%s systemd-cryptsetup' "${__depends}")
+        else
+            __depends=$(printf '%s systemd' "${__depends}")
+        fi
     fi
     echo "${__depends}"
     return 255
@@ -32,7 +38,6 @@ install() {
         inst_multiple \
             $systemdsystemunitdir/clevis-luks-askpass.service \
             $systemdsystemunitdir/clevis-luks-askpass.path \
-            $systemdsystemunitdir/cryptsetup.target \
             @SYSTEMD_REPLY_PASS@ \
             @libexecdir@/clevis-luks-askpass