diff --git a/bindep.txt b/bindep.txt index c27bf85f..50a38981 100644 --- a/bindep.txt +++ b/bindep.txt @@ -3,3 +3,4 @@ python3-pycurl [platform:rpm] python3-netaddr [platform:rpm] python3-jmespath [platform:rpm] python3-passlib [platform:rpm] +qemu-img [platform:rpm] diff --git a/changelogs/fragments/183-ovirt_disk-fix-upload-detection.yml b/changelogs/fragments/183-ovirt_disk-fix-upload-detection.yml new file mode 100644 index 00000000..fba3f93a --- /dev/null +++ b/changelogs/fragments/183-ovirt_disk-fix-upload-detection.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - ovirt_disk - automatically detect virtual size of qcow image (https://github.com/oVirt/ovirt-ansible-collection/pull/183). diff --git a/ovirt-ansible-collection.spec.in b/ovirt-ansible-collection.spec.in index 7e99e0a4..192b6e71 100644 --- a/ovirt-ansible-collection.spec.in +++ b/ovirt-ansible-collection.spec.in @@ -16,6 +16,7 @@ Requires: python3-ovirt-engine-sdk4 >= 4.4.0 Requires: python3-netaddr Requires: python3-jmespath Requires: python3-passlib +Requires: qemu-img Obsoletes: ovirt-ansible-cluster-upgrade Obsoletes: ovirt-ansible-disaster-recovery diff --git a/plugins/modules/ovirt_disk.py b/plugins/modules/ovirt_disk.py index 4bb0b6ee..bb8231a6 100644 --- a/plugins/modules/ovirt_disk.py +++ b/plugins/modules/ovirt_disk.py @@ -358,10 +358,12 @@ type: dict ''' +import json import os +import ssl +import subprocess import time import traceback -import ssl from ansible.module_utils.six.moves.http_client import HTTPSConnection, IncompleteRead from ansible.module_utils.six.moves.urllib.parse import urlparse @@ -538,7 +540,10 @@ def build_entity(self): logical_unit = self._module.params.get('logical_unit') size = convert_to_bytes(self._module.params.get('size')) if not size and self._module.params.get('upload_image_path'): - size = os.path.getsize(self._module.params.get('upload_image_path')) + out = subprocess.check_output( + ["qemu-img", "info", "--output", "json", self._module.params.get('upload_image_path')]) + image_info = json.loads(out) + size = image_info["virtual-size"] disk = otypes.Disk( id=self._module.params.get('id'), name=self._module.params.get('name'), @@ -589,7 +594,15 @@ def build_entity(self): ) if logical_unit else None, ) if hasattr(disk, 'initial_size') and self._module.params['upload_image_path']: - disk.initial_size = size + out = subprocess.check_output([ + 'qemu-img', + 'measure', + '-O', 'qcow2' if self._module.params.get('format') == 'cow' else 'raw', + '--output', 'json', + self._module.params['upload_image_path'] + ]) + measure = json.loads(out) + disk.initial_size = measure["required"] return disk