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);
13 use PVE
::RPCEnvironment
;
15 use PVE
::HA
::Tools
':exit_codes';
18 use PVE
::HA
::FenceConfig
;
19 use PVE
::HA
::Resources
;
20 use PVE
::HA
::Resources
::PVEVM
;
21 use PVE
::HA
::Resources
::PVECT
;
23 PVE
::HA
::Resources
::PVEVM-
>register();
24 PVE
::HA
::Resources
::PVECT-
>register();
26 PVE
::HA
::Resources-
>init();
28 my $lockdir = "/etc/pve/priv/lock";
31 my ($this, $nodename) = @_;
33 die "missing nodename" if !$nodename;
35 my $class = ref($this) || $this;
37 my $self = bless {}, $class;
39 $self->{nodename
} = $nodename;
47 return $self->{nodename
};
53 die "hardware is for testing and simulation only";
56 sub read_manager_status
{
59 return PVE
::HA
::Config
::read_manager_status
();
62 sub write_manager_status
{
63 my ($self, $status_obj) = @_;
65 PVE
::HA
::Config
::write_manager_status
($status_obj);
69 my ($self, $node) = @_;
71 $node = $self->{nodename
} if !defined($node);
73 return PVE
::HA
::Config
::read_lrm_status
($node);
76 sub write_lrm_status
{
77 my ($self, $status_obj) = @_;
79 my $node = $self->{nodename
};
81 PVE
::HA
::Config
::write_lrm_status
($node, $status_obj);
84 sub is_node_shutdown
{
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+/);
99 my $cmd = ['/bin/systemctl', '--full', 'list-jobs'];
100 eval { PVE
::Tools
::run_command
($cmd, outfunc
=> $code, noerr
=> 1); };
102 return ($shutdown, $reboot);
105 sub queue_crm_commands
{
106 my ($self, $cmd) = @_;
108 return PVE
::HA
::Config
::queue_crm_commands
($cmd);
111 sub read_crm_commands
{
114 return PVE
::HA
::Config
::read_crm_commands
();
117 sub read_service_config
{
120 return PVE
::HA
::Config
::read_and_check_resources_config
();
123 sub read_fence_config
{
126 return PVE
::HA
::Config
::read_fence_config
();
132 my $datacenterconfig = cfs_read_file
('datacenter.cfg');
134 return 'watchdog' if !$datacenterconfig->{fencing
};
136 return $datacenterconfig->{fencing
};
139 sub exec_fence_agent
{
140 my ($self, $agent, $node, @param) = @_;
142 # setup execution environment
143 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
145 my $cmd = "$agent " . PVE
::HA
::FenceConfig
::gen_arg_str
(@param);
151 # this is only allowed by the master to recover a _fenced_ service
153 my ($self, $sid, $current_node, $new_node) = @_;
155 my (undef, $type, $name) = PVE
::HA
::Tools
::parse_sid
($sid);
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";
167 sub read_group_config
{
170 return PVE
::HA
::Config
::read_group_config
();
173 # this should return a hash containing info
174 # what nodes are members and online.
178 my ($node_info, $quorate) = ({}, 0);
180 my $nodename = $self->{nodename
};
182 $quorate = PVE
::Cluster
::check_cfs_quorum
(1) || 0;
184 my $members = PVE
::Cluster
::get_members
();
186 foreach my $node (keys %$members) {
187 my $d = $members->{$node};
188 $node_info->{$node}->{online
} = $d->{online
};
191 $node_info->{$nodename}->{online
} = 1; # local node is always up
193 return ($node_info, $quorate);
197 my ($self, $level, $msg) = @_;
201 syslog
($level, $msg);
205 my ($self, $subject, $text) = @_;
207 # Leave it to postfix to append the correct hostname
208 my $mailfrom = 'root';
209 # /root/.forward makes pvemailforward redirect the
210 # mail to the address configured in the datacenter
213 PVE
::Tools
::sendmail
($mailto, $subject, $text, undef, $mailfrom);
216 my $last_lock_status_hash = {};
219 my ($self, $lockid) = @_;
223 my $filename = "$lockdir/$lockid";
225 $last_lock_status_hash->{$lockid} //= { lock_time
=> 0, got_lock
=> 0};
226 my $last = $last_lock_status_hash->{$lockid};
229 my $last_lock_time = $last->{lock_time
} // 0;
230 my $last_got_lock = $last->{got_lock
};
232 my $retry_timeout = 120; # hardcoded lock lifetime limit from pmxcfs
238 # pve cluster filesystem not online
239 die "can't create '$lockdir' (pmxcfs not mounted?)\n" if ! -d
$lockdir;
241 if (($ctime - $last_lock_time) < $retry_timeout) {
242 # try cfs lock update request (utime)
243 if (utime(0, $ctime, $filename)) {
247 die "cfs lock update failed - $!\n";
250 if (mkdir $filename) {
255 utime 0, 0, $filename; # cfs unlock request
256 die "can't get cfs lock\n";
261 #$self->log('err', $err) if $err; # for debugging
263 $last->{got_lock
} = $got_lock;
264 $last->{lock_time
} = $ctime if $got_lock;
266 if (!!$got_lock != !!$last_got_lock) {
268 $self->log('info', "successfully acquired lock '$lockid'");
270 my $msg = "lost lock '$lockid";
271 $msg .= " - $err" if $err;
272 $self->log('err', $msg);
279 sub get_ha_manager_lock
{
282 return $self->get_pve_lock("ha_manager_lock");
285 # release the cluster wide manager lock.
286 # when released another CRM may step up and get the lock, thus this should only
287 # get called when shutting down/deactivating the current master
288 sub release_ha_manager_lock
{
291 return rmdir("$lockdir/ha_manager_lock");
294 sub get_ha_agent_lock
{
295 my ($self, $node) = @_;
297 $node = $self->nodename() if !defined($node);
299 return $self->get_pve_lock("ha_agent_${node}_lock");
302 # release the respective node agent lock.
303 # this should only get called if the nodes LRM gracefully shuts down with
304 # all services already cleanly stopped!
305 sub release_ha_agent_lock
{
308 my $node = $self->nodename();
310 return rmdir("$lockdir/ha_agent_${node}_lock");
318 $quorate = PVE
::Cluster
::check_cfs_quorum
();
331 my ($self, $delay) = @_;
337 my ($self, $end_time) = @_;
340 my $cur_time = time();
342 last if $cur_time >= $end_time;
348 sub loop_start_hook
{
351 PVE
::Cluster
::cfs_update
();
353 $self->{loop_start
} = $self->get_time();
359 my $delay = $self->get_time() - $self->{loop_start
};
361 warn "loop take too long ($delay seconds)\n" if $delay > 30;
369 die "watchdog already open\n" if defined($watchdog_fh);
371 $watchdog_fh = IO
::Socket
::UNIX-
>new(
372 Type
=> SOCK_STREAM
(),
373 Peer
=> "/run/watchdog-mux.sock") ||
374 die "unable to open watchdog socket - $!\n";
376 $self->log('info', "watchdog active");
379 sub watchdog_update
{
380 my ($self, $wfh) = @_;
382 my $res = $watchdog_fh->syswrite("\0", 1);
383 if (!defined($res)) {
384 $self->log('err', "watchdog update failed - $!\n");
388 $self->log('err', "watchdog update failed - write $res bytes\n");
396 my ($self, $wfh) = @_;
398 $watchdog_fh->syswrite("V", 1); # magic watchdog close
399 if (!$watchdog_fh->close()) {
400 $self->log('err', "watchdog close failed - $!");
402 $watchdog_fh = undef;
403 $self->log('info', "watchdog closed (disabled)");
410 # close inherited inotify FD from parent and reopen our own
411 PVE
::INotify
::inotify_close
();
412 PVE
::INotify
::inotify_init
();
414 PVE
::Cluster
::cfs_update
();
417 sub get_max_workers
{
420 my $datacenterconfig = cfs_read_file
('datacenter.cfg');
422 return $datacenterconfig->{max_workers
} || 4;