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

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