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