]> git.proxmox.com Git - pve-ha-manager.git/blame_incremental - src/PVE/HA/Env/PVE2.pm
fix #1073: do not count backup-suspended VMs as running
[pve-ha-manager.git] / src / PVE / HA / Env / PVE2.pm
... / ...
CommitLineData
1package PVE::HA::Env::PVE2;
2
3use strict;
4use warnings;
5use POSIX qw(:errno_h :fcntl_h);
6use IO::File;
7use IO::Socket::UNIX;
8
9use PVE::SafeSyslog;
10use PVE::Tools;
11use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
12use PVE::INotify;
13use PVE::RPCEnvironment;
14
15use PVE::HA::Tools ':exit_codes';
16use PVE::HA::Env;
17use PVE::HA::Config;
18use PVE::HA::FenceConfig;
19use PVE::HA::Resources;
20use PVE::HA::Resources::PVEVM;
21use PVE::HA::Resources::PVECT;
22
23PVE::HA::Resources::PVEVM->register();
24PVE::HA::Resources::PVECT->register();
25
26PVE::HA::Resources->init();
27
28my $lockdir = "/etc/pve/priv/lock";
29
30sub new {
31 my ($this, $nodename) = @_;
32
33 die "missing nodename" if !$nodename;
34
35 my $class = ref($this) || $this;
36
37 my $self = bless {}, $class;
38
39 $self->{nodename} = $nodename;
40
41 return $self;
42}
43
44sub nodename {
45 my ($self) = @_;
46
47 return $self->{nodename};
48}
49
50sub hardware {
51 my ($self) = @_;
52
53 die "hardware is for testing and simulation only";
54}
55
56sub read_manager_status {
57 my ($self) = @_;
58
59 return PVE::HA::Config::read_manager_status();
60}
61
62sub write_manager_status {
63 my ($self, $status_obj) = @_;
64
65 PVE::HA::Config::write_manager_status($status_obj);
66}
67
68sub read_lrm_status {
69 my ($self, $node) = @_;
70
71 $node = $self->{nodename} if !defined($node);
72
73 return PVE::HA::Config::read_lrm_status($node);
74}
75
76sub write_lrm_status {
77 my ($self, $status_obj) = @_;
78
79 my $node = $self->{nodename};
80
81 PVE::HA::Config::write_lrm_status($node, $status_obj);
82}
83
84sub is_node_shutdown {
85 my ($self) = @_;
86
87 my $shutdown = 0;
88 my $reboot = 0;
89
90 my $code = sub {
91 my $line = shift;
92
93 # ensure we match the full unit name by matching /^JOB_ID UNIT /
94 # see: man systemd.special
95 $shutdown = 1 if ($line =~ m/^\d+\s+shutdown\.target\s+/);
96 $reboot = 1 if ($line =~ m/^\d+\s+reboot\.target\s+/);
97 };
98
99 my $cmd = ['/bin/systemctl', '--full', 'list-jobs'];
100 eval { PVE::Tools::run_command($cmd, outfunc => $code, noerr => 1); };
101
102 return ($shutdown, $reboot);
103}
104
105sub queue_crm_commands {
106 my ($self, $cmd) = @_;
107
108 return PVE::HA::Config::queue_crm_commands($cmd);
109}
110
111sub read_crm_commands {
112 my ($self) = @_;
113
114 return PVE::HA::Config::read_crm_commands();
115}
116
117sub read_service_config {
118 my ($self) = @_;
119
120 return PVE::HA::Config::read_and_check_resources_config();
121}
122
123sub read_fence_config {
124 my ($self) = @_;
125
126 return PVE::HA::Config::read_fence_config();
127}
128
129sub fencing_mode {
130 my ($self) = @_;
131
132 my $datacenterconfig = cfs_read_file('datacenter.cfg');
133
134 return 'watchdog' if !$datacenterconfig->{fencing};
135
136 return $datacenterconfig->{fencing};
137}
138
139sub exec_fence_agent {
140 my ($self, $agent, $node, @param) = @_;
141
142 # setup execution environment
143 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
144
145 my $cmd = "$agent " . PVE::HA::FenceConfig::gen_arg_str(@param);
146
147 exec($cmd);
148 exit -1;
149}
150
151# this is only allowed by the master to recover a _fenced_ service
152sub steal_service {
153 my ($self, $sid, $current_node, $new_node) = @_;
154
155 my (undef, $type, $name) = PVE::HA::Tools::parse_sid($sid);
156
157 if(my $plugin = PVE::HA::Resources->lookup($type)) {
158 my $old = $plugin->config_file($name, $current_node);
159 my $new = $plugin->config_file($name, $new_node);
160 rename($old, $new) ||
161 die "rename '$old' to '$new' failed - $!\n";
162 } else {
163 die "implement me";
164 }
165}
166
167sub read_group_config {
168 my ($self) = @_;
169
170 return PVE::HA::Config::read_group_config();
171}
172
173# this should return a hash containing info
174# what nodes are members and online.
175sub get_node_info {
176 my ($self) = @_;
177
178 my ($node_info, $quorate) = ({}, 0);
179
180 my $nodename = $self->{nodename};
181
182 $quorate = PVE::Cluster::check_cfs_quorum(1) || 0;
183
184 my $members = PVE::Cluster::get_members();
185
186 foreach my $node (keys %$members) {
187 my $d = $members->{$node};
188 $node_info->{$node}->{online} = $d->{online};
189 }
190
191 $node_info->{$nodename}->{online} = 1; # local node is always up
192
193 return ($node_info, $quorate);
194}
195
196sub log {
197 my ($self, $level, $msg) = @_;
198
199 chomp $msg;
200
201 syslog($level, $msg);
202}
203
204sub sendmail {
205 my ($self, $subject, $text) = @_;
206
207 my $mailfrom = 'root@' . $self->nodename();
208 my $mailto = 'root@localhost';
209
210 PVE::Tools::sendmail($mailto, $subject, $text, undef, $mailfrom);
211}
212
213my $last_lock_status_hash = {};
214
215sub get_pve_lock {
216 my ($self, $lockid) = @_;
217
218 my $got_lock = 0;
219
220 my $filename = "$lockdir/$lockid";
221
222 $last_lock_status_hash->{$lockid} //= { lock_time => 0, got_lock => 0};
223 my $last = $last_lock_status_hash->{$lockid};
224
225 my $ctime = time();
226 my $last_lock_time = $last->{lock_time} // 0;
227 my $last_got_lock = $last->{got_lock};
228
229 my $retry_timeout = 120; # hardcoded lock lifetime limit from pmxcfs
230
231 eval {
232
233 mkdir $lockdir;
234
235 # pve cluster filesystem not online
236 die "can't create '$lockdir' (pmxcfs not mounted?)\n" if ! -d $lockdir;
237
238 if (($ctime - $last_lock_time) < $retry_timeout) {
239 # try cfs lock update request (utime)
240 if (utime(0, $ctime, $filename)) {
241 $got_lock = 1;
242 return;
243 }
244 die "cfs lock update failed - $!\n";
245 }
246
247 if (mkdir $filename) {
248 $got_lock = 1;
249 return;
250 }
251
252 utime 0, 0, $filename; # cfs unlock request
253 die "can't get cfs lock\n";
254 };
255
256 my $err = $@;
257
258 #$self->log('err', $err) if $err; # for debugging
259
260 $last->{got_lock} = $got_lock;
261 $last->{lock_time} = $ctime if $got_lock;
262
263 if (!!$got_lock != !!$last_got_lock) {
264 if ($got_lock) {
265 $self->log('info', "successfully acquired lock '$lockid'");
266 } else {
267 my $msg = "lost lock '$lockid";
268 $msg .= " - $err" if $err;
269 $self->log('err', $msg);
270 }
271 }
272
273 return $got_lock;
274}
275
276sub get_ha_manager_lock {
277 my ($self) = @_;
278
279 return $self->get_pve_lock("ha_manager_lock");
280}
281
282# release the cluster wide manager lock.
283# when released another CRM may step up and get the lock, thus this should only
284# get called when shutting down/deactivating the current master
285sub release_ha_manager_lock {
286 my ($self) = @_;
287
288 return rmdir("$lockdir/ha_manager_lock");
289}
290
291sub get_ha_agent_lock {
292 my ($self, $node) = @_;
293
294 $node = $self->nodename() if !defined($node);
295
296 return $self->get_pve_lock("ha_agent_${node}_lock");
297}
298
299# release the respective node agent lock.
300# this should only get called if the nodes LRM gracefully shuts down with
301# all services already cleanly stopped!
302sub release_ha_agent_lock {
303 my ($self) = @_;
304
305 my $node = $self->nodename();
306
307 return rmdir("$lockdir/ha_agent_${node}_lock");
308}
309
310sub quorate {
311 my ($self) = @_;
312
313 my $quorate = 0;
314 eval {
315 $quorate = PVE::Cluster::check_cfs_quorum();
316 };
317
318 return $quorate;
319}
320
321sub get_time {
322 my ($self) = @_;
323
324 return time();
325}
326
327sub sleep {
328 my ($self, $delay) = @_;
329
330 CORE::sleep($delay);
331}
332
333sub sleep_until {
334 my ($self, $end_time) = @_;
335
336 for (;;) {
337 my $cur_time = time();
338
339 last if $cur_time >= $end_time;
340
341 $self->sleep(1);
342 }
343}
344
345sub loop_start_hook {
346 my ($self) = @_;
347
348 PVE::Cluster::cfs_update();
349
350 $self->{loop_start} = $self->get_time();
351}
352
353sub loop_end_hook {
354 my ($self) = @_;
355
356 my $delay = $self->get_time() - $self->{loop_start};
357
358 warn "loop take too long ($delay seconds)\n" if $delay > 30;
359}
360
361my $watchdog_fh;
362
363sub watchdog_open {
364 my ($self) = @_;
365
366 die "watchdog already open\n" if defined($watchdog_fh);
367
368 $watchdog_fh = IO::Socket::UNIX->new(
369 Type => SOCK_STREAM(),
370 Peer => "/run/watchdog-mux.sock") ||
371 die "unable to open watchdog socket - $!\n";
372
373 $self->log('info', "watchdog active");
374}
375
376sub watchdog_update {
377 my ($self, $wfh) = @_;
378
379 my $res = $watchdog_fh->syswrite("\0", 1);
380 if (!defined($res)) {
381 $self->log('err', "watchdog update failed - $!\n");
382 return 0;
383 }
384 if ($res != 1) {
385 $self->log('err', "watchdog update failed - write $res bytes\n");
386 return 0;
387 }
388
389 return 1;
390}
391
392sub watchdog_close {
393 my ($self, $wfh) = @_;
394
395 $watchdog_fh->syswrite("V", 1); # magic watchdog close
396 if (!$watchdog_fh->close()) {
397 $self->log('err', "watchdog close failed - $!");
398 } else {
399 $watchdog_fh = undef;
400 $self->log('info', "watchdog closed (disabled)");
401 }
402}
403
404sub after_fork {
405 my ($self) = @_;
406
407 # close inherited inotify FD from parent and reopen our own
408 PVE::INotify::inotify_close();
409 PVE::INotify::inotify_init();
410
411 PVE::Cluster::cfs_update();
412}
413
414sub get_max_workers {
415 my ($self) = @_;
416
417 my $datacenterconfig = cfs_read_file('datacenter.cfg');
418
419 return $datacenterconfig->{max_workers} || 4;
420}
421
4221;