3 # todo: maybe we do not need update_file() ?
13 use Fcntl
qw(:DEFAULT :flock);
15 use PVE
::Exception
qw(raise_param_exc);
18 use Storable
qw(dclone);
23 our @EXPORT_OK = qw(read_file write_file register_file);
32 '/etc/network/interfaces' => '/etc/network/interfaces.new',
35 # to enable cached operation, you need to call 'inotify_init'
36 # inotify handles are a limited resource, so use with care (only
37 # enable the cache if you really need it)
39 # Note: please close the inotify handle after you fork
41 sub ccache_default_writer
{
42 my ($filename, $data) = @_;
44 die "undefined config writer for '$filename' :ERROR";
47 sub ccache_default_parser
{
48 my ($filename, $srcfd) = @_;
50 die "undefined config reader for '$filename' :ERROR";
53 sub ccache_compute_diff
{
54 my ($filename, $shadow) = @_;
58 open (TMP
, "diff -b -N -u '$filename' '$shadow'|");
60 while (my $line = <TMP
>) {
66 $diff = undef if !$diff;
74 foreach my $uid (keys %$ccacheregex) {
75 my $ccinfo = $ccacheregex->{$uid};
76 my $dir = $ccinfo->{dir
};
77 my $regex = $ccinfo->{regex
};
78 if ($filename =~ m
|^$dir/+$regex$|) {
79 if (!$ccache->{$filename}) {
81 while (my ($k, $v) = each %$ccinfo) {
84 $ccache->{$filename} = $cp;
86 return ($ccache->{$filename}, $filename);
90 $filename = $ccachemap->{$filename} if defined ($ccachemap->{$filename});
92 die "file '$filename' not added :ERROR" if !defined ($ccache->{$filename});
94 return ($ccache->{$filename}, $filename);
98 my ($fileid, $data, $full) = @_;
100 my ($ccinfo, $filename) = ccache_info
($fileid);
102 my $writer = $ccinfo->{writer
};
104 my $realname = $filename;
107 if ($shadow = $shadowfiles->{$filename}) {
111 my $perm = $ccinfo->{perm
} || 0644;
113 my $tmpname = "$realname.tmp.$$";
117 my $fh = IO
::File-
>new($tmpname, O_WRONLY
|O_CREAT
, $perm);
118 die "unable to open file '$tmpname' - $!\n" if !$fh;
120 $res = &$writer($filename, $fh, $data);
122 die "closing file '$tmpname' failed - $!\n" unless close $fh;
126 $ccinfo->{version
} = undef;
133 if (!rename($tmpname, $realname)) {
134 my $msg = "close (rename) atomic file '$filename' failed: $!\n";
140 if ($shadow && $full) {
141 $diff = ccache_compute_diff
($filename, $shadow);
145 return { data
=> $res, changes
=> $diff };
152 my ($fileid, $data, @args) = @_;
154 my ($ccinfo, $filename) = ccache_info
($fileid);
156 my $update = $ccinfo->{update
};
158 die "unable to update/merge data" if !$update;
160 my $lkfn = "$filename.lock";
168 $fd = IO
::File-
>new ($filename, "r");
170 my $new = &$update($filename, $fd, $data, @args);
173 PVE
::Tools
::file_set_contents
($filename, $new, $ccinfo->{perm
});
179 PVE
::Tools
::lock_file
($lkfn, $timeout, $code);
182 close($fd) if defined($fd);
189 sub discard_changes
{
190 my ($fileid, $full) = @_;
192 my ($ccinfo, $filename) = ccache_info
($fileid);
194 if (my $copy = $shadowfiles->{$filename}) {
198 return read_file
($filename, $full);
202 my ($fileid, $full) = @_;
206 my ($ccinfo, $filename) = ccache_info
($fileid);
208 $parser = $ccinfo->{parser
};
213 poll
() if $inotify; # read new inotify events
215 $versions->{$filename} = 0 if !defined ($versions->{$filename});
217 my $cver = $versions->{$filename};
219 if (my $copy = $shadowfiles->{$filename}) {
220 if ($fd = IO
::File-
>new ($copy, "r")) {
223 $fd = IO
::File-
>new ($filename, "r");
226 $fd = IO
::File-
>new ($filename, "r");
229 my $acp = $ccinfo->{always_call_parser
};
232 $ccinfo->{version
} = undef;
233 $ccinfo->{data
} = undef;
234 $ccinfo->{diff
} = undef;
235 return undef if !$acp;
238 my $noclone = $ccinfo->{noclone
};
241 if (!$ccinfo->{nocache
} &&
242 $inotify && $versions->{$filename} &&
243 defined ($ccinfo->{data
}) &&
244 defined ($ccinfo->{version
}) &&
245 ($ccinfo->{readonce
} ||
246 ($ccinfo->{version
} == $versions->{$filename}))) {
249 if (!$noclone && ref ($ccinfo->{data
})) {
250 $ret->{data
} = dclone
($ccinfo->{data
});
252 $ret->{data
} = $ccinfo->{data
};
254 $ret->{changes
} = $ccinfo->{diff
};
256 return $full ?
$ret : $ret->{data
};
262 $diff = ccache_compute_diff
($filename, $shadow);
265 my $res = &$parser($filename, $fd);
267 if (!$ccinfo->{nocache
}) {
268 $ccinfo->{version
} = $cver;
271 # we cache data with references, so we always need to
272 # dclone this data. Else the original data may get
274 $ccinfo->{data
} = $res;
277 $ccinfo->{diff
} = $diff;
280 if (!$noclone && ref ($ccinfo->{data
})) {
281 $ret->{data
} = dclone
($ccinfo->{data
});
283 $ret->{data
} = $ccinfo->{data
};
285 $ret->{changes
} = $ccinfo->{diff
};
287 return $full ?
$ret : $ret->{data
};
290 sub parse_ccache_options
{
291 my ($ccinfo, %options) = @_;
293 foreach my $opt (keys %options) {
294 my $v = $options{$opt};
295 if ($opt eq 'readonce') {
296 $ccinfo->{$opt} = $v;
297 } elsif ($opt eq 'nocache') {
298 $ccinfo->{$opt} = $v;
299 } elsif ($opt eq 'shadow') {
300 $ccinfo->{$opt} = $v;
301 } elsif ($opt eq 'perm') {
302 $ccinfo->{$opt} = $v;
303 } elsif ($opt eq 'noclone') {
304 # noclone flag for large read-only data chunks like aplinfo
305 $ccinfo->{$opt} = $v;
306 } elsif ($opt eq 'always_call_parser') {
307 # when set, we call parser even when the file does not exists.
308 # this allows the parser to return some default
309 $ccinfo->{$opt} = $v;
311 die "internal error - unsupported option '$opt'";
317 my ($id, $filename, $parser, $writer, $update, %options) = @_;
319 die "can't register file '$filename' after inotify_init" if $inotify;
321 die "file '$filename' already added :ERROR" if defined ($ccache->{$filename});
322 die "ID '$id' already used :ERROR" if defined ($ccachemap->{$id});
327 $ccinfo->{parser
} = $parser || \
&ccache_default_parser
;
328 $ccinfo->{writer
} = $writer || \
&ccache_default_writer
;
329 $ccinfo->{update
} = $update;
331 parse_ccache_options
($ccinfo, %options);
333 if ($options{shadow
}) {
334 $shadowfiles->{$filename} = $options{shadow
};
337 $ccachemap->{$id} = $filename;
338 $ccache->{$filename} = $ccinfo;
342 my ($dir, $regex, $parser, $writer, $update, %options) = @_;
344 die "can't register regex after inotify_init" if $inotify;
346 my $uid = "$dir/$regex";
347 die "regular expression '$uid' already added :ERROR" if defined ($ccacheregex->{$uid});
351 $ccinfo->{dir
} = $dir;
352 $ccinfo->{regex
} = $regex;
353 $ccinfo->{parser
} = $parser || \
&ccache_default_parser
;
354 $ccinfo->{writer
} = $writer || \
&ccache_default_writer
;
355 $ccinfo->{update
} = $update;
357 parse_ccache_options
($ccinfo, %options);
359 $ccacheregex->{$uid} = $ccinfo;
365 if ($inotify_pid != $$) {
366 syslog
('err', "got inotify poll request in wrong process - disabling inotify");
369 1 while $inotify && $inotify->poll;
374 foreach my $filename (keys %$ccache) {
375 $ccache->{$filename}->{version
} = undef;
376 $ccache->{$filename}->{data
} = undef;
377 $ccache->{$filename}->{diff
} = undef;
387 die "only one inotify instance allowed" if $inotify;
389 $inotify = Linux
::Inotify2-
>new()
390 || die "Unable to create new inotify object: $!";
392 $inotify->blocking (0);
397 foreach my $fn (keys %$ccache) {
398 my $dir = dirname
($fn);
399 my $base = basename
($fn);
401 $dirhash->{$dir}->{$base} = $fn;
403 if (my $sf = $shadowfiles->{$fn}) {
404 $base = basename
($sf);
405 $dir = dirname
($sf);
406 $dirhash->{$dir}->{$base} = $fn; # change version of original file!
410 foreach my $uid (keys %$ccacheregex) {
411 my $ccinfo = $ccacheregex->{$uid};
412 $dirhash->{$ccinfo->{dir
}}->{_regex
} = 1;
417 foreach my $dir (keys %$dirhash) {
419 my $evlist = IN_MODIFY
|IN_ATTRIB
|IN_MOVED_FROM
|IN_MOVED_TO
|IN_DELETE
|IN_CREATE
;
420 $inotify->watch ($dir, $evlist, sub {
424 if ($inotify_pid != $$) {
425 syslog
('err', "got inotify event in wrong process");
428 if ($e->IN_ISDIR || !$name) {
432 if ($e->IN_Q_OVERFLOW) {
433 syslog
('info', "got inotify overflow - flushing cache");
438 if ($e->IN_UNMOUNT) {
439 syslog
('err', "got 'unmount' event on '$name' - disabling inotify");
442 if ($e->IN_IGNORED) {
443 syslog
('err', "got 'ignored' event on '$name' - disabling inotify");
447 if ($dirhash->{$dir}->{_regex
}) {
448 foreach my $uid (keys %$ccacheregex) {
449 my $ccinfo = $ccacheregex->{$uid};
450 next if $dir ne $ccinfo->{dir
};
451 my $regex = $ccinfo->{regex
};
452 if ($regex && ($name =~ m
|^$regex$|)) {
454 my $fn = "$dir/$name";
456 #print "VERSION:$fn:$versions->{$fn}\n";
459 } elsif (my $fn = $dirhash->{$dir}->{$name}) {
462 #print "VERSION:$fn:$versions->{$fn}\n";
467 foreach my $dir (keys %$dirhash) {
468 foreach my $name (keys %{$dirhash->{$dir}}) {
469 if ($name eq '_regex') {
470 foreach my $uid (keys %$ccacheregex) {
471 my $ccinfo = $ccacheregex->{$uid};
472 next if $dir ne $ccinfo->{dir
};
473 my $re = $ccinfo->{regex
};
474 if (my $fd = IO
::Dir-
>new ($dir)) {
475 while (defined(my $de = $fd->read)) {
476 if ($de =~ m/^$re$/) {
478 $versions->{$fn}++; # init with version
479 #print "init:$fn:$versions->{$fn}\n";
485 my $fn = $dirhash->{$dir}->{$name};
486 $versions->{$fn}++; # init with version
487 #print "init:$fn:$versions->{$fn}\n";
497 return $cached_nodename if $cached_nodename;
499 my ($sysname, $nodename) = POSIX
::uname
();
501 $nodename =~ s/\..*$//; # strip domain part, if any
503 die "unable to read node name\n" if !$nodename;
505 $cached_nodename = $nodename;
507 return $cached_nodename;
510 sub read_etc_hostname
{
511 my ($filename, $fd) = @_;
513 my $hostname = <$fd>;
517 $hostname =~ s/\..*$//; # strip domain part, if any
522 sub write_etc_hostname
{
523 my ($filename, $fh, $hostname) = @_;
525 die "write failed: $!" unless print $fh "$hostname\n";
530 register_file
('hostname', "/etc/hostname",
532 \
&write_etc_hostname
);
534 sub read_etc_resolv_conf
{
535 my ($filename, $fh) = @_;
540 while (my $line = <$fh>) {
542 if ($line =~ m/^(search|domain)\s+(\S+)\s*/) {
544 } elsif ($line =~ m/^\s*nameserver\s+($PVE::Tools::IPRE)\s*/) {
547 $res->{"dns$nscount"} = $1;
555 sub update_etc_resolv_conf
{
556 my ($filename, $fh, $resolv, @args) = @_;
560 $data = "search $resolv->{search}\n"
561 if $resolv->{search
};
564 foreach my $k ("dns1", "dns2", "dns3") {
565 my $ns = $resolv->{$k};
566 if ($ns && $ns ne '0.0.0.0' && !$written->{$ns}) {
568 $data .= "nameserver $ns\n";
572 while (my $line = <$fh>) {
573 next if $line =~ m/^(search|domain|nameserver)\s+/;
580 register_file
('resolvconf', "/etc/resolv.conf",
581 \
&read_etc_resolv_conf
, undef,
582 \
&update_etc_resolv_conf
);
584 sub read_etc_timezone
{
585 my ($filename, $fd) = @_;
587 my $timezone = <$fd>;
594 sub write_etc_timezone
{
595 my ($filename, $fh, $timezone) = @_;
597 my $tzinfo = "/usr/share/zoneinfo/$timezone";
599 raise_param_exc
({ 'timezone' => "No such timezone" })
602 ($timezone) = $timezone =~ m/^(.*)$/; # untaint
604 print $fh "$timezone\n";
606 unlink ("/etc/localtime");
607 symlink ("/usr/share/zoneinfo/$timezone", "/etc/localtime");
611 register_file
('timezone', "/etc/timezone",
613 \
&write_etc_timezone
);
615 sub read_active_workers
{
616 my ($filename, $fh) = @_;
621 while (defined (my $line = <$fh>)) {
622 if ($line =~ m/^(\S+)\s(0|1)(\s([0-9A-Za-z]{8})(\s(\s*\S.*))?)?$/) {
627 if ((my $task = PVE
::Tools
::upid_decode
($upid, 1))) {
628 $task->{upid
} = $upid;
629 $task->{saved
} = $saved;
630 $task->{endtime
} = hex($endtime) if $endtime;
631 $task->{status
} = $status if $status;
635 warn "unable to parse line: $line";
643 sub write_active_workers
{
644 my ($filename, $fh, $tasklist) = @_;
647 foreach my $task (@$tasklist) {
648 my $upid = $task->{upid
};
649 my $saved = $task->{saved
} ?
1 : 0;
650 if ($task->{endtime
}) {
651 if ($task->{status
}) {
652 $raw .= sprintf("%s %s %08X %s\n", $upid, $saved, $task->{endtime
}, $task->{status
});
654 $raw .= sprintf("%s %s %08X\n", $upid, $saved, $task->{endtime
});
657 $raw .= "$upid $saved\n";
661 PVE
::Tools
::safe_print
($filename, $fh, $raw) if $raw;
664 register_file
('active', "/var/log/pve/tasks/active",
665 \
&read_active_workers
,
666 \
&write_active_workers
);
669 our $bond_modes = { 'balance-rr' => 0,
670 'active-backup' => 1,
678 my $ovs_bond_modes = {
679 'active-backup' => 1,
681 'lacp-balance-slb' => 1,
682 'lacp-balance-tcp' => 1,
685 #sub get_bond_modes {
686 # return $bond_modes;
689 my $parse_ovs_option = sub {
693 foreach my $kv (split (/\s+/, $data || '')) {
694 my ($k, $v) = split('=', $kv, 2);
695 $opts->{$k} = $v if $k && $v;
700 my $set_ovs_option = sub {
701 my ($d, %params) = @_;
703 my $opts = &$parse_ovs_option($d->{ovs_options
});
705 foreach my $k (keys %params) {
715 foreach my $k (keys %$opts) {
716 push @$res, "$k=$opts->{$k}";
719 if (my $new = join(' ', @$res)) {
720 $d->{ovs_options
} = $new;
721 return $d->{ovs_options
};
723 delete $d->{ovs_options
};
728 my $extract_ovs_option = sub {
731 my $opts = &$parse_ovs_option($d->{ovs_options
});
733 my $v = delete $opts->{$name};
736 foreach my $k (keys %$opts) {
737 push @$res, "$k=$opts->{$k}";
740 if (my $new = join(' ', @$res)) {
741 $d->{ovs_options
} = $new;
743 delete $d->{ovs_options
};
752 # <optional> exists => BOOL,
753 # <optional> active => BOOL,
754 # <optional> autostart => BOOL,
755 # <auto> priority => INT,
757 # type => "eth" | "bridge" | "bond" | "loopback" | "OVS*" | ... ,
759 # families => ["inet", "inet6", ...],
761 # method => "manual" | "static" | "dhcp" | ... ,
766 # comments => [ "..." ],
768 # method6 => "manual" | "static" | "dhcp" | ... ,
770 # netmask6 => SUBNET,
772 # comments6 => [ "..." ],
774 # <known options>, # like bridge_ports, ovs_*
776 # # extra/unknown options stored by-family:
777 # options => { <inet options>... }
778 # options6 => { <inet6 options>... }
782 # # mappings end up here as well, as we don't need to understand them
786 sub read_etc_network_interfaces
{
787 my ($filename, $fh) = @_;
788 my $proc_net_dev = IO
::File-
>new('/proc/net/dev', 'r');
789 my $active = PVE
::ProcFSTools
::get_active_network_interfaces
();
790 return __read_etc_network_interfaces
($fh, $proc_net_dev, $active);
793 sub __read_etc_network_interfaces
{
794 my ($fh, $proc_net_dev, $active_ifaces) = @_;
797 my $ifaces = $config->{ifaces
} = {};
798 my $options = $config->{options
} = [];
803 while (defined ($line = <$proc_net_dev>)) {
804 if ($line =~ m/^\s*(eth\d+|en[^:.]+|ib\d+):.*/) {
805 $ifaces->{$1}->{exists} = 1;
808 close($proc_net_dev);
811 # we try to keep order inside the file
812 my $priority = 2; # 1 is reserved for lo
814 SECTION
: while (defined ($line = <$fh>)) {
816 next if $line =~ m/^\s*#/;
818 if ($line =~ m/^\s*auto\s+(.*)$/) {
819 my @aa = split (/\s+/, $1);
821 foreach my $a (@aa) {
822 $ifaces->{$a}->{autostart
} = 1;
825 } elsif ($line =~ m/^\s*iface\s+(\S+)\s+(inet6?)\s+(\S+)\s*$/) {
828 my $f = { method => $3 }; # by family, merged to $d with a $suffix
829 (my $suffix = $family) =~ s/^inet//;
831 my $d = $ifaces->{$i} ||= {};
832 $d->{priority
} = $priority++ if !$d->{priority
};
833 push @{$d->{families
}}, $family;
835 while (defined ($line = <$fh>)) {
837 if ($line =~ m/^\s*#(.*?)\s*$/) {
838 # NOTE: we use 'comments' instead of 'comment' to
839 # avoid automatic utf8 conversion
840 $f->{comments
} = '' if !$f->{comments
};
841 $f->{comments
} .= "$1\n";
842 } elsif ($line =~ m
/^\s
*(?
:iface\s
850 } elsif ($line =~ m/^\s*((\S+)\s+(.+))$/) {
852 my ($id, $value) = ($2, $3);
853 if (($id eq 'address') || ($id eq 'netmask') || ($id eq 'broadcast') || ($id eq 'gateway')) {
855 } elsif ($id eq 'ovs_type' || $id eq 'ovs_options'|| $id eq 'ovs_bridge' ||
856 $id eq 'ovs_bonds' || $id eq 'ovs_ports') {
858 } elsif ($id eq 'slaves' || $id eq 'bridge_ports') {
860 foreach my $p (split (/\s+/, $value)) {
861 next if $p eq 'none';
864 my $str = join (' ', sort keys %{$devs});
866 $d->{$id} .= ' ' . $str if $str;
868 $d->{$id} = $str || '';
870 } elsif ($id eq 'bridge_stp') {
871 if ($value =~ m/^\s*(on|yes)\s*$/i) {
876 } elsif ($id eq 'bridge_fd') {
878 } elsif ($id eq 'bridge_vlan_aware') {
880 } elsif ($id eq 'bond_miimon') {
882 } elsif ($id eq 'bond_xmit_hash_policy') {
884 } elsif ($id eq 'bond_mode') {
886 foreach my $bm (keys %$bond_modes) {
887 my $id = $bond_modes->{$bm};
895 push @{$f->{options
}}, $option;
901 $d->{"$_$suffix"} = $f->{$_} foreach (keys %$f);
902 last SECTION
if !defined($line);
904 } elsif ($line =~ /\w/) {
905 push @$options, [$priority++, $line];
909 foreach my $ifname (@$active_ifaces) {
910 if (my $iface = $ifaces->{$ifname}) {
911 $iface->{active
} = 1;
915 if (!$ifaces->{lo
}) {
916 $ifaces->{lo
}->{priority
} = 1;
917 $ifaces->{lo
}->{method} = 'loopback';
918 $ifaces->{lo
}->{type
} = 'loopback';
919 $ifaces->{lo
}->{autostart
} = 1;
922 foreach my $iface (keys %$ifaces) {
923 my $d = $ifaces->{$iface};
924 if ($iface =~ m/^bond\d+$/) {
925 if (!$d->{ovs_type
}) {
927 } elsif ($d->{ovs_type
} eq 'OVSBond') {
928 $d->{type
} = $d->{ovs_type
};
929 # translate: ovs_options => bond_mode
930 $d->{'bond_mode'} = &$extract_ovs_option($d, 'bond_mode');
931 my $lacp = &$extract_ovs_option($d, 'lacp');
932 if ($lacp && $lacp eq 'active') {
933 if ($d->{'bond_mode'} eq 'balance-slb') {
934 $d->{'bond_mode'} = 'lacp-balance-slb';
937 # Note: balance-tcp needs lacp
938 if ($d->{'bond_mode'} eq 'balance-tcp') {
939 $d->{'bond_mode'} = 'lacp-balance-tcp';
941 my $tag = &$extract_ovs_option($d, 'tag');
942 $d->{ovs_tag
} = $tag if defined($tag);
944 $d->{type
} = 'unknown';
946 } elsif ($iface =~ m/^vmbr\d+$/) {
947 if (!$d->{ovs_type
}) {
948 $d->{type
} = 'bridge';
950 if (!defined ($d->{bridge_fd
})) {
953 if (!defined ($d->{bridge_stp
})) {
954 $d->{bridge_stp
} = 'off';
956 } elsif ($d->{ovs_type
} eq 'OVSBridge') {
957 $d->{type
} = $d->{ovs_type
};
959 $d->{type
} = 'unknown';
961 } elsif ($iface =~ m/^(\S+):\d+$/) {
962 $d->{type
} = 'alias';
963 if (defined ($ifaces->{$1})) {
964 $d->{exists} = $ifaces->{$1}->{exists};
966 $ifaces->{$1}->{exists} = 0;
969 } elsif ($iface =~ m/^(\S+)\.\d+$/) {
971 if (defined ($ifaces->{$1})) {
972 $d->{exists} = $ifaces->{$1}->{exists};
974 $ifaces->{$1}->{exists} = 0;
977 } elsif ($iface =~ m/^(?:eth\d+|en[^:.]+|ib\d+)$/) {
978 if (!$d->{ovs_type
}) {
980 } elsif ($d->{ovs_type
} eq 'OVSPort') {
981 $d->{type
} = $d->{ovs_type
};
982 my $tag = &$extract_ovs_option($d, 'tag');
983 $d->{ovs_tag
} = $tag if defined($tag);
985 $d->{type
} = 'unknown';
987 } elsif ($iface =~ m/^lo$/) {
988 $d->{type
} = 'loopback';
990 if (!$d->{ovs_type
}) {
991 $d->{type
} = 'unknown';
992 } elsif ($d->{ovs_type
} eq 'OVSIntPort') {
993 $d->{type
} = $d->{ovs_type
};
994 my $tag = &$extract_ovs_option($d, 'tag');
995 $d->{ovs_tag
} = $tag if defined($tag);
999 $d->{method} = 'manual' if !$d->{method};
1000 $d->{method6
} = 'manual' if !$d->{method6
};
1002 $d->{families
} ||= ['inet'];
1005 # OVS bridges create "allow-$BRIDGE $IFACE" lines which we need to remove
1006 # from the {options} hash for them to be removed correctly.
1007 @$options = grep {defined($_)} map {
1008 my ($pri, $line) = @$_;
1009 if ($line =~ /^allow-(\S+)\s+(.*)$/) {
1011 my @ports = split(/\s+/, $2);
1012 if (defined(my $br = $ifaces->{$bridge})) {
1013 # if this port is part of a bridge, remove it
1014 my %in_ovs_ports = map {$_=>1} split(/\s+/, $br->{ovs_ports
});
1015 @ports = grep { not $in_ovs_ports{$_} } @ports;
1017 # create the allow line for the remaining ports, or delete if empty
1019 [$pri, "allow-$bridge " . join(' ', @ports)];
1024 # don't modify other lines
1032 sub __interface_to_string
{
1033 my ($iface, $d, $family, $first_block) = @_;
1035 (my $suffix = $family) =~ s/^inet//;
1037 return '' if !($d && $d->{"method$suffix"});
1041 $raw .= "iface $iface $family " . $d->{"method$suffix"} . "\n";
1042 $raw .= "\taddress " . $d->{"address$suffix"} . "\n" if $d->{"address$suffix"};
1043 $raw .= "\tnetmask " . $d->{"netmask$suffix"} . "\n" if $d->{"netmask$suffix"};
1044 $raw .= "\tgateway " . $d->{"gateway$suffix"} . "\n" if $d->{"gateway$suffix"};
1045 $raw .= "\tbroadcast " . $d->{"broadcast$suffix"} . "\n" if $d->{"broadcast$suffix"};
1047 my $done = { type
=> 1, priority
=> 1, method => 1, active
=> 1, exists => 1,
1048 comments
=> 1, autostart
=> 1, options
=> 1,
1049 address
=> 1, netmask
=> 1, gateway
=> 1, broadcast
=> 1,
1050 method6
=> 1, families
=> 1, options6
=> 1,
1051 address6
=> 1, netmask6
=> 1, gateway6
=> 1, broadcast6
=> 1 };
1053 if (!$first_block) {
1054 # not printing out options
1055 } elsif ($d->{type
} eq 'bridge') {
1057 my $ports = $d->{bridge_ports
} || 'none';
1058 $raw .= "\tbridge_ports $ports\n";
1059 $done->{bridge_ports
} = 1;
1061 my $v = defined($d->{bridge_stp
}) ?
$d->{bridge_stp
} : 'off';
1062 $raw .= "\tbridge_stp $v\n";
1063 $done->{bridge_stp
} = 1;
1065 $v = defined($d->{bridge_fd
}) ?
$d->{bridge_fd
} : 0;
1066 $raw .= "\tbridge_fd $v\n";
1067 $done->{bridge_fd
} = 1;
1069 if( defined($d->{bridge_vlan_aware
})) {
1070 $raw .= "\tbridge_vlan_aware yes\n";
1072 $done->{bridge_vlan_aware
} = 1;
1074 } elsif ($d->{type
} eq 'bond') {
1076 my $slaves = $d->{slaves
} || 'none';
1077 $raw .= "\tslaves $slaves\n";
1078 $done->{slaves
} = 1;
1080 my $v = defined ($d->{'bond_miimon'}) ?
$d->{'bond_miimon'} : 100;
1081 $raw .= "\tbond_miimon $v\n";
1082 $done->{'bond_miimon'} = 1;
1084 $v = defined ($d->{'bond_mode'}) ?
$d->{'bond_mode'} : 'balance-rr';
1085 $raw .= "\tbond_mode $v\n";
1086 $done->{'bond_mode'} = 1;
1088 if ($d->{'bond_mode'} && $d->{'bond_xmit_hash_policy'} &&
1089 ($d->{'bond_mode'} eq 'balance-xor' || $d->{'bond_mode'} eq '802.3ad')) {
1090 $raw .= "\tbond_xmit_hash_policy $d->{'bond_xmit_hash_policy'}\n";
1092 $done->{'bond_xmit_hash_policy'} = 1;
1094 } elsif ($d->{type
} eq 'OVSBridge') {
1096 $raw .= "\tovs_type $d->{type}\n";
1097 $done->{ovs_type
} = 1;
1099 $raw .= "\tovs_ports $d->{ovs_ports}\n" if $d->{ovs_ports
};
1100 $done->{ovs_ports
} = 1;
1101 } elsif ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' ||
1102 $d->{type
} eq 'OVSBond') {
1104 $d->{autostart
} = 0; # started by the bridge
1106 if (defined($d->{ovs_tag
})) {
1107 &$set_ovs_option($d, tag
=> $d->{ovs_tag
});
1109 $done->{ovs_tag
} = 1;
1111 if ($d->{type
} eq 'OVSBond') {
1113 $d->{bond_mode
} = 'active-backup' if !$d->{bond_mode
};
1115 $ovs_bond_modes->{$d->{bond_mode
}} ||
1116 die "OVS does not support bond mode '$d->{bond_mode}\n";
1118 if ($d->{bond_mode
} eq 'lacp-balance-slb') {
1119 &$set_ovs_option($d, lacp
=> 'active');
1120 &$set_ovs_option($d, bond_mode
=> 'balance-slb');
1121 } elsif ($d->{bond_mode
} eq 'lacp-balance-tcp') {
1122 &$set_ovs_option($d, lacp
=> 'active');
1123 &$set_ovs_option($d, bond_mode
=> 'balance-tcp');
1125 &$set_ovs_option($d, lacp
=> undef);
1126 &$set_ovs_option($d, bond_mode
=> $d->{bond_mode
});
1128 $done->{bond_mode
} = 1;
1130 $raw .= "\tovs_bonds $d->{ovs_bonds}\n" if $d->{ovs_bonds
};
1131 $done->{ovs_bonds
} = 1;
1134 $raw .= "\tovs_type $d->{type}\n";
1135 $done->{ovs_type
} = 1;
1137 if ($d->{ovs_bridge
}) {
1138 $raw = "allow-$d->{ovs_bridge} $iface\n$raw";
1139 $raw .= "\tovs_bridge $d->{ovs_bridge}\n";
1140 $done->{ovs_bridge
} = 1;
1145 # print other settings
1146 foreach my $k (keys %$d) {
1147 next if $done->{$k};
1149 $raw .= "\t$k $d->{$k}\n";
1153 foreach my $option (@{$d->{"options$suffix"}}) {
1154 $raw .= "\t$option\n";
1158 my $comments = $d->{"comments$suffix"} || '';
1159 foreach my $cl (split(/\n/, $comments)) {
1169 sub write_etc_network_interfaces
{
1170 my ($filename, $fh, $config) = @_;
1171 my $raw = __write_etc_network_interfaces
($config);
1172 PVE
::Tools
::safe_print
($filename, $fh, $raw);
1174 sub __write_etc_network_interfaces
{
1177 my $ifaces = $config->{ifaces
};
1178 my @options = @{$config->{options
}};
1180 my $used_ports = {};
1182 foreach my $iface (keys %$ifaces) {
1183 my $d = $ifaces->{$iface};
1186 foreach my $k (qw(bridge_ports ovs_ports slaves ovs_bonds)) {
1187 $ports .= " $d->{$k}" if $d->{$k};
1190 foreach my $p (PVE
::Tools
::split_list
($ports)) {
1191 die "port '$p' is already used on interface '$used_ports->{$p}'\n"
1192 if $used_ports->{$p} && $used_ports->{$p} ne $iface;
1193 $used_ports->{$p} = $iface;
1197 # delete unused OVS ports
1198 foreach my $iface (keys %$ifaces) {
1199 my $d = $ifaces->{$iface};
1200 if ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' ||
1201 $d->{type
} eq 'OVSBond') {
1202 my $brname = $used_ports->{$iface};
1203 if (!$brname || !$ifaces->{$brname}) {
1204 if ($iface =~ /^(?:eth|en|ib)/) {
1205 $ifaces->{$iface} = { type
=> 'eth',
1208 families
=> ['inet'] };
1210 delete $ifaces->{$iface};
1214 my $bd = $ifaces->{$brname};
1215 if ($bd->{type
} ne 'OVSBridge') {
1216 delete $ifaces->{$iface};
1222 # create OVS bridge ports
1223 foreach my $iface (keys %$ifaces) {
1224 my $d = $ifaces->{$iface};
1225 if ($d->{type
} eq 'OVSBridge' && $d->{ovs_ports
}) {
1226 foreach my $p (split (/\s+/, $d->{ovs_ports
})) {
1227 my $n = $ifaces->{$p};
1228 die "OVS bridge '$iface' - unable to find port '$p'\n"
1230 $n->{autostart
} = 0;
1231 if ($n->{type
} eq 'eth') {
1232 $n->{type
} = 'OVSPort';
1233 $n->{ovs_bridge
} = $iface;
1234 } elsif ($n->{type
} eq 'OVSBond' || $n->{type
} eq 'OVSPort' ||
1235 $n->{type
} eq 'OVSIntPort') {
1236 $n->{ovs_bridge
} = $iface;
1238 die "interface '$p' is not defined as OVS port/bond\n";
1244 # check OVS bond ports
1245 foreach my $iface (keys %$ifaces) {
1246 my $d = $ifaces->{$iface};
1247 if ($d->{type
} eq 'OVSBond' && $d->{ovs_bonds
}) {
1248 foreach my $p (split (/\s+/, $d->{ovs_bonds
})) {
1249 my $n = $ifaces->{$p};
1250 die "OVS bond '$iface' - unable to find slave '$p'\n"
1252 die "OVS bond '$iface' - wrong interface type on slave '$p' " .
1253 "('$n->{type}' != 'eth')\n" if $n->{type
} ne 'eth';
1258 my $raw = <<'NETWORKDOC';
1259 # network interface settings; autogenerated
1260 # Please do NOT modify this file directly, unless you know what
1263 # If you want to manage part of the network configuration manually,
1264 # please utilize the 'source' or 'source-directory' directives to do
1266 # PVE will preserve these directives, but will NOT its network
1267 # configuration from sourced files, so do not attempt to move any of
1268 # the PVE managed interfaces into external files!
1274 my $if_type_hash = {
1281 my $lookup_type_prio = sub {
1285 if ($iface =~ m/^(\S+)(\.|:)\d+$/) {
1291 if ($iface eq 'lo') {
1292 $pri = $if_type_hash->{loopback
};
1293 } elsif ($iface =~ m/^(?:eth\d+|ib\d+|en[^:.]+)$/) {
1294 $pri = $if_type_hash->{eth
} + $child;
1295 } elsif ($iface =~ m/^bond\d+$/) {
1296 $pri = $if_type_hash->{bond
} + $child;
1297 } elsif ($iface =~ m/^vmbr\d+$/) {
1298 $pri = $if_type_hash->{bridge
} + $child;
1304 foreach my $iface (sort {
1305 my $ref1 = $ifaces->{$a};
1306 my $ref2 = $ifaces->{$b};
1307 my $tp1 = &$lookup_type_prio($a);
1308 my $tp2 = &$lookup_type_prio($b);
1310 # Only recognized types are in relation to each other. If one type
1311 # is unknown then only consider the interfaces' priority attributes.
1312 $tp1 = $tp2 = 0 if !defined($tp1) || !defined($tp2);
1314 my $p1 = $tp1 + ($ref1->{priority
} // 50000);
1315 my $p2 = $tp2 + ($ref2->{priority
} // 50000);
1317 return $p1 <=> $p2 if $p1 != $p2;
1321 next if $printed->{$iface};
1323 my $d = $ifaces->{$iface};
1324 my $pri = $d->{priority
} // 0;
1325 if (@options && $options[0]->[0] < $pri) {
1327 $raw .= (shift @options)->[1] . "\n";
1328 } while (@options && $options[0]->[0] < $pri);
1332 $printed->{$iface} = 1;
1333 $raw .= "auto $iface\n" if $d->{autostart
};
1334 my $i = 0; # some options should be printed only once
1335 $raw .= __interface_to_string
($iface, $d, $_, !$i++) foreach @{$d->{families
}};
1338 $raw .= $_->[1] . "\n" foreach @options;
1342 register_file
('interfaces', "/etc/network/interfaces",
1343 \
&read_etc_network_interfaces
,
1344 \
&write_etc_network_interfaces
);
1347 sub read_iscsi_initiatorname
{
1348 my ($filename, $fd) = @_;
1350 while (defined(my $line = <$fd>)) {
1351 if ($line =~ m/^InitiatorName=(\S+)$/) {
1359 register_file
('initiatorname', "/etc/iscsi/initiatorname.iscsi",
1360 \
&read_iscsi_initiatorname
);
1363 my ($filename, $fd) = @_;
1367 my $raw = defined($fd) ?
<$fd> : '';
1372 my @tokens = split(/\s+/, $raw);
1377 while (defined(my $tok = shift @tokens)) {
1379 $machine = shift @tokens if $tok eq 'machine';
1381 $data->{$machine} = {} if !$data->{$machine};
1383 $data->{$machine}->{login
} = shift @tokens if $tok eq 'login';
1384 $data->{$machine}->{password
} = shift @tokens if $tok eq 'password';
1390 my $format_apt_auth_data = sub {
1395 foreach my $machine (sort keys %$data) {
1396 my $d = $data->{$machine};
1397 $raw .= "machine $machine\n";
1398 $raw .= " login $d->{login}\n" if $d->{login
};
1399 $raw .= " password $d->{password}\n" if $d->{password
};
1406 sub write_apt_auth
{
1407 my ($filename, $fh, $data) = @_;
1409 my $raw = &$format_apt_auth_data($data);
1411 die "write failed: $!" unless print $fh "$raw\n";
1416 sub update_apt_auth
{
1417 my ($filename, $fh, $data) = @_;
1419 my $orig = read_apt_auth
($filename, $fh);
1421 foreach my $machine (keys %$data) {
1422 $orig->{$machine} = $data->{$machine};
1425 return &$format_apt_auth_data($orig);
1428 register_file
('apt-auth', "/etc/apt/auth.conf",
1429 \
&read_apt_auth
, \
&write_apt_auth
,
1430 \
&update_apt_auth
, perm
=> 0640);