[pve-devel] [PATCH v4 qemu-server 1/8] refactor: create QemuServer::Helpers and move file/dir code

Stefan Reiter s.reiter at proxmox.com
Tue Nov 19 12:23:44 CET 2019


Also remove unused $confdir variable in QemuConfig, but leave it and
$lock_dir there, since those paths should only be used with
cfs_config_path anyway.

nodename() is still called in multiple places, but since it's cached by
INotify it doesn't really matter.

Signed-off-by: Stefan Reiter <s.reiter at proxmox.com>
---
 PVE/CLI/qm.pm             |  3 ++-
 PVE/QMPClient.pm          |  6 +++---
 PVE/QemuConfig.pm         |  4 ++--
 PVE/QemuServer.pm         | 41 ++++++++-------------------------------
 PVE/QemuServer/Helpers.pm | 31 +++++++++++++++++++++++++++++
 PVE/QemuServer/Makefile   |  1 +
 6 files changed, 47 insertions(+), 39 deletions(-)
 create mode 100644 PVE/QemuServer/Helpers.pm

diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
index 1361581..44be39d 100755
--- a/PVE/CLI/qm.pm
+++ b/PVE/CLI/qm.pm
@@ -27,6 +27,7 @@ use PVE::Tools qw(extract_param);
 
 use PVE::API2::Qemu::Agent;
 use PVE::API2::Qemu;
+use PVE::QemuServer::Helpers;
 use PVE::QemuServer::Agent qw(agent_available);
 use PVE::QemuServer::ImportDisk;
 use PVE::QemuServer::OVF;
@@ -209,7 +210,7 @@ __PACKAGE__->register_method ({
 	my ($param) = @_;
 
 	my $vmid = $param->{vmid};
-	my $vnc_socket = PVE::QemuServer::vnc_socket($vmid);
+	my $vnc_socket = PVE::QemuServer::Helpers::vnc_socket($vmid);
 
 	if (my $ticket = $ENV{LC_PVE_TICKET}) {  # NOTE: ssh on debian only pass LC_* variables
 	    PVE::QemuServer::vm_mon_cmd($vmid, "change", device => 'vnc', target => "unix:$vnc_socket,password");
diff --git a/PVE/QMPClient.pm b/PVE/QMPClient.pm
index 96b6a24..b5c6921 100644
--- a/PVE/QMPClient.pm
+++ b/PVE/QMPClient.pm
@@ -10,7 +10,7 @@ use Scalar::Util qw(weaken);
 use Time::HiRes qw(usleep gettimeofday tv_interval);
 
 use PVE::IPCC;
-use PVE::QemuServer;
+use PVE::QemuServer::Helpers;
 
 # Qemu Monitor Protocol (QMP) client.
 #
@@ -58,7 +58,7 @@ my $push_cmd_to_queue = sub {
 
     my $qga = ($execute =~ /^guest\-+/) ? 1 : 0;
 
-    my $sname = PVE::QemuServer::qmp_socket($vmid, $qga);
+    my $sname = PVE::QemuServer::Helpers::qmp_socket($vmid, $qga);
 
     $self->{queue_info}->{$sname} = { qga => $qga, vmid => $vmid, sname => $sname, cmds => [] }
         if !$self->{queue_info}->{$sname};
@@ -186,7 +186,7 @@ my $open_connection = sub {
     my $vmid = $queue_info->{vmid};
     my $qga = $queue_info->{qga};
 
-    my $sname = PVE::QemuServer::qmp_socket($vmid, $qga);
+    my $sname = PVE::QemuServer::Helpers::qmp_socket($vmid, $qga);
 
     $timeout = 1 if !$timeout;
 
diff --git a/PVE/QemuConfig.pm b/PVE/QemuConfig.pm
index e9796a3..2c23d67 100644
--- a/PVE/QemuConfig.pm
+++ b/PVE/QemuConfig.pm
@@ -5,6 +5,7 @@ use warnings;
 
 use PVE::AbstractConfig;
 use PVE::INotify;
+use PVE::QemuServer::Helpers;
 use PVE::QemuServer;
 use PVE::Storage;
 use PVE::Tools;
@@ -14,8 +15,7 @@ use base qw(PVE::AbstractConfig);
 my $nodename = PVE::INotify::nodename();
 
 mkdir "/etc/pve/nodes/$nodename";
-my $confdir = "/etc/pve/nodes/$nodename/qemu-server";
-mkdir $confdir;
+mkdir "/etc/pve/nodes/$nodename/qemu-server";
 
 my $lock_dir = "/var/lock/qemu-server";
 mkdir $lock_dir;
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 11e7169..f667968 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -42,6 +42,7 @@ use PVE::Tools qw(run_command lock_file lock_file_full file_read_firstline dir_g
 
 use PVE::QMPClient;
 use PVE::QemuConfig;
+use PVE::QemuServer::Helpers;
 use PVE::QemuServer::Cloudinit;
 use PVE::QemuServer::Memory;
 use PVE::QemuServer::PCI qw(print_pci_addr print_pcie_addr print_pcie_root_port);
@@ -110,16 +111,6 @@ sub cgroups_write {
 
 my $nodename = PVE::INotify::nodename();
 
-mkdir "/etc/pve/nodes/$nodename";
-my $confdir = "/etc/pve/nodes/$nodename/qemu-server";
-mkdir $confdir;
-
-my $var_run_tmpdir = "/var/run/qemu-server";
-mkdir $var_run_tmpdir;
-
-my $lock_dir = "/var/lock/qemu-server";
-mkdir $lock_dir;
-
 my $cpu_vendor_list = {
     # Intel CPUs
     486 => 'GenuineIntel',
@@ -2990,7 +2981,7 @@ sub check_running {
     die "unable to find configuration file for VM $vmid - no such machine\n"
 	if !$nocheck && ! -f $filename;
 
-    my $pidfile = pidfile_name($vmid);
+    my $pidfile = PVE::QemuServer::Helpers::pidfile_name($vmid);
 
     if (my $fd = IO::File->new("<$pidfile")) {
 	my $st = stat($fd);
@@ -3019,7 +3010,7 @@ sub vzlist {
 
     my $vzlist = config_list();
 
-    my $fd = IO::Dir->new($var_run_tmpdir) || return $vzlist;
+    my $fd = IO::Dir->new($PVE::QemuServer::Helpers::var_run_tmpdir) || return $vzlist;
 
     while (defined(my $de = $fd->read)) {
 	next if $de !~ m/^(\d+)\.pid$/;
@@ -3559,7 +3550,7 @@ sub config_to_command {
 
     my $use_virtio = 0;
 
-    my $qmpsocket = qmp_socket($vmid);
+    my $qmpsocket = PVE::QemuServer::Helpers::qmp_socket($vmid);
     push @$cmd, '-chardev', "socket,id=qmp,path=$qmpsocket,server,nowait";
     push @$cmd, '-mon', "chardev=qmp,mode=control";
 
@@ -3568,7 +3559,7 @@ sub config_to_command {
 	push @$cmd, '-mon', "chardev=qmp-event,mode=control";
     }
 
-    push @$cmd, '-pidfile' , pidfile_name($vmid);
+    push @$cmd, '-pidfile' , PVE::QemuServer::Helpers::pidfile_name($vmid);
 
     push @$cmd, '-daemonize';
 
@@ -3849,7 +3840,7 @@ sub config_to_command {
 
     if ($vga->{type} && $vga->{type} !~ m/^serial\d+$/ && $vga->{type} ne 'none'){
 	push @$devices, '-device', print_vga_device($conf, $vga, $arch, $machine_type, undef, $qxlnum, $bridges);
-	my $socket = vnc_socket($vmid);
+	my $socket = PVE::QemuServer::Helpers::vnc_socket($vmid);
 	push @$cmd,  '-vnc', "unix:$socket,password";
     } else {
 	push @$cmd, '-vga', 'none' if $vga->{type} eq 'none';
@@ -3902,7 +3893,7 @@ sub config_to_command {
     my $guest_agent = parse_guest_agent($conf);
 
     if ($guest_agent->{enabled}) {
-	my $qgasocket = qmp_socket($vmid, 1);
+	my $qgasocket = PVE::QemuServer::Helpers::qmp_socket($vmid, 1);
 	push @$devices, '-chardev', "socket,path=$qgasocket,server,nowait,id=qga0";
 
 	if ($guest_agent->{type} eq 'virtio') {
@@ -4121,11 +4112,6 @@ sub config_to_command {
     return wantarray ? ($cmd, $vollist, $spice_port) : $cmd;
 }
 
-sub vnc_socket {
-    my ($vmid) = @_;
-    return "${var_run_tmpdir}/$vmid.vnc";
-}
-
 sub spice_port {
     my ($vmid) = @_;
 
@@ -4134,17 +4120,6 @@ sub spice_port {
     return $res->{'tls-port'} || $res->{'port'} || die "no spice port\n";
 }
 
-sub qmp_socket {
-    my ($vmid, $qga) = @_;
-    my $sockettype = $qga ? 'qga' : 'qmp';
-    return "${var_run_tmpdir}/$vmid.$sockettype";
-}
-
-sub pidfile_name {
-    my ($vmid) = @_;
-    return "${var_run_tmpdir}/$vmid.pid";
-}
-
 sub vm_devices_list {
     my ($vmid) = @_;
 
@@ -5607,7 +5582,7 @@ sub vm_qmp_command {
 
     eval {
 	die "VM $vmid not running\n" if !check_running($vmid, $nocheck);
-	my $sname = qmp_socket($vmid);
+	my $sname = PVE::QemuServer::Helpers::qmp_socket($vmid);
 	if (-e $sname) { # test if VM is reasonambe new and supports qmp/qga
 	    my $qmpclient = PVE::QMPClient->new();
 
diff --git a/PVE/QemuServer/Helpers.pm b/PVE/QemuServer/Helpers.pm
new file mode 100644
index 0000000..97f33c4
--- /dev/null
+++ b/PVE/QemuServer/Helpers.pm
@@ -0,0 +1,31 @@
+package PVE::QemuServer::Helpers;
+
+use strict;
+use warnings;
+
+use PVE::INotify;
+
+my $nodename = PVE::INotify::nodename();
+
+# Paths and directories
+
+our $var_run_tmpdir = "/var/run/qemu-server";
+mkdir $var_run_tmpdir;
+
+sub qmp_socket {
+    my ($vmid, $qga) = @_;
+    my $sockettype = $qga ? 'qga' : 'qmp';
+    return "${var_run_tmpdir}/$vmid.$sockettype";
+}
+
+sub pidfile_name {
+    my ($vmid) = @_;
+    return "${var_run_tmpdir}/$vmid.pid";
+}
+
+sub vnc_socket {
+    my ($vmid) = @_;
+    return "${var_run_tmpdir}/$vmid.vnc";
+}
+
+1;
diff --git a/PVE/QemuServer/Makefile b/PVE/QemuServer/Makefile
index afc39a3..56d1493 100644
--- a/PVE/QemuServer/Makefile
+++ b/PVE/QemuServer/Makefile
@@ -5,6 +5,7 @@ SOURCES=PCI.pm		\
 	OVF.pm		\
 	Cloudinit.pm	\
 	Agent.pm	\
+	Helpers.pm	\
 
 .PHONY: install
 install: ${SOURCES}
-- 
2.20.1





More information about the pve-devel mailing list