Blame SOURCES/0011-sysprep-make-an-effort-to-cope-with-LUKS-on-LVM.patch

7b180b
From d15d829d20c1a0d21da584257c4634517d4271d1 Mon Sep 17 00:00:00 2001
7b180b
From: Laszlo Ersek <lersek@redhat.com>
7b180b
Date: Thu, 14 Jul 2022 12:40:04 +0200
7b180b
Subject: [PATCH] sysprep: make an effort to cope with LUKS-on-LVM
7b180b
7b180b
If the guest disk uses the LUKS-on-LVM scheme, then sysprep has a problem:
7b180b
7b180b
- the "fs-uuids" blockdev operation depends on the decrypted LUKS devices
7b180b
  being open,
7b180b
7b180b
- the "lvm-uuids" blockdev operation depends on the same devices being
7b180b
  closed.
7b180b
7b180b
Attempt to deal with this in "lvm-uuids".
7b180b
7b180b
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2106286
7b180b
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
7b180b
Message-Id: <20220714104005.8334-2-lersek@redhat.com>
7b180b
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
7b180b
(cherry picked from commit 361a447bcb7aef399abad8075ee41197c4071f71)
7b180b
---
7b180b
 sysprep/sysprep_operation_lvm_uuids.ml | 42 +++++++++++++++++++++++++-
7b180b
 1 file changed, 41 insertions(+), 1 deletion(-)
7b180b
7b180b
diff --git a/sysprep/sysprep_operation_lvm_uuids.ml b/sysprep/sysprep_operation_lvm_uuids.ml
7b180b
index c67b21487..5fc623039 100644
7b180b
--- a/sysprep/sysprep_operation_lvm_uuids.ml
7b180b
+++ b/sysprep/sysprep_operation_lvm_uuids.ml
7b180b
@@ -30,7 +30,46 @@ let rec lvm_uuids_perform g root side_effects =
7b180b
       try g#available [|"lvm2"|]; true with G.Error _ -> false in
7b180b
     if has_lvm2_feature then (
7b180b
       let has_pvs, has_vgs = g#pvs () <> [||], g#vgs () <> [||] in
7b180b
-      if has_pvs || has_vgs then g#vg_activate_all false;
7b180b
+      if has_pvs || has_vgs then (
7b180b
+        try g#vg_activate_all false
7b180b
+        with G.Error _ as exn ->
7b180b
+          (* If the "luks" feature is not available, re-raise the exception. *)
7b180b
+          (try g#available [|"luks"|] with G.Error _ -> raise exn);
7b180b
+
7b180b
+          (* Assume VG deactivation failed due to the guest using the
7b180b
+           * FS-on-LUKS-on-LVM scheme.
7b180b
+           *
7b180b
+           * By now, we have unmounted filesystems, but the decrypted LUKS
7b180b
+           * devices still keep the LVs open. Therefore, attempt closing all
7b180b
+           * decrypted LUKS devices that were opened by inspection (i.e., device
7b180b
+           * nodes with pathnames like "/dev/mapper/luks-<uuid>"). Closing the
7b180b
+           * decrypted LUKS devices should remove the references from their
7b180b
+           * underlying LVs, and then VG deactivation should succeed too.
7b180b
+           *
7b180b
+           * Note that closing the decrypted LUKS devices prevents the
7b180b
+           * blockdev-level manipulation of those filesystems that reside on
7b180b
+           * said decrypted LUKS devices, such as the "fs-uuids" operation. But
7b180b
+           * that should be OK, as we order the present operation after all
7b180b
+           * other block device ops.
7b180b
+           *
7b180b
+           * In case the guest uses the FS-on-LVM-on-LUKS scheme, then the
7b180b
+           * original VG deactivation must have failed for a different reason.
7b180b
+           * (As we have unmounted filesystems earlier, and LUKS is below, not
7b180b
+           * on top of, LVM.) The LUKS-closing attempts below will fail then,
7b180b
+           * due to LVM keeping the decrypted LUKS devices open. This failure is
7b180b
+           * harmless and can be considered a no-op. The final, retried VG
7b180b
+           * deactivation should reproduce the original failure.
7b180b
+           *)
7b180b
+          let luks_re = PCRE.compile ("^/dev/mapper/luks" ^
7b180b
+                                      "-[[:xdigit:]]{8}" ^
7b180b
+                                      "(?:-[[:xdigit:]]{4}){3}" ^
7b180b
+                                      "-[[:xdigit:]]{12}$")
7b180b
+          and dmdevs = Array.to_list (g#list_dm_devices ()) in
7b180b
+          let plaintext_devs = List.filter (PCRE.matches luks_re) dmdevs in
7b180b
+          List.iter (fun dev -> try g#cryptsetup_close dev with _ -> ())
7b180b
+            plaintext_devs;
7b180b
+          g#vg_activate_all false
7b180b
+      );
7b180b
       if has_pvs then g#pvchange_uuid_all ();
7b180b
       if has_vgs then g#vgchange_uuid_all ();
7b180b
       if has_pvs || has_vgs then g#vg_activate_all true
7b180b
@@ -39,6 +78,7 @@ let rec lvm_uuids_perform g root side_effects =
7b180b
 
7b180b
 let op = {
7b180b
   defaults with
7b180b
+    order = 99; (* Run it after other block device ops. *)
7b180b
     name = "lvm-uuids";
7b180b
     enabled_by_default = true;
7b180b
     heading = s_"Change LVM2 PV and VG UUIDs";
7b180b
-- 
7b180b
2.31.1
7b180b