From a9a82b2e9bdf0549cdbc2b8ad1f6d81e5ca63de3 Mon Sep 17 00:00:00 2001 From: Lucian Petrut Date: Thu, 29 Nov 2018 13:48:56 +0200 Subject: [PATCH] Report the correct image format to glance when snapshotting When taking instance snapshots, the image format is always set to "vhd", but it may as well be vhdx. This happens because of legacy reasons, as Glance used to reject vhdx images. Cinder started to validate the image formats a few releases ago for security reasons, so it will reject creating volumes from Glance images that have incorrect image formats. This change ensures that we'll set the right image format when taking snapshots. Closes-Bug: #1805442 Change-Id: Ie151ed15bc806e0c16a363734d0c078fe358b899 --- compute_hyperv/nova/snapshotops.py | 4 +++- compute_hyperv/tests/unit/test_snapshotops.py | 10 +++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/compute_hyperv/nova/snapshotops.py b/compute_hyperv/nova/snapshotops.py index 108e5635..820743ea 100644 --- a/compute_hyperv/nova/snapshotops.py +++ b/compute_hyperv/nova/snapshotops.py @@ -39,10 +39,12 @@ class SnapshotOps(object): self._vhdutils = utilsfactory.get_vhdutils() def _save_glance_image(self, context, image_id, image_vhd_path): + image_format = self._vhdutils.get_vhd_format(image_vhd_path).lower() + (glance_image_service, image_id) = glance.get_remote_image_service(context, image_id) image_metadata = {"is_public": False, - "disk_format": "vhd", + "disk_format": image_format, "container_format": "bare"} with self._pathutils.open(image_vhd_path, 'rb') as f: glance_image_service.update(context, image_id, image_metadata, f, diff --git a/compute_hyperv/tests/unit/test_snapshotops.py b/compute_hyperv/tests/unit/test_snapshotops.py index ad21de66..492fda32 100644 --- a/compute_hyperv/tests/unit/test_snapshotops.py +++ b/compute_hyperv/tests/unit/test_snapshotops.py @@ -38,17 +38,25 @@ class SnapshotOpsTestCase(test_base.HyperVBaseTestCase): self.context = 'fake_context' self._snapshotops = snapshotops.SnapshotOps() + self._vhdutils = self._snapshotops._vhdutils + @mock.patch('nova.image.glance.get_remote_image_service') def test_save_glance_image(self, mock_get_remote_image_service): + fake_fmt = 'fake_fmt' image_metadata = {"is_public": False, - "disk_format": "vhd", + "disk_format": fake_fmt, "container_format": "bare"} glance_image_service = mock.MagicMock() + self._vhdutils.get_vhd_format.return_value = fake_fmt.upper() mock_get_remote_image_service.return_value = (glance_image_service, mock.sentinel.IMAGE_ID) + self._snapshotops._save_glance_image(context=self.context, image_id=mock.sentinel.IMAGE_ID, image_vhd_path=mock.sentinel.PATH) + + self._vhdutils.get_vhd_format.assert_called_once_with( + mock.sentinel.PATH) mock_get_remote_image_service.assert_called_once_with( self.context, mock.sentinel.IMAGE_ID) self._snapshotops._pathutils.open.assert_called_with(