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