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";
181 sub read_group_config
{
184 return PVE
::HA
::Config
::read_group_config
();
187 # this should return a hash containing info
188 # what nodes are members and online.
192 my ($node_info, $quorate) = ({}, 0);
194 my $nodename = $self->{nodename
};
196 $quorate = PVE
::Cluster
::check_cfs_quorum
(1) || 0;
198 my $members = PVE
::Cluster
::get_members
();
200 foreach my $node (keys %$members) {
201 my $d = $members->{$node};
202 $node_info->{$node}->{online
} = $d->{online
};
205 $node_info->{$nodename}->{online
} = 1; # local node is always up
207 return ($node_info, $quorate);
211 my ($self, $level, $msg) = @_;
215 syslog
($level, $msg);
219 my ($self, $subject, $text) = @_;
221 # Leave it to postfix to append the correct hostname
222 my $mailfrom = 'root';
223 # /root/.forward makes pvemailforward redirect the
224 # mail to the address configured in the datacenter
227 PVE
::Tools
::sendmail
($mailto, $subject, $text, undef, $mailfrom);
230 my $last_lock_status_hash = {};
233 my ($self, $lockid) = @_;
237 my $filename = "$lockdir/$lockid";
239 $last_lock_status_hash->{$lockid} //= { lock_time
=> 0, got_lock
=> 0};
240 my $last = $last_lock_status_hash->{$lockid};
243 my $last_lock_time = $last->{lock_time
} // 0;
244 my $last_got_lock = $last->{got_lock
};
246 my $retry_timeout = 120; # hardcoded lock lifetime limit from pmxcfs
252 # pve cluster filesystem not online
253 die "can't create '$lockdir' (pmxcfs not mounted?)\n" if ! -d
$lockdir;
255 if (($ctime - $last_lock_time) < $retry_timeout) {
256 # try cfs lock update request (utime)
257 if (utime(0, $ctime, $filename)) {
261 die "cfs lock update failed - $!\n";
264 if (mkdir $filename) {
269 utime 0, 0, $filename; # cfs unlock request
270 die "can't get cfs lock\n";
275 #$self->log('err', $err) if $err; # for debugging
277 $last->{got_lock
} = $got_lock;
278 $last->{lock_time
} = $ctime if $got_lock;
280 if (!!$got_lock != !!$last_got_lock) {
282 $self->log('info', "successfully acquired lock '$lockid'");
284 my $msg = "lost lock '$lockid";
285 $msg .= " - $err" if $err;
286 $self->log('err', $msg);
293 sub get_ha_manager_lock
{
296 return $self->get_pve_lock("ha_manager_lock");
299 # release the cluster wide manager lock.
300 # when released another CRM may step up and get the lock, thus this should only
301 # get called when shutting down/deactivating the current master
302 sub release_ha_manager_lock
{
305 return rmdir("$lockdir/ha_manager_lock");
308 sub get_ha_agent_lock
{
309 my ($self, $node) = @_;
311 $node = $self->nodename() if !defined($node);
313 return $self->get_pve_lock("ha_agent_${node}_lock");
316 # release the respective node agent lock.
317 # this should only get called if the nodes LRM gracefully shuts down with
318 # all services already cleanly stopped!
319 sub release_ha_agent_lock
{
322 my $node = $self->nodename();
324 return rmdir("$lockdir/ha_agent_${node}_lock");
332 $quorate = PVE
::Cluster
::check_cfs_quorum
();
345 my ($self, $delay) = @_;
351 my ($self, $end_time) = @_;
354 my $cur_time = time();
356 last if $cur_time >= $end_time;
362 sub loop_start_hook
{
365 $self->{loop_start
} = $self->get_time();
372 my $delay = $self->get_time() - $self->{loop_start
};
374 warn "loop take too long ($delay seconds)\n" if $delay > 30;
377 sub cluster_state_update
{
380 eval { PVE
::Cluster
::cfs_update
(1) };
382 $self->log('warn', "cluster file system update failed - $err");
394 die "watchdog already open\n" if defined($watchdog_fh);
396 $watchdog_fh = IO
::Socket
::UNIX-
>new(
397 Type
=> SOCK_STREAM
(),
398 Peer
=> "/run/watchdog-mux.sock") ||
399 die "unable to open watchdog socket - $!\n";
401 $self->log('info', "watchdog active");
404 sub watchdog_update
{
405 my ($self, $wfh) = @_;
407 my $res = $watchdog_fh->syswrite("\0", 1);
408 if (!defined($res)) {
409 $self->log('err', "watchdog update failed - $!\n");
413 $self->log('err', "watchdog update failed - write $res bytes\n");
421 my ($self, $wfh) = @_;
423 $watchdog_fh->syswrite("V", 1); # magic watchdog close
424 if (!$watchdog_fh->close()) {
425 $self->log('err', "watchdog close failed - $!");
427 $watchdog_fh = undef;
428 $self->log('info', "watchdog closed (disabled)");
435 # close inherited inotify FD from parent and reopen our own
436 PVE
::INotify
::inotify_close
();
437 PVE
::INotify
::inotify_init
();
439 PVE
::Cluster
::cfs_update
();
442 sub get_max_workers
{
445 my $datacenterconfig = cfs_read_file
('datacenter.cfg');
447 return $datacenterconfig->{max_workers
} || 4;
450 # return cluster wide enforced HA settings
451 sub get_ha_settings
{
454 my $datacenterconfig = eval { cfs_read_file
('datacenter.cfg') };
456 $self->log('err', "unable to get HA settings from datacenter.cfg - $err");
460 return $datacenterconfig->{ha
};
463 sub get_static_node_stats
{
466 my $stats = PVE
::Cluster
::get_node_kv
('static-info');
467 for my $node (keys $stats->%*) {
468 $stats->{$node} = eval { decode_json
($stats->{$node}) };
469 $self->log('err', "unable to decode static node info for '$node' - $@") if $@;