abcc46d36c00ce381279b4c61d26b69e1a94705f
[pve-cluster.git] / data / PVE / Cluster.pm
1 package PVE::Cluster;
2
3 use strict;
4 use warnings;
5
6 use Encode;
7 use File::stat qw();
8 use File::Path qw(make_path);
9 use JSON;
10 use Net::SSLeay;
11 use POSIX qw(ENOENT);
12 use Socket;
13 use Storable qw(dclone);
14
15 use PVE::Certificate;
16 use PVE::INotify;
17 use PVE::IPCC;
18 use PVE::JSONSchema;
19 use PVE::Network;
20 use PVE::SafeSyslog;
21 use PVE::Tools qw(run_command);
22
23 use PVE::Cluster::IPCConst;
24
25 use base 'Exporter';
26
27 our @EXPORT_OK = qw(
28 cfs_read_file
29 cfs_write_file
30 cfs_register_file
31 cfs_lock_file);
32
33 # x509 certificate utils
34
35 my $basedir = "/etc/pve";
36 my $authdir = "$basedir/priv";
37 my $lockdir = "/etc/pve/priv/lock";
38
39 # cfs and corosync files
40 my $dbfile = "/var/lib/pve-cluster/config.db";
41 my $dbbackupdir = "/var/lib/pve-cluster/backup";
42
43 # this is just a readonly copy, the relevant one is in status.c from pmxcfs
44 # observed files are the one we can get directly through IPCC, they are cached
45 # using a computed version and only those can be used by the cfs_*_file methods
46 my $observed = {
47 'vzdump.cron' => 1,
48 'jobs.cfg' => 1,
49 'storage.cfg' => 1,
50 'datacenter.cfg' => 1,
51 'replication.cfg' => 1,
52 'corosync.conf' => 1,
53 'corosync.conf.new' => 1,
54 'user.cfg' => 1,
55 'domains.cfg' => 1,
56 'priv/shadow.cfg' => 1,
57 'priv/tfa.cfg' => 1,
58 'priv/token.cfg' => 1,
59 'priv/acme/plugins.cfg' => 1,
60 'priv/ipam.db' => 1,
61 '/qemu-server/' => 1,
62 '/openvz/' => 1,
63 '/lxc/' => 1,
64 'ha/crm_commands' => 1,
65 'ha/manager_status' => 1,
66 'ha/resources.cfg' => 1,
67 'ha/groups.cfg' => 1,
68 'ha/fence.cfg' => 1,
69 'status.cfg' => 1,
70 'ceph.conf' => 1,
71 'sdn/vnets.cfg' => 1,
72 'sdn/zones.cfg' => 1,
73 'sdn/controllers.cfg' => 1,
74 'sdn/subnets.cfg' => 1,
75 'sdn/ipams.cfg' => 1,
76 'sdn/dns.cfg' => 1,
77 'sdn/.running-config' => 1,
78 'virtual-guest/cpu-models.conf' => 1,
79 };
80
81 sub prepare_observed_file_basedirs {
82
83 if (!check_cfs_is_mounted(1)) {
84 warn "pmxcfs isn't mounted (/etc/pve), chickening out..\n";
85 return;
86 }
87
88 for my $f (sort keys %$observed) {
89 next if $f !~ m!^(.*)/[^/]+$!;
90 my $dir = "$basedir/$1";
91 next if -e $dir; # can also be a link, so just use -e xist check
92 print "creating directory '$dir' for observerd files\n";
93 make_path($dir);
94 }
95 }
96
97 sub base_dir {
98 return $basedir;
99 }
100
101 sub auth_dir {
102 return $authdir;
103 }
104
105 sub check_cfs_quorum {
106 my ($noerr) = @_;
107
108 # note: -w filename always return 1 for root, so wee need
109 # to use File::lstat here
110 my $st = File::stat::lstat("$basedir/local");
111 my $quorate = ($st && (($st->mode & 0200) != 0));
112
113 die "cluster not ready - no quorum?\n" if !$quorate && !$noerr;
114
115 return $quorate;
116 }
117
118 sub check_cfs_is_mounted {
119 my ($noerr) = @_;
120
121 my $res = -l "$basedir/local";
122
123 die "pve configuration filesystem (pmxcfs) not mounted\n" if !$res && !$noerr;
124
125 return $res;
126 }
127
128 my $versions = {};
129 my $vmlist = {};
130 my $clinfo = {};
131
132 my $ipcc_send_rec = sub {
133 my ($msgid, $data) = @_;
134
135 my $res = PVE::IPCC::ipcc_send_rec($msgid, $data);
136
137 die "ipcc_send_rec[$msgid] failed: $!\n" if !defined($res) && ($! != 0);
138
139 return $res;
140 };
141
142 my $ipcc_send_rec_json = sub {
143 my ($msgid, $data) = @_;
144
145 my $res = PVE::IPCC::ipcc_send_rec($msgid, $data);
146
147 die "ipcc_send_rec[$msgid] failed: $!\n" if !defined($res) && ($! != 0);
148
149 return decode_json($res);
150 };
151
152 my $ipcc_get_config = sub {
153 my ($path) = @_;
154
155 my $bindata = pack "Z*", $path;
156 my $res = PVE::IPCC::ipcc_send_rec(CFS_IPC_GET_CONFIG, $bindata);
157 if (!defined($res)) {
158 if ($! != 0) {
159 return undef if $! == ENOENT;
160 die "$!\n";
161 }
162 return '';
163 }
164
165 return $res;
166 };
167
168 my $ipcc_get_status = sub {
169 my ($name, $nodename) = @_;
170
171 my $bindata = pack "Z[256]Z[256]", $name, ($nodename || "");
172 return PVE::IPCC::ipcc_send_rec(CFS_IPC_GET_STATUS, $bindata);
173 };
174
175 my $ipcc_remove_status = sub {
176 my ($name) = @_;
177 # we just omit the data payload, pmxcfs takes this as hint and removes this
178 # key from the status hashtable
179 my $bindata = pack "Z[256]", $name;
180 return &$ipcc_send_rec(CFS_IPC_SET_STATUS, $bindata);
181 };
182
183 my $ipcc_update_status = sub {
184 my ($name, $data) = @_;
185
186 my $raw = ref($data) ? encode_json($data) : $data;
187 # update status
188 my $bindata = pack "Z[256]Z*", $name, $raw;
189
190 return &$ipcc_send_rec(CFS_IPC_SET_STATUS, $bindata);
191 };
192
193 my $ipcc_log = sub {
194 my ($priority, $ident, $tag, $msg) = @_;
195
196 my $bindata = pack "CCCZ*Z*Z*", $priority, bytes::length($ident) + 1,
197 bytes::length($tag) + 1, $ident, $tag, $msg;
198
199 return &$ipcc_send_rec(CFS_IPC_LOG_CLUSTER_MSG, $bindata);
200 };
201
202 my $ipcc_get_cluster_log = sub {
203 my ($user, $max) = @_;
204
205 $max = 0 if !defined($max);
206
207 my $bindata = pack "VVVVZ*", $max, 0, 0, 0, ($user || "");
208 return &$ipcc_send_rec(CFS_IPC_GET_CLUSTER_LOG, $bindata);
209 };
210
211 my $ipcc_verify_token = sub {
212 my ($full_token) = @_;
213
214 my $bindata = pack "Z*", $full_token;
215 my $res = PVE::IPCC::ipcc_send_rec(CFS_IPC_VERIFY_TOKEN, $bindata);
216
217 return 1 if $! == 0;
218 return 0 if $! == ENOENT;
219
220 die "$!\n";
221 };
222
223 my $ccache = {};
224
225 sub cfs_update {
226 my ($fail) = @_;
227 eval {
228 my $res = &$ipcc_send_rec_json(CFS_IPC_GET_FS_VERSION);
229 die "no starttime\n" if !$res->{starttime};
230
231 if (!$res->{starttime} || !$versions->{starttime} ||
232 $res->{starttime} != $versions->{starttime}) {
233 #print "detected changed starttime\n";
234 $vmlist = {};
235 $clinfo = {};
236 $ccache = {};
237 }
238
239 $versions = $res;
240 };
241 my $err = $@;
242 if ($err) {
243 $versions = {};
244 $vmlist = {};
245 $clinfo = {};
246 $ccache = {};
247 die $err if $fail;
248 warn $err;
249 }
250
251 eval {
252 if (!$clinfo->{version} || $clinfo->{version} != $versions->{clinfo}) {
253 #warn "detected new clinfo\n";
254 $clinfo = &$ipcc_send_rec_json(CFS_IPC_GET_CLUSTER_INFO);
255 }
256 };
257 $err = $@;
258 if ($err) {
259 $clinfo = {};
260 die $err if $fail;
261 warn $err;
262 }
263
264 eval {
265 if (!$vmlist->{version} || $vmlist->{version} != $versions->{vmlist}) {
266 #warn "detected new vmlist1\n";
267 $vmlist = &$ipcc_send_rec_json(CFS_IPC_GET_GUEST_LIST);
268 }
269 };
270 $err = $@;
271 if ($err) {
272 $vmlist = {};
273 die $err if $fail;
274 warn $err;
275 }
276 }
277
278 sub get_vmlist {
279 return $vmlist;
280 }
281
282 sub get_clinfo {
283 return $clinfo;
284 }
285
286 sub get_members {
287 return $clinfo->{nodelist};
288 }
289
290 sub get_nodelist {
291 my $nodelist = $clinfo->{nodelist};
292
293 my $nodename = PVE::INotify::nodename();
294
295 if (!$nodelist || !$nodelist->{$nodename}) {
296 return [ $nodename ];
297 }
298
299 return [ keys %$nodelist ];
300 }
301
302 # only stored in a in-memory hashtable inside pmxcfs, local data is gone after
303 # a restart (of pmxcfs or the node), peer data is still available then
304 # best used for status data, like running (ceph) services, package versions, ...
305 sub broadcast_node_kv {
306 my ($key, $data) = @_;
307
308 if (!defined($data)) {
309 eval { $ipcc_remove_status->("kv/$key") };
310 } else {
311 die "cannot send a reference\n" if ref($data);
312 my $size = length($data);
313 die "data for '$key' too big\n" if $size >= (32 * 1024); # limit from pmxfs
314
315 eval { $ipcc_update_status->("kv/$key", $data) };
316 }
317 warn $@ if $@;
318 }
319
320 # nodename is optional
321 sub get_node_kv {
322 my ($key, $nodename) = @_;
323
324 my $res = {};
325 my $get_node_data = sub {
326 my ($node) = @_;
327 my $raw = $ipcc_get_status->("kv/$key", $node);
328 $res->{$node} = unpack("Z*", $raw) if $raw;
329 };
330
331 if ($nodename) {
332 $get_node_data->($nodename);
333 } else {
334 for my $node (get_nodelist()->@*) {
335 $get_node_data->($node);
336 }
337 }
338
339 return $res;
340 }
341
342 # property: a config property you want to get, e.g., this is perfect to get
343 # the 'lock' entry of a guest _fast_ (>100 faster than manual parsing here)
344 # vmid: optional, if a valid is passed we only check that one, else return all
345 # NOTE: does *not* searches snapshot and PENDING entries sections!
346 sub get_guest_config_property {
347 my ($property, $vmid) = @_;
348
349 die "property is required" if !defined($property);
350
351 my $bindata = pack "VZ*", $vmid // 0, $property;
352 my $res = $ipcc_send_rec_json->(CFS_IPC_GET_GUEST_CONFIG_PROPERTY, $bindata);
353
354 return $res;
355 }
356
357 # $data must be a chronological descending ordered array of tasks
358 sub broadcast_tasklist {
359 my ($data) = @_;
360
361 # the serialized list may not get bigger than 128 KiB (CFS_MAX_STATUS_SIZE from pmxcfs)
362 # drop older items until we satisfy this constraint
363 my $size = length(encode_json($data));
364 while ($size >= (32 * 1024)) { # TODO: update to 128 KiB in PVE 8.x
365 pop @$data;
366 $size = length(encode_json($data));
367 }
368
369 eval { $ipcc_update_status->("tasklist", $data) };
370 warn $@ if $@;
371 }
372
373 my $tasklistcache = {};
374
375 sub get_tasklist {
376 my ($nodename) = @_;
377
378 my $kvstore = $versions->{kvstore} || {};
379
380 my $nodelist = get_nodelist();
381
382 my $res = [];
383 foreach my $node (@$nodelist) {
384 next if $nodename && ($nodename ne $node);
385 eval {
386 my $ver = $kvstore->{$node}->{tasklist} if $kvstore->{$node};
387 my $cache = $tasklistcache->{$node};
388 if (!$cache || !$ver || !$cache->{version} || ($cache->{version} != $ver)) {
389 my $tasks = [];
390 if (my $raw = $ipcc_get_status->("tasklist", $node)) {
391 my $json_str = unpack("Z*", $raw);
392 $tasks = decode_json($json_str);
393 }
394 push @$res, @$tasks;
395 $tasklistcache->{$node} = {
396 data => $tasks,
397 version => $ver,
398 };
399 } elsif ($cache && $cache->{data}) {
400 push @$res, $cache->{data}->@*;
401 }
402 };
403 my $err = $@;
404 syslog('err', $err) if $err;
405 }
406
407 return $res;
408 }
409
410 sub broadcast_rrd {
411 my ($rrdid, $data) = @_;
412
413 eval {
414 &$ipcc_update_status("rrd/$rrdid", $data);
415 };
416 my $err = $@;
417
418 warn $err if $err;
419 }
420
421 my $last_rrd_dump = 0;
422 my $last_rrd_data = "";
423
424 sub rrd_dump {
425
426 my $ctime = time();
427
428 my $diff = $ctime - $last_rrd_dump;
429 if ($diff < 2) {
430 return $last_rrd_data;
431 }
432
433 my $raw;
434 eval {
435 $raw = &$ipcc_send_rec(CFS_IPC_GET_RRD_DUMP);
436 };
437 my $err = $@;
438
439 if ($err) {
440 warn $err;
441 return {};
442 }
443
444 my $res = {};
445
446 if ($raw) {
447 while ($raw =~ s/^(.*)\n//) {
448 my ($key, @ela) = split(/:/, $1);
449 next if !$key;
450 next if !(scalar(@ela) > 1);
451 $res->{$key} = [ map { $_ eq 'U' ? undef : $_ } @ela ];
452 }
453 }
454
455 $last_rrd_dump = $ctime;
456 $last_rrd_data = $res;
457
458 return $res;
459 }
460
461
462 # a fast way to read files (avoid fuse overhead)
463 sub get_config {
464 my ($path) = @_;
465
466 return &$ipcc_get_config($path);
467 }
468
469 sub get_cluster_log {
470 my ($user, $max) = @_;
471
472 return &$ipcc_get_cluster_log($user, $max);
473 }
474
475 sub verify_token {
476 my ($userid, $token) = @_;
477
478 return &$ipcc_verify_token("$userid $token");
479 }
480
481 my $file_info = {};
482
483 sub cfs_register_file {
484 my ($filename, $parser, $writer) = @_;
485
486 $observed->{$filename} || die "unknown file '$filename'";
487
488 die "file '$filename' already registered" if $file_info->{$filename};
489
490 $file_info->{$filename} = {
491 parser => $parser,
492 writer => $writer,
493 };
494 }
495
496 my $ccache_read = sub {
497 my ($filename, $parser, $version) = @_;
498
499 $ccache->{$filename} = {} if !$ccache->{$filename};
500
501 my $ci = $ccache->{$filename};
502
503 if (!$ci->{version} || !$version || $ci->{version} != $version) {
504 # we always call the parser, even when the file does not exist
505 # (in that case $data is undef)
506 my $data = get_config($filename);
507 $ci->{data} = &$parser("/etc/pve/$filename", $data);
508 $ci->{version} = $version;
509 }
510
511 my $res = ref($ci->{data}) ? dclone($ci->{data}) : $ci->{data};
512
513 return $res;
514 };
515
516 sub cfs_file_version {
517 my ($filename) = @_;
518
519 my $version;
520 my $infotag;
521 if ($filename =~ m!^nodes/[^/]+/(openvz|lxc|qemu-server)/(\d+)\.conf$!) {
522 my ($type, $vmid) = ($1, $2);
523 if ($vmlist && $vmlist->{ids} && $vmlist->{ids}->{$vmid}) {
524 $version = $vmlist->{ids}->{$vmid}->{version};
525 }
526 $infotag = "/$type/";
527 } else {
528 $infotag = $filename;
529 $version = $versions->{$filename};
530 }
531
532 my $info = $file_info->{$infotag} ||
533 die "unknown file type '$filename'\n";
534
535 return wantarray ? ($version, $info) : $version;
536 }
537
538 sub cfs_read_file {
539 my ($filename) = @_;
540
541 my ($version, $info) = cfs_file_version($filename);
542 my $parser = $info->{parser};
543
544 return &$ccache_read($filename, $parser, $version);
545 }
546
547 sub cfs_write_file {
548 my ($filename, $data) = @_;
549
550 my ($version, $info) = cfs_file_version($filename);
551
552 my $writer = $info->{writer} || die "no writer defined";
553
554 my $fsname = "/etc/pve/$filename";
555
556 my $raw = &$writer($fsname, $data);
557
558 if (my $ci = $ccache->{$filename}) {
559 $ci->{version} = undef;
560 }
561
562 PVE::Tools::file_set_contents($fsname, $raw);
563 }
564
565 my $cfs_lock = sub {
566 my ($lockid, $timeout, $code, @param) = @_;
567
568 my $prev_alarm = alarm(0); # suspend outer alarm early
569
570 my $res;
571 my $got_lock = 0;
572
573 # this timeout is for acquire the lock
574 $timeout = 10 if !$timeout;
575
576 my $filename = "$lockdir/$lockid";
577
578 my $is_code_err = 0;
579 eval {
580
581 mkdir $lockdir;
582
583 if (! -d $lockdir) {
584 die "pve cluster filesystem not online.\n";
585 }
586
587 my $timeout_err = sub { die "got lock request timeout\n"; };
588 local $SIG{ALRM} = $timeout_err;
589
590 while (1) {
591 alarm ($timeout);
592 $got_lock = mkdir($filename);
593 $timeout = alarm(0) - 1; # we'll sleep for 1s, see down below
594
595 last if $got_lock;
596
597 $timeout_err->() if $timeout <= 0;
598
599 print STDERR "trying to acquire cfs lock '$lockid' ...\n";
600 utime (0, 0, $filename); # cfs unlock request
601 sleep(1);
602 }
603
604 # fixed command timeout: cfs locks have a timeout of 120
605 # using 60 gives us another 60 seconds to abort the task
606 local $SIG{ALRM} = sub { die "'$lockid'-locked command timed out - aborting\n"; };
607 alarm(60);
608
609 cfs_update(); # make sure we read latest versions inside code()
610
611 $is_code_err = 1; # allows to differ between locking and actual-work errors
612
613 $res = &$code(@param);
614
615 alarm(0);
616 };
617
618 my $err = $@;
619
620 $err = "no quorum!\n" if !$got_lock && !check_cfs_quorum(1);
621
622 rmdir $filename if $got_lock; # if we held the lock always unlock again
623
624 alarm($prev_alarm);
625
626 if ($err) {
627 if (ref($err) eq 'PVE::Exception' || $is_code_err) {
628 # re-raise defined exceptions
629 $@ = $err;
630 } else {
631 # add lock info for plain errors comming from the locking itself
632 $@ = "cfs-lock '$lockid' error: $err";
633 }
634 return undef;
635 }
636
637 $@ = undef;
638
639 return $res;
640 };
641
642 sub cfs_lock_file {
643 my ($filename, $timeout, $code, @param) = @_;
644
645 my $info = $observed->{$filename} || die "unknown file '$filename'";
646
647 my $lockid = "file-$filename";
648 $lockid =~ s/[.\/]/_/g;
649
650 &$cfs_lock($lockid, $timeout, $code, @param);
651 }
652
653 sub cfs_lock_storage {
654 my ($storeid, $timeout, $code, @param) = @_;
655
656 my $lockid = "storage-$storeid";
657
658 &$cfs_lock($lockid, $timeout, $code, @param);
659 }
660
661 sub cfs_lock_domain {
662 my ($domainname, $timeout, $code, @param) = @_;
663
664 my $lockid = "domain-$domainname";
665
666 &$cfs_lock($lockid, $timeout, $code, @param);
667 }
668
669 sub cfs_lock_acme {
670 my ($account, $timeout, $code, @param) = @_;
671
672 my $lockid = "acme-$account";
673
674 &$cfs_lock($lockid, $timeout, $code, @param);
675 }
676
677 sub cfs_lock_authkey {
678 my ($timeout, $code, @param) = @_;
679
680 $cfs_lock->('authkey', $timeout, $code, @param);
681 }
682
683 sub cfs_lock_firewall {
684 my ($scope, $timeout, $code, @param) = @_;
685
686 my $lockid = "firewall-$scope";
687
688 $cfs_lock->($lockid, $timeout, $code, @param);
689 }
690
691 my $log_levels = {
692 "emerg" => 0,
693 "alert" => 1,
694 "crit" => 2,
695 "critical" => 2,
696 "err" => 3,
697 "error" => 3,
698 "warn" => 4,
699 "warning" => 4,
700 "notice" => 5,
701 "info" => 6,
702 "debug" => 7,
703 };
704
705 sub log_msg {
706 my ($priority, $ident, $msg) = @_;
707
708 if (my $tmp = $log_levels->{$priority}) {
709 $priority = $tmp;
710 }
711
712 die "need numeric log priority" if $priority !~ /^\d+$/;
713
714 my $tag = PVE::SafeSyslog::tag();
715
716 $msg = "empty message" if !$msg;
717
718 $ident = "" if !$ident;
719 $ident = encode("ascii", $ident,
720 sub { sprintf "\\u%04x", shift });
721
722 my $ascii = encode("ascii", $msg, sub { sprintf "\\u%04x", shift });
723
724 if ($ident) {
725 syslog($priority, "<%s> %s", $ident, $ascii);
726 } else {
727 syslog($priority, "%s", $ascii);
728 }
729
730 eval { &$ipcc_log($priority, $ident, $tag, $ascii); };
731
732 syslog("err", "writing cluster log failed: $@") if $@;
733 }
734
735 sub check_vmid_unused {
736 my ($vmid, $noerr) = @_;
737
738 my $vmlist = get_vmlist();
739
740 my $d = $vmlist->{ids}->{$vmid};
741 return 1 if !defined($d);
742
743 return undef if $noerr;
744
745 my $vmtypestr = $d->{type} eq 'qemu' ? 'VM' : 'CT';
746 die "$vmtypestr $vmid already exists on node '$d->{node}'\n";
747 }
748
749 sub check_node_exists {
750 my ($nodename, $noerr) = @_;
751
752 my $nodelist = $clinfo->{nodelist};
753 return 1 if $nodelist && $nodelist->{$nodename};
754
755 return undef if $noerr;
756
757 die "no such cluster node '$nodename'\n";
758 }
759
760 # this is also used to get the IP of the local node
761 sub remote_node_ip {
762 my ($nodename, $noerr) = @_;
763
764 my $nodelist = $clinfo->{nodelist};
765 if ($nodelist && $nodelist->{$nodename}) {
766 if (my $ip = $nodelist->{$nodename}->{ip}) {
767 return $ip if !wantarray;
768 my $family = $nodelist->{$nodename}->{address_family};
769 if (!$family) {
770 $nodelist->{$nodename}->{address_family} =
771 $family =
772 PVE::Tools::get_host_address_family($ip);
773 }
774 return wantarray ? ($ip, $family) : $ip;
775 }
776 }
777
778 # fallback: try to get IP by other means
779 return PVE::Network::get_ip_from_hostname($nodename, $noerr);
780 }
781
782 sub get_node_fingerprint {
783 my ($node) = @_;
784
785 my $cert_path = "/etc/pve/nodes/$node/pve-ssl.pem";
786 my $custom_cert_path = "/etc/pve/nodes/$node/pveproxy-ssl.pem";
787
788 $cert_path = $custom_cert_path if -f $custom_cert_path;
789
790 return PVE::Certificate::get_certificate_fingerprint($cert_path);
791 }
792
793 # bash completion helpers
794
795 sub complete_next_vmid {
796
797 my $vmlist = get_vmlist() || {};
798 my $idlist = $vmlist->{ids} || {};
799
800 for (my $i = 100; $i < 10000; $i++) {
801 return [$i] if !defined($idlist->{$i});
802 }
803
804 return [];
805 }
806
807 sub complete_vmid {
808
809 my $vmlist = get_vmlist();
810 my $ids = $vmlist->{ids} || {};
811
812 return [ keys %$ids ];
813 }
814
815 sub complete_local_vmid {
816
817 my $vmlist = get_vmlist();
818 my $ids = $vmlist->{ids} || {};
819
820 my $nodename = PVE::INotify::nodename();
821
822 my $res = [];
823 foreach my $vmid (keys %$ids) {
824 my $d = $ids->{$vmid};
825 next if !$d->{node} || $d->{node} ne $nodename;
826 push @$res, $vmid;
827 }
828
829 return $res;
830 }
831
832 sub complete_migration_target {
833
834 my $res = [];
835
836 my $nodename = PVE::INotify::nodename();
837
838 my $nodelist = get_nodelist();
839 foreach my $node (@$nodelist) {
840 next if $node eq $nodename;
841 push @$res, $node;
842 }
843
844 return $res;
845 }
846
847
848 # NOTE: filesystem must be offline here, no DB changes allowed
849 sub cfs_backup_database {
850 mkdir $dbbackupdir;
851
852 my $ctime = time();
853 my $backup_fn = "$dbbackupdir/config-$ctime.sql.gz";
854
855 print "backup old database to '$backup_fn'\n";
856
857 my $cmd = [ ['sqlite3', $dbfile, '.dump'], ['gzip', '-', \ ">${backup_fn}"] ];
858 run_command($cmd, 'errmsg' => "cannot backup old database\n");
859
860 my $maxfiles = 10; # purge older backup
861 my $backups = [ sort { $b cmp $a } <$dbbackupdir/config-*.sql.gz> ];
862
863 if ((my $count = scalar(@$backups)) > $maxfiles) {
864 foreach my $f (@$backups[$maxfiles..$count-1]) {
865 next if $f !~ m/^(\S+)$/; # untaint
866 print "delete old backup '$1'\n";
867 unlink $1;
868 }
869 }
870
871 return $dbfile;
872 }
873
874 1;