Merge "images: Allow the output format of qemu-img info to be controlled"

This commit is contained in:
Zuul 2020-03-19 12:53:43 +00:00 committed by Gerrit Code Review
commit 6b38a47425
3 changed files with 39 additions and 6 deletions

View File

@ -85,17 +85,20 @@ def unprivileged_convert_image(source, dest, in_format, out_format,
@nova.privsep.sys_admin_pctxt.entrypoint
def privileged_qemu_img_info(path, format=None, qemu_version=None):
def privileged_qemu_img_info(path, format=None, qemu_version=None,
output_format=None):
"""Return an oject containing the parsed output from qemu-img info
This is a privileged call to qemu-img info using the sys_admin_pctxt
entrypoint allowing host block devices etc to be accessed.
"""
return unprivileged_qemu_img_info(
path, format=format, qemu_version=qemu_version)
path, format=format, qemu_version=qemu_version,
output_format=output_format)
def unprivileged_qemu_img_info(path, format=None, qemu_version=None):
def unprivileged_qemu_img_info(path, format=None, qemu_version=None,
output_format=None):
"""Return an object containing the parsed output from qemu-img info."""
try:
# The following check is about ploop images that reside within
@ -107,6 +110,8 @@ def unprivileged_qemu_img_info(path, format=None, qemu_version=None):
cmd = ('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path)
if format is not None:
cmd = cmd + ('-f', format)
if output_format is not None:
cmd = cmd + ("--output=%s" % (output_format),)
# Check to see if the qemu version is >= 2.10 because if so, we need
# to add the --force-share flag.
if qemu_version and operator.ge(qemu_version, QEMU_VERSION_REQ_SHARED):

View File

@ -151,6 +151,30 @@ disk size: 96K
return_value=(output, '')) as mock_execute:
self._test_disk_size(mock_execute, path, i)
@mock.patch('os.path.exists', return_value=True)
@mock.patch('oslo_concurrency.processutils.execute')
def test_qemu_img_info_json(self, mock_execute, mock_exists):
path = "disk.config"
example_output = """{
"virtual-size": 67108864,
"filename": "disk.config",
"cluster-size": 65536,
"format": "raw",
"actual-size": 98304
}
"""
mock_execute.return_value = (example_output, '')
image_info = images.qemu_img_info(path, output_format='json')
mock_execute.assert_called_once_with(
'env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path,
'--output=json', prlimit=nova.privsep.qemu.QEMU_IMG_LIMITS)
mock_exists.assert_called_once_with(path)
self.assertEqual('disk.config', image_info.image)
self.assertEqual('raw', image_info.file_format)
self.assertEqual(67108864, image_info.virtual_size)
self.assertEqual(98304, image_info.disk_size)
self.assertEqual(65536, image_info.cluster_size)
@mock.patch('os.path.exists', return_value=True)
@mock.patch('oslo_concurrency.processutils.execute')
def test_qemu_info_canon(self, mock_execute, mock_exists):

View File

@ -43,7 +43,7 @@ CONF = nova.conf.CONF
IMAGE_API = glance.API()
def qemu_img_info(path, format=None):
def qemu_img_info(path, format=None, output_format=None):
"""Return an object containing the parsed output from qemu-img info."""
# TODO(mikal): this code should not be referring to a libvirt specific
# flag.
@ -51,8 +51,12 @@ def qemu_img_info(path, format=None):
raise exception.DiskNotFound(location=path)
info = nova.privsep.qemu.unprivileged_qemu_img_info(
path, format=format, qemu_version=QEMU_VERSION)
return imageutils.QemuImgInfo(info)
path, format=format, qemu_version=QEMU_VERSION,
output_format=output_format)
if output_format:
return imageutils.QemuImgInfo(info, format=output_format)
else:
return imageutils.QemuImgInfo(info)
def convert_image(source, dest, in_format, out_format, run_as_root=False,