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