Browse Source

libvirt: set device address tag only if setting disk unit

In Pike, we began setting disk unit values manually for the
'virtio-scsi' controller model in order to allow up to 256 devices [1].
We do this by setting the disk unit of the address tag manually for the
guest config. If we do not set the address tag manually, libvirt would
autogenerate it for us.

A problem occurs when a user has a SCSI disk that is a volume or isn't
using the 'virtio-scsi' controller model because we're not guarding our
manual setting of the address tag in the guest config by the disk unit,
in addition to the SCSI bus. This means that for a SCSI volume, we
generate an address tag like '<address type="drive" controller="0"/>'
for any SCSI volume, so a user with more than one device will get the
following error when they try to boot an instance:

  Failed to start libvirt guest: libvirtError: unsupported
    configuration: Found duplicate drive address for disk with target name
    'sda' controller='0' bus='0' target='0' unit='0'

This updates the conditionals to only manually set the address tag if
the bus is SCSI _and_ the disk unit has been specified. Otherwise, let
libvirt autogenerate the address tag and take care of avoiding
collisions.

[1] https://bugs.launchpad.net/nova/+bug/1686116

Closes-Bug: #1792077

Change-Id: Iefab05e84ccc0bf8f15bdbbf515a290d282dbc5d
(cherry picked from commit 48fd81648a)
(cherry picked from commit 7500c1910c)
(cherry picked from commit 8703282508)
tags/17.0.11
melanie witt 11 months ago
parent
commit
c8ad9f4e19

+ 32
- 0
nova/tests/unit/virt/libvirt/test_imagebackend.py View File

@@ -21,6 +21,7 @@ import shutil
21 21
 import tempfile
22 22
 
23 23
 from castellan import key_manager
24
+import ddt
24 25
 import fixtures
25 26
 import mock
26 27
 from oslo_concurrency import lockutils
@@ -57,6 +58,7 @@ class FakeConn(object):
57 58
         return FakeSecret()
58 59
 
59 60
 
61
+@ddt.ddt
60 62
 class _ImageTestCase(object):
61 63
 
62 64
     def mock_create_image(self, image):
@@ -203,6 +205,24 @@ class _ImageTestCase(object):
203 205
         self.assertEqual(2361393152, image.get_disk_size(image.path))
204 206
         get_disk_size.assert_called_once_with(image.path)
205 207
 
208
+    def _test_libvirt_info_scsi_with_unit(self, disk_unit):
209
+        # The address should be set if bus is scsi and unit is set.
210
+        # Otherwise, it should not be set at all.
211
+        image = self.image_class(self.INSTANCE, self.NAME)
212
+        disk = image.libvirt_info(disk_bus='scsi', disk_dev='/dev/sda',
213
+                                  device_type='disk', cache_mode='none',
214
+                                  extra_specs={}, hypervisor_version=4004001,
215
+                                  disk_unit=disk_unit)
216
+        if disk_unit:
217
+            self.assertEqual(0, disk.device_addr.controller)
218
+            self.assertEqual(disk_unit, disk.device_addr.unit)
219
+        else:
220
+            self.assertIsNone(disk.device_addr)
221
+
222
+    @ddt.data(5, None)
223
+    def test_libvirt_info_scsi_with_unit(self, disk_unit):
224
+        self._test_libvirt_info_scsi_with_unit(disk_unit)
225
+
206 226
 
207 227
 class FlatTestCase(_ImageTestCase, test.NoDBTestCase):
208 228
 
@@ -1276,6 +1296,7 @@ class EncryptedLvmTestCase(_ImageTestCase, test.NoDBTestCase):
1276 1296
                          model)
1277 1297
 
1278 1298
 
1299
+@ddt.ddt
1279 1300
 class RbdTestCase(_ImageTestCase, test.NoDBTestCase):
1280 1301
     FSID = "FakeFsID"
1281 1302
     POOL = "FakePool"
@@ -1500,6 +1521,17 @@ class RbdTestCase(_ImageTestCase, test.NoDBTestCase):
1500 1521
 
1501 1522
         super(RbdTestCase, self).test_libvirt_info()
1502 1523
 
1524
+    @ddt.data(5, None)
1525
+    @mock.patch.object(rbd_utils.RBDDriver, "get_mon_addrs")
1526
+    def test_libvirt_info_scsi_with_unit(self, disk_unit, mock_mon_addrs):
1527
+        def get_mon_addrs():
1528
+            hosts = ["server1", "server2"]
1529
+            ports = ["1899", "1920"]
1530
+            return hosts, ports
1531
+        mock_mon_addrs.side_effect = get_mon_addrs
1532
+
1533
+        super(RbdTestCase, self)._test_libvirt_info_scsi_with_unit(disk_unit)
1534
+
1503 1535
     @mock.patch.object(rbd_utils.RBDDriver, "get_mon_addrs")
1504 1536
     def test_get_model(self, mock_mon_addrs):
1505 1537
         pool = "FakePool"

+ 20
- 0
nova/tests/unit/virt/libvirt/volume/test_volume.py View File

@@ -13,6 +13,7 @@
13 13
 #    License for the specific language governing permissions and limitations
14 14
 #    under the License.
15 15
 
16
+import ddt
16 17
 import mock
17 18
 
18 19
 from nova import exception
@@ -130,6 +131,7 @@ class LibvirtISCSIVolumeBaseTestCase(LibvirtVolumeBaseTestCase):
130 131
         return ret
131 132
 
132 133
 
134
+@ddt.ddt
133 135
 class LibvirtVolumeTestCase(LibvirtISCSIVolumeBaseTestCase):
134 136
 
135 137
     def _assertDiskInfoEquals(self, tree, disk_info):
@@ -373,3 +375,21 @@ class LibvirtVolumeTestCase(LibvirtISCSIVolumeBaseTestCase):
373 375
         conf = libvirt_driver.get_config(connection_info, self.disk_info)
374 376
         tree = conf.format_dom()
375 377
         self.assertIsNone(tree.find("encryption"))
378
+
379
+    @ddt.data(5, None)
380
+    def test_libvirt_volume_driver_address_tag_scsi_unit(self, disk_unit):
381
+        # The address tag should be set if bus is scsi and unit is set.
382
+        # Otherwise, it should not be set at all.
383
+        libvirt_driver = volume.LibvirtVolumeDriver(self.fake_host)
384
+        connection_info = {'data': {'device_path': '/foo'}}
385
+        disk_info = {'bus': 'scsi', 'dev': 'sda', 'type': 'disk'}
386
+        if disk_unit:
387
+            disk_info['unit'] = disk_unit
388
+        conf = libvirt_driver.get_config(connection_info, disk_info)
389
+        tree = conf.format_dom()
390
+        address = tree.find('address')
391
+        if disk_unit:
392
+            self.assertEqual('0', address.attrib['controller'])
393
+            self.assertEqual(str(disk_unit), address.attrib['unit'])
394
+        else:
395
+            self.assertIsNone(address)

+ 10
- 4
nova/virt/libvirt/imagebackend.py View File

@@ -180,11 +180,17 @@ class Image(object):
180 180
         return info
181 181
 
182 182
     def disk_scsi(self, info, disk_unit):
183
-        # The driver is responsible to create the SCSI controller
184
-        # at index 0.
185
-        info.device_addr = vconfig.LibvirtConfigGuestDeviceAddressDrive()
186
-        info.device_addr.controller = 0
183
+        # NOTE(melwitt): We set the device address unit number manually in the
184
+        # case of the virtio-scsi controller, in order to allow attachment of
185
+        # up to 256 devices. So, we should only be setting the address tag
186
+        # if we intend to set the unit number. Otherwise, we will let libvirt
187
+        # handle autogeneration of the address tag.
188
+        # See https://bugs.launchpad.net/nova/+bug/1792077 for details.
187 189
         if disk_unit is not None:
190
+            # The driver is responsible to create the SCSI controller
191
+            # at index 0.
192
+            info.device_addr = vconfig.LibvirtConfigGuestDeviceAddressDrive()
193
+            info.device_addr.controller = 0
188 194
             # In order to allow up to 256 disks handled by one
189 195
             # virtio-scsi controller, the device addr should be
190 196
             # specified.

+ 11
- 6
nova/virt/libvirt/volume/volume.py View File

@@ -94,16 +94,21 @@ class LibvirtBaseVolumeDriver(object):
94 94
         if data.get('discard', False) is True:
95 95
             conf.driver_discard = 'unmap'
96 96
 
97
-        if disk_info['bus'] == 'scsi':
97
+        # NOTE(melwitt): We set the device address unit number manually in the
98
+        # case of the virtio-scsi controller, in order to allow attachment of
99
+        # up to 256 devices. So, we should only be setting the address tag
100
+        # if we intend to set the unit number. Otherwise, we will let libvirt
101
+        # handle autogeneration of the address tag.
102
+        # See https://bugs.launchpad.net/nova/+bug/1792077 for details.
103
+        if disk_info['bus'] == 'scsi' and 'unit' in disk_info:
98 104
             # The driver is responsible to create the SCSI controller
99 105
             # at index 0.
100 106
             conf.device_addr = vconfig.LibvirtConfigGuestDeviceAddressDrive()
101 107
             conf.device_addr.controller = 0
102
-            if 'unit' in disk_info:
103
-                # In order to allow up to 256 disks handled by one
104
-                # virtio-scsi controller, the device addr should be
105
-                # specified.
106
-                conf.device_addr.unit = disk_info['unit']
108
+            # In order to allow up to 256 disks handled by one
109
+            # virtio-scsi controller, the device addr should be
110
+            # specified.
111
+            conf.device_addr.unit = disk_info['unit']
107 112
 
108 113
         if connection_info.get('multiattach', False):
109 114
             # Note that driver_cache should be disabled (none) when using

Loading…
Cancel
Save