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

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