1 package PVE
::HA
::Env
::PVE2
;
5 use POSIX
qw(:errno_h :fcntl_h);
11 use PVE
::Cluster
qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
12 use PVE
::DataCenterConfig
;
14 use PVE
::RPCEnvironment
;
16 use PVE
::HA
::Tools
':exit_codes';
19 use PVE
::HA
::FenceConfig
;
20 use PVE
::HA
::Resources
;
21 use PVE
::HA
::Resources
::PVEVM
;
22 use PVE
::HA
::Resources
::PVECT
;
24 PVE
::HA
::Resources
::PVEVM-
>register();
25 PVE
::HA
::Resources
::PVECT-
>register();
27 PVE
::HA
::Resources-
>init();
29 my $lockdir = "/etc/pve/priv/lock";
32 my ($this, $nodename) = @_;
34 die "missing nodename" if !$nodename;
36 my $class = ref($this) || $this;
38 my $self = bless {}, $class;
40 $self->{nodename
} = $nodename;
48 return $self->{nodename
};
54 die "hardware is for testing and simulation only";
57 sub read_manager_status
{
60 return PVE
::HA
::Config
::read_manager_status
();
63 sub write_manager_status
{
64 my ($self, $status_obj) = @_;
66 PVE
::HA
::Config
::write_manager_status
($status_obj);
70 my ($self, $node) = @_;
72 $node = $self->{nodename
} if !defined($node);
74 return PVE
::HA
::Config
::read_lrm_status
($node);
77 sub write_lrm_status
{
78 my ($self, $status_obj) = @_;
80 my $node = $self->{nodename
};
82 PVE
::HA
::Config
::write_lrm_status
($node, $status_obj);
85 sub is_node_shutdown
{
94 # ensure we match the full unit name by matching /^JOB_ID UNIT /
95 # see: man systemd.special
96 $shutdown = 1 if ($line =~ m/^\d+\s+shutdown\.target\s+/);
97 $reboot = 1 if ($line =~ m/^\d+\s+reboot\.target\s+/);
100 my $cmd = ['/bin/systemctl', '--full', 'list-jobs'];
101 eval { PVE
::Tools
::run_command
($cmd, outfunc
=> $code, noerr
=> 1); };
103 return ($shutdown, $reboot);
106 sub queue_crm_commands
{
107 my ($self, $cmd) = @_;
109 return PVE
::HA
::Config
::queue_crm_commands
($cmd);
112 sub read_crm_commands
{
115 return PVE
::HA
::Config
::read_crm_commands
();
118 sub read_service_config
{
121 return PVE
::HA
::Config
::read_and_check_resources_config
();
124 sub update_service_config
{
125 my ($self, $sid, $param) = @_;
127 return PVE
::HA
::Config
::update_resources_config
($sid, $param);
131 my ($self, $sid) = @_;
133 return PVE
::HA
::Config
::parse_sid
($sid);
136 sub read_fence_config
{
139 return PVE
::HA
::Config
::read_fence_config
();
145 my $datacenterconfig = cfs_read_file
('datacenter.cfg');
147 return 'watchdog' if !$datacenterconfig->{fencing
};
149 return $datacenterconfig->{fencing
};
152 sub exec_fence_agent
{
153 my ($self, $agent, $node, @param) = @_;
155 # setup execution environment
156 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
158 my $cmd = "$agent " . PVE
::HA
::FenceConfig
::gen_arg_str
(@param);
164 # this is only allowed by the master to recover a _fenced_ service
166 my ($self, $sid, $current_node, $new_node) = @_;
168 my (undef, $type, $name) = PVE
::HA
::Config
::parse_sid
($sid);
170 if(my $plugin = PVE
::HA
::Resources-
>lookup($type)) {
171 my $old = $plugin->config_file($name, $current_node);
172 my $new = $plugin->config_file($name, $new_node);
173 rename($old, $new) ||
174 die "rename '$old' to '$new' failed - $!\n";
180 sub read_group_config
{
183 return PVE
::HA
::Config
::read_group_config
();
186 # this should return a hash containing info
187 # what nodes are members and online.
191 my ($node_info, $quorate) = ({}, 0);
193 my $nodename = $self->{nodename
};
195 $quorate = PVE
::Cluster
::check_cfs_quorum
(1) || 0;
197 my $members = PVE
::Cluster
::get_members
();
199 foreach my $node (keys %$members) {
200 my $d = $members->{$node};
201 $node_info->{$node}->{online
} = $d->{online
};
204 $node_info->{$nodename}->{online
} = 1; # local node is always up
206 return ($node_info, $quorate);
210 my ($self, $level, $msg) = @_;
214 syslog
($level, $msg);
218 my ($self, $subject, $text) = @_;
220 # Leave it to postfix to append the correct hostname
221 my $mailfrom = 'root';
222 # /root/.forward makes pvemailforward redirect the
223 # mail to the address configured in the datacenter
226 PVE
::Tools
::sendmail
($mailto, $subject, $text, undef, $mailfrom);
229 my $last_lock_status_hash = {};
232 my ($self, $lockid) = @_;
236 my $filename = "$lockdir/$lockid";
238 $last_lock_status_hash->{$lockid} //= { lock_time
=> 0, got_lock
=> 0};
239 my $last = $last_lock_status_hash->{$lockid};
242 my $last_lock_time = $last->{lock_time
} // 0;
243 my $last_got_lock = $last->{got_lock
};
245 my $retry_timeout = 120; # hardcoded lock lifetime limit from pmxcfs
251 # pve cluster filesystem not online
252 die "can't create '$lockdir' (pmxcfs not mounted?)\n" if ! -d
$lockdir;
254 if (($ctime - $last_lock_time) < $retry_timeout) {
255 # try cfs lock update request (utime)
256 if (utime(0, $ctime, $filename)) {
260 die "cfs lock update failed - $!\n";
263 if (mkdir $filename) {
268 utime 0, 0, $filename; # cfs unlock request
269 die "can't get cfs lock\n";
274 #$self->log('err', $err) if $err; # for debugging
276 $last->{got_lock
} = $got_lock;
277 $last->{lock_time
} = $ctime if $got_lock;
279 if (!!$got_lock != !!$last_got_lock) {
281 $self->log('info', "successfully acquired lock '$lockid'");
283 my $msg = "lost lock '$lockid";
284 $msg .= " - $err" if $err;
285 $self->log('err', $msg);
292 sub get_ha_manager_lock
{
295 return $self->get_pve_lock("ha_manager_lock");
298 # release the cluster wide manager lock.
299 # when released another CRM may step up and get the lock, thus this should only
300 # get called when shutting down/deactivating the current master
301 sub release_ha_manager_lock
{
304 return rmdir("$lockdir/ha_manager_lock");
307 sub get_ha_agent_lock
{
308 my ($self, $node) = @_;
310 $node = $self->nodename() if !defined($node);
312 return $self->get_pve_lock("ha_agent_${node}_lock");
315 # release the respective node agent lock.
316 # this should only get called if the nodes LRM gracefully shuts down with
317 # all services already cleanly stopped!
318 sub release_ha_agent_lock
{
321 my $node = $self->nodename();
323 return rmdir("$lockdir/ha_agent_${node}_lock");
331 $quorate = PVE
::Cluster
::check_cfs_quorum
();
344 my ($self, $delay) = @_;
350 my ($self, $end_time) = @_;
353 my $cur_time = time();
355 last if $cur_time >= $end_time;
361 sub loop_start_hook
{
364 $self->{loop_start
} = $self->get_time();
371 my $delay = $self->get_time() - $self->{loop_start
};
373 warn "loop take too long ($delay seconds)\n" if $delay > 30;
376 sub cluster_state_update
{
379 eval { PVE
::Cluster
::cfs_update
(1) };
381 $self->log('warn', "cluster file system update failed - $err");
393 die "watchdog already open\n" if defined($watchdog_fh);
395 $watchdog_fh = IO
::Socket
::UNIX-
>new(
396 Type
=> SOCK_STREAM
(),
397 Peer
=> "/run/watchdog-mux.sock") ||
398 die "unable to open watchdog socket - $!\n";
400 $self->log('info', "watchdog active");
403 sub watchdog_update
{
404 my ($self, $wfh) = @_;
406 my $res = $watchdog_fh->syswrite("\0", 1);
407 if (!defined($res)) {
408 $self->log('err', "watchdog update failed - $!\n");
412 $self->log('err', "watchdog update failed - write $res bytes\n");
420 my ($self, $wfh) = @_;
422 $watchdog_fh->syswrite("V", 1); # magic watchdog close
423 if (!$watchdog_fh->close()) {
424 $self->log('err', "watchdog close failed - $!");
426 $watchdog_fh = undef;
427 $self->log('info', "watchdog closed (disabled)");
434 # close inherited inotify FD from parent and reopen our own
435 PVE
::INotify
::inotify_close
();
436 PVE
::INotify
::inotify_init
();
438 PVE
::Cluster
::cfs_update
();
441 sub get_max_workers
{
444 my $datacenterconfig = cfs_read_file
('datacenter.cfg');
446 return $datacenterconfig->{max_workers
} || 4;
449 # return cluster wide enforced HA settings
450 sub get_ha_settings
{
453 my $datacenterconfig = eval { cfs_read_file
('datacenter.cfg') };
455 $self->log('err', "unable to get HA settings from datacenter.cfg - $err");
459 return $datacenterconfig->{ha
};