mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone
Blob Blame History Raw
From a0e44877a4ba2df153ec5334b24eb254f9c2aeb3 Mon Sep 17 00:00:00 2001
From: Pino Toscano <ptoscano@redhat.com>
Date: Wed, 20 Mar 2019 12:32:02 +0100
Subject: [PATCH] v2v: linux: improve arch detection from modules
 (RHBZ#1690574)

Try to look for a well known kernel module (so far only virtio, or kvm)
to use for detecting the architecture of a kernel.  This way, we can
avoid picking 3rd party modules that cause troubles.

(cherry picked from commit 363b5e0b4ecebe861a9aafe8bce5a8390b54571c)
---
 v2v/linux_kernels.ml | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/v2v/linux_kernels.ml b/v2v/linux_kernels.ml
index 889ec2f2a..30160f0da 100644
--- a/v2v/linux_kernels.ml
+++ b/v2v/linux_kernels.ml
@@ -185,11 +185,35 @@ let detect_kernels (g : G.guestfs) inspect family bootloader =
              assert (List.length modules > 0);
 
              (* Determine the kernel architecture by looking at the
-              * architecture of an arbitrary kernel module.
+              * architecture of a kernel module.
+              *
+              * To avoid architecture detection issues with 3rd party
+              * modules (RHBZ#1690574), try to pick one of the well
+              * known modules, if available.  Otherwise, an arbitrary
+              * module is used.
               *)
              let arch =
-               let any_module = modpath ^ List.hd modules in
-               g#file_architecture (g#realpath any_module) in
+               (* Well known kernel modules. *)
+               let candidates = [ "virtio"; "kvm" ] in
+               let all_candidates = List.flatten (
+                 List.map (
+                   fun f ->
+                     [ "/" ^ f ^ ".o"; "/" ^ f ^ ".ko"; "/" ^ f ^ ".ko.xz" ]
+                 ) candidates
+               ) in
+               let candidate =
+                 try
+                   List.find (
+                     fun m ->
+                       List.exists (String.is_suffix m) all_candidates
+                   ) modules
+                 with Not_found ->
+                   (* No known module found, pick an arbitrary one
+                    * (the first).
+                    *)
+                   List.hd modules in
+               let candidate = modpath ^ candidate in
+               g#file_architecture (g#realpath candidate) in
 
              (* Just return the module names, without path or extension. *)
              let modules = List.filter_map (
-- 
2.21.0