]> git.ipfire.org Git - thirdparty/dracut-ng.git/commitdiff
refactor(70crypt): extract luks_open_interactive into crypt-lib
authorNadzeya Hutsko <nadzeya.hutsko@canonical.com>
Fri, 10 Apr 2026 18:50:46 +0000 (20:50 +0200)
committerNeal Gompa (ニール・ゴンパ) <ngompa13@gmail.com>
Sat, 11 Apr 2026 15:44:45 +0000 (11:44 -0400)
Move the interactive LUKS open logic into a shared
luks_open_interactive function, allowing overlayfs-crypt to reuse it.

modules.d/70crypt/crypt-lib.sh
modules.d/70crypt/cryptroot-ask.sh

index 766982821625c656cee98cb2df507252fe012d85..82a672fb604df11b6f306ccb9d1c2c12d5f019f0 100755 (executable)
@@ -135,6 +135,28 @@ ask_for_password() {
     return $ret
 }
 
+# luks_open_interactive dev mapname [prompt] [cryptsetupopts]
+#
+# Prompt for a LUKS passphrase and open the device.  Uses Plymouth if
+# available, otherwise falls back to TTY.  Reads rd.luks.timeout for
+# the passphrase input timeout.
+luks_open_interactive() {
+    local dev="$1" mapname="$2"
+    local prompt="${3:-Password ($dev)}"
+    local cryptsetupopts="${4:-}"
+    local luks_open _timeout
+
+    _timeout=$(getarg rd.luks.timeout)
+    _timeout="${_timeout:-0}"
+    luks_open="$(command -v cryptsetup) $cryptsetupopts luksOpen"
+    ask_for_password \
+        --ply-tries 5 \
+        --ply-cmd "$luks_open -T1 $dev $mapname" \
+        --ply-prompt "$prompt" \
+        --tty-tries 1 \
+        --tty-cmd "$luks_open -T5 -t $_timeout $dev $mapname"
+}
+
 # Try to mount specified device (by path, by UUID or by label) and check
 # the path with 'test'.
 #
index edae9cdab91c41873c87fc114d72c7ac080220f6..952a5e71d3fae9d8ba122175da3ced12f7230433 100755 (executable)
@@ -78,7 +78,7 @@ asked_file=/tmp/cryptroot-asked-$luksname
 # load dm_crypt if it is not already loaded
 [ -d /sys/module/dm_crypt ] || modprobe dm_crypt
 
-command -v ask_for_password > /dev/null || . /lib/dracut-crypt-lib.sh
+command -v luks_open_interactive > /dev/null || . /lib/dracut-crypt-lib.sh
 
 #
 # Open LUKS device
@@ -175,16 +175,7 @@ else
 fi
 
 if [ $ask_passphrase -ne 0 ]; then
-    luks_open="$(command -v cryptsetup) $cryptsetupopts luksOpen"
-    _timeout=$(getarg "rd.luks.timeout")
-    _timeout=${_timeout:-0}
-    ask_for_password --ply-tries 5 \
-        --ply-cmd "$luks_open -T1 $device $luksname" \
-        --ply-prompt "Password ($device)" \
-        --tty-tries 1 \
-        --tty-cmd "$luks_open -T5 -t $_timeout $device $luksname"
-    unset luks_open
-    unset _timeout
+    luks_open_interactive "$device" "$luksname" "Password ($device)" "$cryptsetupopts"
 fi
 
 if [ "$is_keysource" -ne 0 ] && [ "${luksname##luks-}" != "$luksname" ]; then