luks: improve idempotency for enrolling FIDO2

Using FIDO2 automatically generates a temporary password for setting up LUKS
which is not available on subsequent runs of the script causing the format
script to crash with `password: unbound variable`.
This commit is contained in:
Michael Hoang committed 2026-05-20 00:07:06 +02:00
1 parent 65fb947964
commit d60b2b5e00
1 file changed
+35 -29
+35 -29
View File
@@ -236,44 +236,50 @@ in
${toString ( ${toString (
lib.forEach config.additionalKeyFiles (keyFile: '' lib.forEach config.additionalKeyFiles (keyFile: ''
cryptsetup luksAddKey "${config.device}" ${keyFile} ${formatKeyFileArgs} if ! cryptsetup open --test-passphrase --key-file=${keyFile} "${config.device}" 2>/dev/null; then
cryptsetup luksAddKey "${config.device}" ${keyFile} ${formatKeyFileArgs}
fi
'') '')
)} )}
${lib.optionalString config.enrollRecovery '' ${lib.optionalString config.enrollRecovery ''
systemd-cryptenroll \ if ! systemd-cryptenroll "${config.device}" 2>/dev/null | grep -qw recovery; then
--recovery-key \ systemd-cryptenroll \
--unlock-key-file=${formatKeyFile} \ --recovery-key \
"${config.device}" --unlock-key-file=${formatKeyFile} \
if [ -z ''${IN_DISKO_TEST+x} ]; then "${config.device}"
set +x; read -p "Press Enter when you scanned the QR code offscreen or that the recovery key is stored securely."; set -x if [ -z ''${IN_DISKO_TEST+x} ]; then
set +x; read -p "Press Enter when you scanned the QR code offscreen or that the recovery key is stored securely."; set -x
fi
fi fi
''} ''}
${lib.optionalString config.enrollFido2 '' ${lib.optionalString config.enrollFido2 ''
wait_for_token() { if ! systemd-cryptenroll "${config.device}" 2>/dev/null | grep -qw fido2; then
set +f wait_for_token() {
echo "Waiting for FIDO2 token insertion..." set +f
echo "Waiting for FIDO2 token insertion..."
# Check if any FIDO2 device is available via /dev/hidraw* # Check if any FIDO2 device is available via /dev/hidraw*
while true; do while true; do
if ls /dev/hidraw* &>/dev/null; then if ls /dev/hidraw* &>/dev/null; then
echo "FIDO2 device detected." echo "FIDO2 device detected."
break break
else else
echo "FIDO2 device not detected, waiting..." echo "FIDO2 device not detected, waiting..."
sleep 2 sleep 2
fi fi
done done
set -f set -f
} }
wait_for_token wait_for_token
systemd-cryptenroll \ systemd-cryptenroll \
--fido2-device=auto \ --fido2-device=auto \
''${SLOT_ZERO_TO_DELETE:+--wipe-slot=0} \ ''${SLOT_ZERO_TO_DELETE:+--wipe-slot=0} \
--unlock-key-file=${formatKeyFile} \ --unlock-key-file=${formatKeyFile} \
${toString config.extraFido2EnrollArgs} \ ${toString config.extraFido2EnrollArgs} \
"${config.device}" "${config.device}"
fi
''} ''}
${lib.optionalString (config.content != null) config.content._create} ${lib.optionalString (config.content != null) config.content._create}
''; '';