]>
Commit | Line | Data |
---|---|---|
e143e9d8 DM |
1 | package PVE::INotify; |
2 | ||
3 | # todo: maybe we do not need update_file() ? | |
4 | ||
5 | use strict; | |
c36f332e DM |
6 | use warnings; |
7 | ||
e143e9d8 DM |
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); | |
3dabe28a | 16 | use PVE::Network; |
e143e9d8 | 17 | use PVE::Tools; |
f0d1b04f | 18 | use PVE::ProcFSTools; |
2896245e | 19 | use PVE::JSONSchema; |
32ef45ae | 20 | use Clone qw(clone); |
e143e9d8 DM |
21 | use Linux::Inotify2; |
22 | use base 'Exporter'; | |
69758574 | 23 | use JSON; |
9194ee06 | 24 | use Digest::SHA; |
69758574 | 25 | use Encode qw(encode decode); |
e143e9d8 DM |
26 | |
27 | our @EXPORT_OK = qw(read_file write_file register_file); | |
28 | ||
29 | my $ccache; | |
30 | my $ccachemap; | |
31 | my $ccacheregex; | |
32 | my $inotify; | |
33 | my $inotify_pid = 0; | |
34 | my $versions; | |
35 | my $shadowfiles = { | |
36 | '/etc/network/interfaces' => '/etc/network/interfaces.new', | |
37 | }; | |
38 | ||
39 | # to enable cached operation, you need to call 'inotify_init' | |
40 | # inotify handles are a limited resource, so use with care (only | |
41 | # enable the cache if you really need it) | |
42 | ||
43 | # Note: please close the inotify handle after you fork | |
44 | ||
45 | sub ccache_default_writer { | |
46 | my ($filename, $data) = @_; | |
47 | ||
48 | die "undefined config writer for '$filename' :ERROR"; | |
49 | } | |
50 | ||
51 | sub ccache_default_parser { | |
52 | my ($filename, $srcfd) = @_; | |
53 | ||
54 | die "undefined config reader for '$filename' :ERROR"; | |
55 | } | |
56 | ||
57 | sub ccache_compute_diff { | |
58 | my ($filename, $shadow) = @_; | |
59 | ||
60 | my $diff = ''; | |
61 | ||
69758574 DC |
62 | my $cmd = ['/usr/bin/diff', '-b', '-N', '-u', $filename, $shadow]; |
63 | PVE::Tools::run_command($cmd, noerr => 1, outfunc => sub { | |
64 | my ($line) = @_; | |
65 | $diff .= decode('UTF-8', $line) . "\n"; | |
66 | }); | |
e143e9d8 DM |
67 | |
68 | $diff = undef if !$diff; | |
69 | ||
70 | return $diff; | |
71 | } | |
72 | ||
73 | sub ccache_info { | |
74 | my ($filename) = @_; | |
75 | ||
76 | foreach my $uid (keys %$ccacheregex) { | |
77 | my $ccinfo = $ccacheregex->{$uid}; | |
78 | my $dir = $ccinfo->{dir}; | |
79 | my $regex = $ccinfo->{regex}; | |
80 | if ($filename =~ m|^$dir/+$regex$|) { | |
81 | if (!$ccache->{$filename}) { | |
82 | my $cp = {}; | |
83 | while (my ($k, $v) = each %$ccinfo) { | |
84 | $cp->{$k} = $v; | |
85 | } | |
86 | $ccache->{$filename} = $cp; | |
9bbc4e17 | 87 | } |
e143e9d8 DM |
88 | return ($ccache->{$filename}, $filename); |
89 | } | |
90 | } | |
9bbc4e17 | 91 | |
e143e9d8 DM |
92 | $filename = $ccachemap->{$filename} if defined ($ccachemap->{$filename}); |
93 | ||
94 | die "file '$filename' not added :ERROR" if !defined ($ccache->{$filename}); | |
9bbc4e17 | 95 | |
e143e9d8 DM |
96 | return ($ccache->{$filename}, $filename); |
97 | } | |
98 | ||
99 | sub write_file { | |
100 | my ($fileid, $data, $full) = @_; | |
101 | ||
102 | my ($ccinfo, $filename) = ccache_info($fileid); | |
103 | ||
104 | my $writer = $ccinfo->{writer}; | |
105 | ||
106 | my $realname = $filename; | |
107 | ||
108 | my $shadow; | |
109 | if ($shadow = $shadowfiles->{$filename}) { | |
110 | $realname = $shadow; | |
111 | } | |
112 | ||
113 | my $perm = $ccinfo->{perm} || 0644; | |
114 | ||
115 | my $tmpname = "$realname.tmp.$$"; | |
116 | ||
117 | my $res; | |
118 | eval { | |
119 | my $fh = IO::File->new($tmpname, O_WRONLY|O_CREAT, $perm); | |
120 | die "unable to open file '$tmpname' - $!\n" if !$fh; | |
121 | ||
122 | $res = &$writer($filename, $fh, $data); | |
123 | ||
124 | die "closing file '$tmpname' failed - $!\n" unless close $fh; | |
125 | }; | |
126 | my $err = $@; | |
127 | ||
128 | $ccinfo->{version} = undef; | |
129 | ||
130 | if ($err) { | |
131 | unlink $tmpname; | |
132 | die $err; | |
133 | } | |
134 | ||
135 | if (!rename($tmpname, $realname)) { | |
136 | my $msg = "close (rename) atomic file '$filename' failed: $!\n"; | |
137 | unlink $tmpname; | |
9bbc4e17 | 138 | die $msg; |
e143e9d8 DM |
139 | } |
140 | ||
141 | my $diff; | |
142 | if ($shadow && $full) { | |
143 | $diff = ccache_compute_diff ($filename, $shadow); | |
144 | } | |
145 | ||
146 | if ($full) { | |
147 | return { data => $res, changes => $diff }; | |
148 | } | |
149 | ||
150 | return $res; | |
151 | } | |
152 | ||
153 | sub update_file { | |
154 | my ($fileid, $data, @args) = @_; | |
155 | ||
156 | my ($ccinfo, $filename) = ccache_info($fileid); | |
157 | ||
158 | my $update = $ccinfo->{update}; | |
159 | ||
160 | die "unable to update/merge data" if !$update; | |
161 | ||
162 | my $lkfn = "$filename.lock"; | |
163 | ||
164 | my $timeout = 10; | |
165 | ||
166 | my $fd; | |
167 | ||
168 | my $code = sub { | |
169 | ||
170 | $fd = IO::File->new ($filename, "r"); | |
9bbc4e17 | 171 | |
e143e9d8 DM |
172 | my $new = &$update($filename, $fd, $data, @args); |
173 | ||
174 | if (defined($new)) { | |
175 | PVE::Tools::file_set_contents($filename, $new, $ccinfo->{perm}); | |
176 | } else { | |
177 | unlink $filename; | |
178 | } | |
179 | }; | |
180 | ||
181 | PVE::Tools::lock_file($lkfn, $timeout, $code); | |
182 | my $err = $@; | |
183 | ||
184 | close($fd) if defined($fd); | |
185 | ||
186 | die $err if $err; | |
187 | ||
188 | return undef; | |
189 | } | |
190 | ||
191 | sub discard_changes { | |
192 | my ($fileid, $full) = @_; | |
193 | ||
194 | my ($ccinfo, $filename) = ccache_info($fileid); | |
195 | ||
196 | if (my $copy = $shadowfiles->{$filename}) { | |
197 | unlink $copy; | |
198 | } | |
199 | ||
200 | return read_file ($filename, $full); | |
201 | } | |
202 | ||
3b671d82 DM |
203 | sub poll_changes { |
204 | my ($filename) = @_; | |
205 | ||
206 | poll() if $inotify; # read new inotify events | |
207 | ||
208 | $versions->{$filename} = 0 if !defined ($versions->{$filename}); | |
209 | ||
210 | return $versions->{$filename}; | |
211 | } | |
212 | ||
e143e9d8 DM |
213 | sub read_file { |
214 | my ($fileid, $full) = @_; | |
215 | ||
216 | my $parser; | |
217 | ||
218 | my ($ccinfo, $filename) = ccache_info($fileid); | |
9bbc4e17 | 219 | |
e143e9d8 | 220 | $parser = $ccinfo->{parser}; |
9bbc4e17 | 221 | |
e143e9d8 DM |
222 | my $fd; |
223 | my $shadow; | |
224 | ||
3b671d82 | 225 | my $cver = poll_changes($filename); |
e143e9d8 DM |
226 | |
227 | if (my $copy = $shadowfiles->{$filename}) { | |
228 | if ($fd = IO::File->new ($copy, "r")) { | |
229 | $shadow = $copy; | |
230 | } else { | |
231 | $fd = IO::File->new ($filename, "r"); | |
232 | } | |
233 | } else { | |
234 | $fd = IO::File->new ($filename, "r"); | |
235 | } | |
236 | ||
237 | my $acp = $ccinfo->{always_call_parser}; | |
238 | ||
239 | if (!$fd) { | |
240 | $ccinfo->{version} = undef; | |
9bbc4e17 | 241 | $ccinfo->{data} = undef; |
e143e9d8 DM |
242 | $ccinfo->{diff} = undef; |
243 | return undef if !$acp; | |
244 | } | |
245 | ||
246 | my $noclone = $ccinfo->{noclone}; | |
247 | ||
248 | # file unchanged? | |
249 | if (!$ccinfo->{nocache} && | |
04cba6c8 | 250 | $inotify && $cver && |
e143e9d8 DM |
251 | defined ($ccinfo->{data}) && |
252 | defined ($ccinfo->{version}) && | |
253 | ($ccinfo->{readonce} || | |
04cba6c8 | 254 | ($ccinfo->{version} == $cver))) { |
e143e9d8 DM |
255 | |
256 | my $ret; | |
257 | if (!$noclone && ref ($ccinfo->{data})) { | |
32ef45ae | 258 | $ret->{data} = clone ($ccinfo->{data}); |
e143e9d8 DM |
259 | } else { |
260 | $ret->{data} = $ccinfo->{data}; | |
261 | } | |
262 | $ret->{changes} = $ccinfo->{diff}; | |
9bbc4e17 | 263 | |
e143e9d8 DM |
264 | return $full ? $ret : $ret->{data}; |
265 | } | |
266 | ||
267 | my $diff; | |
268 | ||
269 | if ($shadow) { | |
270 | $diff = ccache_compute_diff ($filename, $shadow); | |
271 | } | |
272 | ||
273 | my $res = &$parser($filename, $fd); | |
274 | ||
275 | if (!$ccinfo->{nocache}) { | |
276 | $ccinfo->{version} = $cver; | |
277 | } | |
278 | ||
279 | # we cache data with references, so we always need to | |
32ef45ae | 280 | # clone this data. Else the original data may get |
e143e9d8 DM |
281 | # modified. |
282 | $ccinfo->{data} = $res; | |
283 | ||
284 | # also store diff | |
285 | $ccinfo->{diff} = $diff; | |
286 | ||
287 | my $ret; | |
288 | if (!$noclone && ref ($ccinfo->{data})) { | |
32ef45ae | 289 | $ret->{data} = clone ($ccinfo->{data}); |
e143e9d8 DM |
290 | } else { |
291 | $ret->{data} = $ccinfo->{data}; | |
292 | } | |
293 | $ret->{changes} = $ccinfo->{diff}; | |
294 | ||
295 | return $full ? $ret : $ret->{data}; | |
9bbc4e17 | 296 | } |
e143e9d8 DM |
297 | |
298 | sub parse_ccache_options { | |
299 | my ($ccinfo, %options) = @_; | |
300 | ||
301 | foreach my $opt (keys %options) { | |
302 | my $v = $options{$opt}; | |
303 | if ($opt eq 'readonce') { | |
304 | $ccinfo->{$opt} = $v; | |
305 | } elsif ($opt eq 'nocache') { | |
306 | $ccinfo->{$opt} = $v; | |
307 | } elsif ($opt eq 'shadow') { | |
308 | $ccinfo->{$opt} = $v; | |
309 | } elsif ($opt eq 'perm') { | |
310 | $ccinfo->{$opt} = $v; | |
311 | } elsif ($opt eq 'noclone') { | |
312 | # noclone flag for large read-only data chunks like aplinfo | |
313 | $ccinfo->{$opt} = $v; | |
314 | } elsif ($opt eq 'always_call_parser') { | |
1f929ade | 315 | # when set, we call parser even when the file does not exist. |
e143e9d8 DM |
316 | # this allows the parser to return some default |
317 | $ccinfo->{$opt} = $v; | |
318 | } else { | |
319 | die "internal error - unsupported option '$opt'"; | |
320 | } | |
321 | } | |
322 | } | |
323 | ||
324 | sub register_file { | |
325 | my ($id, $filename, $parser, $writer, $update, %options) = @_; | |
326 | ||
26a8d824 | 327 | die "can't register file '$filename' after inotify_init" if $inotify; |
e143e9d8 DM |
328 | |
329 | die "file '$filename' already added :ERROR" if defined ($ccache->{$filename}); | |
330 | die "ID '$id' already used :ERROR" if defined ($ccachemap->{$id}); | |
331 | ||
332 | my $ccinfo = {}; | |
333 | ||
334 | $ccinfo->{id} = $id; | |
335 | $ccinfo->{parser} = $parser || \&ccache_default_parser; | |
336 | $ccinfo->{writer} = $writer || \&ccache_default_writer; | |
337 | $ccinfo->{update} = $update; | |
338 | ||
339 | parse_ccache_options($ccinfo, %options); | |
9bbc4e17 | 340 | |
e143e9d8 DM |
341 | if ($options{shadow}) { |
342 | $shadowfiles->{$filename} = $options{shadow}; | |
343 | } | |
344 | ||
345 | $ccachemap->{$id} = $filename; | |
346 | $ccache->{$filename} = $ccinfo; | |
347 | } | |
348 | ||
349 | sub register_regex { | |
350 | my ($dir, $regex, $parser, $writer, $update, %options) = @_; | |
351 | ||
b8246bfc | 352 | die "can't register regex after inotify_init" if $inotify; |
e143e9d8 DM |
353 | |
354 | my $uid = "$dir/$regex"; | |
355 | die "regular expression '$uid' already added :ERROR" if defined ($ccacheregex->{$uid}); | |
9bbc4e17 | 356 | |
e143e9d8 DM |
357 | my $ccinfo = {}; |
358 | ||
359 | $ccinfo->{dir} = $dir; | |
360 | $ccinfo->{regex} = $regex; | |
361 | $ccinfo->{parser} = $parser || \&ccache_default_parser; | |
362 | $ccinfo->{writer} = $writer || \&ccache_default_writer; | |
363 | $ccinfo->{update} = $update; | |
364 | ||
365 | parse_ccache_options($ccinfo, %options); | |
366 | ||
367 | $ccacheregex->{$uid} = $ccinfo; | |
368 | } | |
369 | ||
370 | sub poll { | |
371 | return if !$inotify; | |
372 | ||
373 | if ($inotify_pid != $$) { | |
374 | syslog ('err', "got inotify poll request in wrong process - disabling inotify"); | |
375 | $inotify = undef; | |
376 | } else { | |
377 | 1 while $inotify && $inotify->poll; | |
378 | } | |
379 | } | |
380 | ||
381 | sub flushcache { | |
382 | foreach my $filename (keys %$ccache) { | |
383 | $ccache->{$filename}->{version} = undef; | |
384 | $ccache->{$filename}->{data} = undef; | |
385 | $ccache->{$filename}->{diff} = undef; | |
386 | } | |
387 | } | |
388 | ||
389 | sub inotify_close { | |
390 | $inotify = undef; | |
391 | } | |
392 | ||
393 | sub inotify_init { | |
394 | ||
395 | die "only one inotify instance allowed" if $inotify; | |
396 | ||
397 | $inotify = Linux::Inotify2->new() | |
398 | || die "Unable to create new inotify object: $!"; | |
399 | ||
400 | $inotify->blocking (0); | |
401 | ||
402 | $versions = {}; | |
403 | ||
404 | my $dirhash = {}; | |
405 | foreach my $fn (keys %$ccache) { | |
406 | my $dir = dirname ($fn); | |
407 | my $base = basename ($fn); | |
408 | ||
409 | $dirhash->{$dir}->{$base} = $fn; | |
410 | ||
411 | if (my $sf = $shadowfiles->{$fn}) { | |
412 | $base = basename ($sf); | |
413 | $dir = dirname ($sf); | |
414 | $dirhash->{$dir}->{$base} = $fn; # change version of original file! | |
415 | } | |
416 | } | |
417 | ||
418 | foreach my $uid (keys %$ccacheregex) { | |
419 | my $ccinfo = $ccacheregex->{$uid}; | |
9bbc4e17 | 420 | $dirhash->{$ccinfo->{dir}}->{_regex} = 1; |
e143e9d8 DM |
421 | } |
422 | ||
423 | $inotify_pid = $$; | |
424 | ||
425 | foreach my $dir (keys %$dirhash) { | |
426 | ||
427 | my $evlist = IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_DELETE|IN_CREATE; | |
428 | $inotify->watch ($dir, $evlist, sub { | |
429 | my $e = shift; | |
430 | my $name = $e->name; | |
431 | ||
432 | if ($inotify_pid != $$) { | |
433 | syslog ('err', "got inotify event in wrong process"); | |
434 | } | |
435 | ||
436 | if ($e->IN_ISDIR || !$name) { | |
437 | return; | |
438 | } | |
439 | ||
440 | if ($e->IN_Q_OVERFLOW) { | |
441 | syslog ('info', "got inotify overflow - flushing cache"); | |
442 | flushcache(); | |
443 | return; | |
444 | } | |
445 | ||
446 | if ($e->IN_UNMOUNT) { | |
447 | syslog ('err', "got 'unmount' event on '$name' - disabling inotify"); | |
448 | $inotify = undef; | |
449 | } | |
9bbc4e17 | 450 | if ($e->IN_IGNORED) { |
e143e9d8 DM |
451 | syslog ('err', "got 'ignored' event on '$name' - disabling inotify"); |
452 | $inotify = undef; | |
453 | } | |
454 | ||
455 | if ($dirhash->{$dir}->{_regex}) { | |
456 | foreach my $uid (keys %$ccacheregex) { | |
457 | my $ccinfo = $ccacheregex->{$uid}; | |
458 | next if $dir ne $ccinfo->{dir}; | |
459 | my $regex = $ccinfo->{regex}; | |
460 | if ($regex && ($name =~ m|^$regex$|)) { | |
461 | ||
462 | my $fn = "$dir/$name"; | |
463 | $versions->{$fn}++; | |
464 | #print "VERSION:$fn:$versions->{$fn}\n"; | |
465 | } | |
466 | } | |
467 | } elsif (my $fn = $dirhash->{$dir}->{$name}) { | |
468 | ||
469 | $versions->{$fn}++; | |
470 | #print "VERSION:$fn:$versions->{$fn}\n"; | |
471 | } | |
472 | }); | |
473 | } | |
474 | ||
475 | foreach my $dir (keys %$dirhash) { | |
476 | foreach my $name (keys %{$dirhash->{$dir}}) { | |
477 | if ($name eq '_regex') { | |
478 | foreach my $uid (keys %$ccacheregex) { | |
479 | my $ccinfo = $ccacheregex->{$uid}; | |
480 | next if $dir ne $ccinfo->{dir}; | |
481 | my $re = $ccinfo->{regex}; | |
482 | if (my $fd = IO::Dir->new ($dir)) { | |
9bbc4e17 | 483 | while (defined(my $de = $fd->read)) { |
e143e9d8 DM |
484 | if ($de =~ m/^$re$/) { |
485 | my $fn = "$dir/$de"; | |
486 | $versions->{$fn}++; # init with version | |
487 | #print "init:$fn:$versions->{$fn}\n"; | |
488 | } | |
489 | } | |
490 | } | |
491 | } | |
492 | } else { | |
493 | my $fn = $dirhash->{$dir}->{$name}; | |
494 | $versions->{$fn}++; # init with version | |
495 | #print "init:$fn:$versions->{$fn}\n"; | |
496 | } | |
497 | } | |
498 | } | |
499 | } | |
500 | ||
501 | my $cached_nodename; | |
502 | ||
503 | sub nodename { | |
504 | ||
505 | return $cached_nodename if $cached_nodename; | |
506 | ||
507 | my ($sysname, $nodename) = POSIX::uname(); | |
508 | ||
509 | $nodename =~ s/\..*$//; # strip domain part, if any | |
510 | ||
511 | die "unable to read node name\n" if !$nodename; | |
512 | ||
513 | $cached_nodename = $nodename; | |
514 | ||
515 | return $cached_nodename; | |
516 | } | |
517 | ||
518 | sub read_etc_hostname { | |
519 | my ($filename, $fd) = @_; | |
520 | ||
521 | my $hostname = <$fd>; | |
522 | ||
523 | chomp $hostname; | |
524 | ||
525 | $hostname =~ s/\..*$//; # strip domain part, if any | |
526 | ||
527 | return $hostname; | |
528 | } | |
529 | ||
530 | sub write_etc_hostname { | |
531 | my ($filename, $fh, $hostname) = @_; | |
532 | ||
533 | die "write failed: $!" unless print $fh "$hostname\n"; | |
534 | ||
535 | return $hostname; | |
536 | } | |
537 | ||
9bbc4e17 TL |
538 | register_file('hostname', "/etc/hostname", |
539 | \&read_etc_hostname, | |
e143e9d8 DM |
540 | \&write_etc_hostname); |
541 | ||
9194ee06 DC |
542 | sub read_etc_hosts { |
543 | my ($filename, $fh) = @_; | |
544 | ||
545 | my $raw = ''; | |
546 | my $data = ''; | |
547 | ||
548 | while (my $line = <$fh>) { | |
549 | $raw .= $line; | |
550 | if ($line =~ m/^\s*#/) { | |
551 | $line = decode('UTF-8', $line); | |
552 | } | |
553 | $data .= $line; | |
554 | } | |
555 | ||
556 | return { | |
557 | digest => Digest::SHA::sha1_hex($raw), | |
558 | data => $data, | |
559 | } | |
560 | } | |
561 | ||
562 | sub write_etc_hosts { | |
563 | my ($filename, $fh, $hosts, @args) = @_; | |
564 | ||
565 | # check validity of ips/names | |
566 | for my $line (split("\n", $hosts)) { | |
567 | next if $line =~ m/^\s*#/; # comments | |
568 | next if $line =~ m/^\s*$/; # whitespace/empty lines | |
569 | ||
570 | my ($ip, @names) = split(/\s+/, $line); | |
571 | ||
572 | raise_param_exc({ 'data' => "Invalid IP '$ip'" }) | |
573 | if $ip !~ m/^$PVE::Tools::IPRE$/; | |
574 | ||
575 | for my $name (@names) { | |
576 | raise_param_exc({ 'data' => "Invalid Hostname '$name'" }) | |
577 | if $name !~ m/^[.\-a-zA-Z0-9]+$/; | |
578 | } | |
579 | } | |
580 | ||
581 | die "write failed: $!" if !print $fh encode('UTF-8', $hosts); | |
582 | ||
583 | return $hosts; | |
584 | } | |
585 | ||
586 | register_file('etchosts', "/etc/hosts", | |
587 | \&read_etc_hosts, | |
588 | \&write_etc_hosts); | |
589 | ||
e143e9d8 DM |
590 | sub read_etc_resolv_conf { |
591 | my ($filename, $fh) = @_; | |
592 | ||
593 | my $res = {}; | |
594 | ||
595 | my $nscount = 0; | |
596 | while (my $line = <$fh>) { | |
597 | chomp $line; | |
598 | if ($line =~ m/^(search|domain)\s+(\S+)\s*/) { | |
599 | $res->{search} = $2; | |
176b1186 | 600 | } elsif ($line =~ m/^\s*nameserver\s+($PVE::Tools::IPRE)\s*/) { |
e143e9d8 DM |
601 | $nscount++; |
602 | if ($nscount <= 3) { | |
603 | $res->{"dns$nscount"} = $1; | |
604 | } | |
605 | } | |
606 | } | |
607 | ||
608 | return $res; | |
609 | } | |
610 | ||
611 | sub update_etc_resolv_conf { | |
612 | my ($filename, $fh, $resolv, @args) = @_; | |
613 | ||
614 | my $data = ""; | |
615 | ||
616 | $data = "search $resolv->{search}\n" | |
617 | if $resolv->{search}; | |
618 | ||
619 | my $written = {}; | |
620 | foreach my $k ("dns1", "dns2", "dns3") { | |
621 | my $ns = $resolv->{$k}; | |
622 | if ($ns && $ns ne '0.0.0.0' && !$written->{$ns}) { | |
623 | $written->{$ns} = 1; | |
624 | $data .= "nameserver $ns\n"; | |
625 | } | |
626 | } | |
627 | ||
628 | while (my $line = <$fh>) { | |
629 | next if $line =~ m/^(search|domain|nameserver)\s+/; | |
630 | $data .= $line | |
631 | } | |
9bbc4e17 | 632 | |
e143e9d8 DM |
633 | return $data; |
634 | } | |
635 | ||
9bbc4e17 TL |
636 | register_file('resolvconf', "/etc/resolv.conf", |
637 | \&read_etc_resolv_conf, undef, | |
e143e9d8 DM |
638 | \&update_etc_resolv_conf); |
639 | ||
640 | sub read_etc_timezone { | |
641 | my ($filename, $fd) = @_; | |
642 | ||
643 | my $timezone = <$fd>; | |
644 | ||
645 | chomp $timezone; | |
646 | ||
647 | return $timezone; | |
648 | } | |
649 | ||
650 | sub write_etc_timezone { | |
651 | my ($filename, $fh, $timezone) = @_; | |
652 | ||
653 | my $tzinfo = "/usr/share/zoneinfo/$timezone"; | |
654 | ||
655 | raise_param_exc({ 'timezone' => "No such timezone" }) | |
656 | if (! -f $tzinfo); | |
657 | ||
658 | ($timezone) = $timezone =~ m/^(.*)$/; # untaint | |
659 | ||
660 | print $fh "$timezone\n"; | |
661 | ||
662 | unlink ("/etc/localtime"); | |
663 | symlink ("/usr/share/zoneinfo/$timezone", "/etc/localtime"); | |
664 | ||
665 | } | |
666 | ||
9bbc4e17 TL |
667 | register_file('timezone', "/etc/timezone", |
668 | \&read_etc_timezone, | |
e143e9d8 DM |
669 | \&write_etc_timezone); |
670 | ||
671 | sub read_active_workers { | |
672 | my ($filename, $fh) = @_; | |
673 | ||
674 | return [] if !$fh; | |
675 | ||
9bbc4e17 | 676 | my $res = []; |
e143e9d8 | 677 | while (defined (my $line = <$fh>)) { |
75ed54f1 | 678 | if ($line =~ m/^(\S+)\s(0|1)(\s([0-9A-Za-z]{8})(\s(\s*\S.*))?)?$/) { |
e143e9d8 DM |
679 | my $upid = $1; |
680 | my $saved = $2; | |
681 | my $endtime = $4; | |
682 | my $status = $6; | |
683 | if ((my $task = PVE::Tools::upid_decode($upid, 1))) { | |
684 | $task->{upid} = $upid; | |
685 | $task->{saved} = $saved; | |
686 | $task->{endtime} = hex($endtime) if $endtime; | |
687 | $task->{status} = $status if $status; | |
688 | push @$res, $task; | |
689 | } | |
690 | } else { | |
691 | warn "unable to parse line: $line"; | |
692 | } | |
693 | } | |
694 | ||
695 | return $res; | |
696 | ||
697 | } | |
698 | ||
699 | sub write_active_workers { | |
700 | my ($filename, $fh, $tasklist) = @_; | |
701 | ||
702 | my $raw = ''; | |
703 | foreach my $task (@$tasklist) { | |
704 | my $upid = $task->{upid}; | |
705 | my $saved = $task->{saved} ? 1 : 0; | |
706 | if ($task->{endtime}) { | |
707 | if ($task->{status}) { | |
e34c27f4 | 708 | $raw .= sprintf("%s %s %08X %s\n", $upid, $saved, $task->{endtime}, $task->{status}); |
e143e9d8 | 709 | } else { |
e34c27f4 | 710 | $raw .= sprintf("%s %s %08X\n", $upid, $saved, $task->{endtime}); |
e143e9d8 DM |
711 | } |
712 | } else { | |
713 | $raw .= "$upid $saved\n"; | |
714 | } | |
715 | } | |
716 | ||
717 | PVE::Tools::safe_print($filename, $fh, $raw) if $raw; | |
718 | } | |
719 | ||
9bbc4e17 | 720 | register_file('active', "/var/log/pve/tasks/active", |
e143e9d8 DM |
721 | \&read_active_workers, |
722 | \&write_active_workers); | |
723 | ||
724 | ||
c8e5d28e | 725 | our $bond_modes = { 'balance-rr' => 0, |
e143e9d8 DM |
726 | 'active-backup' => 1, |
727 | 'balance-xor' => 2, | |
728 | 'broadcast' => 3, | |
729 | '802.3ad' => 4, | |
730 | 'balance-tlb' => 5, | |
731 | 'balance-alb' => 6, | |
732 | }; | |
733 | ||
9b7b13a1 DM |
734 | my $ovs_bond_modes = { |
735 | 'active-backup' => 1, | |
9b7b13a1 | 736 | 'balance-slb' => 1, |
a68760b6 | 737 | 'lacp-balance-slb' => 1, |
9bbc4e17 | 738 | 'lacp-balance-tcp' => 1, |
9b7b13a1 DM |
739 | }; |
740 | ||
e143e9d8 DM |
741 | #sub get_bond_modes { |
742 | # return $bond_modes; | |
743 | #} | |
744 | ||
9b7b13a1 DM |
745 | my $parse_ovs_option = sub { |
746 | my ($data) = @_; | |
747 | ||
748 | my $opts = {}; | |
749 | foreach my $kv (split (/\s+/, $data || '')) { | |
750 | my ($k, $v) = split('=', $kv, 2); | |
751 | $opts->{$k} = $v if $k && $v; | |
752 | } | |
753 | return $opts; | |
754 | }; | |
755 | ||
756 | my $set_ovs_option = sub { | |
757 | my ($d, %params) = @_; | |
758 | ||
759 | my $opts = &$parse_ovs_option($d->{ovs_options}); | |
760 | ||
761 | foreach my $k (keys %params) { | |
762 | my $v = $params{$k}; | |
763 | if ($v) { | |
764 | $opts->{$k} = $v; | |
765 | } else { | |
766 | delete $opts->{$k}; | |
767 | } | |
768 | } | |
769 | ||
770 | my $res = []; | |
771 | foreach my $k (keys %$opts) { | |
772 | push @$res, "$k=$opts->{$k}"; | |
773 | } | |
774 | ||
775 | if (my $new = join(' ', @$res)) { | |
776 | $d->{ovs_options} = $new; | |
777 | return $d->{ovs_options}; | |
778 | } else { | |
779 | delete $d->{ovs_options}; | |
780 | return undef; | |
781 | } | |
782 | }; | |
783 | ||
784 | my $extract_ovs_option = sub { | |
785 | my ($d, $name) = @_; | |
786 | ||
787 | my $opts = &$parse_ovs_option($d->{ovs_options}); | |
788 | ||
789 | my $v = delete $opts->{$name}; | |
790 | ||
791 | my $res = []; | |
792 | foreach my $k (keys %$opts) { | |
793 | push @$res, "$k=$opts->{$k}"; | |
794 | } | |
795 | ||
796 | if (my $new = join(' ', @$res)) { | |
797 | $d->{ovs_options} = $new; | |
798 | } else { | |
799 | delete $d->{ovs_options}; | |
800 | } | |
801 | ||
802 | return $v; | |
803 | }; | |
804 | ||
9b053d70 | 805 | my $check_mtu = sub { |
cebd1c85 | 806 | my ($ifaces, $parent, $child) = @_; |
9b053d70 | 807 | |
cebd1c85 WB |
808 | die "check mtu - missing parent interface\n" if !$parent; |
809 | die "check mtu - missing child interface\n" if !$child; | |
9b053d70 | 810 | |
000e32bc AD |
811 | my $cmtu = $ifaces->{$child}->{mtu}; |
812 | return if !$cmtu; | |
9b053d70 | 813 | |
000e32bc AD |
814 | my $parentdata = $ifaces->{$parent}; |
815 | my $pmtu = $parentdata->{mtu}; | |
816 | $pmtu = $cmtu if $parentdata->{type} eq 'bond' && !$pmtu; | |
817 | $pmtu = 1500 if !$pmtu; | |
818 | ||
819 | die "interface '$parent' - mtu $pmtu is lower than '$child' - mtu $cmtu\n" | |
820 | if $pmtu < $cmtu; | |
9b053d70 AD |
821 | }; |
822 | ||
48ab17b3 WB |
823 | # config => { |
824 | # ifaces => { | |
825 | # $ifname => { | |
826 | # <optional> exists => BOOL, | |
827 | # <optional> active => BOOL, | |
828 | # <optional> autostart => BOOL, | |
829 | # <auto> priority => INT, | |
830 | # | |
831 | # type => "eth" | "bridge" | "bond" | "loopback" | "OVS*" | ... , | |
832 | # | |
833 | # families => ["inet", "inet6", ...], | |
834 | # | |
835 | # method => "manual" | "static" | "dhcp" | ... , | |
836 | # address => IP, | |
837 | # netmask => SUBNET, | |
838 | # broadcast => IP, | |
839 | # gateway => IP, | |
840 | # comments => [ "..." ], | |
841 | # | |
842 | # method6 => "manual" | "static" | "dhcp" | ... , | |
843 | # address6 => IP, | |
844 | # netmask6 => SUBNET, | |
845 | # gateway6 => IP, | |
846 | # comments6 => [ "..." ], | |
847 | # | |
848 | # <known options>, # like bridge_ports, ovs_* | |
849 | # | |
850 | # # extra/unknown options stored by-family: | |
851 | # options => { <inet options>... } | |
852 | # options6 => { <inet6 options>... } | |
853 | # } | |
854 | # }, | |
855 | # options => [ | |
856 | # # mappings end up here as well, as we don't need to understand them | |
857 | # [priority,line] | |
858 | # ] | |
859 | # } | |
e143e9d8 DM |
860 | sub read_etc_network_interfaces { |
861 | my ($filename, $fh) = @_; | |
78325766 | 862 | my $proc_net_dev = IO::File->new('/proc/net/dev', 'r'); |
f0d1b04f | 863 | my $active = PVE::ProcFSTools::get_active_network_interfaces(); |
12a235d6 | 864 | return __read_etc_network_interfaces($fh, $proc_net_dev, $active); |
78325766 WB |
865 | } |
866 | ||
867 | sub __read_etc_network_interfaces { | |
12a235d6 | 868 | my ($fh, $proc_net_dev, $active_ifaces) = @_; |
e143e9d8 | 869 | |
48ab17b3 WB |
870 | my $config = {}; |
871 | my $ifaces = $config->{ifaces} = {}; | |
872 | my $options = $config->{options} = []; | |
e143e9d8 | 873 | |
1b1fb9f7 AD |
874 | my $options_alternatives = { |
875 | 'bond-slaves' => 'slaves', | |
876 | 'bond_slaves' => 'slaves', | |
877 | 'bond-xmit-hash-policy' => 'bond_xmit_hash_policy', | |
878 | 'bond-mode' => 'bond_mode', | |
879 | 'bond-miimon' =>'bond_miimon', | |
880 | 'bridge-vlan-aware' => 'bridge_vlan_aware', | |
881 | 'bridge-fd' => 'bridge_fd', | |
882 | 'bridge-stp' => 'bridge_stp', | |
95d0e633 AD |
883 | 'bridge-ports' => 'bridge_ports', |
884 | 'bridge-vids' => 'bridge_vids' | |
1b1fb9f7 AD |
885 | }; |
886 | ||
e143e9d8 DM |
887 | my $line; |
888 | ||
78325766 WB |
889 | if ($proc_net_dev) { |
890 | while (defined ($line = <$proc_net_dev>)) { | |
3dabe28a | 891 | if ($line =~ m/^\s*($PVE::Network::PHYSICAL_NIC_RE):.*/) { |
e143e9d8 DM |
892 | $ifaces->{$1}->{exists} = 1; |
893 | } | |
894 | } | |
78325766 | 895 | close($proc_net_dev); |
e143e9d8 DM |
896 | } |
897 | ||
c9dc8645 | 898 | # we try to keep order inside the file |
9bbc4e17 | 899 | my $priority = 2; # 1 is reserved for lo |
c9dc8645 | 900 | |
b1d7951f | 901 | SECTION: while (defined ($line = <$fh>)) { |
e143e9d8 | 902 | chomp ($line); |
b1d7951f | 903 | next if $line =~ m/^\s*#/; |
c86cfb8b | 904 | next if $line =~ m/^\s*(allow-hotplug)\s+(.*)$/; |
9bbc4e17 | 905 | |
f48815f8 AD |
906 | if ($line =~ m/^\s*(auto|allow-ovs)\s+(.*)$/) { |
907 | my @aa = split (/\s+/, $2); | |
e143e9d8 DM |
908 | |
909 | foreach my $a (@aa) { | |
910 | $ifaces->{$a}->{autostart} = 1; | |
911 | } | |
912 | ||
48ab17b3 | 913 | } elsif ($line =~ m/^\s*iface\s+(\S+)\s+(inet6?)\s+(\S+)\s*$/) { |
e143e9d8 | 914 | my $i = $1; |
48ab17b3 WB |
915 | my $family = $2; |
916 | my $f = { method => $3 }; # by family, merged to $d with a $suffix | |
917 | (my $suffix = $family) =~ s/^inet//; | |
e143e9d8 | 918 | |
cb07ff78 | 919 | my $d = $ifaces->{$i} ||= {}; |
48ab17b3 WB |
920 | $d->{priority} = $priority++ if !$d->{priority}; |
921 | push @{$d->{families}}, $family; | |
922 | ||
72ca6520 | 923 | while (defined ($line = <$fh>)) { |
b1d7951f WB |
924 | chomp $line; |
925 | if ($line =~ m/^\s*#(.*?)\s*$/) { | |
48ab17b3 | 926 | $f->{comments} = '' if !$f->{comments}; |
1b505ae2 DC |
927 | my $comment = decode('UTF-8', $1); |
928 | $f->{comments} .= "$comment\n"; | |
b1d7951f WB |
929 | } elsif ($line =~ m/^\s*(?:iface\s |
930 | |mapping\s | |
931 | |auto\s | |
932 | |allow- | |
933 | |source\s | |
934 | |source-directory\s | |
935 | )/x) { | |
936 | last; | |
937 | } elsif ($line =~ m/^\s*((\S+)\s+(.+))$/) { | |
72ca6520 DM |
938 | my $option = $1; |
939 | my ($id, $value) = ($2, $3); | |
1b1fb9f7 AD |
940 | |
941 | $id = $options_alternatives->{$id} if $options_alternatives->{$id}; | |
942 | ||
9a052564 | 943 | my $simple_options = { |
9b053d70 | 944 | 'mtu' => 1, |
9a052564 AD |
945 | 'ovs_type' => 1, |
946 | 'ovs_options' => 1, | |
947 | 'ovs_bridge' => 1, | |
948 | 'ovs_bonds' => 1, | |
949 | 'ovs_ports' => 1, | |
950 | 'bridge_fd' => 1, | |
951 | 'bridge_vids' => 1, | |
952 | 'bridge-access' => 1, | |
953 | 'bridge-learning' => 1, | |
954 | 'bridge-arp-nd-suppress' => 1, | |
955 | 'bridge-unicast-flood' => 1, | |
956 | 'bridge-multicast-flood' => 1, | |
957 | 'bond_miimon' => 1, | |
958 | 'bond_xmit_hash_policy' => 1, | |
d949babe | 959 | 'uplink-id' => 1, |
8f5d56bf | 960 | 'vlan-protocol' => 1, |
9a052564 AD |
961 | 'vxlan-id' => 1, |
962 | 'vxlan-svcnodeip' => 1, | |
963 | 'vxlan-physdev' => 1, | |
964 | 'vxlan-local-tunnelip' => 1 }; | |
965 | ||
0541ec90 | 966 | if (($id eq 'address') || ($id eq 'netmask') || ($id eq 'broadcast') || ($id eq 'gateway')) { |
48ab17b3 | 967 | $f->{$id} = $value; |
9a052564 | 968 | } elsif ($simple_options->{$id}) { |
9b7b13a1 | 969 | $d->{$id} = $value; |
72ca6520 DM |
970 | } elsif ($id eq 'slaves' || $id eq 'bridge_ports') { |
971 | my $devs = {}; | |
972 | foreach my $p (split (/\s+/, $value)) { | |
973 | next if $p eq 'none'; | |
974 | $devs->{$p} = 1; | |
e143e9d8 | 975 | } |
72ca6520 | 976 | my $str = join (' ', sort keys %{$devs}); |
48ab17b3 WB |
977 | if ($d->{$id}) { |
978 | $d->{$id} .= ' ' . $str if $str; | |
979 | } else { | |
980 | $d->{$id} = $str || ''; | |
981 | } | |
72ca6520 DM |
982 | } elsif ($id eq 'bridge_stp') { |
983 | if ($value =~ m/^\s*(on|yes)\s*$/i) { | |
984 | $d->{$id} = 'on'; | |
985 | } else { | |
986 | $d->{$id} = 'off'; | |
987 | } | |
685a2528 AD |
988 | } elsif ($id eq 'bridge_vlan_aware') { |
989 | $d->{$id} = 1; | |
72ca6520 DM |
990 | } elsif ($id eq 'bond_mode') { |
991 | # always use names | |
992 | foreach my $bm (keys %$bond_modes) { | |
993 | my $id = $bond_modes->{$bm}; | |
994 | if ($id eq $value) { | |
995 | $value = $bm; | |
996 | last; | |
997 | } | |
998 | } | |
999 | $d->{$id} = $value; | |
95aa8788 AD |
1000 | } elsif ($id eq 'vxlan-remoteip') { |
1001 | push @{$d->{$id}}, $value; | |
72ca6520 | 1002 | } else { |
48ab17b3 | 1003 | push @{$f->{options}}, $option; |
e143e9d8 | 1004 | } |
e143e9d8 | 1005 | } else { |
72ca6520 | 1006 | last; |
e143e9d8 DM |
1007 | } |
1008 | } | |
48ab17b3 | 1009 | $d->{"$_$suffix"} = $f->{$_} foreach (keys %$f); |
b1d7951f WB |
1010 | last SECTION if !defined($line); |
1011 | redo SECTION; | |
48ab17b3 WB |
1012 | } elsif ($line =~ /\w/) { |
1013 | push @$options, [$priority++, $line]; | |
e143e9d8 DM |
1014 | } |
1015 | } | |
1016 | ||
12a235d6 WB |
1017 | foreach my $ifname (@$active_ifaces) { |
1018 | if (my $iface = $ifaces->{$ifname}) { | |
1019 | $iface->{active} = 1; | |
1020 | } | |
1021 | } | |
e143e9d8 DM |
1022 | |
1023 | if (!$ifaces->{lo}) { | |
c9dc8645 | 1024 | $ifaces->{lo}->{priority} = 1; |
e143e9d8 DM |
1025 | $ifaces->{lo}->{method} = 'loopback'; |
1026 | $ifaces->{lo}->{type} = 'loopback'; | |
1027 | $ifaces->{lo}->{autostart} = 1; | |
1028 | } | |
1029 | ||
1030 | foreach my $iface (keys %$ifaces) { | |
1031 | my $d = $ifaces->{$iface}; | |
1032 | if ($iface =~ m/^bond\d+$/) { | |
9b7b13a1 DM |
1033 | if (!$d->{ovs_type}) { |
1034 | $d->{type} = 'bond'; | |
1035 | } elsif ($d->{ovs_type} eq 'OVSBond') { | |
1036 | $d->{type} = $d->{ovs_type}; | |
1037 | # translate: ovs_options => bond_mode | |
1038 | $d->{'bond_mode'} = &$extract_ovs_option($d, 'bond_mode'); | |
a68760b6 DM |
1039 | my $lacp = &$extract_ovs_option($d, 'lacp'); |
1040 | if ($lacp && $lacp eq 'active') { | |
1041 | if ($d->{'bond_mode'} eq 'balance-slb') { | |
1042 | $d->{'bond_mode'} = 'lacp-balance-slb'; | |
1043 | } | |
1044 | } | |
1045 | # Note: balance-tcp needs lacp | |
1046 | if ($d->{'bond_mode'} eq 'balance-tcp') { | |
1047 | $d->{'bond_mode'} = 'lacp-balance-tcp'; | |
1048 | } | |
2a751938 DM |
1049 | my $tag = &$extract_ovs_option($d, 'tag'); |
1050 | $d->{ovs_tag} = $tag if defined($tag); | |
9b7b13a1 DM |
1051 | } else { |
1052 | $d->{type} = 'unknown'; | |
e143e9d8 | 1053 | } |
9b7b13a1 DM |
1054 | } elsif ($iface =~ m/^vmbr\d+$/) { |
1055 | if (!$d->{ovs_type}) { | |
1056 | $d->{type} = 'bridge'; | |
1057 | ||
1058 | if (!defined ($d->{bridge_fd})) { | |
1059 | $d->{bridge_fd} = 0; | |
1060 | } | |
1061 | if (!defined ($d->{bridge_stp})) { | |
1062 | $d->{bridge_stp} = 'off'; | |
1063 | } | |
1064 | } elsif ($d->{ovs_type} eq 'OVSBridge') { | |
1065 | $d->{type} = $d->{ovs_type}; | |
1066 | } else { | |
1067 | $d->{type} = 'unknown'; | |
e143e9d8 DM |
1068 | } |
1069 | } elsif ($iface =~ m/^(\S+):\d+$/) { | |
1070 | $d->{type} = 'alias'; | |
1071 | if (defined ($ifaces->{$1})) { | |
1072 | $d->{exists} = $ifaces->{$1}->{exists}; | |
1073 | } else { | |
1074 | $ifaces->{$1}->{exists} = 0; | |
1075 | $d->{exists} = 0; | |
1076 | } | |
9c808945 AD |
1077 | } elsif ($iface =~ m/^(\S+)\.\d+$/) { |
1078 | $d->{type} = 'vlan'; | |
1079 | if (defined ($ifaces->{$1})) { | |
1080 | $d->{exists} = $ifaces->{$1}->{exists}; | |
1081 | } else { | |
1082 | $ifaces->{$1}->{exists} = 0; | |
1083 | $d->{exists} = 0; | |
1084 | } | |
3dabe28a | 1085 | } elsif ($iface =~ m/^$PVE::Network::PHYSICAL_NIC_RE$/) { |
9b7b13a1 DM |
1086 | if (!$d->{ovs_type}) { |
1087 | $d->{type} = 'eth'; | |
1088 | } elsif ($d->{ovs_type} eq 'OVSPort') { | |
1089 | $d->{type} = $d->{ovs_type}; | |
2a751938 DM |
1090 | my $tag = &$extract_ovs_option($d, 'tag'); |
1091 | $d->{ovs_tag} = $tag if defined($tag); | |
9b7b13a1 DM |
1092 | } else { |
1093 | $d->{type} = 'unknown'; | |
1094 | } | |
e143e9d8 DM |
1095 | } elsif ($iface =~ m/^lo$/) { |
1096 | $d->{type} = 'loopback'; | |
1097 | } else { | |
95aa8788 AD |
1098 | if ($d->{'vxlan-id'}) { |
1099 | $d->{type} = 'vxlan'; | |
1100 | } elsif (!$d->{ovs_type}) { | |
9b7b13a1 DM |
1101 | $d->{type} = 'unknown'; |
1102 | } elsif ($d->{ovs_type} eq 'OVSIntPort') { | |
1103 | $d->{type} = $d->{ovs_type}; | |
2a751938 DM |
1104 | my $tag = &$extract_ovs_option($d, 'tag'); |
1105 | $d->{ovs_tag} = $tag if defined($tag); | |
9b7b13a1 | 1106 | } |
e143e9d8 DM |
1107 | } |
1108 | ||
2896245e DC |
1109 | # map address and netmask to cidr |
1110 | if ($d->{address}) { | |
4515aaec | 1111 | if ($d->{netmask} && $d->{netmask} =~ m/^\d+$/) { # e.g. netmask 20 |
2896245e DC |
1112 | $d->{cidr} = $d->{address} . "/" . $d->{netmask}; |
1113 | } elsif ($d->{netmask} && | |
1114 | (my $cidr = PVE::JSONSchema::get_netmask_bits($d->{netmask}))) { # e.g. netmask 255.255.255.0 | |
1115 | $d->{cidr} = $d->{address} . "/" . $cidr; | |
1116 | } elsif ($d->{address} =~ m!^(.*)/(\d+)$!) { | |
1117 | $d->{cidr} = $d->{address}; | |
1118 | $d->{address} = $1; | |
1119 | $d->{netmask} = $2; | |
1120 | } else { | |
1121 | $d->{cidr} = $d->{address}; | |
1122 | } | |
1123 | } | |
1124 | ||
1125 | # map address6 and netmask6 to cidr6 | |
1126 | if ($d->{address6}) { | |
1127 | $d->{cidr6} = $d->{address6}; | |
1128 | if ($d->{netmask6}) { | |
b6103858 | 1129 | $d->{cidr6} .= "/" . $d->{netmask6}; |
2896245e | 1130 | } elsif ($d->{address6} =~ m!^(.*)/(\d+)$!) { |
2896245e DC |
1131 | $d->{address6} = $1; |
1132 | $d->{netmask6} = $2; | |
1133 | } | |
1134 | } | |
1135 | ||
e143e9d8 | 1136 | $d->{method} = 'manual' if !$d->{method}; |
48ab17b3 | 1137 | $d->{method6} = 'manual' if !$d->{method6}; |
78325766 WB |
1138 | |
1139 | $d->{families} ||= ['inet']; | |
e143e9d8 DM |
1140 | } |
1141 | ||
516a50a4 WB |
1142 | # OVS bridges create "allow-$BRIDGE $IFACE" lines which we need to remove |
1143 | # from the {options} hash for them to be removed correctly. | |
1144 | @$options = grep {defined($_)} map { | |
1145 | my ($pri, $line) = @$_; | |
4ac94c72 AD |
1146 | if ($line =~ /^allow-ovs\s+(.*)$/) { |
1147 | undef; | |
1148 | } elsif ($line =~ /^allow-(\S+)\s+(.*)$/) { | |
516a50a4 WB |
1149 | my $bridge = $1; |
1150 | my @ports = split(/\s+/, $2); | |
1151 | if (defined(my $br = $ifaces->{$bridge})) { | |
1152 | # if this port is part of a bridge, remove it | |
1153 | my %in_ovs_ports = map {$_=>1} split(/\s+/, $br->{ovs_ports}); | |
1154 | @ports = grep { not $in_ovs_ports{$_} } @ports; | |
1155 | } | |
1156 | # create the allow line for the remaining ports, or delete if empty | |
1157 | if (@ports) { | |
1158 | [$pri, "allow-$bridge " . join(' ', @ports)]; | |
1159 | } else { | |
1160 | undef; | |
1161 | } | |
1162 | } else { | |
1163 | # don't modify other lines | |
1164 | $_; | |
1165 | } | |
1166 | } @$options; | |
1167 | ||
48ab17b3 | 1168 | return $config; |
e143e9d8 DM |
1169 | } |
1170 | ||
1171 | sub __interface_to_string { | |
b7c4f378 | 1172 | my ($iface, $d, $family, $first_block, $ifupdown2) = @_; |
48ab17b3 WB |
1173 | |
1174 | (my $suffix = $family) =~ s/^inet//; | |
e143e9d8 | 1175 | |
48ab17b3 | 1176 | return '' if !($d && $d->{"method$suffix"}); |
e143e9d8 DM |
1177 | |
1178 | my $raw = ''; | |
1179 | ||
48ab17b3 WB |
1180 | $raw .= "iface $iface $family " . $d->{"method$suffix"} . "\n"; |
1181 | $raw .= "\taddress " . $d->{"address$suffix"} . "\n" if $d->{"address$suffix"}; | |
1182 | $raw .= "\tnetmask " . $d->{"netmask$suffix"} . "\n" if $d->{"netmask$suffix"}; | |
1183 | $raw .= "\tgateway " . $d->{"gateway$suffix"} . "\n" if $d->{"gateway$suffix"}; | |
1184 | $raw .= "\tbroadcast " . $d->{"broadcast$suffix"} . "\n" if $d->{"broadcast$suffix"}; | |
e143e9d8 | 1185 | |
9b7b13a1 DM |
1186 | my $done = { type => 1, priority => 1, method => 1, active => 1, exists => 1, |
1187 | comments => 1, autostart => 1, options => 1, | |
48ab17b3 WB |
1188 | address => 1, netmask => 1, gateway => 1, broadcast => 1, |
1189 | method6 => 1, families => 1, options6 => 1, | |
d949babe | 1190 | address6 => 1, netmask6 => 1, gateway6 => 1, broadcast6 => 1, 'uplink-id' => 1 }; |
48ab17b3 WB |
1191 | |
1192 | if (!$first_block) { | |
1193 | # not printing out options | |
1194 | } elsif ($d->{type} eq 'bridge') { | |
9b7b13a1 | 1195 | |
fb3a6db1 | 1196 | $d->{bridge_ports} =~ s/[;,\s]+/ /g; |
e143e9d8 | 1197 | my $ports = $d->{bridge_ports} || 'none'; |
1accc6da | 1198 | $raw .= "\tbridge-ports $ports\n"; |
9b7b13a1 | 1199 | $done->{bridge_ports} = 1; |
e143e9d8 | 1200 | |
9b7b13a1 | 1201 | my $v = defined($d->{bridge_stp}) ? $d->{bridge_stp} : 'off'; |
1accc6da | 1202 | $raw .= "\tbridge-stp $v\n"; |
9b7b13a1 | 1203 | $done->{bridge_stp} = 1; |
e143e9d8 | 1204 | |
9b7b13a1 | 1205 | $v = defined($d->{bridge_fd}) ? $d->{bridge_fd} : 0; |
1accc6da | 1206 | $raw .= "\tbridge-fd $v\n"; |
9b7b13a1 | 1207 | $done->{bridge_fd} = 1; |
685a2528 AD |
1208 | |
1209 | if( defined($d->{bridge_vlan_aware})) { | |
1accc6da | 1210 | $raw .= "\tbridge-vlan-aware yes\n"; |
95d0e633 AD |
1211 | $v = defined($d->{bridge_vids}) ? $d->{bridge_vids} : "2-4094"; |
1212 | $raw .= "\tbridge-vids $v\n"; | |
685a2528 AD |
1213 | } |
1214 | $done->{bridge_vlan_aware} = 1; | |
f6c4a563 | 1215 | $done->{bridge_vids} = 1; |
9bbc4e17 | 1216 | |
9b7b13a1 | 1217 | } elsif ($d->{type} eq 'bond') { |
e143e9d8 | 1218 | |
fb3a6db1 | 1219 | $d->{slaves} =~ s/[;,\s]+/ /g; |
e143e9d8 | 1220 | my $slaves = $d->{slaves} || 'none'; |
1accc6da | 1221 | $raw .= "\tbond-slaves $slaves\n"; |
9b7b13a1 | 1222 | $done->{slaves} = 1; |
e143e9d8 | 1223 | |
9b7b13a1 | 1224 | my $v = defined ($d->{'bond_miimon'}) ? $d->{'bond_miimon'} : 100; |
1accc6da | 1225 | $raw .= "\tbond-miimon $v\n"; |
9b7b13a1 | 1226 | $done->{'bond_miimon'} = 1; |
e143e9d8 | 1227 | |
9b7b13a1 | 1228 | $v = defined ($d->{'bond_mode'}) ? $d->{'bond_mode'} : 'balance-rr'; |
1accc6da | 1229 | $raw .= "\tbond-mode $v\n"; |
9b7b13a1 DM |
1230 | $done->{'bond_mode'} = 1; |
1231 | ||
40fae2b3 DM |
1232 | if ($d->{'bond_mode'} && $d->{'bond_xmit_hash_policy'} && |
1233 | ($d->{'bond_mode'} eq 'balance-xor' || $d->{'bond_mode'} eq '802.3ad')) { | |
1accc6da | 1234 | $raw .= "\tbond-xmit-hash-policy $d->{'bond_xmit_hash_policy'}\n"; |
40fae2b3 DM |
1235 | } |
1236 | $done->{'bond_xmit_hash_policy'} = 1; | |
8f5d56bf | 1237 | } elsif ($d->{type} eq 'vlan') { |
9bbc4e17 | 1238 | die "$iface: wrong vlan-protocol $d->{'vlan-protocol'}\n" |
8f5d56bf | 1239 | if $d->{'vlan-protocol'} && $d->{'vlan-protocol'} ne '802.1ad' && $d->{'vlan-protocol'} ne '802.1q'; |
9bbc4e17 | 1240 | |
95aa8788 AD |
1241 | } elsif ($d->{type} eq 'vxlan') { |
1242 | ||
94d786b3 WB |
1243 | foreach my $k (qw(vxlan-id vxlan-svcnodeip vxlan-physdev vxlan-local-tunnelip)) { |
1244 | $raw .= "\t$k $d->{$k}\n" if defined $d->{$k}; | |
95aa8788 | 1245 | $done->{$k} = 1; |
94d786b3 | 1246 | } |
95aa8788 AD |
1247 | |
1248 | if ($d->{'vxlan-remoteip'}) { | |
1249 | foreach my $remoteip (@{$d->{'vxlan-remoteip'}}) { | |
1250 | $raw .= "\tvxlan-remoteip $remoteip\n"; | |
1251 | } | |
1252 | $done->{'vxlan-remoteip'} = 1; | |
1253 | } | |
9b7b13a1 DM |
1254 | } elsif ($d->{type} eq 'OVSBridge') { |
1255 | ||
1256 | $raw .= "\tovs_type $d->{type}\n"; | |
1257 | $done->{ovs_type} = 1; | |
1258 | ||
1259 | $raw .= "\tovs_ports $d->{ovs_ports}\n" if $d->{ovs_ports}; | |
4ac94c72 | 1260 | |
9b7b13a1 | 1261 | $done->{ovs_ports} = 1; |
9b7b13a1 DM |
1262 | } elsif ($d->{type} eq 'OVSPort' || $d->{type} eq 'OVSIntPort' || |
1263 | $d->{type} eq 'OVSBond') { | |
1264 | ||
1265 | $d->{autostart} = 0; # started by the bridge | |
1266 | ||
2a751938 DM |
1267 | if (defined($d->{ovs_tag})) { |
1268 | &$set_ovs_option($d, tag => $d->{ovs_tag}); | |
1269 | } | |
1270 | $done->{ovs_tag} = 1; | |
1271 | ||
9b7b13a1 DM |
1272 | if ($d->{type} eq 'OVSBond') { |
1273 | ||
1274 | $d->{bond_mode} = 'active-backup' if !$d->{bond_mode}; | |
1275 | ||
1276 | $ovs_bond_modes->{$d->{bond_mode}} || | |
1277 | die "OVS does not support bond mode '$d->{bond_mode}\n"; | |
1278 | ||
a68760b6 DM |
1279 | if ($d->{bond_mode} eq 'lacp-balance-slb') { |
1280 | &$set_ovs_option($d, lacp => 'active'); | |
1281 | &$set_ovs_option($d, bond_mode => 'balance-slb'); | |
1282 | } elsif ($d->{bond_mode} eq 'lacp-balance-tcp') { | |
1283 | &$set_ovs_option($d, lacp => 'active'); | |
1284 | &$set_ovs_option($d, bond_mode => 'balance-tcp'); | |
1285 | } else { | |
1286 | &$set_ovs_option($d, lacp => undef); | |
1287 | &$set_ovs_option($d, bond_mode => $d->{bond_mode}); | |
1288 | } | |
9b7b13a1 DM |
1289 | $done->{bond_mode} = 1; |
1290 | ||
1291 | $raw .= "\tovs_bonds $d->{ovs_bonds}\n" if $d->{ovs_bonds}; | |
1292 | $done->{ovs_bonds} = 1; | |
1293 | } | |
1294 | ||
9b7b13a1 DM |
1295 | $raw .= "\tovs_type $d->{type}\n"; |
1296 | $done->{ovs_type} = 1; | |
1297 | ||
bd9cc42d TL |
1298 | if (my $bridge = $d->{ovs_bridge}) { |
1299 | $raw = "allow-$bridge $iface\n$raw"; | |
1300 | $raw .= "\tovs_bridge $bridge\n"; | |
9b7b13a1 DM |
1301 | $done->{ovs_bridge} = 1; |
1302 | } | |
9b7b13a1 DM |
1303 | } |
1304 | ||
48ab17b3 WB |
1305 | if ($first_block) { |
1306 | # print other settings | |
9a052564 | 1307 | foreach my $k (sort keys %$d) { |
48ab17b3 WB |
1308 | next if $done->{$k}; |
1309 | next if !$d->{$k}; | |
1310 | $raw .= "\t$k $d->{$k}\n"; | |
1311 | } | |
e143e9d8 DM |
1312 | } |
1313 | ||
48ab17b3 | 1314 | foreach my $option (@{$d->{"options$suffix"}}) { |
e143e9d8 DM |
1315 | $raw .= "\t$option\n"; |
1316 | } | |
1317 | ||
ed4e6e0c | 1318 | # add comments |
48ab17b3 | 1319 | my $comments = $d->{"comments$suffix"} || ''; |
ed4e6e0c DM |
1320 | foreach my $cl (split(/\n/, $comments)) { |
1321 | $raw .= "#$cl\n"; | |
72ca6520 DM |
1322 | } |
1323 | ||
e143e9d8 DM |
1324 | $raw .= "\n"; |
1325 | ||
1326 | return $raw; | |
1327 | } | |
1328 | ||
78325766 | 1329 | |
e143e9d8 | 1330 | sub write_etc_network_interfaces { |
48ab17b3 | 1331 | my ($filename, $fh, $config) = @_; |
b7c4f378 AD |
1332 | my $ifupdown2 = -e '/usr/share/ifupdown2'; |
1333 | my $raw = __write_etc_network_interfaces($config, $ifupdown2); | |
1b505ae2 | 1334 | PVE::Tools::safe_print($filename, $fh, encode('UTF-8', $raw)); |
78325766 WB |
1335 | } |
1336 | sub __write_etc_network_interfaces { | |
b7c4f378 | 1337 | my ($config, $ifupdown2) = @_; |
48ab17b3 WB |
1338 | |
1339 | my $ifaces = $config->{ifaces}; | |
1340 | my @options = @{$config->{options}}; | |
e143e9d8 | 1341 | |
9b7b13a1 DM |
1342 | my $used_ports = {}; |
1343 | ||
1344 | foreach my $iface (keys %$ifaces) { | |
1345 | my $d = $ifaces->{$iface}; | |
1346 | ||
2896245e DC |
1347 | delete $d->{cidr}; |
1348 | delete $d->{cidr6}; | |
1349 | ||
9b7b13a1 DM |
1350 | my $ports = ''; |
1351 | foreach my $k (qw(bridge_ports ovs_ports slaves ovs_bonds)) { | |
1352 | $ports .= " $d->{$k}" if $d->{$k}; | |
1353 | } | |
1354 | ||
1355 | foreach my $p (PVE::Tools::split_list($ports)) { | |
1356 | die "port '$p' is already used on interface '$used_ports->{$p}'\n" | |
1357 | if $used_ports->{$p} && $used_ports->{$p} ne $iface; | |
1358 | $used_ports->{$p} = $iface; | |
1359 | } | |
1360 | } | |
1361 | ||
1362 | # delete unused OVS ports | |
1363 | foreach my $iface (keys %$ifaces) { | |
1364 | my $d = $ifaces->{$iface}; | |
94d786b3 | 1365 | if ($d->{type} eq 'OVSPort' || $d->{type} eq 'OVSIntPort' || |
9b7b13a1 DM |
1366 | $d->{type} eq 'OVSBond') { |
1367 | my $brname = $used_ports->{$iface}; | |
94d786b3 | 1368 | if (!$brname || !$ifaces->{$brname}) { |
3dabe28a | 1369 | if ($iface =~ /^$PVE::Network::PHYSICAL_NIC_RE/) { |
516a50a4 WB |
1370 | $ifaces->{$iface} = { type => 'eth', |
1371 | exists => 1, | |
1372 | method => 'manual', | |
1373 | families => ['inet'] }; | |
1374 | } else { | |
1375 | delete $ifaces->{$iface}; | |
1376 | } | |
9b7b13a1 DM |
1377 | next; |
1378 | } | |
1379 | my $bd = $ifaces->{$brname}; | |
1380 | if ($bd->{type} ne 'OVSBridge') { | |
1381 | delete $ifaces->{$iface}; | |
1382 | next; | |
1383 | } | |
1384 | } | |
1385 | } | |
1386 | ||
1387 | # create OVS bridge ports | |
1388 | foreach my $iface (keys %$ifaces) { | |
1389 | my $d = $ifaces->{$iface}; | |
1390 | if ($d->{type} eq 'OVSBridge' && $d->{ovs_ports}) { | |
1391 | foreach my $p (split (/\s+/, $d->{ovs_ports})) { | |
1392 | my $n = $ifaces->{$p}; | |
1393 | die "OVS bridge '$iface' - unable to find port '$p'\n" | |
1394 | if !$n; | |
21d32c95 | 1395 | $n->{autostart} = 0; |
9b7b13a1 DM |
1396 | if ($n->{type} eq 'eth') { |
1397 | $n->{type} = 'OVSPort'; | |
94d786b3 | 1398 | $n->{ovs_bridge} = $iface; |
9b7b13a1 DM |
1399 | } elsif ($n->{type} eq 'OVSBond' || $n->{type} eq 'OVSPort' || |
1400 | $n->{type} eq 'OVSIntPort') { | |
1401 | $n->{ovs_bridge} = $iface; | |
1402 | } else { | |
1403 | die "interface '$p' is not defined as OVS port/bond\n"; | |
1404 | } | |
9b053d70 AD |
1405 | |
1406 | &$check_mtu($ifaces, $iface, $p); | |
9b7b13a1 DM |
1407 | } |
1408 | } | |
1409 | } | |
1410 | ||
1411 | # check OVS bond ports | |
1412 | foreach my $iface (keys %$ifaces) { | |
1413 | my $d = $ifaces->{$iface}; | |
1414 | if ($d->{type} eq 'OVSBond' && $d->{ovs_bonds}) { | |
1415 | foreach my $p (split (/\s+/, $d->{ovs_bonds})) { | |
1416 | my $n = $ifaces->{$p}; | |
1417 | die "OVS bond '$iface' - unable to find slave '$p'\n" | |
1418 | if !$n; | |
1419 | die "OVS bond '$iface' - wrong interface type on slave '$p' " . | |
1420 | "('$n->{type}' != 'eth')\n" if $n->{type} ne 'eth'; | |
9b053d70 | 1421 | &$check_mtu($ifaces, $iface, $p); |
9b7b13a1 DM |
1422 | } |
1423 | } | |
1424 | } | |
1425 | ||
0115696f AD |
1426 | # check bond |
1427 | foreach my $iface (keys %$ifaces) { | |
1428 | my $d = $ifaces->{$iface}; | |
1429 | if ($d->{type} eq 'bond' && $d->{slaves}) { | |
1430 | foreach my $p (split (/\s+/, $d->{slaves})) { | |
1431 | my $n = $ifaces->{$p}; | |
1432 | ||
1433 | die "bond '$iface' - unable to find slave '$p'\n" | |
1434 | if !$n; | |
1435 | die "bond '$iface' - wrong interface type on slave '$p' " . | |
1436 | "('$n->{type}' != 'eth')\n" if $n->{type} ne 'eth'; | |
9b053d70 | 1437 | &$check_mtu($ifaces, $iface, $p); |
0115696f AD |
1438 | } |
1439 | } | |
1440 | } | |
1441 | ||
95aa8788 AD |
1442 | # check vxlan |
1443 | my $vxlans = {}; | |
1444 | foreach my $iface (keys %$ifaces) { | |
1445 | my $d = $ifaces->{$iface}; | |
1446 | ||
1447 | if ($d->{type} eq 'vxlan' && $d->{'vxlan-id'}) { | |
1448 | my $vxlanid = $d->{'vxlan-id'}; | |
94d786b3 | 1449 | die "iface $iface - duplicate vxlan-id $vxlanid already used in $vxlans->{$vxlanid}\n" if $vxlans->{$vxlanid}; |
95aa8788 AD |
1450 | $vxlans->{$vxlanid} = $iface; |
1451 | } | |
1452 | ||
1453 | my $ips = 0; | |
1454 | ++$ips if defined $d->{'vxlan-svcnodeip'}; | |
1455 | ++$ips if defined $d->{'vxlan-remoteip'}; | |
1456 | ++$ips if defined $d->{'vxlan-local-tunnelip'}; | |
1457 | if ($ips > 1) { | |
94d786b3 | 1458 | die "iface $iface - vxlan-svcnodeip, vxlan-remoteip and vxlan-localtunnelip are mutually exclusive\n"; |
95aa8788 AD |
1459 | } |
1460 | ||
1461 | if (defined($d->{'vxlan-svcnodeip'}) != defined($d->{'vxlan-physdev'})) { | |
94d786b3 | 1462 | die "iface $iface - vxlan-svcnodeip and vxlan-physdev must be define together\n"; |
95aa8788 | 1463 | } |
9b053d70 | 1464 | #fixme : check if vxlan mtu is lower than 50bytes than physical interface where tunnel is going out |
95aa8788 AD |
1465 | } |
1466 | ||
c4e56470 AD |
1467 | # check vlan |
1468 | foreach my $iface (keys %$ifaces) { | |
1469 | my $d = $ifaces->{$iface}; | |
1470 | if ($d->{type} eq 'vlan' && $iface =~ m/^(\S+)\.\d+$/) { | |
1471 | my $p = $1; | |
1472 | my $n = $ifaces->{$p}; | |
1473 | ||
1474 | die "vlan '$iface' - unable to find parent '$p'\n" | |
fc158d0d | 1475 | if !$n; |
c4e56470 AD |
1476 | |
1477 | if ($n->{type} eq 'bridge' && !$n->{bridge_vlan_aware}) { | |
1478 | die "vlan '$iface' - bridge vlan aware is not enabled on parent '$p'\n"; | |
8f5d56bf | 1479 | } elsif ($n->{type} ne 'eth' && $n->{type} ne 'bridge' && $n->{type} ne 'bond' && $n->{type} ne 'vlan') { |
c4e56470 | 1480 | die "vlan '$iface' - wrong interface type on parent '$p' " . |
8f5d56bf | 1481 | "('$n->{type}' != 'eth|bond|bridge|vlan' )\n"; |
c4e56470 | 1482 | } |
8f5d56bf | 1483 | |
000e32bc | 1484 | &$check_mtu($ifaces, $p, $iface); |
8f5d56bf | 1485 | |
c4e56470 AD |
1486 | } |
1487 | } | |
1488 | ||
d949babe AD |
1489 | # check uplink |
1490 | my $uplinks = {}; | |
1491 | foreach my $iface (keys %$ifaces) { | |
1492 | my $d = $ifaces->{$iface}; | |
1493 | if (my $uplinkid = $d->{'uplink-id'}) { | |
1494 | die "iface '$iface' - uplink-id $uplinkid is only allowed on physical and linux bond interfaces\n" | |
9bbc4e17 | 1495 | if $d->{type} ne 'eth' && $d->{type} ne 'bond'; |
d949babe AD |
1496 | |
1497 | die "iface '$iface' - uplink-id $uplinkid is already assigned on '$uplinks->{$uplinkid}'\n" | |
1498 | if $uplinks->{$uplinkid}; | |
1499 | ||
1500 | $uplinks->{$uplinkid} = $iface; | |
1501 | } | |
1502 | } | |
1503 | ||
9a052564 AD |
1504 | # check bridgeport option |
1505 | my $bridgeports = {}; | |
1506 | my $bridges = {}; | |
1507 | foreach my $iface (keys %$ifaces) { | |
1508 | my $d = $ifaces->{$iface}; | |
1509 | if ($d->{type} eq 'bridge') { | |
1510 | foreach my $p (split (/\s+/, $d->{bridge_ports})) { | |
514c2138 | 1511 | $p =~ s/\.\d+$//; |
9a052564 AD |
1512 | my $n = $ifaces->{$p}; |
1513 | die "bridge '$iface' - unable to find bridge port '$p'\n" | |
1514 | if !$n; | |
9b053d70 | 1515 | &$check_mtu($ifaces, $iface, $p); |
9a052564 AD |
1516 | $bridgeports->{$p} = $iface; |
1517 | } | |
1518 | $bridges->{$iface} = $d; | |
1519 | } | |
1520 | } | |
1521 | ||
1522 | foreach my $iface (keys %$ifaces) { | |
1523 | my $d = $ifaces->{$iface}; | |
1524 | ||
1525 | foreach my $k (qw(bridge-learning bridge-arp-nd-suppress bridge-unicast-flood bridge-multicast-flood bridge-access)) { | |
94d786b3 | 1526 | die "iface $iface - $k: bridge port specific options can be used only on interfaces attached to a bridge\n" |
9a052564 AD |
1527 | if $d->{$k} && !$bridgeports->{$iface}; |
1528 | } | |
1529 | ||
1530 | if ($d->{'bridge-access'} && !$bridges->{$bridgeports->{$iface}}->{bridge_vlan_aware}) { | |
94d786b3 | 1531 | die "iface $iface - bridge-access option can be only used if interface is in a vlan aware bridge\n"; |
9a052564 AD |
1532 | } |
1533 | } | |
1534 | ||
9d27ef41 WB |
1535 | my $raw = <<'NETWORKDOC'; |
1536 | # network interface settings; autogenerated | |
1537 | # Please do NOT modify this file directly, unless you know what | |
1538 | # you're doing. | |
1539 | # | |
b3e3b51b | 1540 | # If you want to manage parts of the network configuration manually, |
9d27ef41 WB |
1541 | # please utilize the 'source' or 'source-directory' directives to do |
1542 | # so. | |
b3e3b51b | 1543 | # PVE will preserve these directives, but will NOT read its network |
9d27ef41 WB |
1544 | # configuration from sourced files, so do not attempt to move any of |
1545 | # the PVE managed interfaces into external files! | |
1546 | ||
1547 | NETWORKDOC | |
e143e9d8 DM |
1548 | |
1549 | my $printed = {}; | |
1550 | ||
ed4e6e0c | 1551 | my $if_type_hash = { |
562fad0b WB |
1552 | loopback => 100000, |
1553 | eth => 200000, | |
51eec390 AD |
1554 | OVSPort => 200000, |
1555 | OVSIntPort => 200000, | |
562fad0b WB |
1556 | bond => 300000, |
1557 | bridge => 400000, | |
51eec390 | 1558 | OVSBridge => 400000, |
95aa8788 | 1559 | vxlan => 500000, |
9b7b13a1 | 1560 | }; |
ed4e6e0c DM |
1561 | |
1562 | my $lookup_type_prio = sub { | |
51eec390 | 1563 | my ($iface, $ifaces) = @_; |
ed4e6e0c | 1564 | |
0c0bcf7c WB |
1565 | my ($rootiface, @rest) = split(/[.:]/, $iface); |
1566 | my $childlevel = scalar(@rest); | |
1567 | my $n = $ifaces->{$rootiface}; | |
ed4e6e0c | 1568 | |
0c0bcf7c | 1569 | my $pri = $if_type_hash->{$n->{type}} + $childlevel |
51eec390 AD |
1570 | if $n->{type} && $n->{type} ne 'unknown'; |
1571 | ||
7a822100 | 1572 | return $pri; |
ed4e6e0c DM |
1573 | }; |
1574 | ||
c9dc8645 DM |
1575 | foreach my $iface (sort { |
1576 | my $ref1 = $ifaces->{$a}; | |
1577 | my $ref2 = $ifaces->{$b}; | |
51eec390 AD |
1578 | my $tp1 = &$lookup_type_prio($a, $ifaces); |
1579 | my $tp2 = &$lookup_type_prio($b, $ifaces); | |
e143e9d8 | 1580 | |
7a822100 WB |
1581 | # Only recognized types are in relation to each other. If one type |
1582 | # is unknown then only consider the interfaces' priority attributes. | |
1583 | $tp1 = $tp2 = 0 if !defined($tp1) || !defined($tp2); | |
1584 | ||
1585 | my $p1 = $tp1 + ($ref1->{priority} // 50000); | |
1586 | my $p2 = $tp2 + ($ref2->{priority} // 50000); | |
ed4e6e0c DM |
1587 | |
1588 | return $p1 <=> $p2 if $p1 != $p2; | |
c9dc8645 DM |
1589 | |
1590 | return $a cmp $b; | |
11e7facb | 1591 | } keys %$ifaces) { |
c9dc8645 | 1592 | next if $printed->{$iface}; |
11e7facb | 1593 | my $d = $ifaces->{$iface}; |
562fad0b WB |
1594 | my $pri = $d->{priority} // 0; |
1595 | if (@options && $options[0]->[0] < $pri) { | |
48ab17b3 WB |
1596 | do { |
1597 | $raw .= (shift @options)->[1] . "\n"; | |
562fad0b | 1598 | } while (@options && $options[0]->[0] < $pri); |
48ab17b3 WB |
1599 | $raw .= "\n"; |
1600 | } | |
1601 | ||
c9dc8645 | 1602 | $printed->{$iface} = 1; |
f48815f8 AD |
1603 | if ($d->{autostart}) { |
1604 | if ($d->{type} eq 'OVSBridge') { | |
1605 | # cannot use 'auto' for OVS, would add race with systemd ifup@.service | |
1606 | $raw .= "allow-ovs $iface\n"; | |
1607 | } else { | |
1608 | $raw .= "auto $iface\n"; | |
1609 | } | |
4ac94c72 AD |
1610 | } |
1611 | ||
48ab17b3 | 1612 | my $i = 0; # some options should be printed only once |
b7c4f378 | 1613 | $raw .= __interface_to_string($iface, $d, $_, !$i++, $ifupdown2) foreach @{$d->{families}}; |
e143e9d8 | 1614 | } |
48ab17b3 WB |
1615 | |
1616 | $raw .= $_->[1] . "\n" foreach @options; | |
78325766 | 1617 | return $raw; |
e143e9d8 DM |
1618 | } |
1619 | ||
1620 | register_file('interfaces', "/etc/network/interfaces", | |
1621 | \&read_etc_network_interfaces, | |
1622 | \&write_etc_network_interfaces); | |
1623 | ||
ddd3d224 DM |
1624 | |
1625 | sub read_iscsi_initiatorname { | |
1626 | my ($filename, $fd) = @_; | |
1627 | ||
1628 | while (defined(my $line = <$fd>)) { | |
1629 | if ($line =~ m/^InitiatorName=(\S+)$/) { | |
1630 | return $1; | |
1631 | } | |
1632 | } | |
1633 | ||
1634 | return 'undefined'; | |
1635 | } | |
1636 | ||
9bbc4e17 | 1637 | register_file('initiatorname', "/etc/iscsi/initiatorname.iscsi", |
ddd3d224 DM |
1638 | \&read_iscsi_initiatorname); |
1639 | ||
6c3aef09 DM |
1640 | sub read_apt_auth { |
1641 | my ($filename, $fd) = @_; | |
1642 | ||
1643 | local $/; | |
1644 | ||
b8e02bc2 | 1645 | my $raw = defined($fd) ? <$fd> : ''; |
6c3aef09 DM |
1646 | |
1647 | $raw =~ s/^\s+//; | |
1648 | ||
9bbc4e17 | 1649 | |
6c3aef09 DM |
1650 | my @tokens = split(/\s+/, $raw); |
1651 | ||
1652 | my $data = {}; | |
1653 | ||
1654 | my $machine; | |
1655 | while (defined(my $tok = shift @tokens)) { | |
1656 | ||
1657 | $machine = shift @tokens if $tok eq 'machine'; | |
1658 | next if !$machine; | |
1659 | $data->{$machine} = {} if !$data->{$machine}; | |
1660 | ||
1661 | $data->{$machine}->{login} = shift @tokens if $tok eq 'login'; | |
1662 | $data->{$machine}->{password} = shift @tokens if $tok eq 'password'; | |
1663 | }; | |
1664 | ||
1665 | return $data; | |
1666 | } | |
1667 | ||
1668 | my $format_apt_auth_data = sub { | |
1669 | my $data = shift; | |
1670 | ||
1671 | my $raw = ''; | |
1672 | ||
1673 | foreach my $machine (sort keys %$data) { | |
1674 | my $d = $data->{$machine}; | |
1675 | $raw .= "machine $machine\n"; | |
1676 | $raw .= " login $d->{login}\n" if $d->{login}; | |
1677 | $raw .= " password $d->{password}\n" if $d->{password}; | |
1678 | $raw .= "\n"; | |
1679 | } | |
1680 | ||
1681 | return $raw; | |
1682 | }; | |
1683 | ||
1684 | sub write_apt_auth { | |
1685 | my ($filename, $fh, $data) = @_; | |
1686 | ||
1687 | my $raw = &$format_apt_auth_data($data); | |
1688 | ||
1689 | die "write failed: $!" unless print $fh "$raw\n"; | |
9bbc4e17 | 1690 | |
6c3aef09 DM |
1691 | return $data; |
1692 | } | |
1693 | ||
1694 | sub update_apt_auth { | |
1695 | my ($filename, $fh, $data) = @_; | |
1696 | ||
1697 | my $orig = read_apt_auth($filename, $fh); | |
1698 | ||
1699 | foreach my $machine (keys %$data) { | |
1700 | $orig->{$machine} = $data->{$machine}; | |
1701 | } | |
1702 | ||
1703 | return &$format_apt_auth_data($orig); | |
1704 | } | |
1705 | ||
9bbc4e17 | 1706 | register_file('apt-auth', "/etc/apt/auth.conf", |
6c3aef09 | 1707 | \&read_apt_auth, \&write_apt_auth, |
b7aae8e5 | 1708 | \&update_apt_auth, perm => 0640); |
6c3aef09 | 1709 | |
e143e9d8 | 1710 | 1; |