| 1 | # Utilities for python-based QEMU tests |
| 2 | # |
| 3 | # Copyright 2024 Red Hat, Inc. |
| 4 | # |
| 5 | # Authors: |
| 6 | # Thomas Huth <thuth@redhat.com> |
| 7 | # |
| 8 | # This work is licensed under the terms of the GNU GPL, version 2 or |
| 9 | # later. See the COPYING file in the top-level directory. |
| 10 | |
| 11 | import os |
| 12 | |
| 13 | from qemu.utils import get_info_usernet_hostfwd_port |
| 14 | |
| 15 | |
| 16 | def get_usernet_hostfwd_port(vm): |
| 17 | res = vm.cmd('x-query-usernet') |
| 18 | for entry in res: |
| 19 | port = get_info_usernet_hostfwd_port(entry['info']) |
| 20 | if port is not None: |
| 21 | return port |
| 22 | return None |
| 23 | |
| 24 | def pow2ceil(x): |
| 25 | """ |
| 26 | Round up to next power of 2 |
| 27 | """ |
| 28 | return 1 if x == 0 else 2**(x - 1).bit_length() |
| 29 | |
| 30 | def file_truncate(path, size): |
| 31 | if size != os.path.getsize(path): |
| 32 | with open(path, 'ab+') as fd: |
| 33 | fd.truncate(size) |
| 34 | |
| 35 | def image_pow2ceil_expand(path): |
| 36 | """ |
| 37 | Expand file size to next power of 2 |
| 38 | """ |
| 39 | size = os.path.getsize(path) |
| 40 | size_aligned = pow2ceil(size) |
| 41 | if size != size_aligned: |
| 42 | with open(path, 'ab+') as fd: |
| 43 | fd.truncate(size_aligned) |