2aacef
From 138a7b2bc72a50ad7c590db3b638547dc2810e47 Mon Sep 17 00:00:00 2001
2aacef
From: Frantisek Sumsal <frantisek@sumsal.cz>
2aacef
Date: Wed, 22 Feb 2023 16:43:42 +0100
2aacef
Subject: [PATCH] test: add coverage for #24177
2aacef
2aacef
Original issue: https://bugzilla.redhat.com/show_bug.cgi?id=1985288
2aacef
2aacef
(cherry picked from commit 6299b6e5e6df32516fcaba9a93d966bad9043748)
2aacef
2aacef
Related: #1985288
2aacef
---
2aacef
 test/units/testsuite-64.sh | 38 +++++++++++++++++++++++++++++++++++++-
2aacef
 1 file changed, 37 insertions(+), 1 deletion(-)
2aacef
2aacef
diff --git a/test/units/testsuite-64.sh b/test/units/testsuite-64.sh
2aacef
index 4017f61f59..201a673d06 100755
2aacef
--- a/test/units/testsuite-64.sh
2aacef
+++ b/test/units/testsuite-64.sh
2aacef
@@ -417,7 +417,7 @@ testcase_lvm_basic() {
2aacef
     lvm vgs
2aacef
     lvm vgchange -ay "$vgroup"
2aacef
     lvm lvcreate -y -L 4M "$vgroup" -n mypart1
2aacef
-    lvm lvcreate -y -L 8M "$vgroup" -n mypart2
2aacef
+    lvm lvcreate -y -L 32M "$vgroup" -n mypart2
2aacef
     lvm lvs
2aacef
     udevadm wait --settle --timeout="$timeout" "/dev/$vgroup/mypart1" "/dev/$vgroup/mypart2"
2aacef
     mkfs.ext4 -L mylvpart1 "/dev/$vgroup/mypart1"
2aacef
@@ -461,6 +461,42 @@ testcase_lvm_basic() {
2aacef
     helper_check_device_symlinks "/dev/disk" "/dev/$vgroup"
2aacef
     helper_check_device_units
2aacef
 
2aacef
+    # Do not "unready" suspended encrypted devices w/o superblock info
2aacef
+    # See:
2aacef
+    #   - https://github.com/systemd/systemd/pull/24177
2aacef
+    #   - https://bugzilla.redhat.com/show_bug.cgi?id=1985288
2aacef
+    dd if=/dev/urandom of=/etc/lvm_keyfile bs=64 count=1 iflag=fullblock
2aacef
+    chmod 0600 /etc/lvm_keyfile
2aacef
+    # Intentionally use weaker cipher-related settings, since we don't care
2aacef
+    # about security here as it's a throwaway LUKS partition
2aacef
+    cryptsetup luksFormat -q --use-urandom --pbkdf pbkdf2 --pbkdf-force-iterations 1000 \
2aacef
+                          "/dev/$vgroup/mypart2" /etc/lvm_keyfile
2aacef
+    # Mount the LUKS partition & create a filesystem on it
2aacef
+    mkdir -p /tmp/lvmluksmnt
2aacef
+    cryptsetup open --key-file=/etc/lvm_keyfile "/dev/$vgroup/mypart2" "lvmluksmap"
2aacef
+    udevadm wait --settle --timeout="$timeout" "/dev/mapper/lvmluksmap"
2aacef
+    mkfs.ext4 -L lvmluksfs "/dev/mapper/lvmluksmap"
2aacef
+    udevadm wait --settle --timeout="$timeout" "/dev/disk/by-label/lvmluksfs"
2aacef
+    # Make systemd "interested" in the mount by adding it to /etc/fstab
2aacef
+    echo "/dev/disk/by-label/lvmluksfs /tmp/lvmluksmnt ext4 defaults 0 2" >>/etc/fstab
2aacef
+    systemctl daemon-reload
2aacef
+    mount "/tmp/lvmluksmnt"
2aacef
+    mountpoint "/tmp/lvmluksmnt"
2aacef
+    # Temporarily suspend the LUKS device and trigger udev - basically what `cryptsetup resize`
2aacef
+    # does but in a more deterministic way suitable for a test/reproducer
2aacef
+    for _ in {0..5}; do
2aacef
+        dmsetup suspend "/dev/mapper/lvmluksmap"
2aacef
+        udevadm trigger -v --settle "/dev/mapper/lvmluksmap"
2aacef
+        dmsetup resume "/dev/mapper/lvmluksmap"
2aacef
+        # The mount should survive this sequence of events
2aacef
+        mountpoint "/tmp/lvmluksmnt"
2aacef
+    done
2aacef
+    # Cleanup
2aacef
+    umount "/tmp/lvmluksmnt"
2aacef
+    cryptsetup close "/dev/mapper/lvmluksmap"
2aacef
+    sed -i "/lvmluksfs/d" "/etc/fstab"
2aacef
+    systemctl daemon-reload
2aacef
+
2aacef
     # Disable the VG and check symlinks...
2aacef
     lvm vgchange -an "$vgroup"
2aacef
     udevadm wait --settle --timeout="$timeout" --removed "/dev/$vgroup" "/dev/disk/by-label/mylvpart1"