From 6104225fd0c7c71811635eb0e0e5cbdee8306fc4 Mon Sep 17 00:00:00 2001 From: Ann Kamyshnikova Date: Fri, 12 Jul 2013 17:17:40 +0400 Subject: [PATCH] MinDisk size based on the flavor's Disk size When you create an instance from image the instance disk size is expanded to the disk size specified in the flavor. Normally you cannot use the snapshot for creating an instance with smaller disk size (flavor) because it potentialy causes a data corruption. Every snapshots minDisk size should be inherited from the flavor. bug 1200279 Change-Id: I1d0e909b78db2c863b4fbb54ad9065207f1a0abe --- nova/compute/api.py | 7 ++++--- nova/tests/compute/test_compute.py | 30 +++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/nova/compute/api.py b/nova/compute/api.py index 9a7ef352cb34..a6794907d3f6 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -1804,13 +1804,14 @@ class API(base.Base): except exception.ImageNotFound: return None, None + flavor = flavors.extract_flavor(instance) #disk format of vhd is non-shrinkable if orig_image.get('disk_format') == 'vhd': - instance_type = flavors.extract_flavor(instance) - min_disk = instance_type['root_gb'] + min_disk = flavor['root_gb'] else: #set new image values to the original image values - min_disk = orig_image.get('min_disk') + min_disk = max(orig_image.get('min_disk'), + flavor['root_gb']) min_ram = orig_image.get('min_ram') diff --git a/nova/tests/compute/test_compute.py b/nova/tests/compute/test_compute.py index 8d4585a1a97b..f5991f6787d8 100644 --- a/nova/tests/compute/test_compute.py +++ b/nova/tests/compute/test_compute.py @@ -6066,6 +6066,21 @@ class ComputeAPITestCase(BaseTestCase): self.assertEqual(properties['instance_uuid'], instance['uuid']) self.assertEqual(properties['extra_param'], 'value1') + def test_snapshot_mindisk_with_bigger_flavor(self): + """If minDisk is smaller than flavor root_gb, the latter should be + used as minDisk + """ + self.fake_image['min_disk'] = 0 + self.stubs.Set(fake_image._FakeImageService, 'show', self.fake_show) + + instance = self._create_fake_instance() + image = self.compute_api.snapshot(self.context, instance, 'snap1', + {'extra_param': 'value1'}) + + self.assertEqual(image['min_disk'], 1) + + db.instance_destroy(self.context, instance['uuid']) + def test_snapshot_minram_mindisk(self): """Ensure a snapshots min_ram and min_disk are correct. @@ -6127,14 +6142,19 @@ class ComputeAPITestCase(BaseTestCase): A snapshots min_ram and min_disk should be set to default if an instances original image cannot be found. """ + # Cells tests will call this a 2nd time in child cell with + # the newly created image_id, and we want that one to succeed. + old_show = fake_image._FakeImageService.show + flag = [] def fake_show(*args): - raise exception.ImageNotFound(image_id="fake") + if not flag: + flag.append(1) + raise exception.ImageNotFound(image_id="fake") + else: + return old_show(*args) - if not self.__class__.__name__ == "CellsComputeAPITestCase": - # Cells tests will call this a 2nd time in child cell with - # the newly created image_id, and we want that one to succeed. - self.stubs.Set(fake_image._FakeImageService, 'show', fake_show) + self.stubs.Set(fake_image._FakeImageService, 'show', fake_show) instance = self._create_fake_instance()