refactor: create QemuServer::Helpers and move file/dir code

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@proxmox.com>
This commit is contained in:
Stefan Reiter 2019-11-19 12:23:44 +01:00 committed by Thomas Lamprecht
parent 60f03a1168
commit d036e418a8
6 changed files with 47 additions and 39 deletions

View File

@ -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");

View File

@ -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;

View File

@ -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;

View File

@ -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',
@ -2978,7 +2969,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);
@ -3007,7 +2998,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$/;
@ -3546,7 +3537,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";
@ -3555,7 +3546,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';
@ -3836,7 +3827,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, $kvmver, $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';
@ -3889,7 +3880,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} || $guest_agent->{type} eq 'virtio') {
@ -4108,11 +4099,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) = @_;
@ -4121,17 +4107,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) = @_;
@ -5594,7 +5569,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();

31
PVE/QemuServer/Helpers.pm Normal file
View File

@ -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;

View File

@ -5,6 +5,7 @@ SOURCES=PCI.pm \
OVF.pm \
Cloudinit.pm \
Agent.pm \
Helpers.pm \
.PHONY: install
install: ${SOURCES}