mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

Blame SOURCES/0063-v2v-Move-target_bus_assignment-to-separate-module.patch

e76f14
From ea4e7be22f84cadf9418f8d031ed7cc7f6914908 Mon Sep 17 00:00:00 2001
e76f14
From: "Richard W.M. Jones" <rjones@redhat.com>
e76f14
Date: Sat, 23 Apr 2016 18:15:19 +0100
e76f14
Subject: [PATCH] v2v: Move target_bus_assignment to separate module.
e76f14
e76f14
This is just code motion.
e76f14
e76f14
(cherry picked from commit 9e3182b4c5fc3abb55e0fe072c5fa3af1ffb0ee3)
e76f14
---
e76f14
 po/POTFILES-ml                |  1 +
e76f14
 v2v/Makefile.am               |  2 +
e76f14
 v2v/target_bus_assignment.ml  | 91 +++++++++++++++++++++++++++++++++++++++++++
e76f14
 v2v/target_bus_assignment.mli | 24 ++++++++++++
e76f14
 v2v/v2v.ml                    | 79 ++-----------------------------------
e76f14
 5 files changed, 121 insertions(+), 76 deletions(-)
e76f14
 create mode 100644 v2v/target_bus_assignment.ml
e76f14
 create mode 100644 v2v/target_bus_assignment.mli
e76f14
e76f14
diff --git a/po/POTFILES-ml b/po/POTFILES-ml
e76f14
index b94d616..b7f3cc2 100644
e76f14
--- a/po/POTFILES-ml
e76f14
+++ b/po/POTFILES-ml
e76f14
@@ -122,6 +122,7 @@ v2v/output_qemu.ml
e76f14
 v2v/output_rhev.ml
e76f14
 v2v/output_vdsm.ml
e76f14
 v2v/stringMap.ml
e76f14
+v2v/target_bus_assignment.ml
e76f14
 v2v/test-harness/v2v_test_harness.ml
e76f14
 v2v/types.ml
e76f14
 v2v/utils.ml
e76f14
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
e76f14
index d9cf986..2d52ab8 100644
e76f14
--- a/v2v/Makefile.am
e76f14
+++ b/v2v/Makefile.am
e76f14
@@ -67,6 +67,7 @@ SOURCES_MLI = \
e76f14
 	output_vdsm.mli \
e76f14
 	OVF.mli \
e76f14
 	stringMap.mli \
e76f14
+	target_bus_assignment.mli \
e76f14
 	types.mli \
e76f14
 	utils.mli \
e76f14
 	vCenter.mli \
e76f14
@@ -105,6 +106,7 @@ SOURCES_ML = \
e76f14
 	output_rhev.ml \
e76f14
 	output_vdsm.ml \
e76f14
 	inspect_source.ml \
e76f14
+	target_bus_assignment.ml \
e76f14
 	cmdline.ml \
e76f14
 	v2v.ml
e76f14
 
e76f14
diff --git a/v2v/target_bus_assignment.ml b/v2v/target_bus_assignment.ml
e76f14
new file mode 100644
e76f14
index 0000000..b82915b
e76f14
--- /dev/null
e76f14
+++ b/v2v/target_bus_assignment.ml
e76f14
@@ -0,0 +1,91 @@
e76f14
+(* virt-v2v
e76f14
+ * Copyright (C) 2009-2016 Red Hat Inc.
e76f14
+ *
e76f14
+ * This program is free software; you can redistribute it and/or modify
e76f14
+ * it under the terms of the GNU General Public License as published by
e76f14
+ * the Free Software Foundation; either version 2 of the License, or
e76f14
+ * (at your option) any later version.
e76f14
+ *
e76f14
+ * This program is distributed in the hope that it will be useful,
e76f14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
e76f14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e76f14
+ * GNU General Public License for more details.
e76f14
+ *
e76f14
+ * You should have received a copy of the GNU General Public License along
e76f14
+ * with this program; if not, write to the Free Software Foundation, Inc.,
e76f14
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
e76f14
+ *)
e76f14
+
e76f14
+open Common_utils
e76f14
+open Common_gettext.Gettext
e76f14
+
e76f14
+open Types
e76f14
+
e76f14
+(* XXX This doesn't do the right thing for PC legacy floppy devices.
e76f14
+ * XXX This could handle slot assignment better when we have a mix of
e76f14
+ * devices desiring their own slot, and others that don't care.  Allocate
e76f14
+ * the first group in the first pass, then the second group afterwards.
e76f14
+ *)
e76f14
+let rec target_bus_assignment source targets guestcaps =
e76f14
+  let virtio_blk_bus = ref [| |]
e76f14
+  and ide_bus = ref [| |]
e76f14
+  and scsi_bus = ref [| |] in
e76f14
+
e76f14
+  (* Add the fixed disks (targets) to either the virtio-blk or IDE bus,
e76f14
+   * depending on whether the guest has virtio drivers or not.
e76f14
+   *)
e76f14
+  iteri (
e76f14
+    fun i t ->
e76f14
+      let t = BusSlotTarget t in
e76f14
+      match guestcaps.gcaps_block_bus with
e76f14
+      | Virtio_blk -> insert virtio_blk_bus i t
e76f14
+      | IDE -> insert ide_bus i t
e76f14
+  ) targets;
e76f14
+
e76f14
+  (* Now try to add the removable disks to the bus at the same slot
e76f14
+   * they originally occupied, but if the slot is occupied, emit a
e76f14
+   * a warning and insert the disk in the next empty slot in that bus.
e76f14
+   *)
e76f14
+  List.iter (
e76f14
+    fun r ->
e76f14
+      let bus = match r.s_removable_controller with
e76f14
+        | None -> ide_bus (* Wild guess, but should be safe. *)
e76f14
+        | Some Source_virtio_blk -> virtio_blk_bus
e76f14
+        | Some Source_IDE -> ide_bus
e76f14
+        | Some Source_SCSI -> scsi_bus in
e76f14
+      match r.s_removable_slot with
e76f14
+      | None -> ignore (insert_after bus 0 (BusSlotRemovable r))
e76f14
+      | Some desired_slot_nr ->
e76f14
+         if not (insert_after bus desired_slot_nr (BusSlotRemovable r)) then
e76f14
+           warning (f_"removable %s device in slot %d clashes with another disk, so it has been moved to a higher numbered slot on the same bus.  This may mean that this removable device has a different name inside the guest (for example a CD-ROM originally called /dev/hdc might move to /dev/hdd, or from D: to E: on a Windows guest).")
e76f14
+                   (match r.s_removable_type with
e76f14
+                    | CDROM -> s_"CD-ROM"
e76f14
+                    | Floppy -> s_"floppy disk")
e76f14
+                   desired_slot_nr
e76f14
+  ) source.s_removables;
e76f14
+
e76f14
+  { target_virtio_blk_bus = !virtio_blk_bus;
e76f14
+    target_ide_bus = !ide_bus;
e76f14
+    target_scsi_bus = !scsi_bus }
e76f14
+
e76f14
+(* Insert a slot into the bus array, making the array bigger if necessary. *)
e76f14
+and insert bus i slot =
e76f14
+  let oldbus = !bus in
e76f14
+  let oldlen = Array.length oldbus in
e76f14
+  if i >= oldlen then (
e76f14
+    bus := Array.make (i+1) BusSlotEmpty;
e76f14
+    Array.blit oldbus 0 !bus 0 oldlen
e76f14
+  );
e76f14
+  Array.set !bus i slot
e76f14
+
e76f14
+(* Insert a slot into the bus, but if the desired slot is not empty, then
e76f14
+ * increment the slot number until we find an empty one.  Returns
e76f14
+ * true if we got the desired slot.
e76f14
+ *)
e76f14
+and insert_after bus i slot =
e76f14
+  let len = Array.length !bus in
e76f14
+  if i >= len || Array.get !bus i = BusSlotEmpty then (
e76f14
+    insert bus i slot; true
e76f14
+  ) else (
e76f14
+    ignore (insert_after bus (i+1) slot); false
e76f14
+  )
e76f14
diff --git a/v2v/target_bus_assignment.mli b/v2v/target_bus_assignment.mli
e76f14
new file mode 100644
e76f14
index 0000000..6a30b2c
e76f14
--- /dev/null
e76f14
+++ b/v2v/target_bus_assignment.mli
e76f14
@@ -0,0 +1,24 @@
e76f14
+(* virt-v2v
e76f14
+ * Copyright (C) 2009-2016 Red Hat Inc.
e76f14
+ *
e76f14
+ * This program is free software; you can redistribute it and/or modify
e76f14
+ * it under the terms of the GNU General Public License as published by
e76f14
+ * the Free Software Foundation; either version 2 of the License, or
e76f14
+ * (at your option) any later version.
e76f14
+ *
e76f14
+ * This program is distributed in the hope that it will be useful,
e76f14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
e76f14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e76f14
+ * GNU General Public License for more details.
e76f14
+ *
e76f14
+ * You should have received a copy of the GNU General Public License along
e76f14
+ * with this program; if not, write to the Free Software Foundation, Inc.,
e76f14
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
e76f14
+ *)
e76f14
+
e76f14
+(** Assign fixed and removable disks to target buses.
e76f14
+
e76f14
+    Do this as best we can.  This is not solvable for all guests,
e76f14
+    but at least avoid overlapping disks (RHBZ#1238053). *)
e76f14
+
e76f14
+val target_bus_assignment : Types.source -> Types.target list -> Types.guestcaps -> Types.target_buses
e76f14
diff --git a/v2v/v2v.ml b/v2v/v2v.ml
e76f14
index c73482b..f2f1cff 100644
e76f14
--- a/v2v/v2v.ml
e76f14
+++ b/v2v/v2v.ml
e76f14
@@ -118,7 +118,9 @@ let rec main () =
e76f14
          get_target_firmware inspect guestcaps source output in
e76f14
 
e76f14
        message (f_"Assigning disks to buses");
e76f14
-       let target_buses = target_bus_assignment source targets guestcaps in
e76f14
+       let target_buses =
e76f14
+         Target_bus_assignment.target_bus_assignment source targets
e76f14
+                                                     guestcaps in
e76f14
        debug "%s" (string_of_target_buses target_buses);
e76f14
 
e76f14
        let targets =
e76f14
@@ -716,81 +718,6 @@ and actual_target_size target =
e76f14
     with Failure _ | Invalid_argument _ -> None in
e76f14
   { target with target_actual_size = size }
e76f14
 
e76f14
-(* Assign fixed and removable disks to target buses, as best we can.
e76f14
- * This is not solvable for all guests, but at least avoid overlapping
e76f14
- * disks (RHBZ#1238053).
e76f14
- *
e76f14
- * XXX This doesn't do the right thing for PC legacy floppy devices.
e76f14
- * XXX This could handle slot assignment better when we have a mix of
e76f14
- * devices desiring their own slot, and others that don't care.  Allocate
e76f14
- * the first group in the first pass, then the second group afterwards.
e76f14
- *)
e76f14
-and target_bus_assignment source targets guestcaps =
e76f14
-  let virtio_blk_bus = ref [| |]
e76f14
-  and ide_bus = ref [| |]
e76f14
-  and scsi_bus = ref [| |] in
e76f14
-
e76f14
-  (* Insert a slot into the bus array, making the array bigger if necessary. *)
e76f14
-  let insert bus i slot =
e76f14
-    let oldbus = !bus in
e76f14
-    let oldlen = Array.length oldbus in
e76f14
-    if i >= oldlen then (
e76f14
-      bus := Array.make (i+1) BusSlotEmpty;
e76f14
-      Array.blit oldbus 0 !bus 0 oldlen
e76f14
-    );
e76f14
-    Array.set !bus i slot
e76f14
-  in
e76f14
-
e76f14
-  (* Insert a slot into the bus, but if the desired slot is not empty, then
e76f14
-   * increment the slot number until we find an empty one.  Returns
e76f14
-   * true if we got the desired slot.
e76f14
-   *)
e76f14
-  let rec insert_after bus i slot =
e76f14
-    let len = Array.length !bus in
e76f14
-    if i >= len || Array.get !bus i = BusSlotEmpty then (
e76f14
-      insert bus i slot; true
e76f14
-    ) else (
e76f14
-      ignore (insert_after bus (i+1) slot); false
e76f14
-    )
e76f14
-  in
e76f14
-
e76f14
-  (* Add the fixed disks (targets) to either the virtio-blk or IDE bus,
e76f14
-   * depending on whether the guest has virtio drivers or not.
e76f14
-   *)
e76f14
-  iteri (
e76f14
-    fun i t ->
e76f14
-      let t = BusSlotTarget t in
e76f14
-      match guestcaps.gcaps_block_bus with
e76f14
-      | Virtio_blk -> insert virtio_blk_bus i t
e76f14
-      | IDE -> insert ide_bus i t
e76f14
-  ) targets;
e76f14
-
e76f14
-  (* Now try to add the removable disks to the bus at the same slot
e76f14
-   * they originally occupied, but if the slot is occupied, emit a
e76f14
-   * a warning and insert the disk in the next empty slot in that bus.
e76f14
-   *)
e76f14
-  List.iter (
e76f14
-    fun r ->
e76f14
-      let bus = match r.s_removable_controller with
e76f14
-        | None -> ide_bus (* Wild guess, but should be safe. *)
e76f14
-        | Some Source_virtio_blk -> virtio_blk_bus
e76f14
-        | Some Source_IDE -> ide_bus
e76f14
-        | Some Source_SCSI -> scsi_bus in
e76f14
-      match r.s_removable_slot with
e76f14
-      | None -> ignore (insert_after bus 0 (BusSlotRemovable r))
e76f14
-      | Some desired_slot_nr ->
e76f14
-         if not (insert_after bus desired_slot_nr (BusSlotRemovable r)) then
e76f14
-           warning (f_"removable %s device in slot %d clashes with another disk, so it has been moved to a higher numbered slot on the same bus.  This may mean that this removable device has a different name inside the guest (for example a CD-ROM originally called /dev/hdc might move to /dev/hdd, or from D: to E: on a Windows guest).")
e76f14
-                   (match r.s_removable_type with
e76f14
-                    | CDROM -> s_"CD-ROM"
e76f14
-                    | Floppy -> s_"floppy disk")
e76f14
-                   desired_slot_nr
e76f14
-  ) source.s_removables;
e76f14
-
e76f14
-  { target_virtio_blk_bus = !virtio_blk_bus;
e76f14
-    target_ide_bus = !ide_bus;
e76f14
-    target_scsi_bus = !scsi_bus }
e76f14
-
e76f14
 (* Save overlays if --debug-overlays option was used. *)
e76f14
 and preserve_overlays overlays src_name =
e76f14
   let overlay_dir = (open_guestfs ())#get_cachedir () in
e76f14
-- 
e76f14
1.8.3.1
e76f14