[PATCH qemu-server 2/4] move get_host_phys_address_bits() to helpers module

Fiona Ebner
2026-03-23T13:29:19Z

To be re-used by a check in the CPU config module.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
src/PVE/QemuServer/Helpers.pm | 18 ++++++++++++++++++
src/PVE/QemuServer/Memory.pm | 23 +++--------------------
src/test/run_config2command_tests.pl | 11 ++++++++---
3 files changed, 29 insertions(+), 23 deletions(-)

diff --git a/src/PVE/QemuServer/Helpers.pm b/src/PVE/QemuServer/Helpers.pm
index e14e9ad4..65f4ce5f 100644
--- a/src/PVE/QemuServer/Helpers.pm
+++ b/src/PVE/QemuServer/Helpers.pm
@@ -369,4 +369,22 @@ sub get_iscsi_initiator_name {
return $initiator;
}

+my $_host_bits;
+
+sub get_host_phys_address_bits {
+ return $_host_bits if defined($_host_bits);
+
+ my $fh = IO::File->new('/proc/cpuinfo', "r") or return;
+ while (defined(my $line = <$fh>)) {
+ # hopefully we never need to care about mixed (big.LITTLE) archs
+ if ($line =~ m/^address sizes\s*:\s*(\d+)\s*bits physical/i) {
+ $_host_bits = int($1);
+ $fh->close();
+ return $_host_bits;
+ }
+ }
+ $fh->close();
+ return; # undef, cannot really do anything..
+}
+
1;
diff --git a/src/PVE/QemuServer/Memory.pm b/src/PVE/QemuServer/Memory.pm
index 7ebfc545..ab337bea 100644
--- a/src/PVE/QemuServer/Memory.pm
+++ b/src/PVE/QemuServer/Memory.pm
@@ -93,24 +93,6 @@ sub parse_memory {
return $res;
}

-my $_host_bits;
-
-sub get_host_phys_address_bits {
- return $_host_bits if defined($_host_bits);
-
- my $fh = IO::File->new('/proc/cpuinfo', "r") or return;
- while (defined(my $line = <$fh>)) {
- # hopefully we never need to care about mixed (big.LITTLE) archs
- if ($line =~ m/^address sizes\s*:\s*(\d+)\s*bits physical/i) {
- $_host_bits = int($1);
- $fh->close();
- return $_host_bits;
- }
- }
- $fh->close();
- return; # undef, cannot really do anything..
-}
-
my sub get_max_mem {
my ($conf) = @_;

@@ -122,14 +104,15 @@ my sub get_max_mem {
my $bits;
if (my $phys_bits = $cpu->{'phys-bits'}) {
if ($phys_bits eq 'host') {
- $bits = get_host_phys_address_bits();
+ $bits = PVE::QemuServer::Helpers::get_host_phys_address_bits();
} elsif ($phys_bits =~ /^(\d+)$/) {
$bits = int($phys_bits);
}
}

if (!defined($bits)) {
- my $host_bits = get_host_phys_address_bits() // 36; # fixme: what fallback?
+ # fixme: what fallback?
+ my $host_bits = PVE::QemuServer::Helpers::get_host_phys_address_bits() // 36;
if ($cpu->{cputype} && $cpu->{cputype} =~ /^(host|max)$/) {
$bits = $host_bits;
} else {
diff --git a/src/test/run_config2command_tests.pl b/src/test/run_config2command_tests.pl
index f7f3a2d5..3c4a695c 100755
--- a/src/test/run_config2command_tests.pl
+++ b/src/test/run_config2command_tests.pl
@@ -370,6 +370,14 @@ $qemu_server_config->mock(
},
);

+my $qemu_server_helpers;
+$qemu_server_helpers = Test::MockModule->new('PVE::QemuServer::Helpers');
+$qemu_server_helpers->mock(
+ get_host_phys_address_bits => sub {
+ return 46;
+ },
+);
+
my $qemu_server_memory;
$qemu_server_memory = Test::MockModule->new('PVE::QemuServer::Memory');
$qemu_server_memory->mock(
@@ -380,9 +388,6 @@ $qemu_server_memory->mock(
my ($id) = @_;
return 1;
},
- get_host_phys_address_bits => sub {
- return 46;
- },
);

my $pve_common_tools;
--
2.47.3