]>
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'; | |
69758574 | 22 | use JSON; |
9194ee06 | 23 | use Digest::SHA; |
69758574 | 24 | use Encode qw(encode decode); |
e143e9d8 DM |
25 | |
26 | our @EXPORT_OK = qw(read_file write_file register_file); | |
27 | ||
28 | my $ccache; | |
29 | my $ccachemap; | |
30 | my $ccacheregex; | |
31 | my $inotify; | |
32 | my $inotify_pid = 0; | |
33 | my $versions; | |
34 | my $shadowfiles = { | |
35 | '/etc/network/interfaces' => '/etc/network/interfaces.new', | |
36 | }; | |
37 | ||
38 | # to enable cached operation, you need to call 'inotify_init' | |
39 | # inotify handles are a limited resource, so use with care (only | |
40 | # enable the cache if you really need it) | |
41 | ||
42 | # Note: please close the inotify handle after you fork | |
43 | ||
44 | sub ccache_default_writer { | |
45 | my ($filename, $data) = @_; | |
46 | ||
47 | die "undefined config writer for '$filename' :ERROR"; | |
48 | } | |
49 | ||
50 | sub ccache_default_parser { | |
51 | my ($filename, $srcfd) = @_; | |
52 | ||
53 | die "undefined config reader for '$filename' :ERROR"; | |
54 | } | |
55 | ||
56 | sub ccache_compute_diff { | |
57 | my ($filename, $shadow) = @_; | |
58 | ||
59 | my $diff = ''; | |
60 | ||
69758574 DC |
61 | my $cmd = ['/usr/bin/diff', '-b', '-N', '-u', $filename, $shadow]; |
62 | PVE::Tools::run_command($cmd, noerr => 1, outfunc => sub { | |
63 | my ($line) = @_; | |
64 | $diff .= decode('UTF-8', $line) . "\n"; | |
65 | }); | |
e143e9d8 DM |
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 | ||
9194ee06 DC |
541 | sub read_etc_hosts { |
542 | my ($filename, $fh) = @_; | |
543 | ||
544 | my $raw = ''; | |
545 | my $data = ''; | |
546 | ||
547 | while (my $line = <$fh>) { | |
548 | $raw .= $line; | |
549 | if ($line =~ m/^\s*#/) { | |
550 | $line = decode('UTF-8', $line); | |
551 | } | |
552 | $data .= $line; | |
553 | } | |
554 | ||
555 | return { | |
556 | digest => Digest::SHA::sha1_hex($raw), | |
557 | data => $data, | |
558 | } | |
559 | } | |
560 | ||
561 | sub write_etc_hosts { | |
562 | my ($filename, $fh, $hosts, @args) = @_; | |
563 | ||
564 | # check validity of ips/names | |
565 | for my $line (split("\n", $hosts)) { | |
566 | next if $line =~ m/^\s*#/; # comments | |
567 | next if $line =~ m/^\s*$/; # whitespace/empty lines | |
568 | ||
569 | my ($ip, @names) = split(/\s+/, $line); | |
570 | ||
571 | raise_param_exc({ 'data' => "Invalid IP '$ip'" }) | |
572 | if $ip !~ m/^$PVE::Tools::IPRE$/; | |
573 | ||
574 | for my $name (@names) { | |
575 | raise_param_exc({ 'data' => "Invalid Hostname '$name'" }) | |
576 | if $name !~ m/^[.\-a-zA-Z0-9]+$/; | |
577 | } | |
578 | } | |
579 | ||
580 | die "write failed: $!" if !print $fh encode('UTF-8', $hosts); | |
581 | ||
582 | return $hosts; | |
583 | } | |
584 | ||
585 | register_file('etchosts', "/etc/hosts", | |
586 | \&read_etc_hosts, | |
587 | \&write_etc_hosts); | |
588 | ||
e143e9d8 DM |
589 | sub read_etc_resolv_conf { |
590 | my ($filename, $fh) = @_; | |
591 | ||
592 | my $res = {}; | |
593 | ||
594 | my $nscount = 0; | |
595 | while (my $line = <$fh>) { | |
596 | chomp $line; | |
597 | if ($line =~ m/^(search|domain)\s+(\S+)\s*/) { | |
598 | $res->{search} = $2; | |
176b1186 | 599 | } elsif ($line =~ m/^\s*nameserver\s+($PVE::Tools::IPRE)\s*/) { |
e143e9d8 DM |
600 | $nscount++; |
601 | if ($nscount <= 3) { | |
602 | $res->{"dns$nscount"} = $1; | |
603 | } | |
604 | } | |
605 | } | |
606 | ||
607 | return $res; | |
608 | } | |
609 | ||
610 | sub update_etc_resolv_conf { | |
611 | my ($filename, $fh, $resolv, @args) = @_; | |
612 | ||
613 | my $data = ""; | |
614 | ||
615 | $data = "search $resolv->{search}\n" | |
616 | if $resolv->{search}; | |
617 | ||
618 | my $written = {}; | |
619 | foreach my $k ("dns1", "dns2", "dns3") { | |
620 | my $ns = $resolv->{$k}; | |
621 | if ($ns && $ns ne '0.0.0.0' && !$written->{$ns}) { | |
622 | $written->{$ns} = 1; | |
623 | $data .= "nameserver $ns\n"; | |
624 | } | |
625 | } | |
626 | ||
627 | while (my $line = <$fh>) { | |
628 | next if $line =~ m/^(search|domain|nameserver)\s+/; | |
629 | $data .= $line | |
630 | } | |
631 | ||
632 | return $data; | |
633 | } | |
634 | ||
635 | register_file('resolvconf', "/etc/resolv.conf", | |
636 | \&read_etc_resolv_conf, undef, | |
637 | \&update_etc_resolv_conf); | |
638 | ||
639 | sub read_etc_timezone { | |
640 | my ($filename, $fd) = @_; | |
641 | ||
642 | my $timezone = <$fd>; | |
643 | ||
644 | chomp $timezone; | |
645 | ||
646 | return $timezone; | |
647 | } | |
648 | ||
649 | sub write_etc_timezone { | |
650 | my ($filename, $fh, $timezone) = @_; | |
651 | ||
652 | my $tzinfo = "/usr/share/zoneinfo/$timezone"; | |
653 | ||
654 | raise_param_exc({ 'timezone' => "No such timezone" }) | |
655 | if (! -f $tzinfo); | |
656 | ||
657 | ($timezone) = $timezone =~ m/^(.*)$/; # untaint | |
658 | ||
659 | print $fh "$timezone\n"; | |
660 | ||
661 | unlink ("/etc/localtime"); | |
662 | symlink ("/usr/share/zoneinfo/$timezone", "/etc/localtime"); | |
663 | ||
664 | } | |
665 | ||
666 | register_file('timezone', "/etc/timezone", | |
667 | \&read_etc_timezone, | |
668 | \&write_etc_timezone); | |
669 | ||
670 | sub read_active_workers { | |
671 | my ($filename, $fh) = @_; | |
672 | ||
673 | return [] if !$fh; | |
674 | ||
675 | my $res = []; | |
676 | while (defined (my $line = <$fh>)) { | |
75ed54f1 | 677 | if ($line =~ m/^(\S+)\s(0|1)(\s([0-9A-Za-z]{8})(\s(\s*\S.*))?)?$/) { |
e143e9d8 DM |
678 | my $upid = $1; |
679 | my $saved = $2; | |
680 | my $endtime = $4; | |
681 | my $status = $6; | |
682 | if ((my $task = PVE::Tools::upid_decode($upid, 1))) { | |
683 | $task->{upid} = $upid; | |
684 | $task->{saved} = $saved; | |
685 | $task->{endtime} = hex($endtime) if $endtime; | |
686 | $task->{status} = $status if $status; | |
687 | push @$res, $task; | |
688 | } | |
689 | } else { | |
690 | warn "unable to parse line: $line"; | |
691 | } | |
692 | } | |
693 | ||
694 | return $res; | |
695 | ||
696 | } | |
697 | ||
698 | sub write_active_workers { | |
699 | my ($filename, $fh, $tasklist) = @_; | |
700 | ||
701 | my $raw = ''; | |
702 | foreach my $task (@$tasklist) { | |
703 | my $upid = $task->{upid}; | |
704 | my $saved = $task->{saved} ? 1 : 0; | |
705 | if ($task->{endtime}) { | |
706 | if ($task->{status}) { | |
e34c27f4 | 707 | $raw .= sprintf("%s %s %08X %s\n", $upid, $saved, $task->{endtime}, $task->{status}); |
e143e9d8 | 708 | } else { |
e34c27f4 | 709 | $raw .= sprintf("%s %s %08X\n", $upid, $saved, $task->{endtime}); |
e143e9d8 DM |
710 | } |
711 | } else { | |
712 | $raw .= "$upid $saved\n"; | |
713 | } | |
714 | } | |
715 | ||
716 | PVE::Tools::safe_print($filename, $fh, $raw) if $raw; | |
717 | } | |
718 | ||
719 | register_file('active', "/var/log/pve/tasks/active", | |
720 | \&read_active_workers, | |
721 | \&write_active_workers); | |
722 | ||
723 | ||
c8e5d28e | 724 | our $bond_modes = { 'balance-rr' => 0, |
e143e9d8 DM |
725 | 'active-backup' => 1, |
726 | 'balance-xor' => 2, | |
727 | 'broadcast' => 3, | |
728 | '802.3ad' => 4, | |
729 | 'balance-tlb' => 5, | |
730 | 'balance-alb' => 6, | |
731 | }; | |
732 | ||
9b7b13a1 DM |
733 | my $ovs_bond_modes = { |
734 | 'active-backup' => 1, | |
9b7b13a1 | 735 | 'balance-slb' => 1, |
a68760b6 DM |
736 | 'lacp-balance-slb' => 1, |
737 | 'lacp-balance-tcp' => 1, | |
9b7b13a1 DM |
738 | }; |
739 | ||
e143e9d8 DM |
740 | #sub get_bond_modes { |
741 | # return $bond_modes; | |
742 | #} | |
743 | ||
9b7b13a1 DM |
744 | my $parse_ovs_option = sub { |
745 | my ($data) = @_; | |
746 | ||
747 | my $opts = {}; | |
748 | foreach my $kv (split (/\s+/, $data || '')) { | |
749 | my ($k, $v) = split('=', $kv, 2); | |
750 | $opts->{$k} = $v if $k && $v; | |
751 | } | |
752 | return $opts; | |
753 | }; | |
754 | ||
755 | my $set_ovs_option = sub { | |
756 | my ($d, %params) = @_; | |
757 | ||
758 | my $opts = &$parse_ovs_option($d->{ovs_options}); | |
759 | ||
760 | foreach my $k (keys %params) { | |
761 | my $v = $params{$k}; | |
762 | if ($v) { | |
763 | $opts->{$k} = $v; | |
764 | } else { | |
765 | delete $opts->{$k}; | |
766 | } | |
767 | } | |
768 | ||
769 | my $res = []; | |
770 | foreach my $k (keys %$opts) { | |
771 | push @$res, "$k=$opts->{$k}"; | |
772 | } | |
773 | ||
774 | if (my $new = join(' ', @$res)) { | |
775 | $d->{ovs_options} = $new; | |
776 | return $d->{ovs_options}; | |
777 | } else { | |
778 | delete $d->{ovs_options}; | |
779 | return undef; | |
780 | } | |
781 | }; | |
782 | ||
783 | my $extract_ovs_option = sub { | |
784 | my ($d, $name) = @_; | |
785 | ||
786 | my $opts = &$parse_ovs_option($d->{ovs_options}); | |
787 | ||
788 | my $v = delete $opts->{$name}; | |
789 | ||
790 | my $res = []; | |
791 | foreach my $k (keys %$opts) { | |
792 | push @$res, "$k=$opts->{$k}"; | |
793 | } | |
794 | ||
795 | if (my $new = join(' ', @$res)) { | |
796 | $d->{ovs_options} = $new; | |
797 | } else { | |
798 | delete $d->{ovs_options}; | |
799 | } | |
800 | ||
801 | return $v; | |
802 | }; | |
803 | ||
9b053d70 | 804 | my $check_mtu = sub { |
cebd1c85 | 805 | my ($ifaces, $parent, $child) = @_; |
9b053d70 | 806 | |
cebd1c85 WB |
807 | die "check mtu - missing parent interface\n" if !$parent; |
808 | die "check mtu - missing child interface\n" if !$child; | |
9b053d70 | 809 | |
000e32bc AD |
810 | my $cmtu = $ifaces->{$child}->{mtu}; |
811 | return if !$cmtu; | |
9b053d70 | 812 | |
000e32bc AD |
813 | my $parentdata = $ifaces->{$parent}; |
814 | my $pmtu = $parentdata->{mtu}; | |
815 | $pmtu = $cmtu if $parentdata->{type} eq 'bond' && !$pmtu; | |
816 | $pmtu = 1500 if !$pmtu; | |
817 | ||
818 | die "interface '$parent' - mtu $pmtu is lower than '$child' - mtu $cmtu\n" | |
819 | if $pmtu < $cmtu; | |
9b053d70 AD |
820 | }; |
821 | ||
48ab17b3 WB |
822 | # config => { |
823 | # ifaces => { | |
824 | # $ifname => { | |
825 | # <optional> exists => BOOL, | |
826 | # <optional> active => BOOL, | |
827 | # <optional> autostart => BOOL, | |
828 | # <auto> priority => INT, | |
829 | # | |
830 | # type => "eth" | "bridge" | "bond" | "loopback" | "OVS*" | ... , | |
831 | # | |
832 | # families => ["inet", "inet6", ...], | |
833 | # | |
834 | # method => "manual" | "static" | "dhcp" | ... , | |
835 | # address => IP, | |
836 | # netmask => SUBNET, | |
837 | # broadcast => IP, | |
838 | # gateway => IP, | |
839 | # comments => [ "..." ], | |
840 | # | |
841 | # method6 => "manual" | "static" | "dhcp" | ... , | |
842 | # address6 => IP, | |
843 | # netmask6 => SUBNET, | |
844 | # gateway6 => IP, | |
845 | # comments6 => [ "..." ], | |
846 | # | |
847 | # <known options>, # like bridge_ports, ovs_* | |
848 | # | |
849 | # # extra/unknown options stored by-family: | |
850 | # options => { <inet options>... } | |
851 | # options6 => { <inet6 options>... } | |
852 | # } | |
853 | # }, | |
854 | # options => [ | |
855 | # # mappings end up here as well, as we don't need to understand them | |
856 | # [priority,line] | |
857 | # ] | |
858 | # } | |
e143e9d8 DM |
859 | sub read_etc_network_interfaces { |
860 | my ($filename, $fh) = @_; | |
78325766 | 861 | my $proc_net_dev = IO::File->new('/proc/net/dev', 'r'); |
f0d1b04f | 862 | my $active = PVE::ProcFSTools::get_active_network_interfaces(); |
12a235d6 | 863 | return __read_etc_network_interfaces($fh, $proc_net_dev, $active); |
78325766 WB |
864 | } |
865 | ||
866 | sub __read_etc_network_interfaces { | |
12a235d6 | 867 | my ($fh, $proc_net_dev, $active_ifaces) = @_; |
e143e9d8 | 868 | |
48ab17b3 WB |
869 | my $config = {}; |
870 | my $ifaces = $config->{ifaces} = {}; | |
871 | my $options = $config->{options} = []; | |
e143e9d8 | 872 | |
1b1fb9f7 AD |
873 | my $options_alternatives = { |
874 | 'bond-slaves' => 'slaves', | |
875 | 'bond_slaves' => 'slaves', | |
876 | 'bond-xmit-hash-policy' => 'bond_xmit_hash_policy', | |
877 | 'bond-mode' => 'bond_mode', | |
878 | 'bond-miimon' =>'bond_miimon', | |
879 | 'bridge-vlan-aware' => 'bridge_vlan_aware', | |
880 | 'bridge-fd' => 'bridge_fd', | |
881 | 'bridge-stp' => 'bridge_stp', | |
95d0e633 AD |
882 | 'bridge-ports' => 'bridge_ports', |
883 | 'bridge-vids' => 'bridge_vids' | |
1b1fb9f7 AD |
884 | }; |
885 | ||
e143e9d8 DM |
886 | my $line; |
887 | ||
78325766 WB |
888 | if ($proc_net_dev) { |
889 | while (defined ($line = <$proc_net_dev>)) { | |
3dabe28a | 890 | if ($line =~ m/^\s*($PVE::Network::PHYSICAL_NIC_RE):.*/) { |
e143e9d8 DM |
891 | $ifaces->{$1}->{exists} = 1; |
892 | } | |
893 | } | |
78325766 | 894 | close($proc_net_dev); |
e143e9d8 DM |
895 | } |
896 | ||
c9dc8645 DM |
897 | # we try to keep order inside the file |
898 | my $priority = 2; # 1 is reserved for lo | |
899 | ||
b1d7951f | 900 | SECTION: while (defined ($line = <$fh>)) { |
e143e9d8 | 901 | chomp ($line); |
b1d7951f | 902 | next if $line =~ m/^\s*#/; |
e143e9d8 | 903 | |
b1d7951f | 904 | if ($line =~ m/^\s*auto\s+(.*)$/) { |
e143e9d8 DM |
905 | my @aa = split (/\s+/, $1); |
906 | ||
907 | foreach my $a (@aa) { | |
908 | $ifaces->{$a}->{autostart} = 1; | |
909 | } | |
910 | ||
48ab17b3 | 911 | } elsif ($line =~ m/^\s*iface\s+(\S+)\s+(inet6?)\s+(\S+)\s*$/) { |
e143e9d8 | 912 | my $i = $1; |
48ab17b3 WB |
913 | my $family = $2; |
914 | my $f = { method => $3 }; # by family, merged to $d with a $suffix | |
915 | (my $suffix = $family) =~ s/^inet//; | |
e143e9d8 | 916 | |
cb07ff78 | 917 | my $d = $ifaces->{$i} ||= {}; |
48ab17b3 WB |
918 | $d->{priority} = $priority++ if !$d->{priority}; |
919 | push @{$d->{families}}, $family; | |
920 | ||
72ca6520 | 921 | while (defined ($line = <$fh>)) { |
b1d7951f WB |
922 | chomp $line; |
923 | if ($line =~ m/^\s*#(.*?)\s*$/) { | |
48ab17b3 | 924 | $f->{comments} = '' if !$f->{comments}; |
1b505ae2 DC |
925 | my $comment = decode('UTF-8', $1); |
926 | $f->{comments} .= "$comment\n"; | |
b1d7951f WB |
927 | } elsif ($line =~ m/^\s*(?:iface\s |
928 | |mapping\s | |
929 | |auto\s | |
930 | |allow- | |
931 | |source\s | |
932 | |source-directory\s | |
933 | )/x) { | |
934 | last; | |
935 | } elsif ($line =~ m/^\s*((\S+)\s+(.+))$/) { | |
72ca6520 DM |
936 | my $option = $1; |
937 | my ($id, $value) = ($2, $3); | |
1b1fb9f7 AD |
938 | |
939 | $id = $options_alternatives->{$id} if $options_alternatives->{$id}; | |
940 | ||
9a052564 | 941 | my $simple_options = { |
9b053d70 | 942 | 'mtu' => 1, |
9a052564 AD |
943 | 'ovs_type' => 1, |
944 | 'ovs_options' => 1, | |
945 | 'ovs_bridge' => 1, | |
946 | 'ovs_bonds' => 1, | |
947 | 'ovs_ports' => 1, | |
948 | 'bridge_fd' => 1, | |
949 | 'bridge_vids' => 1, | |
950 | 'bridge-access' => 1, | |
951 | 'bridge-learning' => 1, | |
952 | 'bridge-arp-nd-suppress' => 1, | |
953 | 'bridge-unicast-flood' => 1, | |
954 | 'bridge-multicast-flood' => 1, | |
955 | 'bond_miimon' => 1, | |
956 | 'bond_xmit_hash_policy' => 1, | |
957 | 'vxlan-id' => 1, | |
958 | 'vxlan-svcnodeip' => 1, | |
959 | 'vxlan-physdev' => 1, | |
960 | 'vxlan-local-tunnelip' => 1 }; | |
961 | ||
0541ec90 | 962 | if (($id eq 'address') || ($id eq 'netmask') || ($id eq 'broadcast') || ($id eq 'gateway')) { |
48ab17b3 | 963 | $f->{$id} = $value; |
9a052564 | 964 | } elsif ($simple_options->{$id}) { |
9b7b13a1 | 965 | $d->{$id} = $value; |
72ca6520 DM |
966 | } elsif ($id eq 'slaves' || $id eq 'bridge_ports') { |
967 | my $devs = {}; | |
968 | foreach my $p (split (/\s+/, $value)) { | |
969 | next if $p eq 'none'; | |
970 | $devs->{$p} = 1; | |
e143e9d8 | 971 | } |
72ca6520 | 972 | my $str = join (' ', sort keys %{$devs}); |
48ab17b3 WB |
973 | if ($d->{$id}) { |
974 | $d->{$id} .= ' ' . $str if $str; | |
975 | } else { | |
976 | $d->{$id} = $str || ''; | |
977 | } | |
72ca6520 DM |
978 | } elsif ($id eq 'bridge_stp') { |
979 | if ($value =~ m/^\s*(on|yes)\s*$/i) { | |
980 | $d->{$id} = 'on'; | |
981 | } else { | |
982 | $d->{$id} = 'off'; | |
983 | } | |
685a2528 AD |
984 | } elsif ($id eq 'bridge_vlan_aware') { |
985 | $d->{$id} = 1; | |
72ca6520 DM |
986 | } elsif ($id eq 'bond_mode') { |
987 | # always use names | |
988 | foreach my $bm (keys %$bond_modes) { | |
989 | my $id = $bond_modes->{$bm}; | |
990 | if ($id eq $value) { | |
991 | $value = $bm; | |
992 | last; | |
993 | } | |
994 | } | |
995 | $d->{$id} = $value; | |
95aa8788 AD |
996 | } elsif ($id eq 'vxlan-remoteip') { |
997 | push @{$d->{$id}}, $value; | |
72ca6520 | 998 | } else { |
48ab17b3 | 999 | push @{$f->{options}}, $option; |
e143e9d8 | 1000 | } |
e143e9d8 | 1001 | } else { |
72ca6520 | 1002 | last; |
e143e9d8 DM |
1003 | } |
1004 | } | |
48ab17b3 | 1005 | $d->{"$_$suffix"} = $f->{$_} foreach (keys %$f); |
b1d7951f WB |
1006 | last SECTION if !defined($line); |
1007 | redo SECTION; | |
48ab17b3 WB |
1008 | } elsif ($line =~ /\w/) { |
1009 | push @$options, [$priority++, $line]; | |
e143e9d8 DM |
1010 | } |
1011 | } | |
1012 | ||
12a235d6 WB |
1013 | foreach my $ifname (@$active_ifaces) { |
1014 | if (my $iface = $ifaces->{$ifname}) { | |
1015 | $iface->{active} = 1; | |
1016 | } | |
1017 | } | |
e143e9d8 DM |
1018 | |
1019 | if (!$ifaces->{lo}) { | |
c9dc8645 | 1020 | $ifaces->{lo}->{priority} = 1; |
e143e9d8 DM |
1021 | $ifaces->{lo}->{method} = 'loopback'; |
1022 | $ifaces->{lo}->{type} = 'loopback'; | |
1023 | $ifaces->{lo}->{autostart} = 1; | |
1024 | } | |
1025 | ||
1026 | foreach my $iface (keys %$ifaces) { | |
1027 | my $d = $ifaces->{$iface}; | |
1028 | if ($iface =~ m/^bond\d+$/) { | |
9b7b13a1 DM |
1029 | if (!$d->{ovs_type}) { |
1030 | $d->{type} = 'bond'; | |
1031 | } elsif ($d->{ovs_type} eq 'OVSBond') { | |
1032 | $d->{type} = $d->{ovs_type}; | |
1033 | # translate: ovs_options => bond_mode | |
1034 | $d->{'bond_mode'} = &$extract_ovs_option($d, 'bond_mode'); | |
a68760b6 DM |
1035 | my $lacp = &$extract_ovs_option($d, 'lacp'); |
1036 | if ($lacp && $lacp eq 'active') { | |
1037 | if ($d->{'bond_mode'} eq 'balance-slb') { | |
1038 | $d->{'bond_mode'} = 'lacp-balance-slb'; | |
1039 | } | |
1040 | } | |
1041 | # Note: balance-tcp needs lacp | |
1042 | if ($d->{'bond_mode'} eq 'balance-tcp') { | |
1043 | $d->{'bond_mode'} = 'lacp-balance-tcp'; | |
1044 | } | |
2a751938 DM |
1045 | my $tag = &$extract_ovs_option($d, 'tag'); |
1046 | $d->{ovs_tag} = $tag if defined($tag); | |
9b7b13a1 DM |
1047 | } else { |
1048 | $d->{type} = 'unknown'; | |
e143e9d8 | 1049 | } |
9b7b13a1 DM |
1050 | } elsif ($iface =~ m/^vmbr\d+$/) { |
1051 | if (!$d->{ovs_type}) { | |
1052 | $d->{type} = 'bridge'; | |
1053 | ||
1054 | if (!defined ($d->{bridge_fd})) { | |
1055 | $d->{bridge_fd} = 0; | |
1056 | } | |
1057 | if (!defined ($d->{bridge_stp})) { | |
1058 | $d->{bridge_stp} = 'off'; | |
1059 | } | |
1060 | } elsif ($d->{ovs_type} eq 'OVSBridge') { | |
1061 | $d->{type} = $d->{ovs_type}; | |
1062 | } else { | |
1063 | $d->{type} = 'unknown'; | |
e143e9d8 DM |
1064 | } |
1065 | } elsif ($iface =~ m/^(\S+):\d+$/) { | |
1066 | $d->{type} = 'alias'; | |
1067 | if (defined ($ifaces->{$1})) { | |
1068 | $d->{exists} = $ifaces->{$1}->{exists}; | |
1069 | } else { | |
1070 | $ifaces->{$1}->{exists} = 0; | |
1071 | $d->{exists} = 0; | |
1072 | } | |
9c808945 AD |
1073 | } elsif ($iface =~ m/^(\S+)\.\d+$/) { |
1074 | $d->{type} = 'vlan'; | |
1075 | if (defined ($ifaces->{$1})) { | |
1076 | $d->{exists} = $ifaces->{$1}->{exists}; | |
1077 | } else { | |
1078 | $ifaces->{$1}->{exists} = 0; | |
1079 | $d->{exists} = 0; | |
1080 | } | |
3dabe28a | 1081 | } elsif ($iface =~ m/^$PVE::Network::PHYSICAL_NIC_RE$/) { |
9b7b13a1 DM |
1082 | if (!$d->{ovs_type}) { |
1083 | $d->{type} = 'eth'; | |
1084 | } elsif ($d->{ovs_type} eq 'OVSPort') { | |
1085 | $d->{type} = $d->{ovs_type}; | |
2a751938 DM |
1086 | my $tag = &$extract_ovs_option($d, 'tag'); |
1087 | $d->{ovs_tag} = $tag if defined($tag); | |
9b7b13a1 DM |
1088 | } else { |
1089 | $d->{type} = 'unknown'; | |
1090 | } | |
e143e9d8 DM |
1091 | } elsif ($iface =~ m/^lo$/) { |
1092 | $d->{type} = 'loopback'; | |
1093 | } else { | |
95aa8788 AD |
1094 | if ($d->{'vxlan-id'}) { |
1095 | $d->{type} = 'vxlan'; | |
1096 | } elsif (!$d->{ovs_type}) { | |
9b7b13a1 DM |
1097 | $d->{type} = 'unknown'; |
1098 | } elsif ($d->{ovs_type} eq 'OVSIntPort') { | |
1099 | $d->{type} = $d->{ovs_type}; | |
2a751938 DM |
1100 | my $tag = &$extract_ovs_option($d, 'tag'); |
1101 | $d->{ovs_tag} = $tag if defined($tag); | |
9b7b13a1 | 1102 | } |
e143e9d8 DM |
1103 | } |
1104 | ||
1105 | $d->{method} = 'manual' if !$d->{method}; | |
48ab17b3 | 1106 | $d->{method6} = 'manual' if !$d->{method6}; |
78325766 WB |
1107 | |
1108 | $d->{families} ||= ['inet']; | |
e143e9d8 DM |
1109 | } |
1110 | ||
516a50a4 WB |
1111 | # OVS bridges create "allow-$BRIDGE $IFACE" lines which we need to remove |
1112 | # from the {options} hash for them to be removed correctly. | |
1113 | @$options = grep {defined($_)} map { | |
1114 | my ($pri, $line) = @$_; | |
1115 | if ($line =~ /^allow-(\S+)\s+(.*)$/) { | |
1116 | my $bridge = $1; | |
1117 | my @ports = split(/\s+/, $2); | |
1118 | if (defined(my $br = $ifaces->{$bridge})) { | |
1119 | # if this port is part of a bridge, remove it | |
1120 | my %in_ovs_ports = map {$_=>1} split(/\s+/, $br->{ovs_ports}); | |
1121 | @ports = grep { not $in_ovs_ports{$_} } @ports; | |
1122 | } | |
1123 | # create the allow line for the remaining ports, or delete if empty | |
1124 | if (@ports) { | |
1125 | [$pri, "allow-$bridge " . join(' ', @ports)]; | |
1126 | } else { | |
1127 | undef; | |
1128 | } | |
1129 | } else { | |
1130 | # don't modify other lines | |
1131 | $_; | |
1132 | } | |
1133 | } @$options; | |
1134 | ||
48ab17b3 | 1135 | return $config; |
e143e9d8 DM |
1136 | } |
1137 | ||
1138 | sub __interface_to_string { | |
b7c4f378 | 1139 | my ($iface, $d, $family, $first_block, $ifupdown2) = @_; |
48ab17b3 WB |
1140 | |
1141 | (my $suffix = $family) =~ s/^inet//; | |
e143e9d8 | 1142 | |
48ab17b3 | 1143 | return '' if !($d && $d->{"method$suffix"}); |
e143e9d8 DM |
1144 | |
1145 | my $raw = ''; | |
1146 | ||
48ab17b3 WB |
1147 | $raw .= "iface $iface $family " . $d->{"method$suffix"} . "\n"; |
1148 | $raw .= "\taddress " . $d->{"address$suffix"} . "\n" if $d->{"address$suffix"}; | |
1149 | $raw .= "\tnetmask " . $d->{"netmask$suffix"} . "\n" if $d->{"netmask$suffix"}; | |
1150 | $raw .= "\tgateway " . $d->{"gateway$suffix"} . "\n" if $d->{"gateway$suffix"}; | |
1151 | $raw .= "\tbroadcast " . $d->{"broadcast$suffix"} . "\n" if $d->{"broadcast$suffix"}; | |
e143e9d8 | 1152 | |
9b7b13a1 DM |
1153 | my $done = { type => 1, priority => 1, method => 1, active => 1, exists => 1, |
1154 | comments => 1, autostart => 1, options => 1, | |
48ab17b3 WB |
1155 | address => 1, netmask => 1, gateway => 1, broadcast => 1, |
1156 | method6 => 1, families => 1, options6 => 1, | |
1157 | address6 => 1, netmask6 => 1, gateway6 => 1, broadcast6 => 1 }; | |
1158 | ||
1159 | if (!$first_block) { | |
1160 | # not printing out options | |
1161 | } elsif ($d->{type} eq 'bridge') { | |
9b7b13a1 | 1162 | |
fb3a6db1 | 1163 | $d->{bridge_ports} =~ s/[;,\s]+/ /g; |
e143e9d8 | 1164 | my $ports = $d->{bridge_ports} || 'none'; |
1accc6da | 1165 | $raw .= "\tbridge-ports $ports\n"; |
9b7b13a1 | 1166 | $done->{bridge_ports} = 1; |
e143e9d8 | 1167 | |
9b7b13a1 | 1168 | my $v = defined($d->{bridge_stp}) ? $d->{bridge_stp} : 'off'; |
1accc6da | 1169 | $raw .= "\tbridge-stp $v\n"; |
9b7b13a1 | 1170 | $done->{bridge_stp} = 1; |
e143e9d8 | 1171 | |
9b7b13a1 | 1172 | $v = defined($d->{bridge_fd}) ? $d->{bridge_fd} : 0; |
1accc6da | 1173 | $raw .= "\tbridge-fd $v\n"; |
9b7b13a1 | 1174 | $done->{bridge_fd} = 1; |
685a2528 AD |
1175 | |
1176 | if( defined($d->{bridge_vlan_aware})) { | |
1accc6da | 1177 | $raw .= "\tbridge-vlan-aware yes\n"; |
95d0e633 AD |
1178 | $v = defined($d->{bridge_vids}) ? $d->{bridge_vids} : "2-4094"; |
1179 | $raw .= "\tbridge-vids $v\n"; | |
685a2528 AD |
1180 | } |
1181 | $done->{bridge_vlan_aware} = 1; | |
f6c4a563 | 1182 | $done->{bridge_vids} = 1; |
9b7b13a1 DM |
1183 | |
1184 | } elsif ($d->{type} eq 'bond') { | |
e143e9d8 | 1185 | |
fb3a6db1 | 1186 | $d->{slaves} =~ s/[;,\s]+/ /g; |
e143e9d8 | 1187 | my $slaves = $d->{slaves} || 'none'; |
1accc6da | 1188 | $raw .= "\tbond-slaves $slaves\n"; |
9b7b13a1 | 1189 | $done->{slaves} = 1; |
e143e9d8 | 1190 | |
9b7b13a1 | 1191 | my $v = defined ($d->{'bond_miimon'}) ? $d->{'bond_miimon'} : 100; |
1accc6da | 1192 | $raw .= "\tbond-miimon $v\n"; |
9b7b13a1 | 1193 | $done->{'bond_miimon'} = 1; |
e143e9d8 | 1194 | |
9b7b13a1 | 1195 | $v = defined ($d->{'bond_mode'}) ? $d->{'bond_mode'} : 'balance-rr'; |
1accc6da | 1196 | $raw .= "\tbond-mode $v\n"; |
9b7b13a1 DM |
1197 | $done->{'bond_mode'} = 1; |
1198 | ||
40fae2b3 DM |
1199 | if ($d->{'bond_mode'} && $d->{'bond_xmit_hash_policy'} && |
1200 | ($d->{'bond_mode'} eq 'balance-xor' || $d->{'bond_mode'} eq '802.3ad')) { | |
1accc6da | 1201 | $raw .= "\tbond-xmit-hash-policy $d->{'bond_xmit_hash_policy'}\n"; |
40fae2b3 DM |
1202 | } |
1203 | $done->{'bond_xmit_hash_policy'} = 1; | |
95aa8788 AD |
1204 | } elsif ($d->{type} eq 'vxlan') { |
1205 | ||
94d786b3 WB |
1206 | foreach my $k (qw(vxlan-id vxlan-svcnodeip vxlan-physdev vxlan-local-tunnelip)) { |
1207 | $raw .= "\t$k $d->{$k}\n" if defined $d->{$k}; | |
95aa8788 | 1208 | $done->{$k} = 1; |
94d786b3 | 1209 | } |
95aa8788 AD |
1210 | |
1211 | if ($d->{'vxlan-remoteip'}) { | |
1212 | foreach my $remoteip (@{$d->{'vxlan-remoteip'}}) { | |
1213 | $raw .= "\tvxlan-remoteip $remoteip\n"; | |
1214 | } | |
1215 | $done->{'vxlan-remoteip'} = 1; | |
1216 | } | |
9b7b13a1 DM |
1217 | } elsif ($d->{type} eq 'OVSBridge') { |
1218 | ||
1219 | $raw .= "\tovs_type $d->{type}\n"; | |
1220 | $done->{ovs_type} = 1; | |
1221 | ||
1222 | $raw .= "\tovs_ports $d->{ovs_ports}\n" if $d->{ovs_ports}; | |
1223 | $done->{ovs_ports} = 1; | |
9b7b13a1 DM |
1224 | } elsif ($d->{type} eq 'OVSPort' || $d->{type} eq 'OVSIntPort' || |
1225 | $d->{type} eq 'OVSBond') { | |
1226 | ||
1227 | $d->{autostart} = 0; # started by the bridge | |
1228 | ||
2a751938 DM |
1229 | if (defined($d->{ovs_tag})) { |
1230 | &$set_ovs_option($d, tag => $d->{ovs_tag}); | |
1231 | } | |
1232 | $done->{ovs_tag} = 1; | |
1233 | ||
9b7b13a1 DM |
1234 | if ($d->{type} eq 'OVSBond') { |
1235 | ||
1236 | $d->{bond_mode} = 'active-backup' if !$d->{bond_mode}; | |
1237 | ||
1238 | $ovs_bond_modes->{$d->{bond_mode}} || | |
1239 | die "OVS does not support bond mode '$d->{bond_mode}\n"; | |
1240 | ||
a68760b6 DM |
1241 | if ($d->{bond_mode} eq 'lacp-balance-slb') { |
1242 | &$set_ovs_option($d, lacp => 'active'); | |
1243 | &$set_ovs_option($d, bond_mode => 'balance-slb'); | |
1244 | } elsif ($d->{bond_mode} eq 'lacp-balance-tcp') { | |
1245 | &$set_ovs_option($d, lacp => 'active'); | |
1246 | &$set_ovs_option($d, bond_mode => 'balance-tcp'); | |
1247 | } else { | |
1248 | &$set_ovs_option($d, lacp => undef); | |
1249 | &$set_ovs_option($d, bond_mode => $d->{bond_mode}); | |
1250 | } | |
9b7b13a1 DM |
1251 | $done->{bond_mode} = 1; |
1252 | ||
1253 | $raw .= "\tovs_bonds $d->{ovs_bonds}\n" if $d->{ovs_bonds}; | |
1254 | $done->{ovs_bonds} = 1; | |
1255 | } | |
1256 | ||
9b7b13a1 DM |
1257 | $raw .= "\tovs_type $d->{type}\n"; |
1258 | $done->{ovs_type} = 1; | |
1259 | ||
1260 | if ($d->{ovs_bridge}) { | |
b7c4f378 AD |
1261 | |
1262 | if ($ifupdown2) { | |
1263 | $raw = "auto $iface\n$raw"; | |
1264 | } else { | |
1265 | $raw = "allow-$d->{ovs_bridge} $iface\n$raw"; | |
1266 | } | |
1267 | ||
9b7b13a1 DM |
1268 | $raw .= "\tovs_bridge $d->{ovs_bridge}\n"; |
1269 | $done->{ovs_bridge} = 1; | |
1270 | } | |
9b7b13a1 DM |
1271 | } |
1272 | ||
48ab17b3 WB |
1273 | if ($first_block) { |
1274 | # print other settings | |
9a052564 | 1275 | foreach my $k (sort keys %$d) { |
48ab17b3 WB |
1276 | next if $done->{$k}; |
1277 | next if !$d->{$k}; | |
1278 | $raw .= "\t$k $d->{$k}\n"; | |
1279 | } | |
e143e9d8 DM |
1280 | } |
1281 | ||
48ab17b3 | 1282 | foreach my $option (@{$d->{"options$suffix"}}) { |
e143e9d8 DM |
1283 | $raw .= "\t$option\n"; |
1284 | } | |
1285 | ||
ed4e6e0c | 1286 | # add comments |
48ab17b3 | 1287 | my $comments = $d->{"comments$suffix"} || ''; |
ed4e6e0c DM |
1288 | foreach my $cl (split(/\n/, $comments)) { |
1289 | $raw .= "#$cl\n"; | |
72ca6520 DM |
1290 | } |
1291 | ||
e143e9d8 DM |
1292 | $raw .= "\n"; |
1293 | ||
1294 | return $raw; | |
1295 | } | |
1296 | ||
78325766 | 1297 | |
e143e9d8 | 1298 | sub write_etc_network_interfaces { |
48ab17b3 | 1299 | my ($filename, $fh, $config) = @_; |
b7c4f378 AD |
1300 | my $ifupdown2 = -e '/usr/share/ifupdown2'; |
1301 | my $raw = __write_etc_network_interfaces($config, $ifupdown2); | |
1b505ae2 | 1302 | PVE::Tools::safe_print($filename, $fh, encode('UTF-8', $raw)); |
78325766 WB |
1303 | } |
1304 | sub __write_etc_network_interfaces { | |
b7c4f378 | 1305 | my ($config, $ifupdown2) = @_; |
48ab17b3 WB |
1306 | |
1307 | my $ifaces = $config->{ifaces}; | |
1308 | my @options = @{$config->{options}}; | |
e143e9d8 | 1309 | |
9b7b13a1 DM |
1310 | my $used_ports = {}; |
1311 | ||
1312 | foreach my $iface (keys %$ifaces) { | |
1313 | my $d = $ifaces->{$iface}; | |
1314 | ||
1315 | my $ports = ''; | |
1316 | foreach my $k (qw(bridge_ports ovs_ports slaves ovs_bonds)) { | |
1317 | $ports .= " $d->{$k}" if $d->{$k}; | |
1318 | } | |
1319 | ||
1320 | foreach my $p (PVE::Tools::split_list($ports)) { | |
1321 | die "port '$p' is already used on interface '$used_ports->{$p}'\n" | |
1322 | if $used_ports->{$p} && $used_ports->{$p} ne $iface; | |
1323 | $used_ports->{$p} = $iface; | |
1324 | } | |
1325 | } | |
1326 | ||
1327 | # delete unused OVS ports | |
1328 | foreach my $iface (keys %$ifaces) { | |
1329 | my $d = $ifaces->{$iface}; | |
94d786b3 | 1330 | if ($d->{type} eq 'OVSPort' || $d->{type} eq 'OVSIntPort' || |
9b7b13a1 DM |
1331 | $d->{type} eq 'OVSBond') { |
1332 | my $brname = $used_ports->{$iface}; | |
94d786b3 | 1333 | if (!$brname || !$ifaces->{$brname}) { |
3dabe28a | 1334 | if ($iface =~ /^$PVE::Network::PHYSICAL_NIC_RE/) { |
516a50a4 WB |
1335 | $ifaces->{$iface} = { type => 'eth', |
1336 | exists => 1, | |
1337 | method => 'manual', | |
1338 | families => ['inet'] }; | |
1339 | } else { | |
1340 | delete $ifaces->{$iface}; | |
1341 | } | |
9b7b13a1 DM |
1342 | next; |
1343 | } | |
1344 | my $bd = $ifaces->{$brname}; | |
1345 | if ($bd->{type} ne 'OVSBridge') { | |
1346 | delete $ifaces->{$iface}; | |
1347 | next; | |
1348 | } | |
1349 | } | |
1350 | } | |
1351 | ||
1352 | # create OVS bridge ports | |
1353 | foreach my $iface (keys %$ifaces) { | |
1354 | my $d = $ifaces->{$iface}; | |
1355 | if ($d->{type} eq 'OVSBridge' && $d->{ovs_ports}) { | |
1356 | foreach my $p (split (/\s+/, $d->{ovs_ports})) { | |
1357 | my $n = $ifaces->{$p}; | |
1358 | die "OVS bridge '$iface' - unable to find port '$p'\n" | |
1359 | if !$n; | |
21d32c95 | 1360 | $n->{autostart} = 0; |
9b7b13a1 DM |
1361 | if ($n->{type} eq 'eth') { |
1362 | $n->{type} = 'OVSPort'; | |
94d786b3 | 1363 | $n->{ovs_bridge} = $iface; |
9b7b13a1 DM |
1364 | } elsif ($n->{type} eq 'OVSBond' || $n->{type} eq 'OVSPort' || |
1365 | $n->{type} eq 'OVSIntPort') { | |
1366 | $n->{ovs_bridge} = $iface; | |
1367 | } else { | |
1368 | die "interface '$p' is not defined as OVS port/bond\n"; | |
1369 | } | |
9b053d70 AD |
1370 | |
1371 | &$check_mtu($ifaces, $iface, $p); | |
9b7b13a1 DM |
1372 | } |
1373 | } | |
1374 | } | |
1375 | ||
1376 | # check OVS bond ports | |
1377 | foreach my $iface (keys %$ifaces) { | |
1378 | my $d = $ifaces->{$iface}; | |
1379 | if ($d->{type} eq 'OVSBond' && $d->{ovs_bonds}) { | |
1380 | foreach my $p (split (/\s+/, $d->{ovs_bonds})) { | |
1381 | my $n = $ifaces->{$p}; | |
1382 | die "OVS bond '$iface' - unable to find slave '$p'\n" | |
1383 | if !$n; | |
1384 | die "OVS bond '$iface' - wrong interface type on slave '$p' " . | |
1385 | "('$n->{type}' != 'eth')\n" if $n->{type} ne 'eth'; | |
9b053d70 | 1386 | &$check_mtu($ifaces, $iface, $p); |
9b7b13a1 DM |
1387 | } |
1388 | } | |
1389 | } | |
1390 | ||
0115696f AD |
1391 | # check bond |
1392 | foreach my $iface (keys %$ifaces) { | |
1393 | my $d = $ifaces->{$iface}; | |
1394 | if ($d->{type} eq 'bond' && $d->{slaves}) { | |
1395 | foreach my $p (split (/\s+/, $d->{slaves})) { | |
1396 | my $n = $ifaces->{$p}; | |
1397 | ||
1398 | die "bond '$iface' - unable to find slave '$p'\n" | |
1399 | if !$n; | |
1400 | die "bond '$iface' - wrong interface type on slave '$p' " . | |
1401 | "('$n->{type}' != 'eth')\n" if $n->{type} ne 'eth'; | |
9b053d70 | 1402 | &$check_mtu($ifaces, $iface, $p); |
0115696f AD |
1403 | } |
1404 | } | |
1405 | } | |
1406 | ||
95aa8788 AD |
1407 | # check vxlan |
1408 | my $vxlans = {}; | |
1409 | foreach my $iface (keys %$ifaces) { | |
1410 | my $d = $ifaces->{$iface}; | |
1411 | ||
1412 | if ($d->{type} eq 'vxlan' && $d->{'vxlan-id'}) { | |
1413 | my $vxlanid = $d->{'vxlan-id'}; | |
94d786b3 | 1414 | die "iface $iface - duplicate vxlan-id $vxlanid already used in $vxlans->{$vxlanid}\n" if $vxlans->{$vxlanid}; |
95aa8788 AD |
1415 | $vxlans->{$vxlanid} = $iface; |
1416 | } | |
1417 | ||
1418 | my $ips = 0; | |
1419 | ++$ips if defined $d->{'vxlan-svcnodeip'}; | |
1420 | ++$ips if defined $d->{'vxlan-remoteip'}; | |
1421 | ++$ips if defined $d->{'vxlan-local-tunnelip'}; | |
1422 | if ($ips > 1) { | |
94d786b3 | 1423 | die "iface $iface - vxlan-svcnodeip, vxlan-remoteip and vxlan-localtunnelip are mutually exclusive\n"; |
95aa8788 AD |
1424 | } |
1425 | ||
1426 | if (defined($d->{'vxlan-svcnodeip'}) != defined($d->{'vxlan-physdev'})) { | |
94d786b3 | 1427 | die "iface $iface - vxlan-svcnodeip and vxlan-physdev must be define together\n"; |
95aa8788 | 1428 | } |
9b053d70 | 1429 | #fixme : check if vxlan mtu is lower than 50bytes than physical interface where tunnel is going out |
95aa8788 AD |
1430 | } |
1431 | ||
c4e56470 AD |
1432 | # check vlan |
1433 | foreach my $iface (keys %$ifaces) { | |
1434 | my $d = $ifaces->{$iface}; | |
1435 | if ($d->{type} eq 'vlan' && $iface =~ m/^(\S+)\.\d+$/) { | |
1436 | my $p = $1; | |
1437 | my $n = $ifaces->{$p}; | |
1438 | ||
1439 | die "vlan '$iface' - unable to find parent '$p'\n" | |
fc158d0d | 1440 | if !$n; |
c4e56470 AD |
1441 | |
1442 | if ($n->{type} eq 'bridge' && !$n->{bridge_vlan_aware}) { | |
1443 | die "vlan '$iface' - bridge vlan aware is not enabled on parent '$p'\n"; | |
1444 | } elsif ($n->{type} ne 'eth' && $n->{type} ne 'bridge' && $n->{type} ne 'bond') { | |
1445 | die "vlan '$iface' - wrong interface type on parent '$p' " . | |
1446 | "('$n->{type}' != 'eth|bond|bridge' )\n"; | |
1447 | } | |
000e32bc | 1448 | &$check_mtu($ifaces, $p, $iface); |
c4e56470 AD |
1449 | } |
1450 | } | |
1451 | ||
9a052564 AD |
1452 | # check bridgeport option |
1453 | my $bridgeports = {}; | |
1454 | my $bridges = {}; | |
1455 | foreach my $iface (keys %$ifaces) { | |
1456 | my $d = $ifaces->{$iface}; | |
1457 | if ($d->{type} eq 'bridge') { | |
1458 | foreach my $p (split (/\s+/, $d->{bridge_ports})) { | |
514c2138 | 1459 | $p =~ s/\.\d+$//; |
9a052564 AD |
1460 | my $n = $ifaces->{$p}; |
1461 | die "bridge '$iface' - unable to find bridge port '$p'\n" | |
1462 | if !$n; | |
9b053d70 | 1463 | &$check_mtu($ifaces, $iface, $p); |
9a052564 AD |
1464 | $bridgeports->{$p} = $iface; |
1465 | } | |
1466 | $bridges->{$iface} = $d; | |
1467 | } | |
1468 | } | |
1469 | ||
1470 | foreach my $iface (keys %$ifaces) { | |
1471 | my $d = $ifaces->{$iface}; | |
1472 | ||
1473 | foreach my $k (qw(bridge-learning bridge-arp-nd-suppress bridge-unicast-flood bridge-multicast-flood bridge-access)) { | |
94d786b3 | 1474 | die "iface $iface - $k: bridge port specific options can be used only on interfaces attached to a bridge\n" |
9a052564 AD |
1475 | if $d->{$k} && !$bridgeports->{$iface}; |
1476 | } | |
1477 | ||
1478 | if ($d->{'bridge-access'} && !$bridges->{$bridgeports->{$iface}}->{bridge_vlan_aware}) { | |
94d786b3 | 1479 | die "iface $iface - bridge-access option can be only used if interface is in a vlan aware bridge\n"; |
9a052564 AD |
1480 | } |
1481 | } | |
1482 | ||
9d27ef41 WB |
1483 | my $raw = <<'NETWORKDOC'; |
1484 | # network interface settings; autogenerated | |
1485 | # Please do NOT modify this file directly, unless you know what | |
1486 | # you're doing. | |
1487 | # | |
1488 | # If you want to manage part of the network configuration manually, | |
1489 | # please utilize the 'source' or 'source-directory' directives to do | |
1490 | # so. | |
1491 | # PVE will preserve these directives, but will NOT its network | |
1492 | # configuration from sourced files, so do not attempt to move any of | |
1493 | # the PVE managed interfaces into external files! | |
1494 | ||
1495 | NETWORKDOC | |
e143e9d8 DM |
1496 | |
1497 | my $printed = {}; | |
1498 | ||
ed4e6e0c | 1499 | my $if_type_hash = { |
562fad0b WB |
1500 | loopback => 100000, |
1501 | eth => 200000, | |
1502 | bond => 300000, | |
1503 | bridge => 400000, | |
95aa8788 | 1504 | vxlan => 500000, |
9b7b13a1 | 1505 | }; |
ed4e6e0c DM |
1506 | |
1507 | my $lookup_type_prio = sub { | |
1508 | my $iface = shift; | |
1509 | ||
9c808945 AD |
1510 | my $child = 0; |
1511 | if ($iface =~ m/^(\S+)(\.|:)\d+$/) { | |
ed4e6e0c | 1512 | $iface = $1; |
9c808945 | 1513 | $child = 1; |
ed4e6e0c DM |
1514 | } |
1515 | ||
1516 | my $pri; | |
1517 | if ($iface eq 'lo') { | |
1518 | $pri = $if_type_hash->{loopback}; | |
3dabe28a | 1519 | } elsif ($iface =~ m/^$PVE::Network::PHYSICAL_NIC_RE$/) { |
9c808945 | 1520 | $pri = $if_type_hash->{eth} + $child; |
ed4e6e0c | 1521 | } elsif ($iface =~ m/^bond\d+$/) { |
9c808945 | 1522 | $pri = $if_type_hash->{bond} + $child; |
ed4e6e0c | 1523 | } elsif ($iface =~ m/^vmbr\d+$/) { |
9c808945 | 1524 | $pri = $if_type_hash->{bridge} + $child; |
ed4e6e0c DM |
1525 | } |
1526 | ||
7a822100 | 1527 | return $pri; |
ed4e6e0c DM |
1528 | }; |
1529 | ||
c9dc8645 DM |
1530 | foreach my $iface (sort { |
1531 | my $ref1 = $ifaces->{$a}; | |
1532 | my $ref2 = $ifaces->{$b}; | |
7a822100 WB |
1533 | my $tp1 = &$lookup_type_prio($a); |
1534 | my $tp2 = &$lookup_type_prio($b); | |
e143e9d8 | 1535 | |
7a822100 WB |
1536 | # Only recognized types are in relation to each other. If one type |
1537 | # is unknown then only consider the interfaces' priority attributes. | |
1538 | $tp1 = $tp2 = 0 if !defined($tp1) || !defined($tp2); | |
1539 | ||
1540 | my $p1 = $tp1 + ($ref1->{priority} // 50000); | |
1541 | my $p2 = $tp2 + ($ref2->{priority} // 50000); | |
ed4e6e0c DM |
1542 | |
1543 | return $p1 <=> $p2 if $p1 != $p2; | |
c9dc8645 DM |
1544 | |
1545 | return $a cmp $b; | |
11e7facb | 1546 | } keys %$ifaces) { |
c9dc8645 DM |
1547 | next if $printed->{$iface}; |
1548 | ||
11e7facb | 1549 | my $d = $ifaces->{$iface}; |
562fad0b WB |
1550 | my $pri = $d->{priority} // 0; |
1551 | if (@options && $options[0]->[0] < $pri) { | |
48ab17b3 WB |
1552 | do { |
1553 | $raw .= (shift @options)->[1] . "\n"; | |
562fad0b | 1554 | } while (@options && $options[0]->[0] < $pri); |
48ab17b3 WB |
1555 | $raw .= "\n"; |
1556 | } | |
1557 | ||
c9dc8645 | 1558 | $printed->{$iface} = 1; |
48ab17b3 WB |
1559 | $raw .= "auto $iface\n" if $d->{autostart}; |
1560 | my $i = 0; # some options should be printed only once | |
b7c4f378 | 1561 | $raw .= __interface_to_string($iface, $d, $_, !$i++, $ifupdown2) foreach @{$d->{families}}; |
e143e9d8 | 1562 | } |
48ab17b3 WB |
1563 | |
1564 | $raw .= $_->[1] . "\n" foreach @options; | |
78325766 | 1565 | return $raw; |
e143e9d8 DM |
1566 | } |
1567 | ||
1568 | register_file('interfaces', "/etc/network/interfaces", | |
1569 | \&read_etc_network_interfaces, | |
1570 | \&write_etc_network_interfaces); | |
1571 | ||
ddd3d224 DM |
1572 | |
1573 | sub read_iscsi_initiatorname { | |
1574 | my ($filename, $fd) = @_; | |
1575 | ||
1576 | while (defined(my $line = <$fd>)) { | |
1577 | if ($line =~ m/^InitiatorName=(\S+)$/) { | |
1578 | return $1; | |
1579 | } | |
1580 | } | |
1581 | ||
1582 | return 'undefined'; | |
1583 | } | |
1584 | ||
1585 | register_file('initiatorname', "/etc/iscsi/initiatorname.iscsi", | |
1586 | \&read_iscsi_initiatorname); | |
1587 | ||
6c3aef09 DM |
1588 | sub read_apt_auth { |
1589 | my ($filename, $fd) = @_; | |
1590 | ||
1591 | local $/; | |
1592 | ||
b8e02bc2 | 1593 | my $raw = defined($fd) ? <$fd> : ''; |
6c3aef09 DM |
1594 | |
1595 | $raw =~ s/^\s+//; | |
1596 | ||
1597 | ||
1598 | my @tokens = split(/\s+/, $raw); | |
1599 | ||
1600 | my $data = {}; | |
1601 | ||
1602 | my $machine; | |
1603 | while (defined(my $tok = shift @tokens)) { | |
1604 | ||
1605 | $machine = shift @tokens if $tok eq 'machine'; | |
1606 | next if !$machine; | |
1607 | $data->{$machine} = {} if !$data->{$machine}; | |
1608 | ||
1609 | $data->{$machine}->{login} = shift @tokens if $tok eq 'login'; | |
1610 | $data->{$machine}->{password} = shift @tokens if $tok eq 'password'; | |
1611 | }; | |
1612 | ||
1613 | return $data; | |
1614 | } | |
1615 | ||
1616 | my $format_apt_auth_data = sub { | |
1617 | my $data = shift; | |
1618 | ||
1619 | my $raw = ''; | |
1620 | ||
1621 | foreach my $machine (sort keys %$data) { | |
1622 | my $d = $data->{$machine}; | |
1623 | $raw .= "machine $machine\n"; | |
1624 | $raw .= " login $d->{login}\n" if $d->{login}; | |
1625 | $raw .= " password $d->{password}\n" if $d->{password}; | |
1626 | $raw .= "\n"; | |
1627 | } | |
1628 | ||
1629 | return $raw; | |
1630 | }; | |
1631 | ||
1632 | sub write_apt_auth { | |
1633 | my ($filename, $fh, $data) = @_; | |
1634 | ||
1635 | my $raw = &$format_apt_auth_data($data); | |
1636 | ||
1637 | die "write failed: $!" unless print $fh "$raw\n"; | |
1638 | ||
1639 | return $data; | |
1640 | } | |
1641 | ||
1642 | sub update_apt_auth { | |
1643 | my ($filename, $fh, $data) = @_; | |
1644 | ||
1645 | my $orig = read_apt_auth($filename, $fh); | |
1646 | ||
1647 | foreach my $machine (keys %$data) { | |
1648 | $orig->{$machine} = $data->{$machine}; | |
1649 | } | |
1650 | ||
1651 | return &$format_apt_auth_data($orig); | |
1652 | } | |
1653 | ||
1654 | register_file('apt-auth', "/etc/apt/auth.conf", | |
1655 | \&read_apt_auth, \&write_apt_auth, | |
b7aae8e5 | 1656 | \&update_apt_auth, perm => 0640); |
6c3aef09 | 1657 | |
e143e9d8 | 1658 | 1; |