From eca509193ed09a1e33d2441d9bc338ac3001e23f Mon Sep 17 00:00:00 2001 From: mnecas Date: Wed, 18 Nov 2020 13:02:07 +0100 Subject: [PATCH 1/4] ovirt_disk: automatically detect virtual size of qcow image --- bindep.txt | 1 + ovirt-ansible-collection.spec.in | 1 + plugins/modules/ovirt_disk.py | 18 ++++++++++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) 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/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..11760add 100644 --- a/plugins/modules/ovirt_disk.py +++ b/plugins/modules/ovirt_disk.py @@ -362,6 +362,8 @@ import time import traceback import ssl +import json +import subprocess 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,16 @@ 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', + '-f', 'qcow2' if self._module.params.get('format') == 'cow' else 'raw', + '-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 From bd39d22ed06ef66cc3fd164aa6468457bc585f87 Mon Sep 17 00:00:00 2001 From: mnecas Date: Wed, 18 Nov 2020 13:05:20 +0100 Subject: [PATCH 2/4] add fragment --- changelogs/fragments/183-ovirt_disk-fix-upload-detection.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelogs/fragments/183-ovirt_disk-fix-upload-detection.yml 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). From 40c263912c3b9b0510055a79b9f6764972b4f1fe Mon Sep 17 00:00:00 2001 From: mnecas Date: Mon, 7 Dec 2020 13:35:24 +0100 Subject: [PATCH 3/4] remove format from measure --- plugins/modules/ovirt_disk.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/modules/ovirt_disk.py b/plugins/modules/ovirt_disk.py index 11760add..9c5103bf 100644 --- a/plugins/modules/ovirt_disk.py +++ b/plugins/modules/ovirt_disk.py @@ -358,11 +358,11 @@ type: dict ''' +import json import os import time import traceback import ssl -import json import subprocess from ansible.module_utils.six.moves.http_client import HTTPSConnection, IncompleteRead @@ -597,7 +597,6 @@ def build_entity(self): out = subprocess.check_output([ 'qemu-img', 'measure', - '-f', 'qcow2' if self._module.params.get('format') == 'cow' else 'raw', '-O', 'qcow2' if self._module.params.get('format') == 'cow' else 'raw', '--output', 'json', self._module.params['upload_image_path'] From db30e897a19d18919e3eab0832df83db6d7e86dd Mon Sep 17 00:00:00 2001 From: mnecas Date: Mon, 7 Dec 2020 13:45:21 +0100 Subject: [PATCH 4/4] resort imports --- plugins/modules/ovirt_disk.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/ovirt_disk.py b/plugins/modules/ovirt_disk.py index 9c5103bf..bb8231a6 100644 --- a/plugins/modules/ovirt_disk.py +++ b/plugins/modules/ovirt_disk.py @@ -360,10 +360,10 @@ import json import os -import time -import traceback import ssl import subprocess +import time +import traceback from ansible.module_utils.six.moves.http_client import HTTPSConnection, IncompleteRead from ansible.module_utils.six.moves.urllib.parse import urlparse