1 package PVE
::HA
::Env
::PVE2
;
5 use POSIX
qw(:errno_h :fcntl_h);
12 use PVE
::Cluster
qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
13 use PVE
::DataCenterConfig
;
15 use PVE
::RPCEnvironment
;
17 use PVE
::HA
::Tools
':exit_codes';
20 use PVE
::HA
::FenceConfig
;
21 use PVE
::HA
::Resources
;
22 use PVE
::HA
::Resources
::PVEVM
;
23 use PVE
::HA
::Resources
::PVECT
;
25 PVE
::HA
::Resources
::PVEVM-
>register();
26 PVE
::HA
::Resources
::PVECT-
>register();
28 PVE
::HA
::Resources-
>init();
30 my $lockdir = "/etc/pve/priv/lock";
33 my ($this, $nodename) = @_;
35 die "missing nodename" if !$nodename;
37 my $class = ref($this) || $this;
39 my $self = bless {}, $class;
41 $self->{nodename
} = $nodename;
49 return $self->{nodename
};
55 die "hardware is for testing and simulation only";
58 sub read_manager_status
{
61 return PVE
::HA
::Config
::read_manager_status
();
64 sub write_manager_status
{
65 my ($self, $status_obj) = @_;
67 PVE
::HA
::Config
::write_manager_status
($status_obj);
71 my ($self, $node) = @_;
73 $node = $self->{nodename
} if !defined($node);
75 return PVE
::HA
::Config
::read_lrm_status
($node);
78 sub write_lrm_status
{
79 my ($self, $status_obj) = @_;
81 my $node = $self->{nodename
};
83 PVE
::HA
::Config
::write_lrm_status
($node, $status_obj);
86 sub is_node_shutdown
{
95 # ensure we match the full unit name by matching /^JOB_ID UNIT /
96 # see: man systemd.special
97 $shutdown = 1 if ($line =~ m/^\d+\s+shutdown\.target\s+/);
98 $reboot = 1 if ($line =~ m/^\d+\s+reboot\.target\s+/);
101 my $cmd = ['/bin/systemctl', '--full', 'list-jobs'];
102 eval { PVE
::Tools
::run_command
($cmd, outfunc
=> $code, noerr
=> 1); };
104 return ($shutdown, $reboot);
107 sub queue_crm_commands
{
108 my ($self, $cmd) = @_;
110 return PVE
::HA
::Config
::queue_crm_commands
($cmd);
113 sub read_crm_commands
{
116 return PVE
::HA
::Config
::read_crm_commands
();
119 sub read_service_config
{
122 return PVE
::HA
::Config
::read_and_check_resources_config
();
125 sub update_service_config
{
126 my ($self, $sid, $param) = @_;
128 return PVE
::HA
::Config
::update_resources_config
($sid, $param);
132 my ($self, $sid) = @_;
134 return PVE
::HA
::Config
::parse_sid
($sid);
137 sub read_fence_config
{
140 return PVE
::HA
::Config
::read_fence_config
();
146 my $datacenterconfig = cfs_read_file
('datacenter.cfg');
148 return 'watchdog' if !$datacenterconfig->{fencing
};
150 return $datacenterconfig->{fencing
};
153 sub exec_fence_agent
{
154 my ($self, $agent, $node, @param) = @_;
156 # setup execution environment
157 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
159 my $cmd = "$agent " . PVE
::HA
::FenceConfig
::gen_arg_str
(@param);
165 # this is only allowed by the master to recover a _fenced_ service
167 my ($self, $sid, $current_node, $new_node) = @_;
169 my (undef, $type, $name) = PVE
::HA
::Config
::parse_sid
($sid);
171 if(my $plugin = PVE
::HA
::Resources-
>lookup($type)) {
172 my $old = $plugin->config_file($name, $current_node);
173 my $new = $plugin->config_file($name, $new_node);
174 rename($old, $new) ||
175 die "rename '$old' to '$new' failed - $!\n";
180 # Necessary for (at least) static usage plugin to always be able to read service config from new
182 $self->cluster_state_update();
185 sub read_group_config
{
188 return PVE
::HA
::Config
::read_group_config
();
191 # this should return a hash containing info
192 # what nodes are members and online.
196 my ($node_info, $quorate) = ({}, 0);
198 my $nodename = $self->{nodename
};
200 $quorate = PVE
::Cluster
::check_cfs_quorum
(1) || 0;
202 my $members = PVE
::Cluster
::get_members
();
204 foreach my $node (keys %$members) {
205 my $d = $members->{$node};
206 $node_info->{$node}->{online
} = $d->{online
};
209 $node_info->{$nodename}->{online
} = 1; # local node is always up
211 return ($node_info, $quorate);
215 my ($self, $level, $msg) = @_;
219 syslog
($level, $msg);
223 my ($self, $subject, $text) = @_;
225 # Leave it to postfix to append the correct hostname
226 my $mailfrom = 'root';
227 # /root/.forward makes pvemailforward redirect the
228 # mail to the address configured in the datacenter
231 PVE
::Tools
::sendmail
($mailto, $subject, $text, undef, $mailfrom);
234 my $last_lock_status_hash = {};
237 my ($self, $lockid) = @_;
241 my $filename = "$lockdir/$lockid";
243 $last_lock_status_hash->{$lockid} //= { lock_time
=> 0, got_lock
=> 0};
244 my $last = $last_lock_status_hash->{$lockid};
247 my $last_lock_time = $last->{lock_time
} // 0;
248 my $last_got_lock = $last->{got_lock
};
250 my $retry_timeout = 120; # hardcoded lock lifetime limit from pmxcfs
256 # pve cluster filesystem not online
257 die "can't create '$lockdir' (pmxcfs not mounted?)\n" if ! -d
$lockdir;
259 if (($ctime - $last_lock_time) < $retry_timeout) {
260 # try cfs lock update request (utime)
261 if (utime(0, $ctime, $filename)) {
265 die "cfs lock update failed - $!\n";
268 if (mkdir $filename) {
273 utime 0, 0, $filename; # cfs unlock request
274 die "can't get cfs lock\n";
279 #$self->log('err', $err) if $err; # for debugging
281 $last->{got_lock
} = $got_lock;
282 $last->{lock_time
} = $ctime if $got_lock;
284 if (!!$got_lock != !!$last_got_lock) {
286 $self->log('info', "successfully acquired lock '$lockid'");
288 my $msg = "lost lock '$lockid";
289 $msg .= " - $err" if $err;
290 $self->log('err', $msg);
297 sub get_ha_manager_lock
{
300 return $self->get_pve_lock("ha_manager_lock");
303 # release the cluster wide manager lock.
304 # when released another CRM may step up and get the lock, thus this should only
305 # get called when shutting down/deactivating the current master
306 sub release_ha_manager_lock
{
309 return rmdir("$lockdir/ha_manager_lock");
312 sub get_ha_agent_lock
{
313 my ($self, $node) = @_;
315 $node = $self->nodename() if !defined($node);
317 return $self->get_pve_lock("ha_agent_${node}_lock");
320 # release the respective node agent lock.
321 # this should only get called if the nodes LRM gracefully shuts down with
322 # all services already cleanly stopped!
323 sub release_ha_agent_lock
{
326 my $node = $self->nodename();
328 return rmdir("$lockdir/ha_agent_${node}_lock");
336 $quorate = PVE
::Cluster
::check_cfs_quorum
();
349 my ($self, $delay) = @_;
355 my ($self, $end_time) = @_;
358 my $cur_time = time();
360 last if $cur_time >= $end_time;
366 sub loop_start_hook
{
369 $self->{loop_start
} = $self->get_time();
376 my $delay = $self->get_time() - $self->{loop_start
};
378 warn "loop take too long ($delay seconds)\n" if $delay > 30;
381 sub cluster_state_update
{
384 eval { PVE
::Cluster
::cfs_update
(1) };
386 $self->log('warn', "cluster file system update failed - $err");
398 die "watchdog already open\n" if defined($watchdog_fh);
400 $watchdog_fh = IO
::Socket
::UNIX-
>new(
401 Type
=> SOCK_STREAM
(),
402 Peer
=> "/run/watchdog-mux.sock") ||
403 die "unable to open watchdog socket - $!\n";
405 $self->log('info', "watchdog active");
408 sub watchdog_update
{
409 my ($self, $wfh) = @_;
411 my $res = $watchdog_fh->syswrite("\0", 1);
412 if (!defined($res)) {
413 $self->log('err', "watchdog update failed - $!\n");
417 $self->log('err', "watchdog update failed - write $res bytes\n");
425 my ($self, $wfh) = @_;
427 $watchdog_fh->syswrite("V", 1); # magic watchdog close
428 if (!$watchdog_fh->close()) {
429 $self->log('err', "watchdog close failed - $!");
431 $watchdog_fh = undef;
432 $self->log('info', "watchdog closed (disabled)");
439 # close inherited inotify FD from parent and reopen our own
440 PVE
::INotify
::inotify_close
();
441 PVE
::INotify
::inotify_init
();
443 PVE
::Cluster
::cfs_update
();
446 sub get_max_workers
{
449 my $datacenterconfig = cfs_read_file
('datacenter.cfg');
451 return $datacenterconfig->{max_workers
} || 4;
454 # return cluster wide enforced HA settings
455 sub get_datacenter_settings
{
458 my $datacenterconfig = eval { cfs_read_file
('datacenter.cfg') };
460 $self->log('err', "unable to get HA settings from datacenter.cfg - $err");
464 return $datacenterconfig->{ha
};
467 sub get_static_node_stats
{
470 my $stats = PVE
::Cluster
::get_node_kv
('static-info');
471 for my $node (keys $stats->%*) {
472 $stats->{$node} = eval { decode_json
($stats->{$node}) };
473 $self->log('err', "unable to decode static node info for '$node' - $@") if $@;