]> git.proxmox.com Git - pve-cluster.git/blame - data/PVE/Cluster.pm
bump version to 4.0-20
[pve-cluster.git] / data / PVE / Cluster.pm
CommitLineData
fe000966
DM
1package PVE::Cluster;
2
3use strict;
7181f622 4use warnings;
62613060 5use POSIX qw(EEXIST);
fe000966
DM
6use File::stat qw();
7use Socket;
8use Storable qw(dclone);
9use IO::File;
10use MIME::Base64;
ec48ec22 11use XML::Parser;
440121dc 12use Digest::SHA;
fe000966
DM
13use Digest::HMAC_SHA1;
14use PVE::Tools;
15use PVE::INotify;
16use PVE::IPCC;
17use PVE::SafeSyslog;
d0ad18e8 18use PVE::JSONSchema;
fe000966
DM
19use JSON;
20use RRDs;
21use Encode;
22use base 'Exporter';
23
24our @EXPORT_OK = qw(
25cfs_read_file
26cfs_write_file
27cfs_register_file
28cfs_lock_file);
29
30use Data::Dumper; # fixme: remove
31
32# x509 certificate utils
33
34my $basedir = "/etc/pve";
35my $authdir = "$basedir/priv";
36my $lockdir = "/etc/pve/priv/lock";
37
38my $authprivkeyfn = "$authdir/authkey.key";
39my $authpubkeyfn = "$basedir/authkey.pub";
40my $pveca_key_fn = "$authdir/pve-root-ca.key";
41my $pveca_srl_fn = "$authdir/pve-root-ca.srl";
42my $pveca_cert_fn = "$basedir/pve-root-ca.pem";
43# this is just a secret accessable by the web browser
44# and is used for CSRF prevention
45my $pvewww_key_fn = "$basedir/pve-www.key";
46
47# ssh related files
48my $ssh_rsa_id_priv = "/root/.ssh/id_rsa";
49my $ssh_rsa_id = "/root/.ssh/id_rsa.pub";
50my $ssh_host_rsa_id = "/etc/ssh/ssh_host_rsa_key.pub";
51my $sshglobalknownhosts = "/etc/ssh/ssh_known_hosts";
52my $sshknownhosts = "/etc/pve/priv/known_hosts";
53my $sshauthkeys = "/etc/pve/priv/authorized_keys";
ac50b36d 54my $sshd_config_fn = "/etc/ssh/sshd_config";
fe000966 55my $rootsshauthkeys = "/root/.ssh/authorized_keys";
6056578e 56my $rootsshauthkeysbackup = "${rootsshauthkeys}.org";
f666cdde 57my $rootsshconfig = "/root/.ssh/config";
fe000966
DM
58
59my $observed = {
e1735a61 60 'vzdump.cron' => 1,
fe000966
DM
61 'storage.cfg' => 1,
62 'datacenter.cfg' => 1,
cafc7309
DM
63 'corosync.conf' => 1,
64 'corosync.conf.new' => 1,
fe000966
DM
65 'user.cfg' => 1,
66 'domains.cfg' => 1,
67 'priv/shadow.cfg' => 1,
68 '/qemu-server/' => 1,
f71eee41 69 '/openvz/' => 1,
7f66b436 70 '/lxc/' => 1,
5a5417e6
DM
71 'ha/crm_commands' => 1,
72 'ha/manager_status' => 1,
73 'ha/resources.cfg' => 1,
74 'ha/groups.cfg' => 1,
9d4f69ff 75 'status.cfg' => 1,
fe000966
DM
76};
77
78# only write output if something fails
79sub run_silent_cmd {
80 my ($cmd) = @_;
81
82 my $outbuf = '';
83
84 my $record_output = sub {
85 $outbuf .= shift;
86 $outbuf .= "\n";
87 };
88
89 eval {
90 PVE::Tools::run_command($cmd, outfunc => $record_output,
91 errfunc => $record_output);
92 };
93
94 my $err = $@;
95
96 if ($err) {
97 print STDERR $outbuf;
98 die $err;
99 }
100}
101
102sub check_cfs_quorum {
01dddfb9
DM
103 my ($noerr) = @_;
104
fe000966
DM
105 # note: -w filename always return 1 for root, so wee need
106 # to use File::lstat here
107 my $st = File::stat::lstat("$basedir/local");
01dddfb9
DM
108 my $quorate = ($st && (($st->mode & 0200) != 0));
109
110 die "cluster not ready - no quorum?\n" if !$quorate && !$noerr;
111
112 return $quorate;
fe000966
DM
113}
114
115sub check_cfs_is_mounted {
116 my ($noerr) = @_;
117
118 my $res = -l "$basedir/local";
119
120 die "pve configuration filesystem not mounted\n"
121 if !$res && !$noerr;
122
123 return $res;
124}
125
126sub gen_local_dirs {
127 my ($nodename) = @_;
128
129 check_cfs_is_mounted();
130
131 my @required_dirs = (
132 "$basedir/priv",
133 "$basedir/nodes",
134 "$basedir/nodes/$nodename",
7f66b436 135 "$basedir/nodes/$nodename/lxc",
a1c08cfa
DM
136 "$basedir/nodes/$nodename/qemu-server",
137 "$basedir/nodes/$nodename/openvz",
fe000966
DM
138 "$basedir/nodes/$nodename/priv");
139
140 foreach my $dir (@required_dirs) {
141 if (! -d $dir) {
62613060 142 mkdir($dir) || $! == EEXIST || die "unable to create directory '$dir' - $!\n";
fe000966
DM
143 }
144 }
145}
146
147sub gen_auth_key {
148
149 return if -f "$authprivkeyfn";
150
151 check_cfs_is_mounted();
152
62613060 153 mkdir $authdir || $! == EEXIST || die "unable to create dir '$authdir' - $!\n";
fe000966
DM
154
155 my $cmd = "openssl genrsa -out '$authprivkeyfn' 2048";
156 run_silent_cmd($cmd);
157
158 $cmd = "openssl rsa -in '$authprivkeyfn' -pubout -out '$authpubkeyfn'";
159 run_silent_cmd($cmd)
160}
161
162sub gen_pveca_key {
163
164 return if -f $pveca_key_fn;
165
166 eval {
167 run_silent_cmd(['openssl', 'genrsa', '-out', $pveca_key_fn, '2048']);
168 };
169
170 die "unable to generate pve ca key:\n$@" if $@;
171}
172
173sub gen_pveca_cert {
174
175 if (-f $pveca_key_fn && -f $pveca_cert_fn) {
176 return 0;
177 }
178
179 gen_pveca_key();
180
181 # we try to generate an unique 'subject' to avoid browser problems
182 # (reused serial numbers, ..)
183 my $nid = (split (/\s/, `md5sum '$pveca_key_fn'`))[0] || time();
184
185 eval {
186 run_silent_cmd(['openssl', 'req', '-batch', '-days', '3650', '-new',
187 '-x509', '-nodes', '-key',
188 $pveca_key_fn, '-out', $pveca_cert_fn, '-subj',
189 "/CN=Proxmox Virtual Environment/OU=$nid/O=PVE Cluster Manager CA/"]);
190 };
191
192 die "generating pve root certificate failed:\n$@" if $@;
193
194 return 1;
195}
196
197sub gen_pve_ssl_key {
198 my ($nodename) = @_;
199
200 die "no node name specified" if !$nodename;
201
202 my $pvessl_key_fn = "$basedir/nodes/$nodename/pve-ssl.key";
203
204 return if -f $pvessl_key_fn;
205
206 eval {
207 run_silent_cmd(['openssl', 'genrsa', '-out', $pvessl_key_fn, '2048']);
208 };
209
210 die "unable to generate pve ssl key for node '$nodename':\n$@" if $@;
211}
212
213sub gen_pve_www_key {
214
215 return if -f $pvewww_key_fn;
216
217 eval {
218 run_silent_cmd(['openssl', 'genrsa', '-out', $pvewww_key_fn, '2048']);
219 };
220
221 die "unable to generate pve www key:\n$@" if $@;
222}
223
224sub update_serial {
225 my ($serial) = @_;
226
227 PVE::Tools::file_set_contents($pveca_srl_fn, $serial);
228}
229
230sub gen_pve_ssl_cert {
231 my ($force, $nodename, $ip) = @_;
232
233 die "no node name specified" if !$nodename;
234 die "no IP specified" if !$ip;
235
236 my $pvessl_cert_fn = "$basedir/nodes/$nodename/pve-ssl.pem";
237
238 return if !$force && -f $pvessl_cert_fn;
239
8acde170 240 my $names = "IP:127.0.0.1,IP:::1,DNS:localhost";
fe000966
DM
241
242 my $rc = PVE::INotify::read_file('resolvconf');
243
244 $names .= ",IP:$ip";
245
246 my $fqdn = $nodename;
247
248 $names .= ",DNS:$nodename";
249
250 if ($rc && $rc->{search}) {
251 $fqdn = $nodename . "." . $rc->{search};
252 $names .= ",DNS:$fqdn";
253 }
254
255 my $sslconf = <<__EOD;
256RANDFILE = /root/.rnd
257extensions = v3_req
258
259[ req ]
260default_bits = 2048
261distinguished_name = req_distinguished_name
262req_extensions = v3_req
263prompt = no
264string_mask = nombstr
265
266[ req_distinguished_name ]
267organizationalUnitName = PVE Cluster Node
268organizationName = Proxmox Virtual Environment
269commonName = $fqdn
270
271[ v3_req ]
272basicConstraints = CA:FALSE
273nsCertType = server
274keyUsage = nonRepudiation, digitalSignature, keyEncipherment
275subjectAltName = $names
276__EOD
277
278 my $cfgfn = "/tmp/pvesslconf-$$.tmp";
279 my $fh = IO::File->new ($cfgfn, "w");
280 print $fh $sslconf;
281 close ($fh);
282
283 my $reqfn = "/tmp/pvecertreq-$$.tmp";
284 unlink $reqfn;
285
286 my $pvessl_key_fn = "$basedir/nodes/$nodename/pve-ssl.key";
287 eval {
288 run_silent_cmd(['openssl', 'req', '-batch', '-new', '-config', $cfgfn,
289 '-key', $pvessl_key_fn, '-out', $reqfn]);
290 };
291
292 if (my $err = $@) {
293 unlink $reqfn;
294 unlink $cfgfn;
295 die "unable to generate pve certificate request:\n$err";
296 }
297
298 update_serial("0000000000000000") if ! -f $pveca_srl_fn;
299
300 eval {
301 run_silent_cmd(['openssl', 'x509', '-req', '-in', $reqfn, '-days', '3650',
302 '-out', $pvessl_cert_fn, '-CAkey', $pveca_key_fn,
303 '-CA', $pveca_cert_fn, '-CAserial', $pveca_srl_fn,
304 '-extfile', $cfgfn]);
305 };
306
307 if (my $err = $@) {
308 unlink $reqfn;
309 unlink $cfgfn;
310 die "unable to generate pve ssl certificate:\n$err";
311 }
312
313 unlink $cfgfn;
314 unlink $reqfn;
315}
316
317sub gen_pve_node_files {
318 my ($nodename, $ip, $opt_force) = @_;
319
320 gen_local_dirs($nodename);
321
322 gen_auth_key();
323
324 # make sure we have a (cluster wide) secret
325 # for CSRFR prevention
326 gen_pve_www_key();
327
328 # make sure we have a (per node) private key
329 gen_pve_ssl_key($nodename);
330
331 # make sure we have a CA
332 my $force = gen_pveca_cert();
333
334 $force = 1 if $opt_force;
335
336 gen_pve_ssl_cert($force, $nodename, $ip);
337}
338
bd0ae7ff
DM
339my $vzdump_cron_dummy = <<__EOD;
340# cluster wide vzdump cron schedule
341# Atomatically generated file - do not edit
342
343PATH="/usr/sbin:/usr/bin:/sbin:/bin"
344
345__EOD
346
347sub gen_pve_vzdump_symlink {
348
e1735a61 349 my $filename = "/etc/pve/vzdump.cron";
bd0ae7ff
DM
350
351 my $link_fn = "/etc/cron.d/vzdump";
352
353 if ((-f $filename) && (! -l $link_fn)) {
354 rename($link_fn, "/root/etc_cron_vzdump.org"); # make backup if file exists
355 symlink($filename, $link_fn);
356 }
357}
358
359sub gen_pve_vzdump_files {
360
e1735a61 361 my $filename = "/etc/pve/vzdump.cron";
bd0ae7ff
DM
362
363 PVE::Tools::file_set_contents($filename, $vzdump_cron_dummy)
364 if ! -f $filename;
365
366 gen_pve_vzdump_symlink();
367};
368
fe000966
DM
369my $versions = {};
370my $vmlist = {};
371my $clinfo = {};
372
373my $ipcc_send_rec = sub {
374 my ($msgid, $data) = @_;
375
376 my $res = PVE::IPCC::ipcc_send_rec($msgid, $data);
377
378 die "ipcc_send_rec failed: $!\n" if !defined($res) && ($! != 0);
379
380 return $res;
381};
382
383my $ipcc_send_rec_json = sub {
384 my ($msgid, $data) = @_;
385
386 my $res = PVE::IPCC::ipcc_send_rec($msgid, $data);
387
388 die "ipcc_send_rec failed: $!\n" if !defined($res) && ($! != 0);
389
390 return decode_json($res);
391};
392
393my $ipcc_get_config = sub {
394 my ($path) = @_;
395
396 my $bindata = pack "Z*", $path;
2db32d95
DM
397 my $res = PVE::IPCC::ipcc_send_rec(6, $bindata);
398 if (!defined($res)) {
399 return undef if ($! != 0);
400 return '';
401 }
402
403 return $res;
fe000966
DM
404};
405
406my $ipcc_get_status = sub {
407 my ($name, $nodename) = @_;
408
409 my $bindata = pack "Z[256]Z[256]", $name, ($nodename || "");
410 return PVE::IPCC::ipcc_send_rec(5, $bindata);
411};
412
413my $ipcc_update_status = sub {
414 my ($name, $data) = @_;
415
416 my $raw = ref($data) ? encode_json($data) : $data;
417 # update status
418 my $bindata = pack "Z[256]Z*", $name, $raw;
419
420 return &$ipcc_send_rec(4, $bindata);
421};
422
423my $ipcc_log = sub {
424 my ($priority, $ident, $tag, $msg) = @_;
425
426 my $bindata = pack "CCCZ*Z*Z*", $priority, bytes::length($ident) + 1,
427 bytes::length($tag) + 1, $ident, $tag, $msg;
428
429 return &$ipcc_send_rec(7, $bindata);
430};
431
432my $ipcc_get_cluster_log = sub {
433 my ($user, $max) = @_;
434
435 $max = 0 if !defined($max);
436
437 my $bindata = pack "VVVVZ*", $max, 0, 0, 0, ($user || "");
438 return &$ipcc_send_rec(8, $bindata);
439};
440
441my $ccache = {};
442
443sub cfs_update {
444 eval {
445 my $res = &$ipcc_send_rec_json(1);
446 #warn "GOT1: " . Dumper($res);
447 die "no starttime\n" if !$res->{starttime};
448
449 if (!$res->{starttime} || !$versions->{starttime} ||
450 $res->{starttime} != $versions->{starttime}) {
451 #print "detected changed starttime\n";
452 $vmlist = {};
453 $clinfo = {};
454 $ccache = {};
455 }
456
457 $versions = $res;
458 };
459 my $err = $@;
460 if ($err) {
461 $versions = {};
462 $vmlist = {};
463 $clinfo = {};
464 $ccache = {};
465 warn $err;
466 }
467
468 eval {
469 if (!$clinfo->{version} || $clinfo->{version} != $versions->{clinfo}) {
470 #warn "detected new clinfo\n";
471 $clinfo = &$ipcc_send_rec_json(2);
472 }
473 };
474 $err = $@;
475 if ($err) {
476 $clinfo = {};
477 warn $err;
478 }
479
480 eval {
481 if (!$vmlist->{version} || $vmlist->{version} != $versions->{vmlist}) {
482 #warn "detected new vmlist1\n";
483 $vmlist = &$ipcc_send_rec_json(3);
484 }
485 };
486 $err = $@;
487 if ($err) {
488 $vmlist = {};
489 warn $err;
490 }
491}
492
493sub get_vmlist {
494 return $vmlist;
495}
496
497sub get_clinfo {
498 return $clinfo;
499}
500
9ddd4ae9
DM
501sub get_members {
502 return $clinfo->{nodelist};
503}
504
fe000966
DM
505sub get_nodelist {
506
507 my $nodelist = $clinfo->{nodelist};
508
509 my $result = [];
510
511 my $nodename = PVE::INotify::nodename();
512
513 if (!$nodelist || !$nodelist->{$nodename}) {
514 return [ $nodename ];
515 }
516
517 return [ keys %$nodelist ];
518}
519
520sub broadcast_tasklist {
521 my ($data) = @_;
522
523 eval {
524 &$ipcc_update_status("tasklist", $data);
525 };
526
527 warn $@ if $@;
528}
529
530my $tasklistcache = {};
531
532sub get_tasklist {
533 my ($nodename) = @_;
534
535 my $kvstore = $versions->{kvstore} || {};
536
537 my $nodelist = get_nodelist();
538
539 my $res = [];
540 foreach my $node (@$nodelist) {
541 next if $nodename && ($nodename ne $node);
542 eval {
543 my $ver = $kvstore->{$node}->{tasklist} if $kvstore->{$node};
544 my $cd = $tasklistcache->{$node};
cebe16ec
DM
545 if (!$cd || !$ver || !$cd->{version} ||
546 ($cd->{version} != $ver)) {
fe000966
DM
547 my $raw = &$ipcc_get_status("tasklist", $node) || '[]';
548 my $data = decode_json($raw);
549 push @$res, @$data;
550 $cd = $tasklistcache->{$node} = {
551 data => $data,
552 version => $ver,
553 };
554 } elsif ($cd && $cd->{data}) {
555 push @$res, @{$cd->{data}};
556 }
557 };
558 my $err = $@;
559 syslog('err', $err) if $err;
560 }
561
562 return $res;
563}
564
565sub broadcast_rrd {
566 my ($rrdid, $data) = @_;
567
568 eval {
569 &$ipcc_update_status("rrd/$rrdid", $data);
570 };
571 my $err = $@;
572
573 warn $err if $err;
574}
575
576my $last_rrd_dump = 0;
577my $last_rrd_data = "";
578
579sub rrd_dump {
580
581 my $ctime = time();
582
583 my $diff = $ctime - $last_rrd_dump;
584 if ($diff < 2) {
585 return $last_rrd_data;
586 }
587
588 my $raw;
589 eval {
590 $raw = &$ipcc_send_rec(10);
591 };
592 my $err = $@;
593
594 if ($err) {
595 warn $err;
596 return {};
597 }
598
599 my $res = {};
600
c3fabca7
DM
601 if ($raw) {
602 while ($raw =~ s/^(.*)\n//) {
603 my ($key, @ela) = split(/:/, $1);
604 next if !$key;
605 next if !(scalar(@ela) > 1);
606 $res->{$key} = \@ela;
607 }
fe000966
DM
608 }
609
610 $last_rrd_dump = $ctime;
611 $last_rrd_data = $res;
612
613 return $res;
614}
615
616sub create_rrd_data {
617 my ($rrdname, $timeframe, $cf) = @_;
618
619 my $rrddir = "/var/lib/rrdcached/db";
620
621 my $rrd = "$rrddir/$rrdname";
622
623 my $setup = {
624 hour => [ 60, 70 ],
625 day => [ 60*30, 70 ],
626 week => [ 60*180, 70 ],
627 month => [ 60*720, 70 ],
628 year => [ 60*10080, 70 ],
629 };
630
631 my ($reso, $count) = @{$setup->{$timeframe}};
632 my $ctime = $reso*int(time()/$reso);
633 my $req_start = $ctime - $reso*$count;
634
635 $cf = "AVERAGE" if !$cf;
636
637 my @args = (
638 "-s" => $req_start,
639 "-e" => $ctime - 1,
640 "-r" => $reso,
641 );
642
643 my $socket = "/var/run/rrdcached.sock";
644 push @args, "--daemon" => "unix:$socket" if -S $socket;
645
646 my ($start, $step, $names, $data) = RRDs::fetch($rrd, $cf, @args);
647
648 my $err = RRDs::error;
649 die "RRD error: $err\n" if $err;
650
651 die "got wrong time resolution ($step != $reso)\n"
652 if $step != $reso;
653
654 my $res = [];
655 my $fields = scalar(@$names);
656 for my $line (@$data) {
657 my $entry = { 'time' => $start };
658 $start += $step;
659 my $found_undefs;
660 for (my $i = 0; $i < $fields; $i++) {
661 my $name = $names->[$i];
662 if (defined(my $val = $line->[$i])) {
663 $entry->{$name} = $val;
664 } else {
665 # we only add entryies with all data defined
666 # extjs chart has problems with undefined values
667 $found_undefs = 1;
668 }
669 }
670 push @$res, $entry if !$found_undefs;
671 }
672
673 return $res;
674}
675
676sub create_rrd_graph {
677 my ($rrdname, $timeframe, $ds, $cf) = @_;
678
679 # Using RRD graph is clumsy - maybe it
680 # is better to simply fetch the data, and do all display
681 # related things with javascript (new extjs html5 graph library).
682
683 my $rrddir = "/var/lib/rrdcached/db";
684
685 my $rrd = "$rrddir/$rrdname";
686
31938ad4
DM
687 my @ids = PVE::Tools::split_list($ds);
688
689 my $ds_txt = join('_', @ids);
690
691 my $filename = "${rrd}_${ds_txt}.png";
fe000966
DM
692
693 my $setup = {
694 hour => [ 60, 60 ],
695 day => [ 60*30, 70 ],
696 week => [ 60*180, 70 ],
697 month => [ 60*720, 70 ],
698 year => [ 60*10080, 70 ],
699 };
700
701 my ($reso, $count) = @{$setup->{$timeframe}};
702
703 my @args = (
704 "--imgformat" => "PNG",
705 "--border" => 0,
706 "--height" => 200,
707 "--width" => 800,
708 "--start" => - $reso*$count,
709 "--end" => 'now' ,
710 );
711
712 my $socket = "/var/run/rrdcached.sock";
713 push @args, "--daemon" => "unix:$socket" if -S $socket;
714
fe000966
DM
715 my @coldef = ('#00ddff', '#ff0000');
716
717 $cf = "AVERAGE" if !$cf;
718
719 my $i = 0;
720 foreach my $id (@ids) {
721 my $col = $coldef[$i++] || die "fixme: no color definition";
722 push @args, "DEF:${id}=$rrd:${id}:$cf";
723 my $dataid = $id;
724 if ($id eq 'cpu' || $id eq 'iowait') {
725 push @args, "CDEF:${id}_per=${id},100,*";
726 $dataid = "${id}_per";
727 }
728 push @args, "LINE2:${dataid}${col}:${id}";
729 }
730
a665376e
DM
731 push @args, '--full-size-mode';
732
31938ad4 733 # we do not really store data into the file
b871db9c 734 my $res = RRDs::graphv('', @args);
fe000966
DM
735
736 my $err = RRDs::error;
737 die "RRD error: $err\n" if $err;
738
31938ad4 739 return { filename => $filename, image => $res->{image} };
fe000966
DM
740}
741
742# a fast way to read files (avoid fuse overhead)
743sub get_config {
744 my ($path) = @_;
745
d3a92ba7 746 return &$ipcc_get_config($path);
fe000966
DM
747}
748
749sub get_cluster_log {
750 my ($user, $max) = @_;
751
752 return &$ipcc_get_cluster_log($user, $max);
753}
754
755my $file_info = {};
756
757sub cfs_register_file {
758 my ($filename, $parser, $writer) = @_;
759
760 $observed->{$filename} || die "unknown file '$filename'";
761
762 die "file '$filename' already registered" if $file_info->{$filename};
763
764 $file_info->{$filename} = {
765 parser => $parser,
766 writer => $writer,
767 };
768}
769
770my $ccache_read = sub {
771 my ($filename, $parser, $version) = @_;
772
773 $ccache->{$filename} = {} if !$ccache->{$filename};
774
775 my $ci = $ccache->{$filename};
776
d3a92ba7
DM
777 if (!$ci->{version} || !$version || $ci->{version} != $version) {
778 # we always call the parser, even when the file does not exists
779 # (in that case $data is undef)
fe000966 780 my $data = get_config($filename);
fe000966
DM
781 $ci->{data} = &$parser("/etc/pve/$filename", $data);
782 $ci->{version} = $version;
783 }
784
785 my $res = ref($ci->{data}) ? dclone($ci->{data}) : $ci->{data};
786
787 return $res;
788};
789
790sub cfs_file_version {
791 my ($filename) = @_;
792
793 my $version;
794 my $infotag;
6e73d5c2 795 if ($filename =~ m!^nodes/[^/]+/(openvz|lxc|qemu-server)/(\d+)\.conf$!) {
f71eee41 796 my ($type, $vmid) = ($1, $2);
fe000966
DM
797 if ($vmlist && $vmlist->{ids} && $vmlist->{ids}->{$vmid}) {
798 $version = $vmlist->{ids}->{$vmid}->{version};
799 }
f71eee41 800 $infotag = "/$type/";
fe000966
DM
801 } else {
802 $infotag = $filename;
803 $version = $versions->{$filename};
804 }
805
806 my $info = $file_info->{$infotag} ||
807 die "unknown file type '$filename'\n";
808
809 return wantarray ? ($version, $info) : $version;
810}
811
812sub cfs_read_file {
813 my ($filename) = @_;
814
815 my ($version, $info) = cfs_file_version($filename);
816 my $parser = $info->{parser};
817
818 return &$ccache_read($filename, $parser, $version);
819}
820
821sub cfs_write_file {
822 my ($filename, $data) = @_;
823
adb84d35 824 my ($version, $info) = cfs_file_version($filename);
fe000966
DM
825
826 my $writer = $info->{writer} || die "no writer defined";
827
828 my $fsname = "/etc/pve/$filename";
829
830 my $raw = &$writer($fsname, $data);
831
832 if (my $ci = $ccache->{$filename}) {
833 $ci->{version} = undef;
834 }
835
836 PVE::Tools::file_set_contents($fsname, $raw);
837}
838
839my $cfs_lock = sub {
840 my ($lockid, $timeout, $code, @param) = @_;
841
842 my $res;
843
844 # this timeout is for aquire the lock
845 $timeout = 10 if !$timeout;
846
847 my $filename = "$lockdir/$lockid";
848
849 my $msg = "can't aquire cfs lock '$lockid'";
850
851 eval {
852
853 mkdir $lockdir;
854
855 if (! -d $lockdir) {
856 die "$msg: pve cluster filesystem not online.\n";
857 }
858
859 local $SIG{ALRM} = sub { die "got lock request timeout\n"; };
860
861 alarm ($timeout);
862
863 if (!(mkdir $filename)) {
864 print STDERR "trying to aquire cfs lock '$lockid' ...";
865 while (1) {
866 if (!(mkdir $filename)) {
867 (utime 0, 0, $filename); # cfs unlock request
868 } else {
869 print STDERR " OK\n";
870 last;
871 }
872 sleep(1);
873 }
874 }
875
876 # fixed command timeout: cfs locks have a timeout of 120
877 # using 60 gives us another 60 seconds to abort the task
878 alarm(60);
879 local $SIG{ALRM} = sub { die "got lock timeout - aborting command\n"; };
880
9c206b2b
DM
881 cfs_update(); # make sure we read latest versions inside code()
882
fe000966
DM
883 $res = &$code(@param);
884
885 alarm(0);
886 };
887
888 my $err = $@;
889
890 alarm(0);
891
892 if ($err && ($err eq "got lock request timeout\n") &&
893 !check_cfs_quorum()){
894 $err = "$msg: no quorum!\n";
895 }
896
897 if (!$err || $err !~ /^got lock timeout -/) {
898 rmdir $filename; # cfs unlock
899 }
900
901 if ($err) {
902 $@ = $err;
903 return undef;
904 }
905
906 $@ = undef;
907
908 return $res;
909};
910
911sub cfs_lock_file {
912 my ($filename, $timeout, $code, @param) = @_;
913
914 my $info = $observed->{$filename} || die "unknown file '$filename'";
915
916 my $lockid = "file-$filename";
917 $lockid =~ s/[.\/]/_/g;
918
919 &$cfs_lock($lockid, $timeout, $code, @param);
920}
921
922sub cfs_lock_storage {
923 my ($storeid, $timeout, $code, @param) = @_;
924
925 my $lockid = "storage-$storeid";
926
927 &$cfs_lock($lockid, $timeout, $code, @param);
928}
929
930my $log_levels = {
931 "emerg" => 0,
932 "alert" => 1,
933 "crit" => 2,
934 "critical" => 2,
935 "err" => 3,
936 "error" => 3,
937 "warn" => 4,
938 "warning" => 4,
939 "notice" => 5,
940 "info" => 6,
941 "debug" => 7,
942};
943
944sub log_msg {
945 my ($priority, $ident, $msg) = @_;
946
947 if (my $tmp = $log_levels->{$priority}) {
948 $priority = $tmp;
949 }
950
951 die "need numeric log priority" if $priority !~ /^\d+$/;
952
953 my $tag = PVE::SafeSyslog::tag();
954
955 $msg = "empty message" if !$msg;
956
957 $ident = "" if !$ident;
958 $ident = encode("ascii", decode_utf8($ident),
959 sub { sprintf "\\u%04x", shift });
960
961 my $utf8 = decode_utf8($msg);
962
963 my $ascii = encode("ascii", $utf8, sub { sprintf "\\u%04x", shift });
964
965 if ($ident) {
966 syslog($priority, "<%s> %s", $ident, $ascii);
967 } else {
968 syslog($priority, "%s", $ascii);
969 }
970
971 eval { &$ipcc_log($priority, $ident, $tag, $ascii); };
972
973 syslog("err", "writing cluster log failed: $@") if $@;
974}
975
9d76a1bb
DM
976sub check_vmid_unused {
977 my ($vmid, $noerr) = @_;
978
979 my $vmlist = get_vmlist();
980
981 my $d = $vmlist->{ids}->{$vmid};
982 return 1 if !defined($d);
983
984 return undef if $noerr;
985
4f66b109
DM
986 my $vmtypestr = $d->{type} eq 'qemu' ? 'VM' : 'CT';
987 die "$vmtypestr $vmid already exists on node '$d->{node}'";
9d76a1bb
DM
988}
989
65ff467f
DM
990sub check_node_exists {
991 my ($nodename, $noerr) = @_;
992
993 my $nodelist = $clinfo->{nodelist};
994 return 1 if $nodelist && $nodelist->{$nodename};
995
996 return undef if $noerr;
997
998 die "no such cluster node '$nodename'\n";
999}
1000
fe000966
DM
1001# this is also used to get the IP of the local node
1002sub remote_node_ip {
1003 my ($nodename, $noerr) = @_;
1004
1005 my $nodelist = $clinfo->{nodelist};
1006 if ($nodelist && $nodelist->{$nodename}) {
1007 if (my $ip = $nodelist->{$nodename}->{ip}) {
fc31f517
WB
1008 return $ip if !wantarray;
1009 my $family = $nodelist->{$nodename}->{address_family};
1010 if (!$family) {
1011 $nodelist->{$nodename}->{address_family} =
1012 $family =
1013 PVE::Tools::get_host_address_family($ip);
1014 }
1015 return ($ip, $family);
fe000966
DM
1016 }
1017 }
1018
1019 # fallback: try to get IP by other means
5720a852 1020 my ($family, $packed_ip);
fe000966 1021
5720a852
WB
1022 eval {
1023 my @res = PVE::Tools::getaddrinfo_all($nodename);
1024 $family = $res[0]->{family};
1025 $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2];
1026 };
fe000966 1027
5720a852
WB
1028 if ($@) {
1029 die "hostname lookup failed:\n$@" if !$noerr;
1030 return undef;
fe000966
DM
1031 }
1032
5720a852
WB
1033 my $ip = Socket::inet_ntop($family, $packed_ip);
1034 if ($ip =~ m/^127\.|^::1$/) {
1035 die "hostname lookup failed - got local IP address ($nodename = $ip)\n" if !$noerr;
1036 return undef;
1037 }
fe000966 1038
5720a852 1039 return wantarray ? ($ip, $family) : $ip;
fe000966
DM
1040}
1041
1042# ssh related utility functions
1043
1044sub ssh_merge_keys {
1045 # remove duplicate keys in $sshauthkeys
1046 # ssh-copy-id simply add keys, so the file can grow to large
1047
1048 my $data = '';
1049 if (-f $sshauthkeys) {
1050 $data = PVE::Tools::file_get_contents($sshauthkeys, 128*1024);
1051 chomp($data);
1052 }
1053
6056578e
DM
1054 my $found_backup;
1055 if (-f $rootsshauthkeysbackup) {
404343d7 1056 $data .= "\n";
6056578e
DM
1057 $data .= PVE::Tools::file_get_contents($rootsshauthkeysbackup, 128*1024);
1058 chomp($data);
1059 $found_backup = 1;
1060 }
1061
fe000966
DM
1062 # always add ourself
1063 if (-f $ssh_rsa_id) {
1064 my $pub = PVE::Tools::file_get_contents($ssh_rsa_id);
1065 chomp($pub);
1066 $data .= "\n$pub\n";
1067 }
1068
1069 my $newdata = "";
1070 my $vhash = {};
2055b0a9
DM
1071 my @lines = split(/\n/, $data);
1072 foreach my $line (@lines) {
7eb37d8d
SP
1073 if ($line !~ /^#/ && $line =~ m/(^|\s)ssh-(rsa|dsa)\s+(\S+)\s+\S+$/) {
1074 next if $vhash->{$3}++;
fe000966 1075 }
2055b0a9 1076 $newdata .= "$line\n";
fe000966 1077 }
fe000966
DM
1078
1079 PVE::Tools::file_set_contents($sshauthkeys, $newdata, 0600);
6056578e
DM
1080
1081 if ($found_backup && -l $rootsshauthkeys) {
1082 # everything went well, so we can remove the backup
1083 unlink $rootsshauthkeysbackup;
1084 }
fe000966
DM
1085}
1086
ac50b36d
DM
1087sub setup_sshd_config {
1088
1089 my $conf = PVE::Tools::file_get_contents($sshd_config_fn);
1090
1091 return if $conf =~ m/^PermitRootLogin\s+yes\s*$/m;
1092
1093 if ($conf !~ s/^#?PermitRootLogin.*$/PermitRootLogin yes/m) {
1094 chomp $conf;
1095 $conf .= "\nPermitRootLogin yes\n";
1096 }
1097
1098 PVE::Tools::file_set_contents($sshd_config_fn, $conf);
1099
1100 PVE::Tools::run_command(['systemctl', 'reload-or-restart', 'sshd']);
1101}
1102
f666cdde
SP
1103sub setup_rootsshconfig {
1104
39df71df
DM
1105 # create ssh key if it does not exist
1106 if (! -f $ssh_rsa_id) {
1107 mkdir '/root/.ssh/';
1108 system ("echo|ssh-keygen -t rsa -N '' -b 2048 -f ${ssh_rsa_id_priv}");
1109 }
1110
f666cdde
SP
1111 # create ssh config if it does not exist
1112 if (! -f $rootsshconfig) {
9aabc24b
DM
1113 mkdir '/root/.ssh';
1114 if (my $fh = IO::File->new($rootsshconfig, O_CREAT|O_WRONLY|O_EXCL, 0640)) {
f666cdde 1115 # this is the default ciphers list from debian openssl0.9.8 except blowfish is added as prefered
9aabc24b 1116 print $fh "Ciphers blowfish-cbc,aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc\n";
f666cdde
SP
1117 close($fh);
1118 }
1119 }
1120}
1121
fe000966
DM
1122sub setup_ssh_keys {
1123
fe000966
DM
1124 mkdir $authdir;
1125
6056578e
DM
1126 my $import_ok;
1127
fe000966 1128 if (! -f $sshauthkeys) {
6056578e
DM
1129 my $old;
1130 if (-f $rootsshauthkeys) {
1131 $old = PVE::Tools::file_get_contents($rootsshauthkeys, 128*1024);
1132 }
fe000966 1133 if (my $fh = IO::File->new ($sshauthkeys, O_CREAT|O_WRONLY|O_EXCL, 0400)) {
6056578e 1134 PVE::Tools::safe_print($sshauthkeys, $fh, $old) if $old;
fe000966 1135 close($fh);
6056578e 1136 $import_ok = 1;
fe000966
DM
1137 }
1138 }
1139
1140 warn "can't create shared ssh key database '$sshauthkeys'\n"
1141 if ! -f $sshauthkeys;
1142
404343d7 1143 if (-f $rootsshauthkeys && ! -l $rootsshauthkeys) {
6056578e
DM
1144 if (!rename($rootsshauthkeys , $rootsshauthkeysbackup)) {
1145 warn "rename $rootsshauthkeys failed - $!\n";
1146 }
fe000966
DM
1147 }
1148
1149 if (! -l $rootsshauthkeys) {
1150 symlink $sshauthkeys, $rootsshauthkeys;
1151 }
fe000966 1152
6056578e
DM
1153 if (! -l $rootsshauthkeys) {
1154 warn "can't create symlink for ssh keys '$rootsshauthkeys' -> '$sshauthkeys'\n";
1155 } else {
1156 unlink $rootsshauthkeysbackup if $import_ok;
1157 }
fe000966
DM
1158}
1159
1160sub ssh_unmerge_known_hosts {
1161 return if ! -l $sshglobalknownhosts;
1162
1163 my $old = '';
1164 $old = PVE::Tools::file_get_contents($sshknownhosts, 128*1024)
1165 if -f $sshknownhosts;
1166
1167 PVE::Tools::file_set_contents($sshglobalknownhosts, $old);
1168}
1169
1170sub ssh_merge_known_hosts {
1171 my ($nodename, $ip_address, $createLink) = @_;
1172
1173 die "no node name specified" if !$nodename;
1174 die "no ip address specified" if !$ip_address;
1175
1176 mkdir $authdir;
1177
1178 if (! -f $sshknownhosts) {
1179 if (my $fh = IO::File->new($sshknownhosts, O_CREAT|O_WRONLY|O_EXCL, 0600)) {
1180 close($fh);
1181 }
1182 }
1183
1184 my $old = PVE::Tools::file_get_contents($sshknownhosts, 128*1024);
1185
1186 my $new = '';
1187
1188 if ((! -l $sshglobalknownhosts) && (-f $sshglobalknownhosts)) {
1189 $new = PVE::Tools::file_get_contents($sshglobalknownhosts, 128*1024);
1190 }
1191
1192 my $hostkey = PVE::Tools::file_get_contents($ssh_host_rsa_id);
1d182ad3
DM
1193 # Note: file sometimes containe emty lines at start, so we use multiline match
1194 die "can't parse $ssh_host_rsa_id" if $hostkey !~ m/^(ssh-rsa\s\S+)(\s.*)?$/m;
fe000966
DM
1195 $hostkey = $1;
1196
1197 my $data = '';
1198 my $vhash = {};
1199
1200 my $found_nodename;
1201 my $found_local_ip;
1202
1203 my $merge_line = sub {
1204 my ($line, $all) = @_;
1205
1206 if ($line =~ m/^(\S+)\s(ssh-rsa\s\S+)(\s.*)?$/) {
1207 my $key = $1;
1208 my $rsakey = $2;
1209 if (!$vhash->{$key}) {
1210 $vhash->{$key} = 1;
1211 if ($key =~ m/\|1\|([^\|\s]+)\|([^\|\s]+)$/) {
1212 my $salt = decode_base64($1);
1213 my $digest = $2;
1214 my $hmac = Digest::HMAC_SHA1->new($salt);
1215 $hmac->add($nodename);
1216 my $hd = $hmac->b64digest . '=';
1217 if ($digest eq $hd) {
1218 if ($rsakey eq $hostkey) {
1219 $found_nodename = 1;
1220 $data .= $line;
1221 }
1222 return;
1223 }
1224 $hmac = Digest::HMAC_SHA1->new($salt);
1225 $hmac->add($ip_address);
1226 $hd = $hmac->b64digest . '=';
1227 if ($digest eq $hd) {
1228 if ($rsakey eq $hostkey) {
1229 $found_local_ip = 1;
1230 $data .= $line;
1231 }
1232 return;
1233 }
1234 }
1235 $data .= $line;
1236 }
1237 } elsif ($all) {
1238 $data .= $line;
1239 }
1240 };
1241
1242 while ($old && $old =~ s/^((.*?)(\n|$))//) {
1243 my $line = "$2\n";
1244 next if $line =~ m/^\s*$/; # skip empty lines
1245 next if $line =~ m/^#/; # skip comments
1246 &$merge_line($line, 1);
1247 }
1248
1249 while ($new && $new =~ s/^((.*?)(\n|$))//) {
1250 my $line = "$2\n";
1251 next if $line =~ m/^\s*$/; # skip empty lines
1252 next if $line =~ m/^#/; # skip comments
1253 &$merge_line($line);
1254 }
1255
1256 my $addIndex = $$;
1257 my $add_known_hosts_entry = sub {
1258 my ($name, $hostkey) = @_;
1259 $addIndex++;
1260 my $hmac = Digest::HMAC_SHA1->new("$addIndex" . time());
1261 my $b64salt = $hmac->b64digest . '=';
1262 $hmac = Digest::HMAC_SHA1->new(decode_base64($b64salt));
1263 $hmac->add($name);
1264 my $digest = $hmac->b64digest . '=';
1265 $data .= "|1|$b64salt|$digest $hostkey\n";
1266 };
1267
1268 if (!$found_nodename || !$found_local_ip) {
1269 &$add_known_hosts_entry($nodename, $hostkey) if !$found_nodename;
1270 &$add_known_hosts_entry($ip_address, $hostkey) if !$found_local_ip;
1271 }
1272
1273 PVE::Tools::file_set_contents($sshknownhosts, $data);
1274
1275 return if !$createLink;
1276
1277 unlink $sshglobalknownhosts;
1278 symlink $sshknownhosts, $sshglobalknownhosts;
1279
1280 warn "can't create symlink for ssh known hosts '$sshglobalknownhosts' -> '$sshknownhosts'\n"
1281 if ! -l $sshglobalknownhosts;
1282
1283}
1284
fe000966
DM
1285my $datacenter_schema = {
1286 type => "object",
1287 additionalProperties => 0,
1288 properties => {
1289 keyboard => {
1290 optional => 1,
1291 type => 'string',
1292 description => "Default keybord layout for vnc server.",
c59334cb 1293 enum => PVE::Tools::kvmkeymaplist(),
fe000966
DM
1294 },
1295 language => {
1296 optional => 1,
1297 type => 'string',
1298 description => "Default GUI language.",
1299 enum => [ 'en', 'de' ],
1300 },
1301 http_proxy => {
1302 optional => 1,
1303 type => 'string',
1304 description => "Specify external http proxy which is used for downloads (example: 'http://username:password\@host:port/')",
1305 pattern => "http://.*",
1306 },
a9323ef0
SP
1307 migration_unsecure => {
1308 optional => 1,
1309 type => 'boolean',
1310 description => "Migration is secure using SSH tunnel by default. For secure private networks you can disable it to speed up migration.",
1311 },
dce47328
DM
1312 console => {
1313 optional => 1,
1314 type => 'string',
66a15f27
DM
1315 description => "Select the default Console viewer. You can either use the builtin java applet (VNC), an external virt-viewer comtatible application (SPICE), or an HTML5 based viewer (noVNC).",
1316 enum => ['applet', 'vv', 'html5'],
dce47328 1317 },
8548bd87
SGE
1318 email_from => {
1319 optional => 1,
1320 type => 'string',
a05baf53 1321 format => 'email-opt',
8548bd87
SGE
1322 description => "Specify email address to send notification from (default is root@\$hostname)",
1323 },
fe000966
DM
1324 },
1325};
1326
1327# make schema accessible from outside (for documentation)
1328sub get_datacenter_schema { return $datacenter_schema };
1329
1330sub parse_datacenter_config {
1331 my ($filename, $raw) = @_;
1332
9ef7896d 1333 return PVE::JSONSchema::parse_config($datacenter_schema, $filename, $raw // '');
fe000966
DM
1334}
1335
1336sub write_datacenter_config {
1337 my ($filename, $cfg) = @_;
1338
1339 return PVE::JSONSchema::dump_config($datacenter_schema, $filename, $cfg);
1340}
1341
1342cfs_register_file('datacenter.cfg',
1343 \&parse_datacenter_config,
1344 \&write_datacenter_config);
ec48ec22 1345
cafc7309
DM
1346# a very simply parser ...
1347sub parse_corosync_conf {
ec48ec22
DM
1348 my ($filename, $raw) = @_;
1349
cafc7309 1350 return {} if !$raw;
ec48ec22 1351
440121dc 1352 my $digest = Digest::SHA::sha1_hex(defined($raw) ? $raw : '');
ec48ec22 1353
cafc7309
DM
1354 $raw =~ s/#.*$//mg;
1355 $raw =~ s/\r?\n/ /g;
1356 $raw =~ s/\s+/ /g;
1357 $raw =~ s/^\s+//;
1358 $raw =~ s/\s*$//;
1359
cafc7309
DM
1360 my @tokens = split(/\s/, $raw);
1361
1362 my $conf = { section => 'main', children => [] };
1d01c3f6 1363
cafc7309
DM
1364 my $stack = [];
1365 my $section = $conf;
1366
1367 while (defined(my $token = shift @tokens)) {
1368 my $nexttok = $tokens[0];
1369
1370 if ($nexttok && ($nexttok eq '{')) {
1371 shift @tokens; # skip '{'
1372 my $new_section = {
1373 section => $token,
1374 children => [],
1375 };
1376 push @{$section->{children}}, $new_section;
1377 push @$stack, $section;
1378 $section = $new_section;
1379 next;
1d01c3f6 1380 }
1d01c3f6 1381
cafc7309
DM
1382 if ($token eq '}') {
1383 $section = pop @$stack;
1384 die "parse error - uncexpected '}'\n" if !$section;
1385 next;
1386 }
1d01c3f6 1387
cafc7309
DM
1388 my $key = $token;
1389 die "missing ':' after key '$key'\n" if ! ($key =~ s/:$//);
1390
1391 die "parse error - no value for '$key'\n" if !defined($nexttok);
1392 my $value = shift @tokens;
1d01c3f6 1393
cafc7309
DM
1394 push @{$section->{children}}, { key => $key, value => $value };
1395 }
1d01c3f6 1396
cafc7309 1397 $conf->{digest} = $digest;
1d01c3f6 1398
cafc7309 1399 return $conf;
1d01c3f6
DM
1400}
1401
cafc7309
DM
1402my $dump_corosync_section;
1403$dump_corosync_section = sub {
1404 my ($section, $prefix) = @_;
1d01c3f6 1405
cafc7309
DM
1406 my $raw = $prefix . $section->{section} . " {\n";
1407
1408 my @list = grep { defined($_->{key}) } @{$section->{children}};
1409 foreach my $child (sort {$a->{key} cmp $b->{key}} @list) {
1410 $raw .= $prefix . " $child->{key}: $child->{value}\n";
1d01c3f6 1411 }
cafc7309
DM
1412
1413 @list = grep { defined($_->{section}) } @{$section->{children}};
1414 foreach my $child (sort {$a->{section} cmp $b->{section}} @list) {
1415 $raw .= &$dump_corosync_section($child, "$prefix ");
1d01c3f6
DM
1416 }
1417
cafc7309
DM
1418 $raw .= $prefix . "}\n\n";
1419
1420 return $raw;
1421
1422};
1d01c3f6 1423
cafc7309
DM
1424sub write_corosync_conf {
1425 my ($filename, $conf) = @_;
ec48ec22 1426
cafc7309 1427 my $raw = '';
ec48ec22 1428
cafc7309
DM
1429 my $prefix = '';
1430
1431 die "no main section" if $conf->{section} ne 'main';
ec48ec22 1432
cafc7309
DM
1433 my @list = grep { defined($_->{key}) } @{$conf->{children}};
1434 foreach my $child (sort {$a->{key} cmp $b->{key}} @list) {
1435 $raw .= "$child->{key}: $child->{value}\n";
1d01c3f6
DM
1436 }
1437
cafc7309
DM
1438 @list = grep { defined($_->{section}) } @{$conf->{children}};
1439 foreach my $child (sort {$a->{section} cmp $b->{section}} @list) {
1440 $raw .= &$dump_corosync_section($child, $prefix);
1441 }
ec48ec22 1442
cafc7309 1443 return $raw;
ec48ec22
DM
1444}
1445
cafc7309
DM
1446sub corosync_conf_version {
1447 my ($conf, $noerr, $new_value) = @_;
1448
1449 foreach my $child (@{$conf->{children}}) {
1450 next if !defined($child->{section});
1451 if ($child->{section} eq 'totem') {
1452 foreach my $e (@{$child->{children}}) {
1453 next if !defined($e->{key});
1454 if ($e->{key} eq 'config_version') {
1455 if ($new_value) {
1456 $e->{value} = $new_value;
1457 return $new_value;
1458 } elsif (my $version = int($e->{value})) {
1459 return $version;
1460 }
1461 last;
1462 }
1463 }
ec48ec22 1464 }
ec48ec22 1465 }
cafc7309
DM
1466
1467 return undef if $noerr;
ec48ec22 1468
cafc7309 1469 die "invalid corosync config - unable to read version\n";
ec48ec22
DM
1470}
1471
cafc7309
DM
1472# read only - use "rename corosync.conf.new corosync.conf" to write
1473PVE::Cluster::cfs_register_file('corosync.conf', \&parse_corosync_conf);
ec48ec22 1474# this is read/write
cafc7309
DM
1475PVE::Cluster::cfs_register_file('corosync.conf.new', \&parse_corosync_conf,
1476 \&write_corosync_conf);
2c66fb58 1477
15df58e6
DM
1478# bash completion helpers
1479
1480sub complete_next_vmid {
1481
1482 my $vmlist = get_vmlist() || {};
1483 my $idlist = $vmlist->{ids} || {};
1484
1485 for (my $i = 100; $i < 10000; $i++) {
1486 return [$i] if !defined($idlist->{$i});
1487 }
1488
1489 return [];
1490}
1491
87515b25
DM
1492sub complete_vmid {
1493
1494 my $vmlist = get_vmlist();
1495 my $ids = $vmlist->{ids} || {};
1496
1497 return [ keys %$ids ];
1498}
1499
15df58e6
DM
1500sub complete_local_vmid {
1501
1502 my $vmlist = get_vmlist();
1503 my $ids = $vmlist->{ids} || {};
1504
1505 my $nodename = PVE::INotify::nodename();
1506
1507 my $res = [];
1508 foreach my $vmid (keys %$ids) {
1509 my $d = $ids->{$vmid};
1510 next if !$d->{node} || $d->{node} ne $nodename;
1511 push @$res, $vmid;
1512 }
1513
1514 return $res;
1515}
1516
4dd189df
DM
1517sub complete_migration_target {
1518
1519 my $res = [];
1520
1521 my $nodename = PVE::INotify::nodename();
1522
1523 my $nodelist = get_nodelist();
1524 foreach my $node (@$nodelist) {
1525 next if $node eq $nodename;
1526 push @$res, $node;
1527 }
1528
1529 return $res;
1530}
1531
ac68281b 15321;