]>
git.proxmox.com Git - pve-ha-manager.git/blob - src/PVE/HA/Resources/PVEVM.pm
1 package PVE
::HA
::Resources
::PVEVM
;
9 if (!$ENV{PVE_GENERATING_DOCS
}) {
10 require PVE
::QemuConfig
;
11 import PVE
::QemuConfig
;
12 require PVE
::QemuServer
;
13 import PVE
::QemuServer
;
14 require PVE
::API2
::Qemu
;
15 import PVE
::API2
::Qemu
;
19 use base
qw(PVE::HA::Resources);
26 my ($class, $name) = @_;
28 die "invalid VMID\n" if $name !~ m/^[1-9][0-9]+$/;
33 state => { optional
=> 1 },
34 group
=> { optional
=> 1 },
35 comment
=> { optional
=> 1 },
36 max_restart
=> { optional
=> 1 },
37 max_relocate
=> { optional
=> 1 },
42 my ($class, $vmid, $nodename) = @_;
44 return PVE
::QemuConfig-
>config_file($vmid, $nodename);
48 my ($class, $vmid, $noerr) = @_;
50 my $vmlist = PVE
::Cluster
::get_vmlist
();
52 if(!defined($vmlist->{ids
}->{$vmid})) {
53 die "resource 'vm:$vmid' does not exists in cluster\n" if !$noerr;
61 my ($class, $haenv, $id) = @_;
63 my $nodename = $haenv->nodename();
70 my $upid = PVE
::API2
::Qemu-
>vm_start($params);
71 PVE
::HA
::Tools
::upid_wait
($upid, $haenv);
75 my ($class, $haenv, $id, $timeout) = @_;
77 my $nodename = $haenv->nodename();
78 my $shutdown_timeout = $timeout // 60;
86 if ($shutdown_timeout) {
87 $params->{timeout
} = $shutdown_timeout;
88 $params->{forceStop
} = 1;
89 $upid = PVE
::API2
::Qemu-
>vm_shutdown($params);
91 $upid = PVE
::API2
::Qemu-
>vm_stop($params);
94 PVE
::HA
::Tools
::upid_wait
($upid, $haenv);
98 my ($class, $haenv, $id, $target, $online) = @_;
100 my $nodename = $haenv->nodename();
105 # bug #2241 forces is for local resource only, people can ensure that
106 # different host have the same hardware, so this can be fine, and qemu
107 # knows when not, so can only win here
113 # explicitly shutdown if $online isn't true (relocate)
114 if (!$online && $class->check_running($haenv, $id)) {
115 $class->shutdown($haenv, $id);
118 my $oldconfig = $class->config_file($id, $nodename);
120 my $upid = PVE
::API2
::Qemu-
>migrate_vm($params);
121 PVE
::HA
::Tools
::upid_wait
($upid, $haenv);
123 # check if vm really moved
124 return !(-f
$oldconfig);
128 my ($class, $haenv, $vmid) = @_;
130 my $nodename = $haenv->nodename();
132 if (PVE
::QemuServer
::check_running
($vmid, 1, $nodename)) {
133 # do not count VMs which are suspended for a backup job as running
134 my $conf = PVE
::QemuConfig-
>load_config($vmid, $nodename);
135 if (defined($conf->{lock}) && $conf->{lock} eq 'backup') {
136 my $qmpstatus = eval {
137 PVE
::QemuServer
::vm_qmp_command
($vmid, { execute
=> 'query-status' })
141 return 0 if defined($qmpstatus) && $qmpstatus->{status
} eq 'prelaunch';
151 my ($self, $haenv, $id, $locks, $service_node) = @_;
153 $service_node = $service_node || $haenv->nodename();
155 my $conf = PVE
::QemuConfig-
>load_config($id, $service_node);
157 return undef if !defined($conf->{lock});
159 foreach my $lock (@$locks) {
160 if ($conf->{lock} eq $lock) {
161 delete $conf->{lock};
163 my $cfspath = PVE
::QemuConfig-
>cfs_config_path($id, $service_node);
164 PVE
::Cluster
::cfs_write_file
($cfspath, $conf);