Blame SOURCES/virt-manager-addhardware-Rework-the-addition-of-controllers-for-v.patch

343f27
From a3c763361d39daa2b724a97c9ac672dfe45287c7 Mon Sep 17 00:00:00 2001
343f27
From: Martin Kletzander <mkletzan@redhat.com>
343f27
Date: Thu, 27 Feb 2014 13:57:37 +0100
343f27
Subject: [PATCH 3/3] addhardware: Rework the addition of controllers for
343f27
 virtio-scsi disks
343f27
343f27
https://bugzilla.redhat.com/show_bug.cgi?id=1036716
343f27
343f27
After this patch the virtio-scsi controller should be added only if
343f27
needed and its index used for the target generation of the added disk.
343f27
343f27
RHEL-only; upstream counterpart of this work was done in commits
343f27
a9c791b5, 078e1a4d, bc5d84b0, 4aec369e, 7c437f6a, 466c2bcf and 0dceb24b.
343f27
343f27
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
343f27
---
343f27
 virtManager/addhardware.py | 57 ++++++++++++++++++++++++++++++++++------------
343f27
 1 file changed, 43 insertions(+), 14 deletions(-)
343f27
343f27
diff --git a/virtManager/addhardware.py b/virtManager/addhardware.py
343f27
index b2f5526..51a01da 100644
343f27
--- a/virtManager/addhardware.py
343f27
+++ b/virtManager/addhardware.py
343f27
@@ -20,6 +20,7 @@
343f27
 
343f27
 import logging
343f27
 import traceback
343f27
+import collections
343f27
 
343f27
 # pylint: disable=E0611
343f27
 from gi.repository import Gtk
343f27
@@ -1467,27 +1468,55 @@ class vmmAddHardware(vmmGObjectUI):
343f27
         disks = (self.vm.get_disk_devices() +
343f27
                  self.vm.get_disk_devices(inactive=True))
343f27
         for d in disks:
343f27
-            used.append(d.target)
343f27
+            if d.target not in used:
343f27
+                used.append(d.target)
343f27
 
343f27
         # Add a SCSI controller with model virtio-scsi if needed
343f27
         disk.vmm_controller = None
343f27
         if (controller_model == "virtio-scsi") and (bus == "scsi"):
343f27
+            # Get SCSI controllers
343f27
             controllers = self.vm.get_controller_devices()
343f27
-            controller = VirtualControllerSCSI(conn=self.conn.vmm)
343f27
-            controller.set_model(controller_model)
343f27
-            disk.vmm_controller = controller
343f27
-            for d in controllers:
343f27
-                if controller.type == d.type:
343f27
-                    controller.index += 1
343f27
-                if controller_model == d.model:
343f27
-                    disk.vmm_controller = None
343f27
-                    controller = d
343f27
+            ctrls_scsi = [x for x in controllers if
343f27
+                          (x.type == VirtualControllerSCSI.CONTROLLER_TYPE_SCSI)]
343f27
+
343f27
+            # Create possible new controller
343f27
+            controller = VirtualControllerSCSI(conn=self.conn.vmm,
343f27
+                                               model=controller_model)
343f27
+
343f27
+            # And set its index
343f27
+            controller.index = 0
343f27
+            if ctrls_scsi:
343f27
+                controller.index = max([int(x.index) for x in ctrls_scsi]) + 1
343f27
+
343f27
+            # Take only virtio-scsi ones
343f27
+            ctrls_scsi = [x for x in ctrls_scsi if
343f27
+                          (x.model == controller_model)]
343f27
+
343f27
+            # Save occupied places per controller
343f27
+            occupied = collections.defaultdict(int)
343f27
+            for d in disks:
343f27
+                if d.get_target_prefix() == disk.get_target_prefix():
343f27
+                    num = 0
343f27
+                    k = 0
343f27
+                    tgt = d.target
343f27
+                    if tgt[0] == 'x':
343f27
+                        # This case is here for 'xvda'
343f27
+                        tgt = tgt[1:]
343f27
+                    for i, c in enumerate(reversed(tgt[2:])):
343f27
+                        if i != 0:
343f27
+                            k = 1
343f27
+                        num += (ord(c) - ord('a') + k) * (26 ** i)
343f27
+                    occupied[num / 7] += 1
343f27
+            for c in ctrls_scsi:
343f27
+                if occupied[int(c.index)] < 7:
343f27
+                    controller = c
343f27
                     break
343f27
+            else:
343f27
+                disk.vmm_controller = controller
343f27
 
343f27
-            disk.address.type = disk.address.ADDRESS_TYPE_DRIVE
343f27
-            disk.address.controller = controller.index
343f27
-
343f27
-        disk.generate_target(used)
343f27
+            disk.generate_target(used, int(controller.index))
343f27
+        else:
343f27
+            disk.generate_target(used)
343f27
 
343f27
         isfatal, errmsg = disk.is_size_conflict()
343f27
         if not isfatal and errmsg:
343f27
-- 
343f27
1.8.5.3
343f27