]> git.proxmox.com Git - pve-ha-manager.git/blob - src/PVE/HA/Resources/PVEVM.pm
2e1698fce4c0005955f3bfcc517cf8048c1de068
[pve-ha-manager.git] / src / PVE / HA / Resources / PVEVM.pm
1 package PVE::HA::Resources::PVEVM;
2
3 use strict;
4 use warnings;
5
6 use PVE::HA::Tools;
7
8 BEGIN {
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::QemuServer::Monitor;
15 import PVE::QemuServer::Monitor;
16 require PVE::API2::Qemu;
17 import PVE::API2::Qemu;
18 }
19 }
20
21 use base qw(PVE::HA::Resources);
22
23 sub type {
24 return 'vm';
25 }
26
27 sub verify_name {
28 my ($class, $name) = @_;
29
30 die "invalid VMID\n" if $name !~ m/^[1-9][0-9]+$/;
31 }
32
33 sub options {
34 return {
35 state => { optional => 1 },
36 group => { optional => 1 },
37 comment => { optional => 1 },
38 max_restart => { optional => 1 },
39 max_relocate => { optional => 1 },
40 };
41 }
42
43 sub config_file {
44 my ($class, $vmid, $nodename) = @_;
45
46 return PVE::QemuConfig->config_file($vmid, $nodename);
47 }
48
49 sub exists {
50 my ($class, $vmid, $noerr) = @_;
51
52 my $vmlist = PVE::Cluster::get_vmlist();
53
54 if(!defined($vmlist->{ids}->{$vmid})) {
55 die "resource 'vm:$vmid' does not exist in cluster\n" if !$noerr;
56 return undef;
57 } else {
58 return 1;
59 }
60 }
61
62 sub start {
63 my ($class, $haenv, $id) = @_;
64
65 my $nodename = $haenv->nodename();
66
67 my $params = {
68 node => $nodename,
69 vmid => $id
70 };
71
72 my $upid = PVE::API2::Qemu->vm_start($params);
73 PVE::HA::Tools::upid_wait($upid, $haenv);
74 }
75
76 sub shutdown {
77 my ($class, $haenv, $id, $timeout) = @_;
78
79 my $nodename = $haenv->nodename();
80 my $shutdown_timeout = $timeout // 60;
81
82 my $upid;
83 my $params = {
84 node => $nodename,
85 vmid => $id,
86 };
87
88 if ($shutdown_timeout) {
89 $params->{timeout} = $shutdown_timeout;
90 $params->{forceStop} = 1;
91 $upid = PVE::API2::Qemu->vm_shutdown($params);
92 } else {
93 $upid = PVE::API2::Qemu->vm_stop($params);
94 }
95
96 PVE::HA::Tools::upid_wait($upid, $haenv);
97 }
98
99 sub migrate {
100 my ($class, $haenv, $id, $target, $online) = @_;
101
102 my $nodename = $haenv->nodename();
103
104 my $params = {
105 node => $nodename,
106 vmid => $id,
107 # bug #2241 forces is for local resource only, people can ensure that
108 # different host have the same hardware, so this can be fine, and qemu
109 # knows when not, so can only win here
110 force => 1,
111 target => $target,
112 online => $online,
113 };
114
115 # explicitly shutdown if $online isn't true (relocate)
116 if (!$online && $class->check_running($haenv, $id)) {
117 $class->shutdown($haenv, $id);
118 }
119
120 my $oldconfig = $class->config_file($id, $nodename);
121
122 my $upid = PVE::API2::Qemu->migrate_vm($params);
123 PVE::HA::Tools::upid_wait($upid, $haenv);
124
125 # check if vm really moved
126 return !(-f $oldconfig);
127 }
128
129 sub check_running {
130 my ($class, $haenv, $vmid) = @_;
131
132 my $nodename = $haenv->nodename();
133
134 if (PVE::QemuServer::check_running($vmid, 1, $nodename)) {
135 # do not count VMs which are suspended for a backup job as running
136 my $conf = PVE::QemuConfig->load_config($vmid, $nodename);
137 if (defined($conf->{lock}) && $conf->{lock} eq 'backup') {
138 my $qmpstatus = eval {
139 PVE::QemuServer::Monitor::mon_cmd($vmid, 'query-status')
140 };
141 warn "$@\n" if $@;
142
143 return 0 if defined($qmpstatus) && $qmpstatus->{status} eq 'prelaunch';
144 }
145
146 return 1;
147 } else {
148 return 0;
149 }
150 }
151
152 sub remove_locks {
153 my ($self, $haenv, $id, $locks, $service_node) = @_;
154
155 $service_node = $service_node || $haenv->nodename();
156
157 my $conf = PVE::QemuConfig->load_config($id, $service_node);
158
159 return undef if !defined($conf->{lock});
160
161 foreach my $lock (@$locks) {
162 if ($conf->{lock} eq $lock) {
163 delete $conf->{lock};
164
165 my $cfspath = PVE::QemuConfig->cfs_config_path($id, $service_node);
166 PVE::Cluster::cfs_write_file($cfspath, $conf);
167
168 return $lock;
169 }
170 }
171
172 return undef;
173 }
174
175 1;