]> git.proxmox.com Git - pve-common.git/blob - src/PVE/INotify.pm
Add check for infiniband to write functions as well
[pve-common.git] / src / PVE / INotify.pm
1 package PVE::INotify;
2
3 # todo: maybe we do not need update_file() ?
4
5 use strict;
6 use warnings;
7
8 use POSIX;
9 use IO::File;
10 use IO::Dir;
11 use File::stat;
12 use File::Basename;
13 use Fcntl qw(:DEFAULT :flock);
14 use PVE::SafeSyslog;
15 use PVE::Exception qw(raise_param_exc);
16 use PVE::Tools;
17 use PVE::ProcFSTools;
18 use Storable qw(dclone);
19 use Linux::Inotify2;
20 use base 'Exporter';
21 use JSON;
22
23 our @EXPORT_OK = qw(read_file write_file register_file);
24
25 my $ccache;
26 my $ccachemap;
27 my $ccacheregex;
28 my $inotify;
29 my $inotify_pid = 0;
30 my $versions;
31 my $shadowfiles = {
32 '/etc/network/interfaces' => '/etc/network/interfaces.new',
33 };
34
35 # to enable cached operation, you need to call 'inotify_init'
36 # inotify handles are a limited resource, so use with care (only
37 # enable the cache if you really need it)
38
39 # Note: please close the inotify handle after you fork
40
41 sub ccache_default_writer {
42 my ($filename, $data) = @_;
43
44 die "undefined config writer for '$filename' :ERROR";
45 }
46
47 sub ccache_default_parser {
48 my ($filename, $srcfd) = @_;
49
50 die "undefined config reader for '$filename' :ERROR";
51 }
52
53 sub ccache_compute_diff {
54 my ($filename, $shadow) = @_;
55
56 my $diff = '';
57
58 open (TMP, "diff -b -N -u '$filename' '$shadow'|");
59
60 while (my $line = <TMP>) {
61 $diff .= $line;
62 }
63
64 close (TMP);
65
66 $diff = undef if !$diff;
67
68 return $diff;
69 }
70
71 sub ccache_info {
72 my ($filename) = @_;
73
74 foreach my $uid (keys %$ccacheregex) {
75 my $ccinfo = $ccacheregex->{$uid};
76 my $dir = $ccinfo->{dir};
77 my $regex = $ccinfo->{regex};
78 if ($filename =~ m|^$dir/+$regex$|) {
79 if (!$ccache->{$filename}) {
80 my $cp = {};
81 while (my ($k, $v) = each %$ccinfo) {
82 $cp->{$k} = $v;
83 }
84 $ccache->{$filename} = $cp;
85 }
86 return ($ccache->{$filename}, $filename);
87 }
88 }
89
90 $filename = $ccachemap->{$filename} if defined ($ccachemap->{$filename});
91
92 die "file '$filename' not added :ERROR" if !defined ($ccache->{$filename});
93
94 return ($ccache->{$filename}, $filename);
95 }
96
97 sub write_file {
98 my ($fileid, $data, $full) = @_;
99
100 my ($ccinfo, $filename) = ccache_info($fileid);
101
102 my $writer = $ccinfo->{writer};
103
104 my $realname = $filename;
105
106 my $shadow;
107 if ($shadow = $shadowfiles->{$filename}) {
108 $realname = $shadow;
109 }
110
111 my $perm = $ccinfo->{perm} || 0644;
112
113 my $tmpname = "$realname.tmp.$$";
114
115 my $res;
116 eval {
117 my $fh = IO::File->new($tmpname, O_WRONLY|O_CREAT, $perm);
118 die "unable to open file '$tmpname' - $!\n" if !$fh;
119
120 $res = &$writer($filename, $fh, $data);
121
122 die "closing file '$tmpname' failed - $!\n" unless close $fh;
123 };
124 my $err = $@;
125
126 $ccinfo->{version} = undef;
127
128 if ($err) {
129 unlink $tmpname;
130 die $err;
131 }
132
133 if (!rename($tmpname, $realname)) {
134 my $msg = "close (rename) atomic file '$filename' failed: $!\n";
135 unlink $tmpname;
136 die $msg;
137 }
138
139 my $diff;
140 if ($shadow && $full) {
141 $diff = ccache_compute_diff ($filename, $shadow);
142 }
143
144 if ($full) {
145 return { data => $res, changes => $diff };
146 }
147
148 return $res;
149 }
150
151 sub update_file {
152 my ($fileid, $data, @args) = @_;
153
154 my ($ccinfo, $filename) = ccache_info($fileid);
155
156 my $update = $ccinfo->{update};
157
158 die "unable to update/merge data" if !$update;
159
160 my $lkfn = "$filename.lock";
161
162 my $timeout = 10;
163
164 my $fd;
165
166 my $code = sub {
167
168 $fd = IO::File->new ($filename, "r");
169
170 my $new = &$update($filename, $fd, $data, @args);
171
172 if (defined($new)) {
173 PVE::Tools::file_set_contents($filename, $new, $ccinfo->{perm});
174 } else {
175 unlink $filename;
176 }
177 };
178
179 PVE::Tools::lock_file($lkfn, $timeout, $code);
180 my $err = $@;
181
182 close($fd) if defined($fd);
183
184 die $err if $err;
185
186 return undef;
187 }
188
189 sub discard_changes {
190 my ($fileid, $full) = @_;
191
192 my ($ccinfo, $filename) = ccache_info($fileid);
193
194 if (my $copy = $shadowfiles->{$filename}) {
195 unlink $copy;
196 }
197
198 return read_file ($filename, $full);
199 }
200
201 sub read_file {
202 my ($fileid, $full) = @_;
203
204 my $parser;
205
206 my ($ccinfo, $filename) = ccache_info($fileid);
207
208 $parser = $ccinfo->{parser};
209
210 my $fd;
211 my $shadow;
212
213 poll() if $inotify; # read new inotify events
214
215 $versions->{$filename} = 0 if !defined ($versions->{$filename});
216
217 my $cver = $versions->{$filename};
218
219 if (my $copy = $shadowfiles->{$filename}) {
220 if ($fd = IO::File->new ($copy, "r")) {
221 $shadow = $copy;
222 } else {
223 $fd = IO::File->new ($filename, "r");
224 }
225 } else {
226 $fd = IO::File->new ($filename, "r");
227 }
228
229 my $acp = $ccinfo->{always_call_parser};
230
231 if (!$fd) {
232 $ccinfo->{version} = undef;
233 $ccinfo->{data} = undef;
234 $ccinfo->{diff} = undef;
235 return undef if !$acp;
236 }
237
238 my $noclone = $ccinfo->{noclone};
239
240 # file unchanged?
241 if (!$ccinfo->{nocache} &&
242 $inotify && $versions->{$filename} &&
243 defined ($ccinfo->{data}) &&
244 defined ($ccinfo->{version}) &&
245 ($ccinfo->{readonce} ||
246 ($ccinfo->{version} == $versions->{$filename}))) {
247
248 my $ret;
249 if (!$noclone && ref ($ccinfo->{data})) {
250 $ret->{data} = dclone ($ccinfo->{data});
251 } else {
252 $ret->{data} = $ccinfo->{data};
253 }
254 $ret->{changes} = $ccinfo->{diff};
255
256 return $full ? $ret : $ret->{data};
257 }
258
259 my $diff;
260
261 if ($shadow) {
262 $diff = ccache_compute_diff ($filename, $shadow);
263 }
264
265 my $res = &$parser($filename, $fd);
266
267 if (!$ccinfo->{nocache}) {
268 $ccinfo->{version} = $cver;
269 }
270
271 # we cache data with references, so we always need to
272 # dclone this data. Else the original data may get
273 # modified.
274 $ccinfo->{data} = $res;
275
276 # also store diff
277 $ccinfo->{diff} = $diff;
278
279 my $ret;
280 if (!$noclone && ref ($ccinfo->{data})) {
281 $ret->{data} = dclone ($ccinfo->{data});
282 } else {
283 $ret->{data} = $ccinfo->{data};
284 }
285 $ret->{changes} = $ccinfo->{diff};
286
287 return $full ? $ret : $ret->{data};
288 }
289
290 sub parse_ccache_options {
291 my ($ccinfo, %options) = @_;
292
293 foreach my $opt (keys %options) {
294 my $v = $options{$opt};
295 if ($opt eq 'readonce') {
296 $ccinfo->{$opt} = $v;
297 } elsif ($opt eq 'nocache') {
298 $ccinfo->{$opt} = $v;
299 } elsif ($opt eq 'shadow') {
300 $ccinfo->{$opt} = $v;
301 } elsif ($opt eq 'perm') {
302 $ccinfo->{$opt} = $v;
303 } elsif ($opt eq 'noclone') {
304 # noclone flag for large read-only data chunks like aplinfo
305 $ccinfo->{$opt} = $v;
306 } elsif ($opt eq 'always_call_parser') {
307 # when set, we call parser even when the file does not exists.
308 # this allows the parser to return some default
309 $ccinfo->{$opt} = $v;
310 } else {
311 die "internal error - unsupported option '$opt'";
312 }
313 }
314 }
315
316 sub register_file {
317 my ($id, $filename, $parser, $writer, $update, %options) = @_;
318
319 die "can't register file '$filename' after inotify_init" if $inotify;
320
321 die "file '$filename' already added :ERROR" if defined ($ccache->{$filename});
322 die "ID '$id' already used :ERROR" if defined ($ccachemap->{$id});
323
324 my $ccinfo = {};
325
326 $ccinfo->{id} = $id;
327 $ccinfo->{parser} = $parser || \&ccache_default_parser;
328 $ccinfo->{writer} = $writer || \&ccache_default_writer;
329 $ccinfo->{update} = $update;
330
331 parse_ccache_options($ccinfo, %options);
332
333 if ($options{shadow}) {
334 $shadowfiles->{$filename} = $options{shadow};
335 }
336
337 $ccachemap->{$id} = $filename;
338 $ccache->{$filename} = $ccinfo;
339 }
340
341 sub register_regex {
342 my ($dir, $regex, $parser, $writer, $update, %options) = @_;
343
344 die "can't register regex after inotify_init" if $inotify;
345
346 my $uid = "$dir/$regex";
347 die "regular expression '$uid' already added :ERROR" if defined ($ccacheregex->{$uid});
348
349 my $ccinfo = {};
350
351 $ccinfo->{dir} = $dir;
352 $ccinfo->{regex} = $regex;
353 $ccinfo->{parser} = $parser || \&ccache_default_parser;
354 $ccinfo->{writer} = $writer || \&ccache_default_writer;
355 $ccinfo->{update} = $update;
356
357 parse_ccache_options($ccinfo, %options);
358
359 $ccacheregex->{$uid} = $ccinfo;
360 }
361
362 sub poll {
363 return if !$inotify;
364
365 if ($inotify_pid != $$) {
366 syslog ('err', "got inotify poll request in wrong process - disabling inotify");
367 $inotify = undef;
368 } else {
369 1 while $inotify && $inotify->poll;
370 }
371 }
372
373 sub flushcache {
374 foreach my $filename (keys %$ccache) {
375 $ccache->{$filename}->{version} = undef;
376 $ccache->{$filename}->{data} = undef;
377 $ccache->{$filename}->{diff} = undef;
378 }
379 }
380
381 sub inotify_close {
382 $inotify = undef;
383 }
384
385 sub inotify_init {
386
387 die "only one inotify instance allowed" if $inotify;
388
389 $inotify = Linux::Inotify2->new()
390 || die "Unable to create new inotify object: $!";
391
392 $inotify->blocking (0);
393
394 $versions = {};
395
396 my $dirhash = {};
397 foreach my $fn (keys %$ccache) {
398 my $dir = dirname ($fn);
399 my $base = basename ($fn);
400
401 $dirhash->{$dir}->{$base} = $fn;
402
403 if (my $sf = $shadowfiles->{$fn}) {
404 $base = basename ($sf);
405 $dir = dirname ($sf);
406 $dirhash->{$dir}->{$base} = $fn; # change version of original file!
407 }
408 }
409
410 foreach my $uid (keys %$ccacheregex) {
411 my $ccinfo = $ccacheregex->{$uid};
412 $dirhash->{$ccinfo->{dir}}->{_regex} = 1;
413 }
414
415 $inotify_pid = $$;
416
417 foreach my $dir (keys %$dirhash) {
418
419 my $evlist = IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_DELETE|IN_CREATE;
420 $inotify->watch ($dir, $evlist, sub {
421 my $e = shift;
422 my $name = $e->name;
423
424 if ($inotify_pid != $$) {
425 syslog ('err', "got inotify event in wrong process");
426 }
427
428 if ($e->IN_ISDIR || !$name) {
429 return;
430 }
431
432 if ($e->IN_Q_OVERFLOW) {
433 syslog ('info', "got inotify overflow - flushing cache");
434 flushcache();
435 return;
436 }
437
438 if ($e->IN_UNMOUNT) {
439 syslog ('err', "got 'unmount' event on '$name' - disabling inotify");
440 $inotify = undef;
441 }
442 if ($e->IN_IGNORED) {
443 syslog ('err', "got 'ignored' event on '$name' - disabling inotify");
444 $inotify = undef;
445 }
446
447 if ($dirhash->{$dir}->{_regex}) {
448 foreach my $uid (keys %$ccacheregex) {
449 my $ccinfo = $ccacheregex->{$uid};
450 next if $dir ne $ccinfo->{dir};
451 my $regex = $ccinfo->{regex};
452 if ($regex && ($name =~ m|^$regex$|)) {
453
454 my $fn = "$dir/$name";
455 $versions->{$fn}++;
456 #print "VERSION:$fn:$versions->{$fn}\n";
457 }
458 }
459 } elsif (my $fn = $dirhash->{$dir}->{$name}) {
460
461 $versions->{$fn}++;
462 #print "VERSION:$fn:$versions->{$fn}\n";
463 }
464 });
465 }
466
467 foreach my $dir (keys %$dirhash) {
468 foreach my $name (keys %{$dirhash->{$dir}}) {
469 if ($name eq '_regex') {
470 foreach my $uid (keys %$ccacheregex) {
471 my $ccinfo = $ccacheregex->{$uid};
472 next if $dir ne $ccinfo->{dir};
473 my $re = $ccinfo->{regex};
474 if (my $fd = IO::Dir->new ($dir)) {
475 while (defined(my $de = $fd->read)) {
476 if ($de =~ m/^$re$/) {
477 my $fn = "$dir/$de";
478 $versions->{$fn}++; # init with version
479 #print "init:$fn:$versions->{$fn}\n";
480 }
481 }
482 }
483 }
484 } else {
485 my $fn = $dirhash->{$dir}->{$name};
486 $versions->{$fn}++; # init with version
487 #print "init:$fn:$versions->{$fn}\n";
488 }
489 }
490 }
491 }
492
493 my $cached_nodename;
494
495 sub nodename {
496
497 return $cached_nodename if $cached_nodename;
498
499 my ($sysname, $nodename) = POSIX::uname();
500
501 $nodename =~ s/\..*$//; # strip domain part, if any
502
503 die "unable to read node name\n" if !$nodename;
504
505 $cached_nodename = $nodename;
506
507 return $cached_nodename;
508 }
509
510 sub read_etc_hostname {
511 my ($filename, $fd) = @_;
512
513 my $hostname = <$fd>;
514
515 chomp $hostname;
516
517 $hostname =~ s/\..*$//; # strip domain part, if any
518
519 return $hostname;
520 }
521
522 sub write_etc_hostname {
523 my ($filename, $fh, $hostname) = @_;
524
525 die "write failed: $!" unless print $fh "$hostname\n";
526
527 return $hostname;
528 }
529
530 register_file('hostname', "/etc/hostname",
531 \&read_etc_hostname,
532 \&write_etc_hostname);
533
534 sub read_etc_resolv_conf {
535 my ($filename, $fh) = @_;
536
537 my $res = {};
538
539 my $nscount = 0;
540 while (my $line = <$fh>) {
541 chomp $line;
542 if ($line =~ m/^(search|domain)\s+(\S+)\s*/) {
543 $res->{search} = $2;
544 } elsif ($line =~ m/^\s*nameserver\s+($PVE::Tools::IPRE)\s*/) {
545 $nscount++;
546 if ($nscount <= 3) {
547 $res->{"dns$nscount"} = $1;
548 }
549 }
550 }
551
552 return $res;
553 }
554
555 sub update_etc_resolv_conf {
556 my ($filename, $fh, $resolv, @args) = @_;
557
558 my $data = "";
559
560 $data = "search $resolv->{search}\n"
561 if $resolv->{search};
562
563 my $written = {};
564 foreach my $k ("dns1", "dns2", "dns3") {
565 my $ns = $resolv->{$k};
566 if ($ns && $ns ne '0.0.0.0' && !$written->{$ns}) {
567 $written->{$ns} = 1;
568 $data .= "nameserver $ns\n";
569 }
570 }
571
572 while (my $line = <$fh>) {
573 next if $line =~ m/^(search|domain|nameserver)\s+/;
574 $data .= $line
575 }
576
577 return $data;
578 }
579
580 register_file('resolvconf', "/etc/resolv.conf",
581 \&read_etc_resolv_conf, undef,
582 \&update_etc_resolv_conf);
583
584 sub read_etc_timezone {
585 my ($filename, $fd) = @_;
586
587 my $timezone = <$fd>;
588
589 chomp $timezone;
590
591 return $timezone;
592 }
593
594 sub write_etc_timezone {
595 my ($filename, $fh, $timezone) = @_;
596
597 my $tzinfo = "/usr/share/zoneinfo/$timezone";
598
599 raise_param_exc({ 'timezone' => "No such timezone" })
600 if (! -f $tzinfo);
601
602 ($timezone) = $timezone =~ m/^(.*)$/; # untaint
603
604 print $fh "$timezone\n";
605
606 unlink ("/etc/localtime");
607 symlink ("/usr/share/zoneinfo/$timezone", "/etc/localtime");
608
609 }
610
611 register_file('timezone', "/etc/timezone",
612 \&read_etc_timezone,
613 \&write_etc_timezone);
614
615 sub read_active_workers {
616 my ($filename, $fh) = @_;
617
618 return [] if !$fh;
619
620 my $res = [];
621 while (defined (my $line = <$fh>)) {
622 if ($line =~ m/^(\S+)\s(0|1)(\s([0-9A-Za-z]{8})(\s(\s*\S.*))?)?$/) {
623 my $upid = $1;
624 my $saved = $2;
625 my $endtime = $4;
626 my $status = $6;
627 if ((my $task = PVE::Tools::upid_decode($upid, 1))) {
628 $task->{upid} = $upid;
629 $task->{saved} = $saved;
630 $task->{endtime} = hex($endtime) if $endtime;
631 $task->{status} = $status if $status;
632 push @$res, $task;
633 }
634 } else {
635 warn "unable to parse line: $line";
636 }
637 }
638
639 return $res;
640
641 }
642
643 sub write_active_workers {
644 my ($filename, $fh, $tasklist) = @_;
645
646 my $raw = '';
647 foreach my $task (@$tasklist) {
648 my $upid = $task->{upid};
649 my $saved = $task->{saved} ? 1 : 0;
650 if ($task->{endtime}) {
651 if ($task->{status}) {
652 $raw .= sprintf("%s %s %08X %s\n", $upid, $saved, $task->{endtime}, $task->{status});
653 } else {
654 $raw .= sprintf("%s %s %08X\n", $upid, $saved, $task->{endtime});
655 }
656 } else {
657 $raw .= "$upid $saved\n";
658 }
659 }
660
661 PVE::Tools::safe_print($filename, $fh, $raw) if $raw;
662 }
663
664 register_file('active', "/var/log/pve/tasks/active",
665 \&read_active_workers,
666 \&write_active_workers);
667
668
669 our $bond_modes = { 'balance-rr' => 0,
670 'active-backup' => 1,
671 'balance-xor' => 2,
672 'broadcast' => 3,
673 '802.3ad' => 4,
674 'balance-tlb' => 5,
675 'balance-alb' => 6,
676 };
677
678 my $ovs_bond_modes = {
679 'active-backup' => 1,
680 'balance-slb' => 1,
681 'lacp-balance-slb' => 1,
682 'lacp-balance-tcp' => 1,
683 };
684
685 #sub get_bond_modes {
686 # return $bond_modes;
687 #}
688
689 my $parse_ovs_option = sub {
690 my ($data) = @_;
691
692 my $opts = {};
693 foreach my $kv (split (/\s+/, $data || '')) {
694 my ($k, $v) = split('=', $kv, 2);
695 $opts->{$k} = $v if $k && $v;
696 }
697 return $opts;
698 };
699
700 my $set_ovs_option = sub {
701 my ($d, %params) = @_;
702
703 my $opts = &$parse_ovs_option($d->{ovs_options});
704
705 foreach my $k (keys %params) {
706 my $v = $params{$k};
707 if ($v) {
708 $opts->{$k} = $v;
709 } else {
710 delete $opts->{$k};
711 }
712 }
713
714 my $res = [];
715 foreach my $k (keys %$opts) {
716 push @$res, "$k=$opts->{$k}";
717 }
718
719 if (my $new = join(' ', @$res)) {
720 $d->{ovs_options} = $new;
721 return $d->{ovs_options};
722 } else {
723 delete $d->{ovs_options};
724 return undef;
725 }
726 };
727
728 my $extract_ovs_option = sub {
729 my ($d, $name) = @_;
730
731 my $opts = &$parse_ovs_option($d->{ovs_options});
732
733 my $v = delete $opts->{$name};
734
735 my $res = [];
736 foreach my $k (keys %$opts) {
737 push @$res, "$k=$opts->{$k}";
738 }
739
740 if (my $new = join(' ', @$res)) {
741 $d->{ovs_options} = $new;
742 } else {
743 delete $d->{ovs_options};
744 }
745
746 return $v;
747 };
748
749 # config => {
750 # ifaces => {
751 # $ifname => {
752 # <optional> exists => BOOL,
753 # <optional> active => BOOL,
754 # <optional> autostart => BOOL,
755 # <auto> priority => INT,
756 #
757 # type => "eth" | "bridge" | "bond" | "loopback" | "OVS*" | ... ,
758 #
759 # families => ["inet", "inet6", ...],
760 #
761 # method => "manual" | "static" | "dhcp" | ... ,
762 # address => IP,
763 # netmask => SUBNET,
764 # broadcast => IP,
765 # gateway => IP,
766 # comments => [ "..." ],
767 #
768 # method6 => "manual" | "static" | "dhcp" | ... ,
769 # address6 => IP,
770 # netmask6 => SUBNET,
771 # gateway6 => IP,
772 # comments6 => [ "..." ],
773 #
774 # <known options>, # like bridge_ports, ovs_*
775 #
776 # # extra/unknown options stored by-family:
777 # options => { <inet options>... }
778 # options6 => { <inet6 options>... }
779 # }
780 # },
781 # options => [
782 # # mappings end up here as well, as we don't need to understand them
783 # [priority,line]
784 # ]
785 # }
786 sub read_etc_network_interfaces {
787 my ($filename, $fh) = @_;
788 my $proc_net_dev = IO::File->new('/proc/net/dev', 'r');
789 my $active = PVE::ProcFSTools::get_active_network_interfaces();
790 return __read_etc_network_interfaces($fh, $proc_net_dev, $active);
791 }
792
793 sub __read_etc_network_interfaces {
794 my ($fh, $proc_net_dev, $active_ifaces) = @_;
795
796 my $config = {};
797 my $ifaces = $config->{ifaces} = {};
798 my $options = $config->{options} = [];
799
800 my $line;
801
802 if ($proc_net_dev) {
803 while (defined ($line = <$proc_net_dev>)) {
804 if ($line =~ m/^\s*(eth\d+|en[^:.]+|ib\d+):.*/) {
805 $ifaces->{$1}->{exists} = 1;
806 }
807 }
808 close($proc_net_dev);
809 }
810
811 # we try to keep order inside the file
812 my $priority = 2; # 1 is reserved for lo
813
814 SECTION: while (defined ($line = <$fh>)) {
815 chomp ($line);
816 next if $line =~ m/^\s*#/;
817
818 if ($line =~ m/^\s*auto\s+(.*)$/) {
819 my @aa = split (/\s+/, $1);
820
821 foreach my $a (@aa) {
822 $ifaces->{$a}->{autostart} = 1;
823 }
824
825 } elsif ($line =~ m/^\s*iface\s+(\S+)\s+(inet6?)\s+(\S+)\s*$/) {
826 my $i = $1;
827 my $family = $2;
828 my $f = { method => $3 }; # by family, merged to $d with a $suffix
829 (my $suffix = $family) =~ s/^inet//;
830
831 my $d = $ifaces->{$i} ||= {};
832 $d->{priority} = $priority++ if !$d->{priority};
833 push @{$d->{families}}, $family;
834
835 while (defined ($line = <$fh>)) {
836 chomp $line;
837 if ($line =~ m/^\s*#(.*?)\s*$/) {
838 # NOTE: we use 'comments' instead of 'comment' to
839 # avoid automatic utf8 conversion
840 $f->{comments} = '' if !$f->{comments};
841 $f->{comments} .= "$1\n";
842 } elsif ($line =~ m/^\s*(?:iface\s
843 |mapping\s
844 |auto\s
845 |allow-
846 |source\s
847 |source-directory\s
848 )/x) {
849 last;
850 } elsif ($line =~ m/^\s*((\S+)\s+(.+))$/) {
851 my $option = $1;
852 my ($id, $value) = ($2, $3);
853 if (($id eq 'address') || ($id eq 'netmask') || ($id eq 'broadcast') || ($id eq 'gateway')) {
854 $f->{$id} = $value;
855 } elsif ($id eq 'ovs_type' || $id eq 'ovs_options'|| $id eq 'ovs_bridge' ||
856 $id eq 'ovs_bonds' || $id eq 'ovs_ports') {
857 $d->{$id} = $value;
858 } elsif ($id eq 'slaves' || $id eq 'bridge_ports') {
859 my $devs = {};
860 foreach my $p (split (/\s+/, $value)) {
861 next if $p eq 'none';
862 $devs->{$p} = 1;
863 }
864 my $str = join (' ', sort keys %{$devs});
865 if ($d->{$id}) {
866 $d->{$id} .= ' ' . $str if $str;
867 } else {
868 $d->{$id} = $str || '';
869 }
870 } elsif ($id eq 'bridge_stp') {
871 if ($value =~ m/^\s*(on|yes)\s*$/i) {
872 $d->{$id} = 'on';
873 } else {
874 $d->{$id} = 'off';
875 }
876 } elsif ($id eq 'bridge_fd') {
877 $d->{$id} = $value;
878 } elsif ($id eq 'bridge_vlan_aware') {
879 $d->{$id} = 1;
880 } elsif ($id eq 'bond_miimon') {
881 $d->{$id} = $value;
882 } elsif ($id eq 'bond_xmit_hash_policy') {
883 $d->{$id} = $value;
884 } elsif ($id eq 'bond_mode') {
885 # always use names
886 foreach my $bm (keys %$bond_modes) {
887 my $id = $bond_modes->{$bm};
888 if ($id eq $value) {
889 $value = $bm;
890 last;
891 }
892 }
893 $d->{$id} = $value;
894 } else {
895 push @{$f->{options}}, $option;
896 }
897 } else {
898 last;
899 }
900 }
901 $d->{"$_$suffix"} = $f->{$_} foreach (keys %$f);
902 last SECTION if !defined($line);
903 redo SECTION;
904 } elsif ($line =~ /\w/) {
905 push @$options, [$priority++, $line];
906 }
907 }
908
909 foreach my $ifname (@$active_ifaces) {
910 if (my $iface = $ifaces->{$ifname}) {
911 $iface->{active} = 1;
912 }
913 }
914
915 if (!$ifaces->{lo}) {
916 $ifaces->{lo}->{priority} = 1;
917 $ifaces->{lo}->{method} = 'loopback';
918 $ifaces->{lo}->{type} = 'loopback';
919 $ifaces->{lo}->{autostart} = 1;
920 }
921
922 foreach my $iface (keys %$ifaces) {
923 my $d = $ifaces->{$iface};
924 if ($iface =~ m/^bond\d+$/) {
925 if (!$d->{ovs_type}) {
926 $d->{type} = 'bond';
927 } elsif ($d->{ovs_type} eq 'OVSBond') {
928 $d->{type} = $d->{ovs_type};
929 # translate: ovs_options => bond_mode
930 $d->{'bond_mode'} = &$extract_ovs_option($d, 'bond_mode');
931 my $lacp = &$extract_ovs_option($d, 'lacp');
932 if ($lacp && $lacp eq 'active') {
933 if ($d->{'bond_mode'} eq 'balance-slb') {
934 $d->{'bond_mode'} = 'lacp-balance-slb';
935 }
936 }
937 # Note: balance-tcp needs lacp
938 if ($d->{'bond_mode'} eq 'balance-tcp') {
939 $d->{'bond_mode'} = 'lacp-balance-tcp';
940 }
941 my $tag = &$extract_ovs_option($d, 'tag');
942 $d->{ovs_tag} = $tag if defined($tag);
943 } else {
944 $d->{type} = 'unknown';
945 }
946 } elsif ($iface =~ m/^vmbr\d+$/) {
947 if (!$d->{ovs_type}) {
948 $d->{type} = 'bridge';
949
950 if (!defined ($d->{bridge_fd})) {
951 $d->{bridge_fd} = 0;
952 }
953 if (!defined ($d->{bridge_stp})) {
954 $d->{bridge_stp} = 'off';
955 }
956 } elsif ($d->{ovs_type} eq 'OVSBridge') {
957 $d->{type} = $d->{ovs_type};
958 } else {
959 $d->{type} = 'unknown';
960 }
961 } elsif ($iface =~ m/^(\S+):\d+$/) {
962 $d->{type} = 'alias';
963 if (defined ($ifaces->{$1})) {
964 $d->{exists} = $ifaces->{$1}->{exists};
965 } else {
966 $ifaces->{$1}->{exists} = 0;
967 $d->{exists} = 0;
968 }
969 } elsif ($iface =~ m/^(\S+)\.\d+$/) {
970 $d->{type} = 'vlan';
971 if (defined ($ifaces->{$1})) {
972 $d->{exists} = $ifaces->{$1}->{exists};
973 } else {
974 $ifaces->{$1}->{exists} = 0;
975 $d->{exists} = 0;
976 }
977 } elsif ($iface =~ m/^(?:eth\d+|en[^:.]+|ib\d+)$/) {
978 if (!$d->{ovs_type}) {
979 $d->{type} = 'eth';
980 } elsif ($d->{ovs_type} eq 'OVSPort') {
981 $d->{type} = $d->{ovs_type};
982 my $tag = &$extract_ovs_option($d, 'tag');
983 $d->{ovs_tag} = $tag if defined($tag);
984 } else {
985 $d->{type} = 'unknown';
986 }
987 } elsif ($iface =~ m/^lo$/) {
988 $d->{type} = 'loopback';
989 } else {
990 if (!$d->{ovs_type}) {
991 $d->{type} = 'unknown';
992 } elsif ($d->{ovs_type} eq 'OVSIntPort') {
993 $d->{type} = $d->{ovs_type};
994 my $tag = &$extract_ovs_option($d, 'tag');
995 $d->{ovs_tag} = $tag if defined($tag);
996 }
997 }
998
999 $d->{method} = 'manual' if !$d->{method};
1000 $d->{method6} = 'manual' if !$d->{method6};
1001
1002 $d->{families} ||= ['inet'];
1003 }
1004
1005 # OVS bridges create "allow-$BRIDGE $IFACE" lines which we need to remove
1006 # from the {options} hash for them to be removed correctly.
1007 @$options = grep {defined($_)} map {
1008 my ($pri, $line) = @$_;
1009 if ($line =~ /^allow-(\S+)\s+(.*)$/) {
1010 my $bridge = $1;
1011 my @ports = split(/\s+/, $2);
1012 if (defined(my $br = $ifaces->{$bridge})) {
1013 # if this port is part of a bridge, remove it
1014 my %in_ovs_ports = map {$_=>1} split(/\s+/, $br->{ovs_ports});
1015 @ports = grep { not $in_ovs_ports{$_} } @ports;
1016 }
1017 # create the allow line for the remaining ports, or delete if empty
1018 if (@ports) {
1019 [$pri, "allow-$bridge " . join(' ', @ports)];
1020 } else {
1021 undef;
1022 }
1023 } else {
1024 # don't modify other lines
1025 $_;
1026 }
1027 } @$options;
1028
1029 return $config;
1030 }
1031
1032 sub __interface_to_string {
1033 my ($iface, $d, $family, $first_block) = @_;
1034
1035 (my $suffix = $family) =~ s/^inet//;
1036
1037 return '' if !($d && $d->{"method$suffix"});
1038
1039 my $raw = '';
1040
1041 $raw .= "iface $iface $family " . $d->{"method$suffix"} . "\n";
1042 $raw .= "\taddress " . $d->{"address$suffix"} . "\n" if $d->{"address$suffix"};
1043 $raw .= "\tnetmask " . $d->{"netmask$suffix"} . "\n" if $d->{"netmask$suffix"};
1044 $raw .= "\tgateway " . $d->{"gateway$suffix"} . "\n" if $d->{"gateway$suffix"};
1045 $raw .= "\tbroadcast " . $d->{"broadcast$suffix"} . "\n" if $d->{"broadcast$suffix"};
1046
1047 my $done = { type => 1, priority => 1, method => 1, active => 1, exists => 1,
1048 comments => 1, autostart => 1, options => 1,
1049 address => 1, netmask => 1, gateway => 1, broadcast => 1,
1050 method6 => 1, families => 1, options6 => 1,
1051 address6 => 1, netmask6 => 1, gateway6 => 1, broadcast6 => 1 };
1052
1053 if (!$first_block) {
1054 # not printing out options
1055 } elsif ($d->{type} eq 'bridge') {
1056
1057 my $ports = $d->{bridge_ports} || 'none';
1058 $raw .= "\tbridge_ports $ports\n";
1059 $done->{bridge_ports} = 1;
1060
1061 my $v = defined($d->{bridge_stp}) ? $d->{bridge_stp} : 'off';
1062 $raw .= "\tbridge_stp $v\n";
1063 $done->{bridge_stp} = 1;
1064
1065 $v = defined($d->{bridge_fd}) ? $d->{bridge_fd} : 0;
1066 $raw .= "\tbridge_fd $v\n";
1067 $done->{bridge_fd} = 1;
1068
1069 if( defined($d->{bridge_vlan_aware})) {
1070 $raw .= "\tbridge_vlan_aware yes\n";
1071 }
1072 $done->{bridge_vlan_aware} = 1;
1073
1074 } elsif ($d->{type} eq 'bond') {
1075
1076 my $slaves = $d->{slaves} || 'none';
1077 $raw .= "\tslaves $slaves\n";
1078 $done->{slaves} = 1;
1079
1080 my $v = defined ($d->{'bond_miimon'}) ? $d->{'bond_miimon'} : 100;
1081 $raw .= "\tbond_miimon $v\n";
1082 $done->{'bond_miimon'} = 1;
1083
1084 $v = defined ($d->{'bond_mode'}) ? $d->{'bond_mode'} : 'balance-rr';
1085 $raw .= "\tbond_mode $v\n";
1086 $done->{'bond_mode'} = 1;
1087
1088 if ($d->{'bond_mode'} && $d->{'bond_xmit_hash_policy'} &&
1089 ($d->{'bond_mode'} eq 'balance-xor' || $d->{'bond_mode'} eq '802.3ad')) {
1090 $raw .= "\tbond_xmit_hash_policy $d->{'bond_xmit_hash_policy'}\n";
1091 }
1092 $done->{'bond_xmit_hash_policy'} = 1;
1093
1094 } elsif ($d->{type} eq 'OVSBridge') {
1095
1096 $raw .= "\tovs_type $d->{type}\n";
1097 $done->{ovs_type} = 1;
1098
1099 $raw .= "\tovs_ports $d->{ovs_ports}\n" if $d->{ovs_ports};
1100 $done->{ovs_ports} = 1;
1101 } elsif ($d->{type} eq 'OVSPort' || $d->{type} eq 'OVSIntPort' ||
1102 $d->{type} eq 'OVSBond') {
1103
1104 $d->{autostart} = 0; # started by the bridge
1105
1106 if (defined($d->{ovs_tag})) {
1107 &$set_ovs_option($d, tag => $d->{ovs_tag});
1108 }
1109 $done->{ovs_tag} = 1;
1110
1111 if ($d->{type} eq 'OVSBond') {
1112
1113 $d->{bond_mode} = 'active-backup' if !$d->{bond_mode};
1114
1115 $ovs_bond_modes->{$d->{bond_mode}} ||
1116 die "OVS does not support bond mode '$d->{bond_mode}\n";
1117
1118 if ($d->{bond_mode} eq 'lacp-balance-slb') {
1119 &$set_ovs_option($d, lacp => 'active');
1120 &$set_ovs_option($d, bond_mode => 'balance-slb');
1121 } elsif ($d->{bond_mode} eq 'lacp-balance-tcp') {
1122 &$set_ovs_option($d, lacp => 'active');
1123 &$set_ovs_option($d, bond_mode => 'balance-tcp');
1124 } else {
1125 &$set_ovs_option($d, lacp => undef);
1126 &$set_ovs_option($d, bond_mode => $d->{bond_mode});
1127 }
1128 $done->{bond_mode} = 1;
1129
1130 $raw .= "\tovs_bonds $d->{ovs_bonds}\n" if $d->{ovs_bonds};
1131 $done->{ovs_bonds} = 1;
1132 }
1133
1134 $raw .= "\tovs_type $d->{type}\n";
1135 $done->{ovs_type} = 1;
1136
1137 if ($d->{ovs_bridge}) {
1138 $raw = "allow-$d->{ovs_bridge} $iface\n$raw";
1139 $raw .= "\tovs_bridge $d->{ovs_bridge}\n";
1140 $done->{ovs_bridge} = 1;
1141 }
1142 }
1143
1144 if ($first_block) {
1145 # print other settings
1146 foreach my $k (keys %$d) {
1147 next if $done->{$k};
1148 next if !$d->{$k};
1149 $raw .= "\t$k $d->{$k}\n";
1150 }
1151 }
1152
1153 foreach my $option (@{$d->{"options$suffix"}}) {
1154 $raw .= "\t$option\n";
1155 }
1156
1157 # add comments
1158 my $comments = $d->{"comments$suffix"} || '';
1159 foreach my $cl (split(/\n/, $comments)) {
1160 $raw .= "#$cl\n";
1161 }
1162
1163 $raw .= "\n";
1164
1165 return $raw;
1166 }
1167
1168
1169 sub write_etc_network_interfaces {
1170 my ($filename, $fh, $config) = @_;
1171 my $raw = __write_etc_network_interfaces($config);
1172 PVE::Tools::safe_print($filename, $fh, $raw);
1173 }
1174 sub __write_etc_network_interfaces {
1175 my ($config) = @_;
1176
1177 my $ifaces = $config->{ifaces};
1178 my @options = @{$config->{options}};
1179
1180 my $used_ports = {};
1181
1182 foreach my $iface (keys %$ifaces) {
1183 my $d = $ifaces->{$iface};
1184
1185 my $ports = '';
1186 foreach my $k (qw(bridge_ports ovs_ports slaves ovs_bonds)) {
1187 $ports .= " $d->{$k}" if $d->{$k};
1188 }
1189
1190 foreach my $p (PVE::Tools::split_list($ports)) {
1191 die "port '$p' is already used on interface '$used_ports->{$p}'\n"
1192 if $used_ports->{$p} && $used_ports->{$p} ne $iface;
1193 $used_ports->{$p} = $iface;
1194 }
1195 }
1196
1197 # delete unused OVS ports
1198 foreach my $iface (keys %$ifaces) {
1199 my $d = $ifaces->{$iface};
1200 if ($d->{type} eq 'OVSPort' || $d->{type} eq 'OVSIntPort' ||
1201 $d->{type} eq 'OVSBond') {
1202 my $brname = $used_ports->{$iface};
1203 if (!$brname || !$ifaces->{$brname}) {
1204 if ($iface =~ /^(?:eth|en|ib)/) {
1205 $ifaces->{$iface} = { type => 'eth',
1206 exists => 1,
1207 method => 'manual',
1208 families => ['inet'] };
1209 } else {
1210 delete $ifaces->{$iface};
1211 }
1212 next;
1213 }
1214 my $bd = $ifaces->{$brname};
1215 if ($bd->{type} ne 'OVSBridge') {
1216 delete $ifaces->{$iface};
1217 next;
1218 }
1219 }
1220 }
1221
1222 # create OVS bridge ports
1223 foreach my $iface (keys %$ifaces) {
1224 my $d = $ifaces->{$iface};
1225 if ($d->{type} eq 'OVSBridge' && $d->{ovs_ports}) {
1226 foreach my $p (split (/\s+/, $d->{ovs_ports})) {
1227 my $n = $ifaces->{$p};
1228 die "OVS bridge '$iface' - unable to find port '$p'\n"
1229 if !$n;
1230 $n->{autostart} = 0;
1231 if ($n->{type} eq 'eth') {
1232 $n->{type} = 'OVSPort';
1233 $n->{ovs_bridge} = $iface;
1234 } elsif ($n->{type} eq 'OVSBond' || $n->{type} eq 'OVSPort' ||
1235 $n->{type} eq 'OVSIntPort') {
1236 $n->{ovs_bridge} = $iface;
1237 } else {
1238 die "interface '$p' is not defined as OVS port/bond\n";
1239 }
1240 }
1241 }
1242 }
1243
1244 # check OVS bond ports
1245 foreach my $iface (keys %$ifaces) {
1246 my $d = $ifaces->{$iface};
1247 if ($d->{type} eq 'OVSBond' && $d->{ovs_bonds}) {
1248 foreach my $p (split (/\s+/, $d->{ovs_bonds})) {
1249 my $n = $ifaces->{$p};
1250 die "OVS bond '$iface' - unable to find slave '$p'\n"
1251 if !$n;
1252 die "OVS bond '$iface' - wrong interface type on slave '$p' " .
1253 "('$n->{type}' != 'eth')\n" if $n->{type} ne 'eth';
1254 }
1255 }
1256 }
1257
1258 my $raw = <<'NETWORKDOC';
1259 # network interface settings; autogenerated
1260 # Please do NOT modify this file directly, unless you know what
1261 # you're doing.
1262 #
1263 # If you want to manage part of the network configuration manually,
1264 # please utilize the 'source' or 'source-directory' directives to do
1265 # so.
1266 # PVE will preserve these directives, but will NOT its network
1267 # configuration from sourced files, so do not attempt to move any of
1268 # the PVE managed interfaces into external files!
1269
1270 NETWORKDOC
1271
1272 my $printed = {};
1273
1274 my $if_type_hash = {
1275 loopback => 100000,
1276 eth => 200000,
1277 bond => 300000,
1278 bridge => 400000,
1279 };
1280
1281 my $lookup_type_prio = sub {
1282 my $iface = shift;
1283
1284 my $child = 0;
1285 if ($iface =~ m/^(\S+)(\.|:)\d+$/) {
1286 $iface = $1;
1287 $child = 1;
1288 }
1289
1290 my $pri;
1291 if ($iface eq 'lo') {
1292 $pri = $if_type_hash->{loopback};
1293 } elsif ($iface =~ m/^(?:eth\d+|ib\d+|en[^:.]+)$/) {
1294 $pri = $if_type_hash->{eth} + $child;
1295 } elsif ($iface =~ m/^bond\d+$/) {
1296 $pri = $if_type_hash->{bond} + $child;
1297 } elsif ($iface =~ m/^vmbr\d+$/) {
1298 $pri = $if_type_hash->{bridge} + $child;
1299 }
1300
1301 return $pri;
1302 };
1303
1304 foreach my $iface (sort {
1305 my $ref1 = $ifaces->{$a};
1306 my $ref2 = $ifaces->{$b};
1307 my $tp1 = &$lookup_type_prio($a);
1308 my $tp2 = &$lookup_type_prio($b);
1309
1310 # Only recognized types are in relation to each other. If one type
1311 # is unknown then only consider the interfaces' priority attributes.
1312 $tp1 = $tp2 = 0 if !defined($tp1) || !defined($tp2);
1313
1314 my $p1 = $tp1 + ($ref1->{priority} // 50000);
1315 my $p2 = $tp2 + ($ref2->{priority} // 50000);
1316
1317 return $p1 <=> $p2 if $p1 != $p2;
1318
1319 return $a cmp $b;
1320 } keys %$ifaces) {
1321 next if $printed->{$iface};
1322
1323 my $d = $ifaces->{$iface};
1324 my $pri = $d->{priority} // 0;
1325 if (@options && $options[0]->[0] < $pri) {
1326 do {
1327 $raw .= (shift @options)->[1] . "\n";
1328 } while (@options && $options[0]->[0] < $pri);
1329 $raw .= "\n";
1330 }
1331
1332 $printed->{$iface} = 1;
1333 $raw .= "auto $iface\n" if $d->{autostart};
1334 my $i = 0; # some options should be printed only once
1335 $raw .= __interface_to_string($iface, $d, $_, !$i++) foreach @{$d->{families}};
1336 }
1337
1338 $raw .= $_->[1] . "\n" foreach @options;
1339 return $raw;
1340 }
1341
1342 register_file('interfaces', "/etc/network/interfaces",
1343 \&read_etc_network_interfaces,
1344 \&write_etc_network_interfaces);
1345
1346
1347 sub read_iscsi_initiatorname {
1348 my ($filename, $fd) = @_;
1349
1350 while (defined(my $line = <$fd>)) {
1351 if ($line =~ m/^InitiatorName=(\S+)$/) {
1352 return $1;
1353 }
1354 }
1355
1356 return 'undefined';
1357 }
1358
1359 register_file('initiatorname', "/etc/iscsi/initiatorname.iscsi",
1360 \&read_iscsi_initiatorname);
1361
1362 sub read_apt_auth {
1363 my ($filename, $fd) = @_;
1364
1365 local $/;
1366
1367 my $raw = defined($fd) ? <$fd> : '';
1368
1369 $raw =~ s/^\s+//;
1370
1371
1372 my @tokens = split(/\s+/, $raw);
1373
1374 my $data = {};
1375
1376 my $machine;
1377 while (defined(my $tok = shift @tokens)) {
1378
1379 $machine = shift @tokens if $tok eq 'machine';
1380 next if !$machine;
1381 $data->{$machine} = {} if !$data->{$machine};
1382
1383 $data->{$machine}->{login} = shift @tokens if $tok eq 'login';
1384 $data->{$machine}->{password} = shift @tokens if $tok eq 'password';
1385 };
1386
1387 return $data;
1388 }
1389
1390 my $format_apt_auth_data = sub {
1391 my $data = shift;
1392
1393 my $raw = '';
1394
1395 foreach my $machine (sort keys %$data) {
1396 my $d = $data->{$machine};
1397 $raw .= "machine $machine\n";
1398 $raw .= " login $d->{login}\n" if $d->{login};
1399 $raw .= " password $d->{password}\n" if $d->{password};
1400 $raw .= "\n";
1401 }
1402
1403 return $raw;
1404 };
1405
1406 sub write_apt_auth {
1407 my ($filename, $fh, $data) = @_;
1408
1409 my $raw = &$format_apt_auth_data($data);
1410
1411 die "write failed: $!" unless print $fh "$raw\n";
1412
1413 return $data;
1414 }
1415
1416 sub update_apt_auth {
1417 my ($filename, $fh, $data) = @_;
1418
1419 my $orig = read_apt_auth($filename, $fh);
1420
1421 foreach my $machine (keys %$data) {
1422 $orig->{$machine} = $data->{$machine};
1423 }
1424
1425 return &$format_apt_auth_data($orig);
1426 }
1427
1428 register_file('apt-auth', "/etc/apt/auth.conf",
1429 \&read_apt_auth, \&write_apt_auth,
1430 \&update_apt_auth, perm => 0640);
1431
1432 1;