Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ovirt_disk: automatically detect virtual size of qcow image #183

Merged
merged 4 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bindep.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ python3-pycurl [platform:rpm]
python3-netaddr [platform:rpm]
python3-jmespath [platform:rpm]
python3-passlib [platform:rpm]
qemu-img [platform:rpm]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- ovirt_disk - automatically detect virtual size of qcow image (https://github.com/oVirt/ovirt-ansible-collection/pull/183).
1 change: 1 addition & 0 deletions ovirt-ansible-collection.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions plugins/modules/ovirt_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@
import time
import traceback
import ssl
import json
import subprocess
mnecas marked this conversation as resolved.
Show resolved Hide resolved

from ansible.module_utils.six.moves.http_client import HTTPSConnection, IncompleteRead
from ansible.module_utils.six.moves.urllib.parse import urlparse
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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',
mnecas marked this conversation as resolved.
Show resolved Hide resolved
'-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"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

measure["required"] is not a boolean value?
disk.initial_size shouldn't be a number?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try to run qemu-img measure? It will answer your question.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. No issues :)
The JSON output is an object of QAPI type BlockMeasureInfo
Fields
fully_allocated: isize
required: isize


return disk

Expand Down