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