virt-v2v/0031-convert-libosinfo_util...

155 lines
5.8 KiB
Diff

From 9725e53665bc64858ce3dd17aa1e365a3bb6e9da Mon Sep 17 00:00:00 2001
From: Laszlo Ersek <lersek@redhat.com>
Date: Tue, 1 Feb 2022 13:32:18 +0100
Subject: [PATCH] convert/libosinfo_utils: extract "best_driver" from
"windows_virtio.ml"
The "copy_from_libosinfo" function in "windows_virtio.ml" filters and
sorts the driver list from libosinfo in order to find the best driver.
Move this logic to a separate function (called "best_driver") in the
Libosinfo_utils module.
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2043333
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Acked-by: Richard W.M. Jones <rjones@redhat.com>
Message-Id: <20220201123219.9317-7-lersek@redhat.com>
(cherry picked from commit 42c4bf8ea222d50cf562e9e49568a70fb2d05bca)
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
---
convert/libosinfo_utils.ml | 35 +++++++++++++++++++++++++++++
convert/libosinfo_utils.mli | 12 ++++++++++
convert/windows_virtio.ml | 44 +------------------------------------
3 files changed, 48 insertions(+), 43 deletions(-)
diff --git a/convert/libosinfo_utils.ml b/convert/libosinfo_utils.ml
index f0d70ffd..8504e2b2 100644
--- a/convert/libosinfo_utils.ml
+++ b/convert/libosinfo_utils.ml
@@ -79,6 +79,41 @@ let string_of_osinfo_device_driver { Libosinfo.architecture; location;
(String.concat "\n" files)
(string_of_osinfo_device_list devices)
+let best_driver drivers arch =
+ let debug_drivers =
+ List.iter (fun d -> debug "Driver: %s" (string_of_osinfo_device_driver d))
+ (* The architecture that "inspect.i_arch" from libguestfs
+ * ("daemon/filearch.ml") calls "i386", the osinfo-db schema
+ * ("data/schema/osinfo.rng.in") calls "i686".
+ *)
+ and arch = if arch = "i386" then "i686" else arch in
+ debug "libosinfo drivers before filtering:";
+ debug_drivers drivers;
+ let drivers =
+ List.filter (
+ fun { Libosinfo.architecture; location; pre_installable } ->
+ if architecture <> arch || not pre_installable then
+ false
+ else
+ try
+ (match Xml.parse_uri location with
+ | { Xml.uri_scheme = Some scheme;
+ Xml.uri_path = Some _ } when scheme = "file" -> true
+ | _ -> false
+ )
+ with Invalid_argument _ -> false
+ ) drivers in
+ debug "libosinfo drivers after filtering:";
+ debug_drivers drivers;
+ let drivers =
+ List.sort (
+ fun { Libosinfo.priority = prioA } { Libosinfo.priority = prioB } ->
+ compare prioB prioA
+ ) drivers in
+ if drivers = [] then
+ raise Not_found;
+ List.hd drivers
+
type os_support = {
q35 : bool;
vio10 : bool;
diff --git a/convert/libosinfo_utils.mli b/convert/libosinfo_utils.mli
index 67be16c4..14991bc2 100644
--- a/convert/libosinfo_utils.mli
+++ b/convert/libosinfo_utils.mli
@@ -31,6 +31,18 @@ val string_of_osinfo_device_list : Libosinfo.osinfo_device list -> string
val string_of_osinfo_device_driver : Libosinfo.osinfo_device_driver -> string
(** Convert a [osinfo_device_driver] to a printable string for debugging. *)
+val best_driver : Libosinfo.osinfo_device_driver list ->
+ string ->
+ Libosinfo.osinfo_device_driver
+(** [best_driver drivers arch] picks the best driver from [drivers] as follows:
+ - filters out drivers that:
+ - target a different architecture,
+ - are not pre-installable,
+ - have an invalid or non-local URL;
+ - sorts the remaining drivers by priority, like libosinfo does;
+ - picks the top driver of the sorted list.
+ Raises Not_found if no driver in [drivers] survives filtering. *)
+
type os_support = {
q35 : bool;
vio10 : bool;
diff --git a/convert/windows_virtio.ml b/convert/windows_virtio.ml
index 3e542631..301f7544 100644
--- a/convert/windows_virtio.ml
+++ b/convert/windows_virtio.ml
@@ -409,53 +409,11 @@ and virtio_iso_path_matches_qemu_ga path inspect =
* Returns list of copied files.
*)
and copy_from_libosinfo g inspect destdir =
- let debug_drivers =
- List.iter (
- fun d ->
- debug "Driver: %s" (Libosinfo_utils.string_of_osinfo_device_driver d)
- )
- in
let { i_osinfo = osinfo; i_arch = arch } = inspect in
- (* The architecture that "inspect.i_arch" from libguestfs
- * ("daemon/filearch.ml") calls "i386", the osinfo-db schema
- * ("data/schema/osinfo.rng.in") calls "i686".
- *)
- let arch = if arch = "i386" then "i686" else arch in
try
let os = Libosinfo_utils.get_os_by_short_id osinfo in
let drivers = os#get_device_drivers () in
- debug "libosinfo drivers before filtering:"; debug_drivers drivers;
- (*
- * Filter out drivers that we cannot use:
- * - for a different architecture
- * - non-pre-installable ones
- * - location is an invalid URL, or a non-local one
- *)
- let drivers =
- List.filter (
- fun { Libosinfo.architecture; location; pre_installable } ->
- if architecture <> arch || not pre_installable then
- false
- else
- try
- (match Xml.parse_uri location with
- | { Xml.uri_scheme = Some scheme;
- Xml.uri_path = Some _ } when scheme = "file" -> true
- | _ -> false
- )
- with Invalid_argument _ -> false
- ) drivers in
- debug "libosinfo drivers after filtering:"; debug_drivers drivers;
- (* Sort the drivers by priority, like libosinfo does. *)
- let drivers =
- List.sort (
- fun { Libosinfo.priority = prioA } { Libosinfo.priority = prioB } ->
- compare prioB prioA
- ) drivers in
- (* Any driver available? *)
- if drivers = [] then
- raise Not_found;
- let driver = List.hd drivers in
+ let driver = Libosinfo_utils.best_driver drivers arch in
let uri = Xml.parse_uri driver.Libosinfo.location in
let basedir =
match uri.Xml.uri_path with
--
2.19.1.3.g30247aa5d201