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