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