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