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