Allow qemu-img to write out zeros to disk

Allow the sparse_size for qemu-img to be specified in order
instruct it to write out all zeros(when set to 0). Doing this
will cause it not to zero out the entire block device
which can be very costly on a slow HDD.

Story: 2009227
Task: 43315
Change-Id: I480352be378020a6063fe247238b18713031f6b0
This commit is contained in:
Derek Higgins 2021-09-14 16:41:30 +01:00
parent 509d9ce360
commit b6416eee71
2 changed files with 9 additions and 5 deletions

View File

@ -507,11 +507,13 @@ def _retry_on_res_temp_unavailable(exc):
stop=tenacity.stop_after_attempt(CONF.disk_utils.image_convert_attempts),
reraise=True)
def convert_image(source, dest, out_format, run_as_root=False, cache=None,
out_of_order=False):
out_of_order=False, sparse_size=None):
"""Convert image to other format."""
cmd = ['qemu-img', 'convert', '-O', out_format]
if cache is not None:
cmd += ['-t', cache]
if sparse_size is not None:
cmd += ['-S', sparse_size]
if out_of_order:
cmd.append('-W')
cmd += [source, dest]
@ -546,7 +548,7 @@ def populate_image(src, dst, conv_flags=None):
if data.file_format == 'raw':
dd(src, dst, conv_flags=conv_flags)
else:
convert_image(src, dst, 'raw', True)
convert_image(src, dst, 'raw', True, sparse_size='0')
def block_uuid(dev):

View File

@ -568,7 +568,8 @@ class PopulateImageTestCase(base.IronicLibTestCase):
type(mock_qinfo.return_value).file_format = mock.PropertyMock(
return_value='qcow2')
disk_utils.populate_image('src', 'dst')
mock_cg.assert_called_once_with('src', 'dst', 'raw', True)
mock_cg.assert_called_once_with('src', 'dst', 'raw', True,
sparse_size='0')
self.assertFalse(mock_dd.called)
@ -638,11 +639,12 @@ class OtherFunctionTestCase(base.IronicLibTestCase):
@mock.patch.object(utils, 'execute', autospec=True)
def test_convert_image_flags(self, execute_mock):
disk_utils.convert_image('source', 'dest', 'out_format',
cache='directsync', out_of_order=True)
cache='directsync', out_of_order=True,
sparse_size='0')
execute_mock.assert_called_once_with(
'qemu-img', 'convert', '-O',
'out_format', '-t', 'directsync',
'-W', 'source', 'dest',
'-S', '0', '-W', 'source', 'dest',
run_as_root=False,
prlimit=mock.ANY,
use_standard_locale=True,