]> git.proxmox.com Git - pve-manager.git/blame - PVE/CLI/pve5to6.pm
pve5to6: refactor out apt-cache package installed checks
[pve-manager.git] / PVE / CLI / pve5to6.pm
CommitLineData
c62eb5b2
FG
1package PVE::CLI::pve5to6;
2
3use strict;
4use warnings;
5
6use PVE::API2::APT;
7use PVE::API2::Ceph;
8use PVE::API2::LXC;
9use PVE::API2::Qemu;
70effe27 10use PVE::API2::Certificates;
c62eb5b2
FG
11
12use PVE::Ceph::Tools;
13use PVE::Cluster;
14use PVE::Corosync;
15use PVE::INotify;
16use PVE::JSONSchema;
17use PVE::RPCEnvironment;
18use PVE::Storage;
e9ad9ab4 19use PVE::Tools qw(run_command $IPV4RE $IPV6RE);
eb7fda99 20use PVE::QemuServer;
a0841973 21use PVE::QemuConfig;
c62eb5b2 22
aa517822 23use AptPkg::Cache;
e9ad9ab4 24use Socket qw(AF_INET AF_INET6 inet_ntop);
23bba5a4
FG
25use Term::ANSIColor;
26
c62eb5b2
FG
27use PVE::CLIHandler;
28
29use base qw(PVE::CLIHandler);
30
31my $nodename = PVE::INotify::nodename();
32
33sub setup_environment {
34 PVE::RPCEnvironment->setup_default_cli_env();
35}
36
6afec5da
TL
37my $min_pve_major = 5;
38my $min_pve_minor = 4;
c62eb5b2
FG
39my $min_pve_pkgrel = 2;
40
41my $counters = {
42 pass => 0,
43 skip => 0,
44 warn => 0,
45 fail => 0,
46};
47
48my $log_line = sub {
49 my ($level, $line) = @_;
50
51 $counters->{$level}++ if defined($level) && defined($counters->{$level});
52
53 print uc($level), ': ' if defined($level);
54 print "$line\n";
55};
56
57sub log_pass {
23bba5a4 58 print color('green');
c62eb5b2 59 $log_line->('pass', @_);
23bba5a4 60 print color('reset');
c62eb5b2
FG
61}
62
63sub log_info {
64 $log_line->('info', @_);
65}
66sub log_skip {
67 $log_line->('skip', @_);
68}
69sub log_warn {
23bba5a4 70 print color('yellow');
c62eb5b2 71 $log_line->('warn', @_);
23bba5a4 72 print color('reset');
c62eb5b2
FG
73}
74sub log_fail {
23bba5a4 75 print color('red');
c62eb5b2 76 $log_line->('fail', @_);
23bba5a4 77 print color('reset');
c62eb5b2
FG
78}
79
f3d40afa
TL
80my $print_header_first = 1;
81sub print_header {
82 my ($h) = @_;
83 print "\n" if !$print_header_first;
84 print "= $h =\n\n";
85 $print_header_first = 0;
86}
87
57d96361
TL
88my $get_systemd_unit_state = sub {
89 my ($unit) = @_;
90
91 my $state;
92 my $filter_output = sub {
93 $state = shift;
94 chomp $state;
95 };
96 eval {
97 run_command(['systemctl', 'is-enabled', "$unit"], outfunc => $filter_output, noerr => 1);
98 return if !defined($state);
99 run_command(['systemctl', 'is-active', "$unit"], outfunc => $filter_output, noerr => 1);
100 };
101
102 return $state // 'unknown';
103};
104my $log_systemd_unit_state = sub {
105 my ($unit, $no_fail_on_inactive) = @_;
106
107 my $log_method = \&log_warn;
108
109 my $state = $get_systemd_unit_state->($unit);
110 if ($state eq 'active') {
111 $log_method = \&log_pass;
112 } elsif ($state eq 'inactive') {
113 $log_method = $no_fail_on_inactive ? \&log_warn : \&log_fail;
114 } elsif ($state eq 'failed') {
115 $log_method = \&log_fail;
116 }
117
118 $log_method->("systemd unit '$unit' is in state '$state'");
119};
120
663ae4d8 121my $versions;
c62eb5b2
FG
122my $get_pkg = sub {
123 my ($pkg) = @_;
124
663ae4d8 125 $versions = eval { PVE::API2::APT->versions({ node => $nodename }) } if !defined($versions);
c62eb5b2
FG
126
127 if (!defined($versions)) {
128 my $msg = "unable to retrieve package version information";
129 $msg .= "- $@" if $@;
130 log_fail("$msg");
131 return undef;
132 }
133
134 my $pkgs = [ grep { $_->{Package} eq $pkg } @$versions ];
135 if (!defined $pkgs || $pkgs == 0) {
136 log_fail("unable to determine installed $pkg version.");
137 return undef;
138 } else {
139 return $pkgs->[0];
140 }
141};
142
797ba640
TL
143my $apt_cache;
144my $get_apt_package_state = sub {
145 my ($pkg) = @_;
146
147 if (!$apt_cache) {
148 if (!($apt_cache = AptPkg::Cache->new())) {
149 log_fail("unable to initialize AptPkg::Cache\n"); # should not happen at all
150 return undef;
151 }
152 }
153 my $p = $apt_cache->{'linux-image-amd64'};
154 if ($p) {
155 return $p->{SelectedState};
156 }
157 return undef;
158};
159
160my $is_pkg_installed = sub {
161 my ($pkg) = @_;
162
163 my $state = $get_apt_package_state->($pkg);
164 return undef if !defined($state); # better die?
165 return lc($state) eq 'install';
166};
167
e9ad9ab4
FG
168# taken from pve-cluster 6.0-4
169my $resolve_hostname_like_corosync = sub {
170 my ($hostname, $corosync_conf) = @_;
171
172 my $corosync_strategy = $corosync_conf->{main}->{totem}->{ip_version};
173 $corosync_strategy = lc ($corosync_strategy // "ipv6-4");
174
175 my $match_ip_and_version = sub {
176 my ($addr) = @_;
177
178 return undef if !defined($addr);
179
180 if ($addr =~ m/^$IPV4RE$/) {
181 return ($addr, 4);
182 } elsif ($addr =~ m/^$IPV6RE$/) {
183 return ($addr, 6);
184 }
185
186 return undef;
187 };
188
189 my ($resolved_ip, $ip_version) = $match_ip_and_version->($hostname);
190
191 return ($resolved_ip, $ip_version) if defined($resolved_ip);
192
193 my $resolved_ip4;
194 my $resolved_ip6;
195
196 my @resolved_raw;
197 eval { @resolved_raw = PVE::Tools::getaddrinfo_all($hostname); };
198
199 return undef if ($@ || !@resolved_raw);
200
201 foreach my $socket_info (@resolved_raw) {
202 next if !$socket_info->{addr};
203
204 my ($family, undef, $host) = PVE::Tools::unpack_sockaddr_in46($socket_info->{addr});
205
206 if ($family == AF_INET && !defined($resolved_ip4)) {
207 $resolved_ip4 = inet_ntop(AF_INET, $host);
208 } elsif ($family == AF_INET6 && !defined($resolved_ip6)) {
209 $resolved_ip6 = inet_ntop(AF_INET6, $host);
210 }
211
212 last if defined($resolved_ip4) && defined($resolved_ip6);
213 }
214
215 # corosync_strategy specifies the order in which IP addresses are resolved
216 # by corosync. We need to match that order, to ensure we create firewall
217 # rules for the correct address family.
218 if ($corosync_strategy eq "ipv4") {
219 $resolved_ip = $resolved_ip4;
220 } elsif ($corosync_strategy eq "ipv6") {
221 $resolved_ip = $resolved_ip6;
222 } elsif ($corosync_strategy eq "ipv6-4") {
223 $resolved_ip = $resolved_ip6 // $resolved_ip4;
224 } elsif ($corosync_strategy eq "ipv4-6") {
225 $resolved_ip = $resolved_ip4 // $resolved_ip6;
226 }
227
228 return $match_ip_and_version->($resolved_ip);
229};
230
c62eb5b2 231sub check_pve_packages {
f3d40afa 232 print_header("CHECKING VERSION INFORMATION FOR PVE PACKAGES");
c62eb5b2 233
f3d40afa 234 print "Checking for package updates..\n";
c62eb5b2
FG
235 my $updates = eval { PVE::API2::APT->list_updates({ node => $nodename }); };
236 if (!defined($updates)) {
237 log_warn("$@") if $@;
238 log_fail("unable to retrieve list of package updates!");
239 } elsif (@$updates > 0) {
240 my $pkgs = join(', ', map { $_->{Package} } @$updates);
3f5db7fa 241 log_warn("updates for the following packages are available:\n $pkgs");
c62eb5b2
FG
242 } else {
243 log_pass("all packages uptodate");
244 }
245
246 print "\nChecking proxmox-ve package version..\n";
247 if (defined(my $proxmox_ve = $get_pkg->('proxmox-ve'))) {
6afec5da 248 my $min_pve_ver = "$min_pve_major.$min_pve_minor-$min_pve_pkgrel";
c62eb5b2 249
6afec5da
TL
250 my ($maj, $min, $pkgrel) = $proxmox_ve->{OldVersion} =~ m/^(\d+)\.(\d+)-(\d+)/;
251
86a3955d
TL
252 my $upgraded = 0;
253
6afec5da
TL
254 if ($maj > $min_pve_major) {
255 log_pass("already upgraded to Proxmox VE " . ($min_pve_major + 1));
86a3955d 256 $upgraded = 1;
6afec5da 257 } elsif ($maj >= $min_pve_major && $min >= $min_pve_minor && $pkgrel >= $min_pve_pkgrel) {
c62eb5b2
FG
258 log_pass("proxmox-ve package has version >= $min_pve_ver");
259 } else {
260 log_fail("proxmox-ve package is too old, please upgrade to >= $min_pve_ver!");
261 }
86a3955d
TL
262
263 my ($krunning, $kinstalled) = (qr/5\./, 'pve-kernel-5.0');
264 if (!$upgraded) {
265 ($krunning, $kinstalled) = (qr/4\.15/, 'pve-kernel-4.15');
266 }
267
7f4b3ea5
FG
268 print "\nChecking running kernel version..\n";
269 my $kernel_ver = $proxmox_ve->{RunningKernel};
270 if (!defined($kernel_ver)) {
271 log_fail("unable to determine running kernel version.");
86a3955d 272 } elsif ($kernel_ver =~ /^$krunning/) {
7f4b3ea5 273 log_pass("expected running kernel '$kernel_ver'.");
86a3955d
TL
274 } elsif ($get_pkg->($kinstalled)) {
275 log_warn("expected kernel '$kinstalled' intalled but not yet rebooted!");
7f4b3ea5 276 } else {
86a3955d 277 log_warn("unexpected running and installed kernel '$kernel_ver'.");
7f4b3ea5 278 }
aa517822
DC
279
280 }
797ba640
TL
281 print "\nChecking for installed stock Debian Kernel..\n";
282 if ($is_pkg_installed->('linux-image-amd64')) {
283 log_fail("Stock Debian kernel package installed. Please remove package 'linux-image-amd64'.");
aa517822 284 } else {
797ba640 285 log_pass("Stock Debian kernel package not installed.");
c62eb5b2
FG
286 }
287}
288
eb7fda99
DC
289sub get_vms_with_vmx {
290 my $res = {
291 cpu => [],
292 flag => [],
293 };
294 my $vmlist = PVE::QemuServer::vzlist();
450957c0 295
eb7fda99
DC
296 foreach my $vmid ( sort { $a <=> $b } keys %$vmlist ) {
297 my $pid = $vmlist->{$vmid}->{pid};
298 next if !$pid; # skip not running vms
450957c0 299
eb7fda99
DC
300 my $cmdline = eval { PVE::Tools::file_get_contents("/proc/$pid/cmdline") };
301 if ($cmdline) {
302 my @args = split(/\0/, $cmdline);
303 for (my $i = 0; $i < scalar(@args); $i++) {
304 next if !$args[$i] || $args[$i] !~ m/^-?-cpu$/;
450957c0 305
eb7fda99
DC
306 my $cpuarg = $args[$i+1];
307 if ($cpuarg =~ m/^(host|max)/) {
308 push @{$res->{cpu}}, $vmid;
309 } elsif ($cpuarg =~ m/\+(vmx|svm)/) {
310 push @{$res->{flag}}, $vmid;
311 }
312 }
313 }
314 }
450957c0
TL
315
316 $res = undef if (scalar(@{$res->{cpu}}) + scalar(@{$res->{flag}})) <= 0;
317
318 return $res;
eb7fda99
DC
319}
320
7bf30805 321sub check_kvm_nested {
450957c0
TL
322 log_info("Checking KVM nesting support, which breaks live migration for VMs using it..");
323
7bf30805
DC
324 my $module_sysdir = "/sys/module";
325 if (-e "$module_sysdir/kvm_amd") {
326 $module_sysdir .= "/kvm_amd/parameters";
327 } elsif (-e "$module_sysdir/kvm_intel") {
328 $module_sysdir .= "/kvm_intel/parameters";
329 } else {
330 log_skip("no kvm module found");
331 return;
332 }
333
334 if (-f "$module_sysdir/nested") {
335 my $val = eval { PVE::Tools::file_read_firstline("$module_sysdir/nested") };
336 if ($val && $val =~ m/Y|1/) {
450957c0
TL
337 my $list = get_vms_with_vmx();
338 if (!defined($list)) {
339 log_pass("KVM nested parameter set, but currently no VM with a 'vmx' or 'svm' flag is running.");
eb7fda99 340 } else {
450957c0 341 my $warnmsg = "KVM nested enabled. It will not be possible to live migrate the following running VMs to PVE 6:\n";
eb7fda99 342 if (@{$list->{cpu}}) {
450957c0 343 $warnmsg .= " VMID(s) with cputype 'host' or 'max': " . join(',', @{$list->{cpu}}) . "\n";
eb7fda99 344 }
eb7fda99 345 if (@{$list->{flag}}) {
450957c0 346 $warnmsg .= " VMID(s) with enforced cpu flag 'vmx' or 'svm': " . join(',', @{$list->{flag}}) . "\n";
eb7fda99 347 }
eb7fda99
DC
348 log_warn($warnmsg);
349 }
7bf30805
DC
350 } else {
351 log_pass("KVM nested parameter not set.")
352 }
353 } else {
354 log_skip("KVM nested parameter not found.");
355 }
356}
357
a0841973
DC
358sub check_vms_with_uefi {
359 log_info("Checking VMs with OVMF enabled, which may need manual intervention...");
360
361 my $vmlist = PVE::QemuServer::vzlist();
362
363 my $vms = [];
364
365 foreach my $vmid ( sort { $a <=> $b } keys %$vmlist ) {
366 my $conf = PVE::QemuConfig->load_config($vmid);
367 if ($conf->{bios} && $conf->{bios} eq 'ovmf' && $conf->{efidisk0}) {
368 my $disk = PVE::QemuServer::parse_drive('efidisk0', $conf->{efidisk0});
369 if (!defined($disk->{size}) || $disk->{size} > 128*1024) {
370 # all efidisks bigger than the default 128k and those
371 # without size in the config
372 push @$vms, $vmid;
373 } elsif ($disk->{file} !~ /\.(raw|qcow2|vmdk)$/) {
374 # all efidisks not on file storage
375 push @$vms, $vmid;
376 }
377 }
378 }
379
380 if (scalar(@$vms) > 0) {
381 my $warnmsg = "VMs with OVMF configured and potentially broken EFI disks: \n";
382 $warnmsg .= " " . join(',', @$vms);
383 $warnmsg .= "\nThere may be manual intervention required. See Known upgrade issues for details\n";
384 log_warn($warnmsg);
385 } else {
386 log_pass("No VMs with OVMF and potentially broken EFI disk found.");
387 }
388}
389
c62eb5b2 390sub check_storage_health {
f3d40afa 391 print_header("CHECKING CONFIGURED STORAGES");
c62eb5b2
FG
392 my $cfg = PVE::Storage::config();
393
394 my $ctime = time();
395
396 my $info = PVE::Storage::storage_info($cfg);
397
398 foreach my $storeid (keys %$info) {
399 my $d = $info->{$storeid};
400 if ($d->{enabled}) {
cd772160 401 if ($d->{type} eq 'sheepdog') {
92c64229 402 log_fail("storage '$storeid' of type 'sheepdog' is enabled - experimental sheepdog support dropped in PVE 6")
cd772160 403 } elsif ($d->{active}) {
c62eb5b2
FG
404 log_pass("storage '$storeid' enabled and active.");
405 } else {
406 log_warn("storage '$storeid' enabled but not active!");
407 }
408 } else {
409 log_skip("storage '$storeid' disabled.");
410 }
411 }
412}
413
414sub check_cluster_corosync {
f3d40afa 415 print_header("CHECKING CLUSTER HEALTH/SETTINGS");
c62eb5b2
FG
416
417 if (!PVE::Corosync::check_conf_exists(1)) {
418 log_skip("standalone node.");
419 return;
420 }
421
57d96361
TL
422 $log_systemd_unit_state->('pve-cluster.service');
423 $log_systemd_unit_state->('corosync.service');
424
c62eb5b2 425 if (PVE::Cluster::check_cfs_quorum(1)) {
084625c2 426 log_pass("Cluster Filesystem is quorate.");
c62eb5b2 427 } else {
084625c2 428 log_fail("Cluster Filesystem readonly, lost quorum?!");
c62eb5b2
FG
429 }
430
431 my $conf = PVE::Cluster::cfs_read_file('corosync.conf');
432 my $conf_nodelist = PVE::Corosync::nodelist($conf);
fbcc7737 433 my $node_votes = 0;
c62eb5b2 434
77f8b841 435 print "\nAnalzying quorum settings and state..\n";
c62eb5b2
FG
436 if (!defined($conf_nodelist)) {
437 log_fail("unable to retrieve nodelist from corosync.conf");
fbcc7737
FG
438 } else {
439 if (grep { $conf_nodelist->{$_}->{quorum_votes} != 1 } keys %$conf_nodelist) {
440 log_warn("non-default quorum_votes distribution detected!");
441 }
442 map { $node_votes += $conf_nodelist->{$_}->{quorum_votes} // 0 } keys %$conf_nodelist;
443 }
444
445 my ($expected_votes, $total_votes);
446 my $filter_output = sub {
447 my $line = shift;
448 ($expected_votes) = $line =~ /^Expected votes:\s*(\d+)\s*$/
449 if !defined($expected_votes);
450 ($total_votes) = $line =~ /^Total votes:\s*(\d+)\s*$/
451 if !defined($total_votes);
452 };
453 eval {
b60916cc 454 run_command(['corosync-quorumtool', '-s'], outfunc => $filter_output, noerr => 1);
fbcc7737
FG
455 };
456
457 if (!defined($expected_votes)) {
458 log_fail("unable to get expected number of votes, setting to 0.");
459 $expected_votes = 0;
460 }
461 if (!defined($total_votes)) {
462 log_fail("unable to get expected number of votes, setting to 0.");
a6fc492a 463 $total_votes = 0;
c62eb5b2
FG
464 }
465
466 my $cfs_nodelist = PVE::Cluster::get_clinfo()->{nodelist};
467 my $offline_nodes = grep { $cfs_nodelist->{$_}->{online} != 1 } keys %$cfs_nodelist;
468 if ($offline_nodes > 0) {
469 log_fail("$offline_nodes nodes are offline!");
470 }
471
fbcc7737
FG
472 my $qdevice_votes = 0;
473 if (my $qdevice_setup = $conf->{main}->{quorum}->{device}) {
474 $qdevice_votes = $qdevice_setup->{votes} // 1;
475 }
476
477 log_info("configured votes - nodes: $node_votes");
478 log_info("configured votes - qdevice: $qdevice_votes");
479 log_info("current expected votes: $expected_votes");
480 log_info("current total votes: $total_votes");
481
482 log_warn("expected votes set to non-standard value '$expected_votes'.")
483 if $expected_votes != $node_votes + $qdevice_votes;
484 log_warn("total votes < expected votes: $total_votes/$expected_votes!")
485 if $total_votes < $expected_votes;
486
c62eb5b2
FG
487 my $conf_nodelist_count = scalar(keys %$conf_nodelist);
488 my $cfs_nodelist_count = scalar(keys %$cfs_nodelist);
489 log_warn("cluster consists of less than three nodes!")
fbcc7737 490 if $conf_nodelist_count < 3 && $conf_nodelist_count + $qdevice_votes < 3;
c62eb5b2
FG
491
492 log_fail("corosync.conf ($conf_nodelist_count) and pmxcfs ($cfs_nodelist_count) don't agree about size of nodelist.")
493 if $conf_nodelist_count != $cfs_nodelist_count;
494
77f8b841 495 print "\nChecking nodelist entries..\n";
c62eb5b2
FG
496 foreach my $cs_node (keys %$conf_nodelist) {
497 my $entry = $conf_nodelist->{$cs_node};
b3a6a2ae 498 log_fail("$cs_node: no name entry in corosync.conf.")
c62eb5b2 499 if !defined($entry->{name});
b3a6a2ae 500 log_fail("$cs_node: no nodeid configured in corosync.conf.")
c62eb5b2 501 if !defined($entry->{nodeid});
a6524bbe
FG
502 log_fail("$cs_node: neither ring0_addr nor ring1_addr defined in corosync.conf.")
503 if !defined($entry->{ring0_addr}) && !defined($entry->{ring1_addr});
c62eb5b2
FG
504
505 my $verify_ring_ip = sub {
506 my $key = shift;
507 my $ring = $entry->{$key};
b78378f3
FG
508 if (defined($ring)) {
509 my ($resolved_ip, undef) = $resolve_hostname_like_corosync->($ring, $conf);
510 if (defined($resolved_ip)) {
511 if ($resolved_ip ne $ring) {
b3a6a2ae 512 log_warn("$cs_node: $key '$ring' resolves to '$resolved_ip'.\n Consider replacing it with the currently resolved IP address.");
b78378f3 513 } else {
b3a6a2ae 514 log_pass("$cs_node: $key is configured to use IP address '$ring'");
b78378f3
FG
515 }
516 } else {
b3a6a2ae 517 log_fail("$cs_node: unable to resolve $key '$ring' to an IP address according to Corosync's resolve strategy - cluster will potentially fail with Corosync 3.x/kronosnet!");
b78378f3 518 }
c62eb5b2
FG
519 }
520 };
521 $verify_ring_ip->('ring0_addr');
522 $verify_ring_ip->('ring1_addr');
523 }
524
77f8b841 525 print "\nChecking totem settings..\n";
c62eb5b2 526 my $totem = $conf->{main}->{totem};
c62eb5b2
FG
527 my $transport = $totem->{transport};
528 if (defined($transport)) {
e0505600
FG
529 if ($transport ne 'knet') {
530 log_fail("Corosync transport explicitly set to '$transport' instead of implicit default!");
531 } else {
532 log_pass("Corosync transport set to '$transport'.");
533 }
534 } else {
535 log_pass("Corosync transport set to implicit default.");
c62eb5b2
FG
536 }
537
538 if ((!defined($totem->{secauth}) || $totem->{secauth} ne 'on') && (!defined($totem->{crypto_cipher}) || $totem->{crypto_cipher} eq 'none')) {
539 log_fail("Corosync authentication/encryption is not explicitly enabled (secauth / crypto_cipher / crypto_hash)!");
e0505600
FG
540 } else {
541 if (defined($totem->{crypto_cipher}) && $totem->{crypto_cipher} eq '3des') {
542 log_fail("Corosync encryption cipher set to '3des', no longer supported in Corosync 3.x!");
543 } else {
544 log_pass("Corosync encryption and authentication enabled.");
545 }
c62eb5b2
FG
546 }
547
02e79acc 548 print "\n";
0e25ab80 549 log_info("run 'pvecm status' to get detailed cluster status..");
c62eb5b2 550
f3d40afa 551 print_header("CHECKING INSTALLED COROSYNC VERSION");
c62eb5b2
FG
552 if (defined(my $corosync = $get_pkg->('corosync'))) {
553 if ($corosync->{OldVersion} =~ m/^2\./) {
554 log_fail("corosync 2.x installed, cluster-wide upgrade to 3.x needed!");
555 } elsif ($corosync->{OldVersion} =~ m/^3\./) {
556 log_pass("corosync 3.x installed.");
557 } else {
558 log_fail("unexpected corosync version installed: $corosync->{OldVersion}!");
559 }
560 }
561}
562
563sub check_ceph {
f3d40afa 564 print_header("CHECKING HYPER-CONVERGED CEPH STATUS");
c62eb5b2
FG
565
566 if (PVE::Ceph::Tools::check_ceph_inited(1)) {
567 log_info("hyper-converged ceph setup detected!");
568 } else {
569 log_skip("no hyper-converged ceph setup detected!");
570 return;
571 }
572
573 log_info("getting Ceph status/health information..");
574 my $ceph_status = eval { PVE::API2::Ceph->status({ node => $nodename }); };
575 my $osd_flags = eval { PVE::API2::Ceph->get_flags({ node => $nodename }); };
a75241fb
FG
576 my $noout_wanted = 1;
577 my $noout = $osd_flags =~ m/noout/ if $osd_flags;
c62eb5b2
FG
578
579 if (!$ceph_status || !$ceph_status->{health}) {
580 log_fail("unable to determine Ceph status!");
581 } else {
582 my $ceph_health = $ceph_status->{health}->{status};
583 if (!$ceph_health) {
584 log_fail("unable to determine Ceph health!");
585 } elsif ($ceph_health eq 'HEALTH_OK') {
586 log_pass("Ceph health reported as 'HEALTH_OK'.");
587 } elsif ($ceph_health eq 'HEALTH_WARN' && $noout && (keys %{$ceph_status->{health}->{checks}} == 1)) {
588 log_pass("Ceph health reported as 'HEALTH_WARN' with a single failing check and 'noout' flag set.");
589 } else {
dc7eea01
TL
590 log_warn("Ceph health reported as '$ceph_health'.\n Use the PVE ".
591 "dashboard or 'ceph -s' to determine the specific issues and try to resolve them.");
c62eb5b2
FG
592 }
593 }
594
595 log_info("getting Ceph OSD flags..");
596 eval {
597 if (!$osd_flags) {
598 log_fail("unable to get Ceph OSD flags!");
599 } else {
600 if ($osd_flags =~ m/recovery_deletes/ && $osd_flags =~ m/purged_snapdirs/) {
601 log_pass("all PGs have been scrubbed at least once while running Ceph Luminous.");
602 } else {
603 log_fail("missing 'recovery_deletes' and/or 'purged_snapdirs' flag, scrub of all PGs required before upgrading to Nautilus!");
604 }
c62eb5b2
FG
605 }
606 };
607
608 log_info("getting Ceph daemon versions..");
609 my $ceph_versions = eval { PVE::Ceph::Tools::get_cluster_versions(undef, 1); };
610 if (!$ceph_versions) {
611 log_fail("unable to determine Ceph daemon versions!");
612 } else {
613 my $services = [
614 { 'key' => 'mon', 'name' => 'monitor' },
615 { 'key' => 'mgr', 'name' => 'manager' },
616 { 'key' => 'mds', 'name' => 'MDS' },
617 { 'key' => 'osd', 'name' => 'OSD' },
618 ];
619
620 foreach my $service (@$services) {
621 my $name = $service->{name};
622 if (my $service_versions = $ceph_versions->{$service->{key}}) {
623 if (keys %$service_versions == 0) {
624 log_skip("no running instances detected for daemon type $name.");
625 } elsif (keys %$service_versions == 1) {
626 log_pass("single running version detected for daemon type $name.");
627 } else {
628 log_warn("multiple running versions detected for daemon type $name!");
629 }
630 } else {
631 log_skip("unable to determine versions of running Ceph $name instances.");
632 }
633 }
634
635 my $overall_versions = $ceph_versions->{overall};
636 if (!$overall_versions) {
637 log_warn("unable to determine overall Ceph daemon versions!");
638 } elsif (keys %$overall_versions == 1) {
639 log_pass("single running overall version detected for all Ceph daemon types.");
a75241fb
FG
640 if ((keys %$overall_versions)[0] =~ /^ceph version 14\./) {
641 $noout_wanted = 0;
642 }
c62eb5b2
FG
643 } else {
644 log_warn("overall version mismatch detected, check 'ceph versions' output for details!");
645 }
646 }
5d2ae292 647
a75241fb
FG
648 if ($noout) {
649 if ($noout_wanted) {
7a6994bc 650 log_pass("'noout' flag set to prevent rebalancing during cluster-wide upgrades.");
a75241fb 651 } else {
7a6994bc 652 log_warn("'noout' flag set, Ceph cluster upgrade seems finished.");
a75241fb
FG
653 }
654 } elsif ($noout_wanted) {
7a6994bc 655 log_warn("'noout' flag not set - recommended to prevent rebalancing during upgrades.");
a75241fb
FG
656 }
657
42bbe3ed
DC
658 log_info("checking Ceph config..");
659 my $conf = PVE::Cluster::cfs_read_file('ceph.conf');
90ec2ddb 660 if (%$conf) {
42bbe3ed 661 my $global = $conf->{global};
8ade3b35
TL
662
663 my $global_monhost = $global->{mon_host} // $global->{"mon host"} // $global->{"mon-host"};
664 if (!defined($global_monhost)) {
665 log_warn("No 'mon_host' entry found in ceph config.\n It's recommended to add mon_host with all monitor addresses (without ports) to the global section.");
42bbe3ed 666 } else {
7a6994bc 667 log_pass("Found 'mon_host' entry.");
42bbe3ed 668 }
c0d772c4 669
6cc9ab61
TL
670 my $ipv6 = $global->{ms_bind_ipv6} // $global->{"ms bind ipv6"} // $global->{"ms-bind-ipv6"};
671 if ($ipv6) {
672 my $ipv4 = $global->{ms_bind_ipv4} // $global->{"ms bind ipv4"} // $global->{"ms-bind-ipv4"};
c0d772c4 673 if ($ipv6 eq 'true' && (!defined($ipv4) || $ipv4 ne 'false')) {
7a6994bc 674 log_warn("'ms_bind_ipv6' is enabled but 'ms_bind_ipv4' is not disabled.\n Make sure to disable 'ms_bind_ipv4' for ipv6 only clusters, or add an ipv4 network to public/cluster network.");
c0d772c4 675 } else {
7a6994bc 676 log_pass("'ms_bind_ipv6' is enabled and 'ms_bind_ipv4' disabled");
c0d772c4
DC
677 }
678 } else {
7a6994bc 679 log_pass("'ms_bind_ipv6' not enabled");
c0d772c4 680 }
a500f71b
FG
681
682 if (defined($global->{keyring})) {
683 log_warn("[global] config section contains 'keyring' option, which will prevent services from starting with Nautilus.\n Move 'keyring' option to [client] section instead.");
684 } else {
685 log_pass("no 'keyring' option in [global] section found.");
686 }
687
42bbe3ed 688 } else {
90ec2ddb 689 log_warn("Empty ceph config found");
42bbe3ed
DC
690 }
691
5d2ae292
FG
692 my $local_ceph_ver = PVE::Ceph::Tools::get_local_version(1);
693 if (defined($local_ceph_ver)) {
694 if ($local_ceph_ver == 14) {
695 my $scanned_osds = PVE::Tools::dir_glob_regex('/etc/ceph/osd', '^.*\.json$');
696 if (-e '/var/lib/ceph/osd/' && !defined($scanned_osds)) {
697 log_warn("local Ceph version is Nautilus, local OSDs detected, but no conversion from ceph-disk to ceph-volume done (yet).");
698 }
699 }
700 } else {
701 log_fail("unable to determine local Ceph version.");
702 }
c62eb5b2
FG
703}
704
705sub check_misc {
f3d40afa 706 print_header("MISCELLANEOUS CHECKS");
c62eb5b2 707 my $ssh_config = eval { PVE::Tools::file_get_contents('/root/.ssh/config') };
5d100641
FG
708 if (defined($ssh_config)) {
709 log_fail("Unsupported SSH Cipher configured for root in /root/.ssh/config: $1")
710 if $ssh_config =~ /^Ciphers .*(blowfish|arcfour|3des).*$/m;
711 } else {
712 log_skip("No SSH config file found.");
713 }
c62eb5b2 714
e5196e84
TL
715 log_info("Checking common daemon services..");
716 $log_systemd_unit_state->('pveproxy.service');
717 $log_systemd_unit_state->('pvedaemon.service');
718 $log_systemd_unit_state->('pvestatd.service');
719
c62eb5b2
FG
720 my $root_free = PVE::Tools::df('/', 10);
721 log_warn("Less than 2G free space on root file system.")
722 if defined($root_free) && $root_free->{avail} < 2*1024*1024*1024;
723
22ad5ba6 724 log_info("Checking for running guests..");
c62eb5b2 725 my $running_guests = 0;
22ad5ba6 726
c62eb5b2
FG
727 my $vms = eval { PVE::API2::Qemu->vmlist({ node => $nodename }) };
728 log_warn("Failed to retrieve information about this node's VMs - $@") if $@;
22ad5ba6
TL
729 $running_guests += grep { $_->{status} eq 'running' } @$vms if defined($vms);
730
c62eb5b2
FG
731 my $cts = eval { PVE::API2::LXC->vmlist({ node => $nodename }) };
732 log_warn("Failed to retrieve information about this node's CTs - $@") if $@;
22ad5ba6
TL
733 $running_guests += grep { $_->{status} eq 'running' } @$cts if defined($cts);
734
735 if ($running_guests > 0) {
736 log_warn("$running_guests running guest(s) detected - consider migrating or stopping them.")
737 } else {
738 log_pass("no running guest detected.")
739 }
9c2f3b73 740
33866e3c 741 log_info("Checking if the local node's hostname '$nodename' is resolvable..");
f94e33c0 742 my $local_ip = eval { PVE::Network::get_ip_from_hostname($nodename) };
9c2f3b73 743 if ($@) {
f94e33c0 744 log_warn("Failed to resolve hostname '$nodename' to IP - $@");
9c2f3b73 745 } else {
329cf22a 746 log_info("Checking if resolved IP is configured on local node..");
9c2f3b73
ML
747 my $cidr = Net::IP::ip_is_ipv6($local_ip) ? "$local_ip/128" : "$local_ip/32";
748 my $configured_ips = PVE::Network::get_local_ip_from_cidr($cidr);
749 my $ip_count = scalar(@$configured_ips);
ebd4de69
TL
750
751 if ($ip_count <= 0) {
f94e33c0 752 log_fail("Resolved node IP '$local_ip' not configured or active for '$nodename'");
ebd4de69
TL
753 } elsif ($ip_count > 1) {
754 log_warn("Resolved node IP '$local_ip' active on multiple ($ip_count) interfaces!");
22ad5ba6 755 } else {
329cf22a 756 log_pass("Resolved node IP '$local_ip' configured and active on single interface.");
ebd4de69 757 }
9c2f3b73 758 }
b0a8f08b 759
12511b12 760 log_info("Check node certificate's RSA key size");
70effe27 761 my $certs = PVE::API2::Certificates->info({ node => $nodename });
12511b12
TL
762 my $certs_check = {
763 'rsaEncryption' => {
764 minsize => 2048,
765 name => 'RSA',
766 },
767 'id-ecPublicKey' => {
768 minsize => 224,
769 name => 'ECC',
770 },
771 };
772
773 my $certs_check_failed = 0;
774 foreach my $cert (@$certs) {
775 my ($type, $size, $fn) = $cert->@{qw(public-key-type public-key-bits filename)};
776
777 if (!defined($type) || !defined($size)) {
778 log_warn("'$fn': cannot check certificate, failed to get it's type or size!");
779 }
780
781 my $check = $certs_check->{$type};
782 if (!defined($check)) {
783 log_warn("'$fn': certificate's public key type '$type' unknown, check Debian Busters release notes");
784 next;
785 }
786
787 if ($size < $check->{minsize}) {
788 log_fail("'$fn', certificate's $check->{name} public key size is less than 2048 bit");
789 $certs_check_failed = 1;
790 } else {
791 log_pass("Certificate '$fn' passed Debian Busters security level for TLS connections ($size >= 2048)");
70effe27
AA
792 }
793 }
70effe27 794
b0a8f08b 795 check_kvm_nested();
a0841973
DC
796
797 check_vms_with_uefi();
c62eb5b2
FG
798}
799
800__PACKAGE__->register_method ({
801 name => 'checklist',
802 path => 'checklist',
803 method => 'GET',
804 description => 'Check (pre-/post-)upgrade conditions.',
805 parameters => {
806 additionalProperties => 0,
807 properties => {
808 },
809 },
810 returns => { type => 'null' },
811 code => sub {
812 my ($param) = @_;
813
814 check_pve_packages();
815 check_cluster_corosync();
816 check_ceph();
817 check_storage_health();
818 check_misc();
819
f3d40afa 820 print_header("SUMMARY");
f814c077
TL
821
822 my $total = 0;
823 $total += $_ for values %$counters;
824
825 print "TOTAL: $total\n";
52d469af
TL
826 print colored("PASSED: $counters->{pass}\n", 'green');
827 print "SKIPPED: $counters->{skip}\n";
23bba5a4
FG
828 print colored("WARNINGS: $counters->{warn}\n", 'yellow');
829 print colored("FAILURES: $counters->{fail}\n", 'red');
c62eb5b2 830
e35e351f
TL
831 if ($counters->{warn} > 0 || $counters->{fail} > 0) {
832 my $color = $counters->{fail} > 0 ? 'red' : 'yellow';
833 print colored("\nATTENTION: Please check the output for detailed information!\n", $color);
64ef9363 834 print colored("Try to solve the problems one at a time and then run this checklist tool again.\n", $color) if $counters->{fail} > 0;
e35e351f 835 }
c62eb5b2
FG
836
837 return undef;
838 }});
839
89abf04d
TL
840our $cmddef = [ __PACKAGE__, 'checklist', [], {}];
841
842# for now drop all unknown params and just check
843@ARGV = ();
c62eb5b2
FG
844
8451;