[pve-devel] [PATCH qemu-server 05/19] move kvm_user_version() function to helpers module

Fiona Ebner f.ebner at proxmox.com
Fri Jan 3 16:57:48 CET 2025


Add an export, since the function is rather commonly used (in
particular inlined in function calls, where prefixing with the module
name would hurt readability) and there won't be much potential for
confusion name-wise.

This was the only user of stat(), so remove the File::stat include.

Signed-off-by: Fiona Ebner <f.ebner at proxmox.com>
---
 PVE/API2/Qemu.pm                 |  2 +-
 PVE/QemuServer.pm                | 32 +-------------------------------
 PVE/QemuServer/Helpers.pm        | 31 +++++++++++++++++++++++++++++++
 test/run_config2command_tests.pl |  3 ++-
 4 files changed, 35 insertions(+), 33 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index dc46e2e0..41ad1cb6 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -853,7 +853,7 @@ sub assert_scsi_feature_compatibility {
 
     my $machine_type = PVE::QemuServer::get_vm_machine($conf, undef, $conf->{arch});
     my $machine_version = PVE::QemuServer::Machine::extract_version(
-	$machine_type, PVE::QemuServer::kvm_user_version());
+	$machine_type, PVE::QemuServer::Helpers::kvm_user_version());
     my $drivetype = PVE::QemuServer::Drive::get_scsi_device_type(
 	$drive, $storecfg, $machine_version);
 
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 468c9a2d..1827c024 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -10,7 +10,6 @@ use Fcntl;
 use File::Basename;
 use File::Copy qw(copy);
 use File::Path;
-use File::stat;
 use Getopt::Long;
 use IO::Dir;
 use IO::File;
@@ -51,7 +50,7 @@ use PVE::Tools qw(run_command file_read_firstline file_get_contents dir_glob_for
 
 use PVE::QMPClient;
 use PVE::QemuConfig;
-use PVE::QemuServer::Helpers qw(config_aware_timeout min_version windows_version);
+use PVE::QemuServer::Helpers qw(config_aware_timeout min_version kvm_user_version windows_version);
 use PVE::QemuServer::Cloudinit;
 use PVE::QemuServer::CGroup;
 use PVE::QemuServer::CPUConfig qw(print_cpu_device get_cpu_options get_cpu_bitness is_native_arch get_amd_sev_object);
@@ -1193,35 +1192,6 @@ sub kvm_version {
     return $kvm_api_version;
 }
 
-my $kvm_user_version = {};
-my $kvm_mtime = {};
-
-sub kvm_user_version {
-    my ($binary) = @_;
-
-    $binary //= PVE::QemuServer::Helpers::get_command_for_arch(get_host_arch()); # get the native arch by default
-    my $st = stat($binary);
-
-    my $cachedmtime = $kvm_mtime->{$binary} // -1;
-    return $kvm_user_version->{$binary} if $kvm_user_version->{$binary} &&
-	$cachedmtime == $st->mtime;
-
-    $kvm_user_version->{$binary} = 'unknown';
-    $kvm_mtime->{$binary} = $st->mtime;
-
-    my $code = sub {
-	my $line = shift;
-	if ($line =~ m/^QEMU( PC)? emulator version (\d+\.\d+(\.\d+)?)(\.\d+)?[,\s]/) {
-	    $kvm_user_version->{$binary} = $2;
-	}
-    };
-
-    eval { run_command([$binary, '--version'], outfunc => $code); };
-    warn $@ if $@;
-
-    return $kvm_user_version->{$binary};
-
-}
 my sub extract_version {
     my ($machine_type, $version) = @_;
     $version = kvm_user_version() if !defined($version);
diff --git a/PVE/QemuServer/Helpers.pm b/PVE/QemuServer/Helpers.pm
index be10a92a..07b2ff6e 100644
--- a/PVE/QemuServer/Helpers.pm
+++ b/PVE/QemuServer/Helpers.pm
@@ -8,11 +8,13 @@ use JSON;
 
 use PVE::INotify;
 use PVE::ProcFSTools;
+use PVE::Tools qw(get_host_arch);
 
 use base 'Exporter';
 our @EXPORT_OK = qw(
 min_version
 config_aware_timeout
+kvm_user_version
 parse_number_sets
 windows_version
 );
@@ -32,6 +34,35 @@ sub get_command_for_arch($) {
     return $cmd;
 }
 
+my $kvm_user_version = {};
+my $kvm_mtime = {};
+
+sub kvm_user_version {
+    my ($binary) = @_;
+
+    $binary //= get_command_for_arch(get_host_arch()); # get the native arch by default
+    my $st = stat($binary);
+
+    my $cachedmtime = $kvm_mtime->{$binary} // -1;
+    return $kvm_user_version->{$binary} if $kvm_user_version->{$binary} &&
+	$cachedmtime == $st->mtime;
+
+    $kvm_user_version->{$binary} = 'unknown';
+    $kvm_mtime->{$binary} = $st->mtime;
+
+    my $code = sub {
+	my $line = shift;
+	if ($line =~ m/^QEMU( PC)? emulator version (\d+\.\d+(\.\d+)?)(\.\d+)?[,\s]/) {
+	    $kvm_user_version->{$binary} = $2;
+	}
+    };
+
+    eval { PVE::Tools::run_command([$binary, '--version'], outfunc => $code); };
+    warn $@ if $@;
+
+    return $kvm_user_version->{$binary};
+}
+
 # Paths and directories
 
 our $var_run_tmpdir = "/var/run/qemu-server";
diff --git a/test/run_config2command_tests.pl b/test/run_config2command_tests.pl
index 5308b1fc..2feebd4a 100755
--- a/test/run_config2command_tests.pl
+++ b/test/run_config2command_tests.pl
@@ -15,6 +15,7 @@ use PVE::SysFSTools;
 
 use PVE::QemuConfig;
 use PVE::QemuServer;
+use PVE::QemuServer::Helpers;
 use PVE::QemuServer::Monitor;
 use PVE::QemuServer::QMPHelpers;
 use PVE::QemuServer::CPUConfig;
@@ -72,7 +73,7 @@ my $base_env = {
 	}
     },
     vmid => 8006,
-    real_qemu_version => PVE::QemuServer::kvm_user_version(), # not yet mocked
+    real_qemu_version => PVE::QemuServer::Helpers::kvm_user_version(), # not yet mocked
 };
 
 my $pci_devs = [
-- 
2.39.5





More information about the pve-devel mailing list