From 6b923903b1f464c637deee95083a00e4c792314f Mon Sep 17 00:00:00 2001 From: Claudiu Belu Date: Tue, 5 Jul 2016 19:05:31 +0300 Subject: [PATCH] Hyper-V: Fixes disk overhead claim issue For each started VM, Hyper-V creates a VM memory file on the local disk. The size of the file is the same as the size of the VM's memory amount. Implements the mentioned method in HyperVDriver. (cherry-picked from commit f3ad794bf75ac4b4251bcbbcbdb5ec7791fbea2e) Change-Id: I51764af539f0a595c81fb3f58ccd8af2d853f5f2 Closes-Bug: #1505681 --- hyperv/nova/driver.py | 3 +++ hyperv/nova/vmops.py | 8 ++++++++ hyperv/tests/unit/test_driver.py | 5 +++++ hyperv/tests/unit/test_vmops.py | 10 ++++++++++ 4 files changed, 26 insertions(+) diff --git a/hyperv/nova/driver.py b/hyperv/nova/driver.py index b0771d06..73ae280b 100644 --- a/hyperv/nova/driver.py +++ b/hyperv/nova/driver.py @@ -150,6 +150,9 @@ class HyperVDriver(driver.ComputeDriver): def list_instances(self): return self._vmops.list_instances() + def estimate_instance_overhead(self, instance_info): + return self._vmops.estimate_instance_overhead(instance_info) + def spawn(self, context, instance, image_meta, injected_files, admin_password, network_info=None, block_device_info=None): image_meta = self._recreate_image_meta(context, instance, image_meta) diff --git a/hyperv/nova/vmops.py b/hyperv/nova/vmops.py index 24e87212..8e959fe4 100644 --- a/hyperv/nova/vmops.py +++ b/hyperv/nova/vmops.py @@ -114,6 +114,14 @@ class VMOps(object): def list_instances(self): return self._vmutils.list_instances() + def estimate_instance_overhead(self, instance_info): + # NOTE(claudiub): When an instance starts, Hyper-V creates a VM memory + # file on the local disk. The file size is the same as the VM's amount + # of memory. Since disk_gb must be an integer, and memory is MB, round + # up from X512 MB. + return {'memory_mb': 0, + 'disk_gb': (instance_info['memory_mb'] + 512) // units.Ki} + def get_info(self, instance): """Get information about the VM.""" LOG.debug("get_info called for instance", instance=instance) diff --git a/hyperv/tests/unit/test_driver.py b/hyperv/tests/unit/test_driver.py index 766b52c3..66ec2a25 100644 --- a/hyperv/tests/unit/test_driver.py +++ b/hyperv/tests/unit/test_driver.py @@ -144,6 +144,11 @@ class HyperVDriverTestCase(test_base.HyperVBaseTestCase): self.driver.list_instances() self.driver._vmops.list_instances.assert_called_once_with() + def test_estimate_instance_overhead(self): + self.driver.estimate_instance_overhead(mock.sentinel.instance) + self.driver._vmops.estimate_instance_overhead.assert_called_once_with( + mock.sentinel.instance) + @mock.patch.object(driver.HyperVDriver, '_recreate_image_meta') def test_spawn(self, mock_recreate_img_meta): self.driver.spawn( diff --git a/hyperv/tests/unit/test_vmops.py b/hyperv/tests/unit/test_vmops.py index b1a43eba..b4102c97 100644 --- a/hyperv/tests/unit/test_vmops.py +++ b/hyperv/tests/unit/test_vmops.py @@ -96,6 +96,16 @@ class VMOpsTestCase(test_base.HyperVBaseTestCase): self._vmops._vmutils.list_instances.assert_called_once_with() self.assertEqual(response, [mock_instance]) + def test_estimate_instance_overhead(self): + instance_info = {'memory_mb': 512} + overhead = self._vmops.estimate_instance_overhead(instance_info) + self.assertEqual(0, overhead['memory_mb']) + self.assertEqual(1, overhead['disk_gb']) + + instance_info = {'memory_mb': 500} + overhead = self._vmops.estimate_instance_overhead(instance_info) + self.assertEqual(0, overhead['disk_gb']) + def _test_get_info(self, vm_exists): mock_instance = fake_instance.fake_instance_obj(self.context) mock_info = mock.MagicMock(spec_set=dict)