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
This commit is contained in:
Lucian Petrut 2018-11-29 13:48:56 +02:00
parent 710e2a2b5f
commit a9a82b2e9b
2 changed files with 12 additions and 2 deletions

View File

@ -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,

View File

@ -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(