Browse Source

libvirt: Fix root disk leak in live mig

This patch makes sure that _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>

(cherry picked from commit c69a619668)

Conflicts:
	nova/virt/libvirt/driver.py
Change-Id: I78aa2f4243899db4f4941e77014a7e18e27fc63e
tags/2013.2.2
Nikola Dipanov 5 years ago
parent
commit
15ee7e17f6
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

@@ -3047,6 +3047,48 @@ class LibvirtConnTestCase(test.TestCase):
3047 3047
         conn._create_images_and_backing(self.context, self.test_instance,
3048 3048
                                         "/fake/instance/dir", disk_info_json)
3049 3049
 
3050
+    def test_create_images_and_backing_ephemeral_gets_created(self):
3051
+        conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
3052
+        disk_info_json = jsonutils.dumps(
3053
+            [{u'backing_file': u'fake_image_backing_file',
3054
+              u'disk_size': 10747904,
3055
+              u'path': u'disk_path',
3056
+              u'type': u'qcow2',
3057
+              u'virt_disk_size': 25165824},
3058
+             {u'backing_file': u'ephemeral_1_default',
3059
+              u'disk_size': 393216,
3060
+              u'over_committed_disk_size': 1073348608,
3061
+              u'path': u'disk_eph_path',
3062
+              u'type': u'qcow2',
3063
+              u'virt_disk_size': 1073741824}])
3064
+
3065
+        base_dir = os.path.join(CONF.instances_path,
3066
+                                CONF.base_dir_name)
3067
+        self.test_instance.update({'name': 'fake_instance',
3068
+                                   'user_id': 'fake-user',
3069
+                                   'os_type': None,
3070
+                                   'project_id': 'fake-project'})
3071
+
3072
+        with contextlib.nested(
3073
+            mock.patch.object(conn, '_fetch_instance_kernel_ramdisk'),
3074
+            mock.patch.object(libvirt_driver.libvirt_utils, 'fetch_image'),
3075
+            mock.patch.object(conn, '_create_ephemeral')
3076
+        ) as (fetch_kernel_ramdisk_mock, fetch_image_mock,
3077
+                create_ephemeral_mock):
3078
+            conn._create_images_and_backing(self.context, self.test_instance,
3079
+                                            "/fake/instance/dir",
3080
+                                            disk_info_json)
3081
+            self.assertEqual(len(create_ephemeral_mock.call_args_list), 1)
3082
+            m_args, m_kwargs = create_ephemeral_mock.call_args_list[0]
3083
+            self.assertEqual(
3084
+                    os.path.join(base_dir, 'ephemeral_1_default'),
3085
+                    m_kwargs['target'])
3086
+            self.assertEqual(len(fetch_image_mock.call_args_list), 1)
3087
+            m_args, m_kwargs = fetch_image_mock.call_args_list[0]
3088
+            self.assertEqual(
3089
+                    os.path.join(base_dir, 'fake_image_backing_file'),
3090
+                    m_kwargs['target'])
3091
+
3050 3092
     def test_create_images_and_backing_disk_info_none(self):
3051 3093
         conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
3052 3094
         self.mox.StubOutWithMock(conn, '_fetch_instance_kernel_ramdisk')

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

@@ -4210,13 +4210,28 @@ class LibvirtDriver(driver.ComputeDriver):
4210 4210
                 image = self.image_backend.image(instance,
4211 4211
                                                  instance_disk,
4212 4212
                                                  CONF.libvirt_images_type)
4213
-                image.cache(fetch_func=libvirt_utils.fetch_image,
4214
-                            context=context,
4215
-                            filename=cache_name,
4216
-                            image_id=instance['image_ref'],
4217
-                            user_id=instance['user_id'],
4218
-                            project_id=instance['project_id'],
4219
-                            size=info['virt_disk_size'])
4213
+                if cache_name.startswith('ephemeral'):
4214
+                    image.cache(fetch_func=self._create_ephemeral,
4215
+                                fs_label=cache_name,
4216
+                                os_type=instance["os_type"],
4217
+                                filename=cache_name,
4218
+                                size=info['virt_disk_size'],
4219
+                                ephemeral_size=instance['ephemeral_gb'])
4220
+                elif cache_name.startswith('swap'):
4221
+                    inst_type = flavors.extract_flavor(instance)
4222
+                    swap_mb = inst_type['swap']
4223
+                    image.cache(fetch_func=self._create_swap,
4224
+                                filename="swap_%s" % swap_mb,
4225
+                                size=swap_mb * (1024 ** 2),
4226
+                                swap_mb=swap_mb)
4227
+                else:
4228
+                    image.cache(fetch_func=libvirt_utils.fetch_image,
4229
+                                context=context,
4230
+                                filename=cache_name,
4231
+                                image_id=instance['image_ref'],
4232
+                                user_id=instance['user_id'],
4233
+                                project_id=instance['project_id'],
4234
+                                size=info['virt_disk_size'])
4220 4235
 
4221 4236
         # if image has kernel and ramdisk, just download
4222 4237
         # following normal way.

Loading…
Cancel
Save