Blame SOURCES/0051-convert-If-listing-RPM-applications-fails-rebuild-DB.patch

8984ae
From 87e5404d20ec54d16d22a7bb8f06ea91076c91f7 Mon Sep 17 00:00:00 2001
8984ae
From: "Richard W.M. Jones" <rjones@redhat.com>
8984ae
Date: Wed, 25 May 2022 16:47:04 +0100
8984ae
Subject: [PATCH] convert: If listing RPM applications fails, rebuild DB and
8984ae
 retry
8984ae
8984ae
In libguestfs before commit 488245ed6c ("daemon: rpm: Check return
8984ae
values from librpm calls") we didn't bother to check the return values
8984ae
from any librpm calls.  In some cases where the RPM database is
8984ae
faulty, this caused us to return a zero-length array of applications
8984ae
(but no error indication).  Libguestfs has subsequently been fixed so
8984ae
now it returns an error if the RPM database is corrupt.
8984ae
8984ae
This commit changes virt-v2v behaviour so that if either
8984ae
guestfs_inspect_list_applications2 returns a zero-length list (ie. old
8984ae
libguestfs) or it throws an error (new libguestfs) then we attempt to
8984ae
rebuild the RPM database and retry the operation.  Rebuilding the
8984ae
database can recover from some but not all RPM DB corruption.
8984ae
8984ae
See-also: https://bugzilla.redhat.com/show_bug.cgi?id=2089623#c12
8984ae
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2089623
8984ae
Reported-by: Xiaodai Wang
8984ae
Reported-by: Ming Xie
8984ae
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
8984ae
(cherry picked from commit 31bf5db25bcfd8a9f5a48cc0523abae28861de9a)
8984ae
---
8984ae
 v2v/inspect_source.ml | 34 ++++++++++++++++++++++++++++++++--
8984ae
 1 file changed, 32 insertions(+), 2 deletions(-)
8984ae
8984ae
diff --git a/v2v/inspect_source.ml b/v2v/inspect_source.ml
8984ae
index b8a3c8ad..554fde1d 100644
8984ae
--- a/v2v/inspect_source.ml
8984ae
+++ b/v2v/inspect_source.ml
8984ae
@@ -34,6 +34,7 @@ let rec inspect_source root_choice g =
8984ae
   reject_if_not_installed_image g root;
8984ae
 
8984ae
   let typ = g#inspect_get_type root in
8984ae
+  let package_format = g#inspect_get_package_format root in
8984ae
 
8984ae
   (* Mount up the filesystems. *)
8984ae
   let mps = g#inspect_get_mountpoints root in
8984ae
@@ -71,7 +72,7 @@ let rec inspect_source root_choice g =
8984ae
   ) mps;
8984ae
 
8984ae
   (* Get list of applications/packages installed. *)
8984ae
-  let apps = g#inspect_list_applications2 root in
8984ae
+  let apps = list_applications g root package_format in
8984ae
   let apps = Array.to_list apps in
8984ae
 
8984ae
   (* A map of app2_name -> application2, for easier lookups.  Note
8984ae
@@ -106,7 +107,7 @@ let rec inspect_source root_choice g =
8984ae
     i_arch = g#inspect_get_arch root;
8984ae
     i_major_version = g#inspect_get_major_version root;
8984ae
     i_minor_version = g#inspect_get_minor_version root;
8984ae
-    i_package_format = g#inspect_get_package_format root;
8984ae
+    i_package_format = package_format;
8984ae
     i_package_management = g#inspect_get_package_management root;
8984ae
     i_product_name = g#inspect_get_product_name root;
8984ae
     i_product_variant = g#inspect_get_product_variant root;
8984ae
@@ -186,6 +187,35 @@ and reject_if_not_installed_image g root =
8984ae
   if fmt <> "installed" then
8984ae
     error (f_"libguestfs thinks this is not an installed operating system (it might be, for example, an installer disk or live CD).  If this is wrong, it is probably a bug in libguestfs.  root=%s fmt=%s") root fmt
8984ae
 
8984ae
+(* Wrapper around g#inspect_list_applications2 which, for RPM
8984ae
+ * guests, on failure tries to rebuild the RPM database before
8984ae
+ * repeating the operation.
8984ae
+ *)
8984ae
+and list_applications g root = function
8984ae
+  | "rpm" ->
8984ae
+     (* RPM guest.
8984ae
+      *
8984ae
+      * In libguestfs before commit 488245ed6c ("daemon: rpm: Check
8984ae
+      * return values from librpm calls"), a corrupt RPM database
8984ae
+      * would return an empty array here with no exception.  Hence
8984ae
+      * the check below which turns empty array => exception.  In
8984ae
+      * libguestfs after that commit, inspect_list_applications2
8984ae
+      * will raise an exception if it detects a corrupt RPM database.
8984ae
+      *)
8984ae
+     (try
8984ae
+        let apps = g#inspect_list_applications2 root in
8984ae
+        if apps = [||] then raise (G.Error "no applications returned");
8984ae
+        apps
8984ae
+      with G.Error msg ->
8984ae
+        debug "%s" msg;
8984ae
+        debug "rebuilding RPM database and retrying ...";
8984ae
+        ignore (g#sh "rpmdb --rebuilddb");
8984ae
+        g#inspect_list_applications2 root
8984ae
+     )
8984ae
+  | _ ->
8984ae
+     (* Non-RPM guest, just do it. *)
8984ae
+     g#inspect_list_applications2 root
8984ae
+
8984ae
 (* See if this guest could use UEFI to boot.  It should use GPT and
8984ae
  * it should have an EFI System Partition (ESP).
8984ae
  *
8984ae
-- 
8984ae
2.31.1
8984ae