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>

This patch also includes part of commit
65386c91910ee03d947c2b8bcc226a53c30e060a, not cherry-picked as a whole
due to the fact that it is a trivial change, and to avoud the
proliferation of patches needed to fix this bug.

(cherry picked from commit c69a619668)

Conflicts:
	nova/tests/test_libvirt.py
	nova/virt/libvirt/driver.py

Change-Id: I78aa2f4243899db4f4941e77014a7e18e27fc63e
tags/2013.1.5
Nikola Dipanov 5 years ago
parent
commit
cbeb5e5188
2 changed files with 85 additions and 9 deletions
  1. 63
    0
      nova/tests/test_libvirt.py
  2. 22
    9
      nova/virt/libvirt/driver.py

+ 63
- 0
nova/tests/test_libvirt.py View File

@@ -2346,6 +2346,69 @@ class LibvirtConnTestCase(test.TestCase):
2346 2346
 
2347 2347
         db.instance_destroy(self.context, instance_ref['uuid'])
2348 2348
 
2349
+    def test_create_images_and_backing(self):
2350
+        conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
2351
+        self.mox.StubOutWithMock(conn, '_fetch_instance_kernel_ramdisk')
2352
+        self.mox.StubOutWithMock(libvirt_driver.libvirt_utils, 'create_image')
2353
+
2354
+        libvirt_driver.libvirt_utils.create_image(mox.IgnoreArg(),
2355
+                                                  mox.IgnoreArg(),
2356
+                                                  mox.IgnoreArg())
2357
+        conn._fetch_instance_kernel_ramdisk(self.context, self.test_instance)
2358
+        self.mox.ReplayAll()
2359
+
2360
+        self.stubs.Set(os.path, 'exists', lambda *args: False)
2361
+        disk_info_json = jsonutils.dumps([{'path': 'foo', 'type': None,
2362
+                                           'disk_size': 0,
2363
+                                           'backing_file': None}])
2364
+        conn._create_images_and_backing(self.context, self.test_instance,
2365
+                                        "/fake/instance/dir", disk_info_json)
2366
+
2367
+    def test_create_images_and_backing_ephemeral_gets_created(self):
2368
+        conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
2369
+        disk_info_json = jsonutils.dumps(
2370
+            [{u'backing_file': u'fake_image_backing_file',
2371
+              u'disk_size': 10747904,
2372
+              u'path': u'disk_path',
2373
+              u'type': u'qcow2',
2374
+              u'virt_disk_size': 25165824},
2375
+             {u'backing_file': u'ephemeral_1_default',
2376
+              u'disk_size': 393216,
2377
+              u'over_committed_disk_size': 1073348608,
2378
+              u'path': u'disk_eph_path',
2379
+              u'type': u'qcow2',
2380
+              u'virt_disk_size': 1073741824}])
2381
+
2382
+        base_dir = os.path.join(CONF.instances_path, '_base')
2383
+        ephemeral_target = os.path.join(base_dir, 'ephemeral_1_default')
2384
+        image_target = os.path.join(base_dir, 'fake_image_backing_file')
2385
+        self.test_instance.update({'name': 'fake_instance',
2386
+                                   'user_id': 'fake-user',
2387
+                                   'os_type': None,
2388
+                                   'project_id': 'fake-project'})
2389
+
2390
+        self.mox.StubOutWithMock(libvirt_driver.libvirt_utils, 'fetch_image')
2391
+        self.mox.StubOutWithMock(conn, '_create_ephemeral')
2392
+        self.mox.StubOutWithMock(conn, '_fetch_instance_kernel_ramdisk')
2393
+
2394
+        conn._create_ephemeral(
2395
+                target=ephemeral_target,
2396
+                ephemeral_size=self.test_instance['ephemeral_gb'],
2397
+                max_size=mox.IgnoreArg(), os_type=mox.IgnoreArg(),
2398
+                fs_label=mox.IgnoreArg())
2399
+        libvirt_driver.libvirt_utils.fetch_image(context=self.context,
2400
+                image_id=mox.IgnoreArg(),
2401
+                user_id=mox.IgnoreArg(), project_id=mox.IgnoreArg(),
2402
+                max_size=mox.IgnoreArg(), target=image_target)
2403
+        conn._fetch_instance_kernel_ramdisk(
2404
+                self.context, self.test_instance).AndReturn(None)
2405
+
2406
+        self.mox.ReplayAll()
2407
+
2408
+        conn._create_images_and_backing(self.context, self.test_instance,
2409
+                                        "/fake/instance/dir",
2410
+                                        disk_info_json)
2411
+
2349 2412
     def test_pre_live_migration_works_correctly_mocked(self):
2350 2413
         # Creating testdata
2351 2414
         vol = {'block_device_mapping': [

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

@@ -3304,19 +3304,32 @@ class LibvirtDriver(driver.ComputeDriver):
3304 3304
             elif info['backing_file']:
3305 3305
                 # Creating backing file follows same way as spawning instances.
3306 3306
                 cache_name = os.path.basename(info['backing_file'])
3307
-                # Remove any size tags which the cache manages
3308
-                cache_name = cache_name.split('_')[0]
3309 3307
 
3310 3308
                 image = self.image_backend.image(instance,
3311 3309
                                                  instance_disk,
3312 3310
                                                  CONF.libvirt_images_type)
3313
-                image.cache(fetch_func=libvirt_utils.fetch_image,
3314
-                            context=ctxt,
3315
-                            filename=cache_name,
3316
-                            image_id=instance['image_ref'],
3317
-                            user_id=instance['user_id'],
3318
-                            project_id=instance['project_id'],
3319
-                            size=info['virt_disk_size'])
3311
+                if cache_name.startswith('ephemeral'):
3312
+                    image.cache(fetch_func=self._create_ephemeral,
3313
+                                fs_label=cache_name,
3314
+                                os_type=instance["os_type"],
3315
+                                filename=cache_name,
3316
+                                size=info['virt_disk_size'],
3317
+                                ephemeral_size=instance['ephemeral_gb'])
3318
+                elif cache_name.startswith('swap'):
3319
+                    inst_type = instance_types.extract_instance_type(instance)
3320
+                    swap_mb = inst_type['swap']
3321
+                    image.cache(fetch_func=self._create_swap,
3322
+                                filename="swap_%s" % swap_mb,
3323
+                                size=swap_mb * (1024 ** 2),
3324
+                                swap_mb=swap_mb)
3325
+                else:
3326
+                    image.cache(fetch_func=libvirt_utils.fetch_image,
3327
+                                context=ctxt,
3328
+                                filename=cache_name,
3329
+                                image_id=instance['image_ref'],
3330
+                                user_id=instance['user_id'],
3331
+                                project_id=instance['project_id'],
3332
+                                size=info['virt_disk_size'])
3320 3333
 
3321 3334
         # if image has kernel and ramdisk, just download
3322 3335
         # following normal way.

Loading…
Cancel
Save