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