1850a1951SThomas Huth# Utilities for python-based QEMU tests 2850a1951SThomas Huth# 3850a1951SThomas Huth# Copyright 2024 Red Hat, Inc. 4850a1951SThomas Huth# 5850a1951SThomas Huth# Authors: 6850a1951SThomas Huth# Thomas Huth <thuth@redhat.com> 7850a1951SThomas Huth# 8850a1951SThomas Huth# This work is licensed under the terms of the GNU GPL, version 2 or 9850a1951SThomas Huth# later. See the COPYING file in the top-level directory. 10850a1951SThomas Huth 11e2e9fd25SThomas Huthimport os 12cfcb4484SDaniel P. Berrangé 13*9fa4fc23SThomas Huthfrom qemu.utils import get_info_usernet_hostfwd_port 14*9fa4fc23SThomas Huth 15*9fa4fc23SThomas Huth 16*9fa4fc23SThomas Huthdef get_usernet_hostfwd_port(vm): 17*9fa4fc23SThomas Huth res = vm.cmd('human-monitor-command', command_line='info usernet') 18*9fa4fc23SThomas Huth return get_info_usernet_hostfwd_port(res) 19*9fa4fc23SThomas Huth 20f7d6b772SThomas Huth""" 21f7d6b772SThomas HuthRound up to next power of 2 22f7d6b772SThomas Huth""" 23f7d6b772SThomas Huthdef pow2ceil(x): 24f7d6b772SThomas Huth return 1 if x == 0 else 2**(x - 1).bit_length() 25f7d6b772SThomas Huth 26f7d6b772SThomas Huthdef file_truncate(path, size): 27f7d6b772SThomas Huth if size != os.path.getsize(path): 28f7d6b772SThomas Huth with open(path, 'ab+') as fd: 29f7d6b772SThomas Huth fd.truncate(size) 30f7d6b772SThomas Huth 31f7d6b772SThomas Huth""" 32f7d6b772SThomas HuthExpand file size to next power of 2 33f7d6b772SThomas Huth""" 34f7d6b772SThomas Huthdef image_pow2ceil_expand(path): 35f7d6b772SThomas Huth size = os.path.getsize(path) 36f7d6b772SThomas Huth size_aligned = pow2ceil(size) 37f7d6b772SThomas Huth if size != size_aligned: 38f7d6b772SThomas Huth with open(path, 'ab+') as fd: 39f7d6b772SThomas Huth fd.truncate(size_aligned) 40