Browse Source

libvirt: Fix root disk leak in live mig

This patch makes sure that i_create_images_and_backing method of the
libvirt driver (called in several places, but most problematic one is
the call in the pre_live_migration method) creates all the files the
instance needs that are not present.

Prioir to this patch - the method would only attempt to download the
image, and if it did so with the path of the ephemeral drives, it could
expose the image to other users as an ephemeral devices. See the related
bug for more detaiis.

After this patch - we properly distinguish between image, ephemeral and
swap files, and make sure that the imagebackend does the correct thing.

Closes-bug: #1251590

Co-authored-by: Loganathan Parthipan <parthipan@hp.com>

Change-Id: I78aa2f4243899db4f4941e77014a7e18e27fc63e
tags/2014.1.b3
Nikola Dipanov 5 years ago
parent
commit
b0d36683fe
2 changed files with 64 additions and 7 deletions
  1. 42
    0
      nova/tests/virt/libvirt/test_libvirt.py
  2. 22
    7
      nova/virt/libvirt/driver.py

+ 42
- 0
nova/tests/virt/libvirt/test_libvirt.py View File

@@ -3361,6 +3361,48 @@ class LibvirtConnTestCase(test.TestCase):
3361 3361
     def test_create_images_and_backing_raw(self):
3362 3362
         self._do_test_create_images_and_backing('raw')
3363 3363
 
3364
+    def test_create_images_and_backing_ephemeral_gets_created(self):
3365
+        conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
3366
+        disk_info_json = jsonutils.dumps(
3367
+            [{u'backing_file': u'fake_image_backing_file',
3368
+              u'disk_size': 10747904,
3369
+              u'path': u'disk_path',
3370
+              u'type': u'qcow2',
3371
+              u'virt_disk_size': 25165824},
3372
+             {u'backing_file': u'ephemeral_1_default',
3373
+              u'disk_size': 393216,
3374
+              u'over_committed_disk_size': 1073348608,
3375
+              u'path': u'disk_eph_path',
3376
+              u'type': u'qcow2',
3377
+              u'virt_disk_size': 1073741824}])
3378
+
3379
+        base_dir = os.path.join(CONF.instances_path,
3380
+                                CONF.image_cache_subdirectory_name)
3381
+        self.test_instance.update({'name': 'fake_instance',
3382
+                                   'user_id': 'fake-user',
3383
+                                   'os_type': None,
3384
+                                   'project_id': 'fake-project'})
3385
+
3386
+        with contextlib.nested(
3387
+            mock.patch.object(conn, '_fetch_instance_kernel_ramdisk'),
3388
+            mock.patch.object(libvirt_driver.libvirt_utils, 'fetch_image'),
3389
+            mock.patch.object(conn, '_create_ephemeral')
3390
+        ) as (fetch_kernel_ramdisk_mock, fetch_image_mock,
3391
+                create_ephemeral_mock):
3392
+            conn._create_images_and_backing(self.context, self.test_instance,
3393
+                                            "/fake/instance/dir",
3394
+                                            disk_info_json)
3395
+            self.assertEqual(len(create_ephemeral_mock.call_args_list), 1)
3396
+            m_args, m_kwargs = create_ephemeral_mock.call_args_list[0]
3397
+            self.assertEqual(
3398
+                    os.path.join(base_dir, 'ephemeral_1_default'),
3399
+                    m_kwargs['target'])
3400
+            self.assertEqual(len(fetch_image_mock.call_args_list), 1)
3401
+            m_args, m_kwargs = fetch_image_mock.call_args_list[0]
3402
+            self.assertEqual(
3403
+                    os.path.join(base_dir, 'fake_image_backing_file'),
3404
+                    m_kwargs['target'])
3405
+
3364 3406
     def test_create_images_and_backing_disk_info_none(self):
3365 3407
         conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
3366 3408
         self.mox.StubOutWithMock(conn, '_fetch_instance_kernel_ramdisk')

+ 22
- 7
nova/virt/libvirt/driver.py View File

@@ -4387,13 +4387,28 @@ class LibvirtDriver(driver.ComputeDriver):
4387 4387
                 image = self.image_backend.image(instance,
4388 4388
                                                  instance_disk,
4389 4389
                                                  CONF.libvirt.images_type)
4390
-                image.cache(fetch_func=libvirt_utils.fetch_image,
4391
-                            context=context,
4392
-                            filename=cache_name,
4393
-                            image_id=instance['image_ref'],
4394
-                            user_id=instance['user_id'],
4395
-                            project_id=instance['project_id'],
4396
-                            size=info['virt_disk_size'])
4390
+                if cache_name.startswith('ephemeral'):
4391
+                    image.cache(fetch_func=self._create_ephemeral,
4392
+                                fs_label=cache_name,
4393
+                                os_type=instance["os_type"],
4394
+                                filename=cache_name,
4395
+                                size=info['virt_disk_size'],
4396
+                                ephemeral_size=instance['ephemeral_gb'])
4397
+                elif cache_name.startswith('swap'):
4398
+                    inst_type = flavors.extract_flavor(instance)
4399
+                    swap_mb = inst_type['swap']
4400
+                    image.cache(fetch_func=self._create_swap,
4401
+                                filename="swap_%s" % swap_mb,
4402
+                                size=swap_mb * units.Mi,
4403
+                                swap_mb=swap_mb)
4404
+                else:
4405
+                    image.cache(fetch_func=libvirt_utils.fetch_image,
4406
+                                context=context,
4407
+                                filename=cache_name,
4408
+                                image_id=instance['image_ref'],
4409
+                                user_id=instance['user_id'],
4410
+                                project_id=instance['project_id'],
4411
+                                size=info['virt_disk_size'])
4397 4412
 
4398 4413
         # if image has kernel and ramdisk, just download
4399 4414
         # following normal way.

Loading…
Cancel
Save