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

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