3 # todo: maybe we do not need update_file() ?
9 use Encode
qw(encode decode);
10 use Fcntl
qw(:DEFAULT :flock);
19 use PVE
::Exception
qw(raise_param_exc);
28 our @EXPORT_OK = qw(read_file write_file register_file);
37 '/etc/network/interfaces' => '/etc/network/interfaces.new',
40 # to enable cached operation, you need to call 'inotify_init'
41 # inotify handles are a limited resource, so use with care (only
42 # enable the cache if you really need it)
44 # Note: please close the inotify handle after you fork
46 sub ccache_default_writer
{
47 my ($filename, $data) = @_;
49 die "undefined config writer for '$filename' :ERROR";
52 sub ccache_default_parser
{
53 my ($filename, $srcfd) = @_;
55 die "undefined config reader for '$filename' :ERROR";
58 sub ccache_compute_diff
{
59 my ($filename, $shadow) = @_;
63 my $cmd = ['/usr/bin/diff', '-b', '-N', '-u', $filename, $shadow];
64 PVE
::Tools
::run_command
($cmd, noerr
=> 1, outfunc
=> sub {
66 $diff .= decode
('UTF-8', $line) . "\n";
69 $diff = undef if !$diff;
77 foreach my $uid (keys %$ccacheregex) {
78 my $ccinfo = $ccacheregex->{$uid};
79 my $dir = $ccinfo->{dir
};
80 my $regex = $ccinfo->{regex
};
81 if ($filename =~ m
|^$dir/+$regex$|) {
82 if (!$ccache->{$filename}) {
84 while (my ($k, $v) = each %$ccinfo) {
87 $ccache->{$filename} = $cp;
89 return ($ccache->{$filename}, $filename);
93 $filename = $ccachemap->{$filename} if defined ($ccachemap->{$filename});
95 die "file '$filename' not added :ERROR" if !defined ($ccache->{$filename});
97 return ($ccache->{$filename}, $filename);
101 my ($fileid, $data, $full) = @_;
103 my ($ccinfo, $filename) = ccache_info
($fileid);
105 my $writer = $ccinfo->{writer
};
107 my $realname = $filename;
110 if ($shadow = $shadowfiles->{$filename}) {
114 my $perm = $ccinfo->{perm
} || 0644;
116 my $tmpname = "$realname.tmp.$$";
120 my $fh = IO
::File-
>new($tmpname, O_WRONLY
|O_CREAT
, $perm);
121 die "unable to open file '$tmpname' - $!\n" if !$fh;
123 $res = &$writer($filename, $fh, $data);
125 die "closing file '$tmpname' failed - $!\n" unless close $fh;
129 $ccinfo->{version
} = undef;
136 if (!rename($tmpname, $realname)) {
137 my $msg = "close (rename) atomic file '$filename' failed: $!\n";
143 if ($shadow && $full) {
144 $diff = ccache_compute_diff
($filename, $shadow);
148 return { data
=> $res, changes
=> $diff };
155 my ($fileid, $data, @args) = @_;
157 my ($ccinfo, $filename) = ccache_info
($fileid);
159 my $update = $ccinfo->{update
};
161 die "unable to update/merge data" if !$update;
163 my $lkfn = "$filename.lock";
171 $fd = IO
::File-
>new ($filename, "r");
173 my $new = &$update($filename, $fd, $data, @args);
176 PVE
::Tools
::file_set_contents
($filename, $new, $ccinfo->{perm
});
182 PVE
::Tools
::lock_file
($lkfn, $timeout, $code);
185 close($fd) if defined($fd);
192 sub discard_changes
{
193 my ($fileid, $full) = @_;
195 my ($ccinfo, $filename) = ccache_info
($fileid);
197 if (my $copy = $shadowfiles->{$filename}) {
201 return read_file
($filename, $full);
207 poll
() if $inotify; # read new inotify events
209 $versions->{$filename} = 0 if !defined ($versions->{$filename});
211 return $versions->{$filename};
215 my ($fileid, $full) = @_;
219 my ($ccinfo, $filename) = ccache_info
($fileid);
221 $parser = $ccinfo->{parser
};
226 my $cver = poll_changes
($filename);
228 if (my $copy = $shadowfiles->{$filename}) {
229 if ($fd = IO
::File-
>new ($copy, "r")) {
232 $fd = IO
::File-
>new ($filename, "r");
235 $fd = IO
::File-
>new ($filename, "r");
238 my $acp = $ccinfo->{always_call_parser
};
241 $ccinfo->{version
} = undef;
242 $ccinfo->{data
} = undef;
243 $ccinfo->{diff
} = undef;
244 return undef if !$acp;
247 my $noclone = $ccinfo->{noclone
};
250 if (!$ccinfo->{nocache
} &&
252 defined ($ccinfo->{data
}) &&
253 defined ($ccinfo->{version
}) &&
254 ($ccinfo->{readonce
} ||
255 ($ccinfo->{version
} == $cver))) {
258 if (!$noclone && ref ($ccinfo->{data
})) {
259 $ret->{data
} = clone
($ccinfo->{data
});
261 $ret->{data
} = $ccinfo->{data
};
263 $ret->{changes
} = $ccinfo->{diff
};
265 return $full ?
$ret : $ret->{data
};
271 $diff = ccache_compute_diff
($filename, $shadow);
274 my $res = &$parser($filename, $fd);
276 if (!$ccinfo->{nocache
}) {
277 $ccinfo->{version
} = $cver;
280 # we cache data with references, so we always need to
281 # clone this data. Else the original data may get
283 $ccinfo->{data
} = $res;
286 $ccinfo->{diff
} = $diff;
289 if (!$noclone && ref ($ccinfo->{data
})) {
290 $ret->{data
} = clone
($ccinfo->{data
});
292 $ret->{data
} = $ccinfo->{data
};
294 $ret->{changes
} = $ccinfo->{diff
};
296 return $full ?
$ret : $ret->{data
};
299 sub parse_ccache_options
{
300 my ($ccinfo, %options) = @_;
302 foreach my $opt (keys %options) {
303 my $v = $options{$opt};
304 if ($opt eq 'readonce') {
305 $ccinfo->{$opt} = $v;
306 } elsif ($opt eq 'nocache') {
307 $ccinfo->{$opt} = $v;
308 } elsif ($opt eq 'shadow') {
309 $ccinfo->{$opt} = $v;
310 } elsif ($opt eq 'perm') {
311 $ccinfo->{$opt} = $v;
312 } elsif ($opt eq 'noclone') {
313 # noclone flag for large read-only data chunks like aplinfo
314 $ccinfo->{$opt} = $v;
315 } elsif ($opt eq 'always_call_parser') {
316 # when set, we call parser even when the file does not exist.
317 # this allows the parser to return some default
318 $ccinfo->{$opt} = $v;
320 die "internal error - unsupported option '$opt'";
326 my ($id, $filename, $parser, $writer, $update, %options) = @_;
328 die "can't register file '$filename' after inotify_init" if $inotify;
330 die "file '$filename' already added :ERROR" if defined ($ccache->{$filename});
331 die "ID '$id' already used :ERROR" if defined ($ccachemap->{$id});
336 $ccinfo->{parser
} = $parser || \
&ccache_default_parser
;
337 $ccinfo->{writer
} = $writer || \
&ccache_default_writer
;
338 $ccinfo->{update
} = $update;
340 parse_ccache_options
($ccinfo, %options);
342 if ($options{shadow
}) {
343 $shadowfiles->{$filename} = $options{shadow
};
346 $ccachemap->{$id} = $filename;
347 $ccache->{$filename} = $ccinfo;
351 my ($dir, $regex, $parser, $writer, $update, %options) = @_;
353 die "can't register regex after inotify_init" if $inotify;
355 my $uid = "$dir/$regex";
356 die "regular expression '$uid' already added :ERROR" if defined ($ccacheregex->{$uid});
360 $ccinfo->{dir
} = $dir;
361 $ccinfo->{regex
} = $regex;
362 $ccinfo->{parser
} = $parser || \
&ccache_default_parser
;
363 $ccinfo->{writer
} = $writer || \
&ccache_default_writer
;
364 $ccinfo->{update
} = $update;
366 parse_ccache_options
($ccinfo, %options);
368 $ccacheregex->{$uid} = $ccinfo;
374 if ($inotify_pid != $$) {
375 syslog
('err', "got inotify poll request in wrong process - disabling inotify");
378 1 while $inotify && $inotify->poll;
383 foreach my $filename (keys %$ccache) {
384 $ccache->{$filename}->{version
} = undef;
385 $ccache->{$filename}->{data
} = undef;
386 $ccache->{$filename}->{diff
} = undef;
396 die "only one inotify instance allowed" if $inotify;
398 $inotify = Linux
::Inotify2-
>new()
399 || die "Unable to create new inotify object: $!";
401 $inotify->blocking (0);
406 foreach my $fn (keys %$ccache) {
407 my $dir = dirname
($fn);
408 my $base = basename
($fn);
410 $dirhash->{$dir}->{$base} = $fn;
412 if (my $sf = $shadowfiles->{$fn}) {
413 $base = basename
($sf);
414 $dir = dirname
($sf);
415 $dirhash->{$dir}->{$base} = $fn; # change version of original file!
419 foreach my $uid (keys %$ccacheregex) {
420 my $ccinfo = $ccacheregex->{$uid};
421 $dirhash->{$ccinfo->{dir
}}->{_regex
} = 1;
426 foreach my $dir (keys %$dirhash) {
428 my $evlist = IN_MODIFY
|IN_ATTRIB
|IN_MOVED_FROM
|IN_MOVED_TO
|IN_DELETE
|IN_CREATE
;
429 $inotify->watch ($dir, $evlist, sub {
433 if ($inotify_pid != $$) {
434 syslog
('err', "got inotify event in wrong process");
437 if ($e->IN_ISDIR || !$name) {
441 if ($e->IN_Q_OVERFLOW) {
442 syslog
('info', "got inotify overflow - flushing cache");
447 if ($e->IN_UNMOUNT) {
448 syslog
('err', "got 'unmount' event on '$name' - disabling inotify");
451 if ($e->IN_IGNORED) {
452 syslog
('err', "got 'ignored' event on '$name' - disabling inotify");
456 if ($dirhash->{$dir}->{_regex
}) {
457 foreach my $uid (keys %$ccacheregex) {
458 my $ccinfo = $ccacheregex->{$uid};
459 next if $dir ne $ccinfo->{dir
};
460 my $regex = $ccinfo->{regex
};
461 if ($regex && ($name =~ m
|^$regex$|)) {
463 my $fn = "$dir/$name";
465 #print "VERSION:$fn:$versions->{$fn}\n";
468 } elsif (my $fn = $dirhash->{$dir}->{$name}) {
471 #print "VERSION:$fn:$versions->{$fn}\n";
476 foreach my $dir (keys %$dirhash) {
477 foreach my $name (keys %{$dirhash->{$dir}}) {
478 if ($name eq '_regex') {
479 foreach my $uid (keys %$ccacheregex) {
480 my $ccinfo = $ccacheregex->{$uid};
481 next if $dir ne $ccinfo->{dir
};
482 my $re = $ccinfo->{regex
};
483 if (my $fd = IO
::Dir-
>new ($dir)) {
484 while (defined(my $de = $fd->read)) {
485 if ($de =~ m/^$re$/) {
487 $versions->{$fn}++; # init with version
488 #print "init:$fn:$versions->{$fn}\n";
494 my $fn = $dirhash->{$dir}->{$name};
495 $versions->{$fn}++; # init with version
496 #print "init:$fn:$versions->{$fn}\n";
506 return $cached_nodename if $cached_nodename;
508 my ($sysname, $nodename) = POSIX
::uname
();
510 $nodename =~ s/\..*$//; # strip domain part, if any
512 die "unable to read node name\n" if !$nodename;
514 $cached_nodename = $nodename;
516 return $cached_nodename;
519 sub read_etc_hostname
{
520 my ($filename, $fd) = @_;
522 my $hostname = <$fd>;
526 $hostname =~ s/\..*$//; # strip domain part, if any
531 sub write_etc_hostname
{
532 my ($filename, $fh, $hostname) = @_;
534 die "write failed: $!" unless print $fh "$hostname\n";
539 register_file
('hostname', "/etc/hostname",
541 \
&write_etc_hostname
);
544 my ($filename, $fh) = @_;
549 while (my $line = <$fh>) {
551 if ($line =~ m/^\s*#/) {
552 $line = decode
('UTF-8', $line);
558 digest
=> Digest
::SHA
::sha1_hex
($raw),
563 sub write_etc_hosts
{
564 my ($filename, $fh, $hosts, @args) = @_;
566 # check validity of ips/names
567 for my $line (split("\n", $hosts)) {
568 next if $line =~ m/^\s*#/; # comments
569 next if $line =~ m/^\s*$/; # whitespace/empty lines
571 my ($ip, @names) = split(/\s+/, $line);
573 raise_param_exc
({ 'data' => "Invalid IP '$ip'" })
574 if $ip !~ m/^$PVE::Tools::IPRE$/;
576 for my $name (@names) {
577 raise_param_exc
({ 'data' => "Invalid Hostname '$name'" })
578 if $name !~ m/^[.\-a-zA-Z0-9]+$/;
582 die "write failed: $!" if !print $fh encode
('UTF-8', $hosts);
587 register_file
('etchosts', "/etc/hosts",
591 sub read_etc_resolv_conf
{
592 my ($filename, $fh) = @_;
597 while (my $line = <$fh>) {
599 if ($line =~ m/^(search|domain)\s+(\S+)\s*/) {
601 } elsif ($line =~ m/^\s*nameserver\s+($PVE::Tools::IPRE)\s*/) {
604 $res->{"dns$nscount"} = $1;
612 sub update_etc_resolv_conf
{
613 my ($filename, $fh, $resolv, @args) = @_;
617 $data = "search $resolv->{search}\n"
618 if $resolv->{search
};
621 foreach my $k ("dns1", "dns2", "dns3") {
622 my $ns = $resolv->{$k};
623 if ($ns && $ns ne '0.0.0.0' && !$written->{$ns}) {
625 $data .= "nameserver $ns\n";
629 while (my $line = <$fh>) {
630 next if $line =~ m/^(search|domain|nameserver)\s+/;
637 register_file
('resolvconf', "/etc/resolv.conf",
638 \
&read_etc_resolv_conf
, undef,
639 \
&update_etc_resolv_conf
);
641 sub read_etc_timezone
{
642 my ($filename, $fd) = @_;
644 my $timezone = <$fd>;
651 sub write_etc_timezone
{
652 my ($filename, $fh, $timezone) = @_;
654 my $tzinfo = "/usr/share/zoneinfo/$timezone";
656 raise_param_exc
({ 'timezone' => "No such timezone" })
659 ($timezone) = $timezone =~ m/^(.*)$/; # untaint
661 print $fh "$timezone\n";
663 unlink ("/etc/localtime");
664 symlink ("/usr/share/zoneinfo/$timezone", "/etc/localtime");
668 register_file
('timezone', "/etc/timezone",
670 \
&write_etc_timezone
);
672 sub read_active_workers
{
673 my ($filename, $fh) = @_;
678 while (defined (my $line = <$fh>)) {
679 if ($line =~ m/^(\S+)\s(0|1)(\s([0-9A-Za-z]{8})(\s(\s*\S.*))?)?$/) {
684 if ((my $task = PVE
::Tools
::upid_decode
($upid, 1))) {
685 $task->{upid
} = $upid;
686 $task->{saved
} = $saved;
687 $task->{endtime
} = hex($endtime) if $endtime;
688 $task->{status
} = $status if $status;
692 warn "unable to parse line: $line";
700 sub write_active_workers
{
701 my ($filename, $fh, $tasklist) = @_;
704 foreach my $task (@$tasklist) {
705 my $upid = $task->{upid
};
706 my $saved = $task->{saved
} ?
1 : 0;
707 if ($task->{endtime
}) {
708 if ($task->{status
}) {
709 $raw .= sprintf("%s %s %08X %s\n", $upid, $saved, $task->{endtime
}, $task->{status
});
711 $raw .= sprintf("%s %s %08X\n", $upid, $saved, $task->{endtime
});
714 $raw .= "$upid $saved\n";
718 PVE
::Tools
::safe_print
($filename, $fh, $raw) if $raw;
721 register_file
('active', "/var/log/pve/tasks/active",
722 \
&read_active_workers
,
723 \
&write_active_workers
);
728 'active-backup' => 1,
736 my $ovs_bond_modes = {
737 'active-backup' => 1,
739 'lacp-balance-slb' => 1,
740 'lacp-balance-tcp' => 1,
743 #sub get_bond_modes {
744 # return $bond_modes;
747 my $parse_ovs_option = sub {
751 foreach my $kv (split (/\s+/, $data || '')) {
752 my ($k, $v) = split('=', $kv, 2);
753 $opts->{$k} = $v if $k && $v;
758 my $set_ovs_option = sub {
759 my ($d, %params) = @_;
761 my $opts = &$parse_ovs_option($d->{ovs_options
});
763 foreach my $k (keys %params) {
773 foreach my $k (keys %$opts) {
774 push @$res, "$k=$opts->{$k}";
777 if (my $new = join(' ', @$res)) {
778 $d->{ovs_options
} = $new;
779 return $d->{ovs_options
};
781 delete $d->{ovs_options
};
786 my $extract_ovs_option = sub {
789 my $opts = &$parse_ovs_option($d->{ovs_options
});
791 my $v = delete $opts->{$name};
794 foreach my $k (keys %$opts) {
795 push @$res, "$k=$opts->{$k}";
798 if (my $new = join(' ', @$res)) {
799 $d->{ovs_options
} = $new;
801 delete $d->{ovs_options
};
807 my $check_mtu = sub {
808 my ($ifaces, $parent, $child) = @_;
810 die "check mtu - missing parent interface\n" if !$parent;
811 die "check mtu - missing child interface\n" if !$child;
813 my $cmtu = $ifaces->{$child}->{mtu
};
816 my $parentdata = $ifaces->{$parent};
817 my $pmtu = $parentdata->{mtu
};
818 $pmtu = $cmtu if $parentdata->{type
} eq 'bond' && !$pmtu;
819 $pmtu = 1500 if !$pmtu;
821 die "interface '$parent' - mtu $pmtu is lower than '$child' - mtu $cmtu\n"
828 # <optional> exists => BOOL,
829 # <optional> active => BOOL,
830 # <optional> autostart => BOOL,
831 # <auto> priority => INT,
833 # type => "eth" | "bridge" | "bond" | "loopback" | "OVS*" | ... ,
835 # families => ["inet", "inet6", ...],
837 # method => "manual" | "static" | "dhcp" | ... ,
842 # comments => [ "..." ],
844 # method6 => "manual" | "static" | "dhcp" | ... ,
846 # netmask6 => SUBNET,
848 # comments6 => [ "..." ],
850 # <known options>, # like bridge_ports, ovs_*
852 # # extra/unknown options stored by-family:
853 # options => { <inet options>... }
854 # options6 => { <inet6 options>... }
858 # # mappings end up here as well, as we don't need to understand them
862 sub read_etc_network_interfaces
{
863 my ($filename, $fh) = @_;
864 my $proc_net_dev = IO
::File-
>new('/proc/net/dev', 'r');
865 my $active = PVE
::ProcFSTools
::get_active_network_interfaces
();
866 return __read_etc_network_interfaces
($fh, $proc_net_dev, $active);
869 sub __read_etc_network_interfaces
{
870 my ($fh, $proc_net_dev, $active_ifaces) = @_;
873 my $ifaces = $config->{ifaces
} = {};
874 my $options = $config->{options
} = [];
876 my $options_alternatives = {
878 'bond-slaves' => 'slaves',
879 'bond_slaves' => 'slaves',
880 'bond-xmit-hash-policy' => 'bond_xmit_hash_policy',
881 'bond-mode' => 'bond_mode',
882 'bond-miimon' =>'bond_miimon',
883 'bridge-vlan-aware' => 'bridge_vlan_aware',
884 'bridge-fd' => 'bridge_fd',
885 'bridge-stp' => 'bridge_stp',
886 'bridge-ports' => 'bridge_ports',
887 'bridge-vids' => 'bridge_vids'
893 while (defined ($line = <$proc_net_dev>)) {
894 if ($line =~ m/^\s*($PVE::Network::PHYSICAL_NIC_RE):.*/) {
895 $ifaces->{$1}->{exists} = 1;
898 close($proc_net_dev);
901 # we try to keep order inside the file
902 my $priority = 2; # 1 is reserved for lo
904 SECTION
: while (defined ($line = <$fh>)) {
906 next if $line =~ m/^\s*#/;
907 next if $line =~ m/^\s*(allow-hotplug)\s+(.*)$/;
909 if ($line =~ m/^\s*(auto|allow-ovs)\s+(.*)$/) {
910 my @aa = split (/\s+/, $2);
912 foreach my $a (@aa) {
913 $ifaces->{$a}->{autostart
} = 1;
916 } elsif ($line =~ m/^\s*iface\s+(\S+)\s+(inet6?)\s+(\S+)\s*$/) {
919 my $f = { method => $3 }; # by family, merged to $d with a $suffix
920 (my $suffix = $family) =~ s/^inet//;
922 my $d = $ifaces->{$i} ||= {};
923 $d->{priority
} = $priority++ if !$d->{priority
};
924 push @{$d->{families
}}, $family;
926 while (defined ($line = <$fh>)) {
927 $line =~ s/\s+$//; # drop trailing whitespaces
929 if ($line =~ m/^\s*#(.*?)\s*$/) {
930 $f->{comments
} = '' if !$f->{comments
};
931 my $comment = decode
('UTF-8', $1);
932 $f->{comments
} .= "$comment\n";
933 } elsif ($line =~ m
/^\s
*(?
:iface\s
941 } elsif ($line =~ m/^\s*((\S+)\s+(.+))$/) {
943 my ($id, $value) = ($2, $3);
945 $id = $options_alternatives->{$id} if $options_alternatives->{$id};
947 my $simple_options = {
956 'bridge-access' => 1,
957 'bridge-learning' => 1,
958 'bridge-arp-nd-suppress' => 1,
959 'bridge-unicast-flood' => 1,
960 'bridge-multicast-flood' => 1,
962 'bond_xmit_hash_policy' => 1,
966 'vlan-protocol' => 1,
967 'vlan-raw-device' => 1,
970 'vxlan-svcnodeip' => 1,
971 'vxlan-physdev' => 1,
972 'vxlan-local-tunnelip' => 1 };
974 if (($id eq 'address') || ($id eq 'netmask') || ($id eq 'broadcast') || ($id eq 'gateway')) {
976 } elsif ($simple_options->{$id}) {
978 } elsif ($id eq 'slaves' || $id eq 'bridge_ports') {
980 foreach my $p (split (/\s+/, $value)) {
981 next if $p eq 'none';
984 my $str = join (' ', sort keys %{$devs});
986 $d->{$id} .= ' ' . $str if $str;
988 $d->{$id} = $str || '';
990 } elsif ($id eq 'bridge_stp') {
991 if ($value =~ m/^\s*(on|yes)\s*$/i) {
996 } elsif ($id eq 'bridge_vlan_aware') {
998 } elsif ($id eq 'bond_mode') {
1000 foreach my $bm (keys %$bond_modes) {
1001 my $id = $bond_modes->{$bm};
1002 if ($id eq $value) {
1008 } elsif ($id eq 'vxlan-remoteip') {
1009 push @{$d->{$id}}, $value;
1011 push @{$f->{options
}}, $option;
1017 $d->{"$_$suffix"} = $f->{$_} foreach (keys %$f);
1018 last SECTION
if !defined($line);
1020 } elsif ($line =~ /\w/) {
1021 push @$options, [$priority++, $line];
1025 foreach my $ifname (@$active_ifaces) {
1026 if (my $iface = $ifaces->{$ifname}) {
1027 $iface->{active
} = 1;
1031 if (!$ifaces->{lo
}) {
1032 $ifaces->{lo
}->{priority
} = 1;
1033 $ifaces->{lo
}->{method} = 'loopback';
1034 $ifaces->{lo
}->{type
} = 'loopback';
1035 $ifaces->{lo
}->{autostart
} = 1;
1038 foreach my $iface (keys %$ifaces) {
1039 my $d = $ifaces->{$iface};
1040 $d->{type
} = 'unknown';
1041 if ($iface =~ m/^bond\d+$/) {
1042 if (!$d->{ovs_type
}) {
1043 $d->{type
} = 'bond';
1044 } elsif ($d->{ovs_type
} eq 'OVSBond') {
1045 $d->{type
} = $d->{ovs_type
};
1046 # translate: ovs_options => bond_mode
1047 $d->{'bond_mode'} = &$extract_ovs_option($d, 'bond_mode');
1048 my $lacp = &$extract_ovs_option($d, 'lacp');
1049 if ($lacp && $lacp eq 'active') {
1050 if ($d->{'bond_mode'} eq 'balance-slb') {
1051 $d->{'bond_mode'} = 'lacp-balance-slb';
1054 # Note: balance-tcp needs lacp
1055 if ($d->{'bond_mode'} eq 'balance-tcp') {
1056 $d->{'bond_mode'} = 'lacp-balance-tcp';
1058 my $tag = &$extract_ovs_option($d, 'tag');
1059 $d->{ovs_tag
} = $tag if defined($tag);
1061 } elsif ($iface =~ m/^vmbr\d+$/) {
1062 if (!$d->{ovs_type
}) {
1063 $d->{type
} = 'bridge';
1064 if (!defined ($d->{bridge_stp
})) {
1065 $d->{bridge_stp
} = 'off';
1067 if (!defined($d->{bridge_fd
}) && $d->{bridge_stp
} eq 'off') {
1068 $d->{bridge_fd
} = 0;
1070 } elsif ($d->{ovs_type
} eq 'OVSBridge') {
1071 $d->{type
} = $d->{ovs_type
};
1073 } elsif ($iface =~ m/^(\S+):\d+$/) {
1074 $d->{type
} = 'alias';
1075 if (defined ($ifaces->{$1})) {
1076 $d->{exists} = $ifaces->{$1}->{exists};
1078 $ifaces->{$1}->{exists} = 0;
1081 } elsif ($iface =~ m/^(\S+)\.(\d+)$/ || $d->{'vlan-raw-device'}) {
1082 $d->{type
} = 'vlan';
1084 my ($dev, $id) = ($1, $2);
1085 $d->{'vlan-raw-device'} = $dev if defined($dev) && !$d->{'vlan-raw-device'};
1087 if (!$id && $iface =~ m/^vlan(\d+)$/) { # VLAN id 0 is not valid, so truthy check it is
1090 $d->{'vlan-id'} = $id if $id;
1092 my $raw_iface = $d->{'vlan-raw-device'};
1094 if (defined ($ifaces->{$raw_iface})) {
1095 $d->{exists} = $ifaces->{$raw_iface}->{exists};
1097 $ifaces->{$raw_iface}->{exists} = 0;
1100 } elsif ($iface =~ m/^$PVE::Network::PHYSICAL_NIC_RE$/) {
1101 if (!$d->{ovs_type
}) {
1103 } elsif ($d->{ovs_type
} eq 'OVSPort') {
1104 $d->{type
} = $d->{ovs_type
};
1105 my $tag = &$extract_ovs_option($d, 'tag');
1106 $d->{ovs_tag
} = $tag if defined($tag);
1108 } elsif ($iface =~ m/^lo$/) {
1109 $d->{type
} = 'loopback';
1111 if ($d->{'vxlan-id'}) {
1112 $d->{type
} = 'vxlan';
1113 } elsif (defined($d->{ovs_type
})) {
1114 if ($d->{ovs_type
} eq 'OVSIntPort') {
1115 $d->{type
} = $d->{ovs_type
};
1116 my $tag = &$extract_ovs_option($d, 'tag');
1117 $d->{ovs_tag
} = $tag if defined($tag);
1119 } elsif (defined($d->{'link-type'})) {
1120 $d->{type
} = $d->{'link-type'} if $d->{'link-type'} eq 'dummy';
1124 # map address and netmask to cidr
1125 if (my $addr = $d->{address
}) {
1126 if (_address_is_cidr
($addr)) {
1128 my ($baseaddr, $mask) = _cidr_split
($addr);
1129 $d->{address
} = $baseaddr;
1130 $d->{netmask
} = $mask;
1131 } elsif (my $cidr = _get_cidr
($d->{address
}, $d->{netmask
})) {
1133 (undef, $d->{netmask
}) = _cidr_split
($cidr);
1135 # no mask, else we'd got a cidr above
1136 $d->{cidr
} = $addr ."/32";
1140 # map address6 and netmask6 to cidr6
1141 if (my $addr6 = $d->{address6
}) {
1142 if (_address_is_cidr
($addr6)) {
1143 $d->{cidr6
} = $addr6;
1144 my ($baseaddr, $mask) = _cidr_split
($addr6);
1145 $d->{address6
} = $baseaddr;
1146 $d->{netmask6
} = $mask;
1147 } elsif (my $cidr6 = _get_cidr
($d->{address6
}, $d->{netmask6
})) {
1148 $d->{cidr6
} = $cidr6;
1150 # no mask, else we'd got a cidr above
1151 $d->{cidr6
} = $addr6 ."/128";
1155 $d->{method} = 'manual' if !$d->{method};
1156 $d->{method6
} = 'manual' if !$d->{method6
};
1158 if (my $comments6 = delete $d->{comments6
}) {
1159 $d->{comments
} = ($d->{comments
} // '') . $comments6;
1162 $d->{families
} ||= ['inet'];
1165 # OVS bridges create "allow-$BRIDGE $IFACE" lines which we need to remove
1166 # from the {options} hash for them to be removed correctly.
1167 @$options = grep {defined($_)} map {
1168 my ($pri, $line) = @$_;
1169 if ($line =~ /^allow-ovs\s+(.*)$/) {
1171 } elsif ($line =~ /^allow-(\S+)\s+(.*)$/) {
1173 my @ports = split(/\s+/, $2);
1174 if (defined(my $br = $ifaces->{$bridge})) {
1175 # if this port is part of a bridge, remove it
1176 my %in_ovs_ports = map {$_=>1} split(/\s+/, $br->{ovs_ports
});
1177 @ports = grep { not $in_ovs_ports{$_} } @ports;
1179 # create the allow line for the remaining ports, or delete if empty
1181 [$pri, "allow-$bridge " . join(' ', @ports)];
1186 # don't modify other lines
1194 sub _address_is_cidr
{
1196 return $addr =~ /\/\d
+$/ ?
1 : 0;
1201 $cidr =~ /^(.+)\/(\d
+)$/;
1202 return ($1, $2); # (address, mask)
1206 my ($addr, $mask) = @_;
1208 return $addr if _address_is_cidr
($addr);
1209 return undef if !$mask;
1211 if ($mask =~ m/^\d+$/) { # cidr notation
1212 return $addr . "/" . $mask;
1213 } elsif (my $cidrmask = PVE
::JSONSchema
::get_netmask_bits
($mask)) {
1214 return $addr . "/" . $cidrmask;
1219 sub __interface_to_string
{
1220 my ($iface, $d, $family, $first_block, $ifupdown2) = @_;
1222 (my $suffix = $family) =~ s/^inet//;
1224 return '' if !($d && $d->{"method$suffix"});
1228 $raw .= "iface $iface $family " . $d->{"method$suffix"} . "\n";
1230 if (my $addr = $d->{"address$suffix"}) {
1232 if ($addr !~ /\/\d
+$/ && $d->{"netmask$suffix"}) {
1233 if ($d->{"netmask$suffix"} =~ m/^\d+$/) {
1234 $addr .= "/" . $d->{"netmask$suffix"};
1235 } elsif (my $mask = PVE
::JSONSchema
::get_netmask_bits
($d->{"netmask$suffix"})) {
1236 $addr .= "/" . $mask;
1240 $raw .= "\taddress " . $addr . "\n";
1243 $raw .= "\tgateway " . $d->{"gateway$suffix"} . "\n" if $d->{"gateway$suffix"};
1245 my $done = { type
=> 1, priority
=> 1, method => 1, active
=> 1, exists => 1,
1246 comments
=> 1, autostart
=> 1, options
=> 1,
1247 address
=> 1, netmask
=> 1, gateway
=> 1, broadcast
=> 1,
1248 method6
=> 1, families
=> 1, options6
=> 1, comments6
=> 1,
1249 address6
=> 1, netmask6
=> 1, gateway6
=> 1, broadcast6
=> 1, 'uplink-id' => 1 };
1251 if (!$first_block) {
1252 # not printing out options
1253 } elsif ($d->{type
} eq 'bridge') {
1255 my $ports = $d->{bridge_ports
} || 'none';
1256 $ports =~ s/[;,\s]+/ /g;
1257 $raw .= "\tbridge-ports $ports\n";
1258 $done->{bridge_ports
} = 1;
1260 my $v = defined($d->{bridge_stp
}) ?
$d->{bridge_stp
} : 'off';
1261 my $no_stp = $v eq 'off';
1263 $raw .= "\tbridge-stp $v\n";
1264 $done->{bridge_stp
} = 1;
1266 $v = defined($d->{bridge_fd
}) ?
$d->{bridge_fd
} : 0;
1267 # 0 is only allowed when STP is disabled
1268 if ($v || $no_stp) {
1269 $raw .= "\tbridge-fd $v\n";
1271 $done->{bridge_fd
} = 1;
1273 if( defined($d->{bridge_vlan_aware
})) {
1274 $raw .= "\tbridge-vlan-aware yes\n";
1275 $v = defined($d->{bridge_vids
}) ?
$d->{bridge_vids
} : "2-4094";
1276 $raw .= "\tbridge-vids $v\n";
1278 $done->{bridge_vlan_aware
} = 1;
1279 $done->{bridge_vids
} = 1;
1281 $raw .= "\tmtu $d->{mtu}\n" if $d->{mtu
};
1284 } elsif ($d->{type
} eq 'bond') {
1286 $d->{slaves
} =~ s/[;,\s]+/ /g;
1287 my $slaves = $d->{slaves
} || 'none';
1288 $raw .= "\tbond-slaves $slaves\n";
1289 $done->{slaves
} = 1;
1291 my $v = defined ($d->{'bond_miimon'}) ?
$d->{'bond_miimon'} : 100;
1292 $raw .= "\tbond-miimon $v\n";
1293 $done->{'bond_miimon'} = 1;
1295 $v = defined ($d->{'bond_mode'}) ?
$d->{'bond_mode'} : 'balance-rr';
1296 $raw .= "\tbond-mode $v\n";
1297 $done->{'bond_mode'} = 1;
1299 if ($d->{'bond_mode'} && $d->{'bond_xmit_hash_policy'} &&
1300 ($d->{'bond_mode'} eq 'balance-xor' || $d->{'bond_mode'} eq '802.3ad')) {
1301 $raw .= "\tbond-xmit-hash-policy $d->{'bond_xmit_hash_policy'}\n";
1303 $done->{'bond_xmit_hash_policy'} = 1;
1305 if ($d->{'bond_mode'} && $d->{'bond_mode'} eq 'active-backup' && $d->{'bond-primary'}) {
1306 $raw .= "\tbond-primary $d->{'bond-primary'}\n";
1308 $done->{'bond-primary'} = 1;
1310 $raw .= "\tmtu $d->{mtu}\n" if $d->{mtu
};
1313 } elsif ($d->{type
} eq 'vlan') {
1314 die "$iface: wrong vlan-protocol $d->{'vlan-protocol'}\n"
1315 if $d->{'vlan-protocol'} && $d->{'vlan-protocol'} ne '802.1ad' && $d->{'vlan-protocol'} ne '802.1q';
1317 } elsif ($d->{type
} eq 'vxlan') {
1319 foreach my $k (qw(vxlan-id vxlan-svcnodeip vxlan-physdev vxlan-local-tunnelip)) {
1320 $raw .= "\t$k $d->{$k}\n" if defined $d->{$k};
1324 if ($d->{'vxlan-remoteip'}) {
1325 foreach my $remoteip (@{$d->{'vxlan-remoteip'}}) {
1326 $raw .= "\tvxlan-remoteip $remoteip\n";
1328 $done->{'vxlan-remoteip'} = 1;
1331 $raw .= "\tmtu $d->{mtu}\n" if $d->{mtu
};
1334 } elsif ($d->{type
} eq 'OVSBridge') {
1336 $raw .= "\tovs_type $d->{type}\n";
1337 $done->{ovs_type
} = 1;
1339 $raw .= "\tovs_ports $d->{ovs_ports}\n" if $d->{ovs_ports
};
1340 $done->{ovs_ports
} = 1;
1342 $raw .= "\tovs_mtu $d->{mtu}\n" if $d->{mtu
};
1345 } elsif ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' ||
1346 $d->{type
} eq 'OVSBond') {
1348 $d->{autostart
} = 0; # started by the bridge
1350 if (defined($d->{ovs_tag
})) {
1351 &$set_ovs_option($d, tag
=> $d->{ovs_tag
});
1353 $done->{ovs_tag
} = 1;
1355 if ($d->{type
} eq 'OVSBond') {
1357 $d->{bond_mode
} = 'active-backup' if !$d->{bond_mode
};
1359 $ovs_bond_modes->{$d->{bond_mode
}} ||
1360 die "OVS does not support bond mode '$d->{bond_mode}\n";
1362 if ($d->{bond_mode
} eq 'lacp-balance-slb') {
1363 &$set_ovs_option($d, lacp
=> 'active');
1364 &$set_ovs_option($d, bond_mode
=> 'balance-slb');
1365 } elsif ($d->{bond_mode
} eq 'lacp-balance-tcp') {
1366 &$set_ovs_option($d, lacp
=> 'active');
1367 &$set_ovs_option($d, bond_mode
=> 'balance-tcp');
1369 &$set_ovs_option($d, lacp
=> undef);
1370 &$set_ovs_option($d, bond_mode
=> $d->{bond_mode
});
1372 $done->{bond_mode
} = 1;
1374 $raw .= "\tovs_bonds $d->{ovs_bonds}\n" if $d->{ovs_bonds
};
1375 $done->{ovs_bonds
} = 1;
1378 $raw .= "\tovs_type $d->{type}\n";
1379 $done->{ovs_type
} = 1;
1381 if (my $bridge = $d->{ovs_bridge
}) {
1383 $raw = "auto $iface\n$raw";
1385 $raw = "allow-$bridge $iface\n$raw";
1388 $raw .= "\tovs_bridge $bridge\n";
1389 $done->{ovs_bridge
} = 1;
1392 $raw .= "\tovs_mtu $d->{mtu}\n" if $d->{mtu
};
1397 # print other settings
1398 foreach my $k (sort keys %$d) {
1399 next if $done->{$k};
1401 $raw .= "\t$k $d->{$k}\n";
1405 foreach my $option (@{$d->{"options$suffix"}}) {
1406 $raw .= "\t$option\n";
1410 my $comments = $d->{"comments$suffix"} || '';
1411 foreach my $cl (split(/\n/, $comments)) {
1421 sub write_etc_network_interfaces
{
1422 my ($filename, $fh, $config) = @_;
1423 my $ifupdown2 = -e
'/usr/share/ifupdown2/ifupdown2';
1424 my $raw = __write_etc_network_interfaces
($config, $ifupdown2);
1425 PVE
::Tools
::safe_print
($filename, $fh, encode
('UTF-8', $raw));
1427 sub __write_etc_network_interfaces
{
1428 my ($config, $ifupdown2) = @_;
1430 my $ifaces = $config->{ifaces
};
1431 my @options = @{$config->{options
}};
1433 my $used_ports = {};
1435 foreach my $iface (keys %$ifaces) {
1436 my $d = $ifaces->{$iface};
1438 my ($cidr, $cidr6) = (delete $d->{cidr
}, delete $d->{cidr6
});
1439 $d->{address
} //= $cidr;
1440 $d->{address6
} //= $cidr6;
1443 foreach my $k (qw(bridge_ports ovs_ports slaves ovs_bonds)) {
1444 $ports .= " $d->{$k}" if $d->{$k};
1447 foreach my $p (PVE
::Tools
::split_list
($ports)) {
1448 die "port '$p' is already used on interface '$used_ports->{$p}'\n"
1449 if $used_ports->{$p} && $used_ports->{$p} ne $iface;
1450 $used_ports->{$p} = $iface;
1454 # delete unused OVS ports
1455 foreach my $iface (keys %$ifaces) {
1456 my $d = $ifaces->{$iface};
1457 if ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' ||
1458 $d->{type
} eq 'OVSBond') {
1459 my $brname = $used_ports->{$iface};
1460 if (!$brname || !$ifaces->{$brname}) {
1461 if ($iface =~ /^$PVE::Network::PHYSICAL_NIC_RE/) {
1462 $ifaces->{$iface} = {
1466 families
=> ['inet'],
1469 delete $ifaces->{$iface};
1473 my $bd = $ifaces->{$brname};
1474 if ($bd->{type
} ne 'OVSBridge') {
1475 delete $ifaces->{$iface};
1481 # create OVS bridge ports
1482 foreach my $iface (keys %$ifaces) {
1483 my $d = $ifaces->{$iface};
1484 if ($d->{type
} eq 'OVSBridge' && $d->{ovs_ports
}) {
1485 foreach my $p (split (/\s+/, $d->{ovs_ports
})) {
1486 my $n = $ifaces->{$p};
1487 die "OVS bridge '$iface' - unable to find port '$p'\n"
1489 $n->{autostart
} = 0;
1490 if ($n->{type
} eq 'eth') {
1491 $n->{type
} = 'OVSPort';
1492 $n->{ovs_bridge
} = $iface;
1493 } elsif ($n->{type
} eq 'OVSBond' || $n->{type
} eq 'OVSPort' ||
1494 $n->{type
} eq 'OVSIntPort') {
1495 $n->{ovs_bridge
} = $iface;
1497 die "interface '$p' is not defined as OVS port/bond\n";
1500 &$check_mtu($ifaces, $iface, $p);
1505 # check OVS bond ports
1506 foreach my $iface (keys %$ifaces) {
1507 my $d = $ifaces->{$iface};
1508 if ($d->{type
} eq 'OVSBond' && $d->{ovs_bonds
}) {
1509 foreach my $p (split (/\s+/, $d->{ovs_bonds
})) {
1510 my $n = $ifaces->{$p};
1511 $n->{autostart
} = 1;
1512 die "OVS bond '$iface' - unable to find slave '$p'\n"
1514 die "OVS bond '$iface' - wrong interface type on slave '$p' " .
1515 "('$n->{type}' != 'eth')\n" if $n->{type
} ne 'eth';
1516 &$check_mtu($ifaces, $iface, $p);
1522 foreach my $iface (keys %$ifaces) {
1523 my $d = $ifaces->{$iface};
1524 next if !($d->{type
} eq 'bond' && $d->{slaves
});
1526 my $bond_primary_is_slave = undef;
1527 foreach my $p (split (/\s+/, $d->{slaves
})) {
1528 my $n = $ifaces->{$p};
1529 $n->{autostart
} = 1;
1531 die "bond '$iface' - unable to find slave '$p'\n" if !$n;
1532 die "bond '$iface' - wrong interface type on slave '$p' ('$n->{type}' != 'eth or bond')\n"
1533 if ($n->{type
} ne 'eth' && $n->{type
} ne 'bond');
1535 $check_mtu->($ifaces, $iface, $p);
1536 $bond_primary_is_slave = 1 if $d->{'bond-primary'} && $d->{'bond-primary'} eq $p;
1538 die "bond '$iface' - bond-primary interface is not a slave" if $d->{'bond-primary'} && !$bond_primary_is_slave;
1543 foreach my $iface (keys %$ifaces) {
1544 my $d = $ifaces->{$iface};
1546 if ($d->{type
} eq 'vxlan' && $d->{'vxlan-id'}) {
1547 my $vxlanid = $d->{'vxlan-id'};
1548 die "iface $iface - duplicate vxlan-id $vxlanid already used in $vxlans->{$vxlanid}\n" if $vxlans->{$vxlanid};
1549 $vxlans->{$vxlanid} = $iface;
1553 ++$ips if defined $d->{'vxlan-svcnodeip'};
1554 ++$ips if defined $d->{'vxlan-remoteip'};
1555 ++$ips if defined $d->{'vxlan-local-tunnelip'};
1557 die "iface $iface - vxlan-svcnodeip, vxlan-remoteip and vxlan-localtunnelip are mutually exclusive\n";
1560 if (defined($d->{'vxlan-svcnodeip'}) != defined($d->{'vxlan-physdev'})) {
1561 die "iface $iface - vxlan-svcnodeip and vxlan-physdev must be define together\n";
1563 #fixme : check if vxlan mtu is lower than 50bytes than physical interface where tunnel is going out
1567 foreach my $iface (keys %$ifaces) {
1568 my $d = $ifaces->{$iface};
1569 if ($d->{type
} eq 'vlan') {
1574 if ($iface =~ m/^(\S+)\.(\d+)$/) {
1577 delete $d->{'vlan-raw-device'} if $d->{'vlan-raw-device'};
1578 delete $d->{'vlan-id'} if $d->{'vlan-id'};
1581 die "missing vlan-raw-device option" if !$d->{'vlan-raw-device'};
1582 $p = $d->{'vlan-raw-device'};
1584 if ($iface =~ m/^vlan(\d+)$/) {
1586 delete $d->{'vlan-id'} if $d->{'vlan-id'};
1588 die "custom vlan interface name need ifupdown2" if !$ifupdown2;
1589 die "missing vlan-id option" if !$d->{'vlan-id'};
1590 $vlanid = $d->{'vlan-id'};
1593 my $n = $ifaces->{$p};
1595 die "vlan '$iface' - vlan-id $vlanid should be <= 4094\n" if $vlanid > 4094;
1596 die "vlan '$iface' - unable to find parent '$p'\n"
1599 if ($n->{type
} ne 'eth' && $n->{type
} ne 'bridge' && $n->{type
} ne 'bond' && $n->{type
} ne 'vlan') {
1600 die "vlan '$iface' - wrong interface type on parent '$p' " .
1601 "('$n->{type}' != 'eth|bond|bridge|vlan' )\n";
1604 &$check_mtu($ifaces, $p, $iface);
1611 foreach my $iface (keys %$ifaces) {
1612 my $d = $ifaces->{$iface};
1613 if (my $uplinkid = $d->{'uplink-id'}) {
1614 die "iface '$iface' - uplink-id $uplinkid is only allowed on physical and linux bond interfaces\n"
1615 if $d->{type
} ne 'eth' && $d->{type
} ne 'bond';
1617 die "iface '$iface' - uplink-id $uplinkid is already assigned on '$uplinks->{$uplinkid}'\n"
1618 if $uplinks->{$uplinkid};
1620 $uplinks->{$uplinkid} = $iface;
1624 # check bridgeport option
1625 my $bridgeports = {};
1627 my $ifaces_copy = { %$ifaces };
1628 foreach my $iface (keys %$ifaces_copy) {
1629 my $d = $ifaces_copy->{$iface};
1630 if ($d->{type
} eq 'bridge') {
1631 foreach my $p (split (/\s+/, $d->{bridge_ports
} // '')) {
1632 if($p =~ m/(\S+)\.(\d+)$/) {
1633 my $vlanparent = $1;
1634 if (!defined($ifaces_copy->{$p})) {
1635 $ifaces_copy->{$p}->{type
} = 'vlan';
1636 $ifaces_copy->{$p}->{method} = 'manual';
1637 $ifaces_copy->{$p}->{method6
} = 'manual';
1638 $ifaces_copy->{$p}->{mtu
} = $ifaces_copy->{$vlanparent}->{mtu
} if defined($ifaces_copy->{$1}->{mtu
});
1641 my $n = $ifaces_copy->{$p};
1642 die "bridge '$iface' - unable to find bridge port '$p'\n" if !$n;
1643 die "iface $p - ip address can't be set on interface if bridged in $iface\n"
1644 if ($n->{method} && $n->{method} eq 'static' && $n->{address
} ne '0.0.0.0') ||
1645 ($n->{method6
} && $n->{method6
} eq 'static' && $n->{address6
} ne '::');
1646 &$check_mtu($ifaces_copy, $p, $iface);
1647 $bridgeports->{$p} = $iface;
1649 $bridges->{$iface} = $d;
1653 foreach my $iface (keys %$ifaces) {
1654 my $d = $ifaces->{$iface};
1656 foreach my $k (qw(bridge-learning bridge-arp-nd-suppress bridge-unicast-flood bridge-multicast-flood bridge-access)) {
1657 die "iface $iface - $k: bridge port specific options can be used only on interfaces attached to a bridge\n"
1658 if $d->{$k} && !$bridgeports->{$iface};
1661 if ($d->{'bridge-access'} && !$bridges->{$bridgeports->{$iface}}->{bridge_vlan_aware
}) {
1662 die "iface $iface - bridge-access option can be only used if interface is in a vlan aware bridge\n";
1666 my $raw = <<'NETWORKDOC';
1667 # network interface settings; autogenerated
1668 # Please do NOT modify this file directly, unless you know what
1671 # If you want to manage parts of the network configuration manually,
1672 # please utilize the 'source' or 'source-directory' directives to do
1674 # PVE will preserve these directives, but will NOT read its network
1675 # configuration from sourced files, so do not attempt to move any of
1676 # the PVE managed interfaces into external files!
1682 my $if_type_hash = {
1687 OVSIntPort
=> 300000,
1691 OVSBridge
=> 500000,
1696 my $lookup_type_prio = sub {
1697 my ($iface, $ifaces) = @_;
1699 my ($rootiface, @rest) = split(/[.:]/, $iface);
1700 my $childlevel = scalar(@rest);
1701 my $type = $ifaces->{$rootiface}->{type
};
1702 return if !$type || $type eq 'unknown';
1704 return $if_type_hash->{$type} + $childlevel
1707 foreach my $iface (sort {
1708 my $ref1 = $ifaces->{$a};
1709 my $ref2 = $ifaces->{$b};
1710 my $tp1 = &$lookup_type_prio($a, $ifaces);
1711 my $tp2 = &$lookup_type_prio($b, $ifaces);
1713 # Only recognized types are in relation to each other. If one type
1714 # is unknown then only consider the interfaces' priority attributes.
1715 $tp1 = $tp2 = 0 if !defined($tp1) || !defined($tp2);
1717 my $p1 = $tp1 + ($ref1->{priority
} // 50000);
1718 my $p2 = $tp2 + ($ref2->{priority
} // 50000);
1720 return $p1 <=> $p2 if $p1 != $p2;
1724 next if $printed->{$iface};
1725 my $d = $ifaces->{$iface};
1726 my $pri = $d->{priority
} // 0;
1727 if (@options && $options[0]->[0] < $pri) {
1729 $raw .= (shift @options)->[1] . "\n";
1730 } while (@options && $options[0]->[0] < $pri);
1734 $printed->{$iface} = 1;
1735 if ($d->{autostart
}) {
1736 if ($d->{type
} eq 'OVSBridge' && !$ifupdown2) {
1737 # cannot use 'auto' for OVS, would add race with systemd ifup@.service
1738 $raw .= "allow-ovs $iface\n";
1740 $raw .= "auto $iface\n";
1744 # if 'inet6' is the only family
1745 if (scalar($d->{families
}->@*) == 1 && $d->{families
}[0] eq 'inet6') {
1746 $d->{comments6
} = delete $d->{comments
};
1749 my $i = 0; # some options should be printed only once
1750 $raw .= __interface_to_string
($iface, $d, $_, !$i++, $ifupdown2) foreach @{$d->{families
}};
1753 $raw .= $_->[1] . "\n" foreach @options;
1757 register_file
('interfaces', "/etc/network/interfaces",
1758 \
&read_etc_network_interfaces
,
1759 \
&write_etc_network_interfaces
);
1762 sub read_iscsi_initiatorname
{
1763 my ($filename, $fd) = @_;
1765 while (defined(my $line = <$fd>)) {
1766 if ($line =~ m/^InitiatorName=(\S+)$/) {
1774 register_file
('initiatorname', "/etc/iscsi/initiatorname.iscsi",
1775 \
&read_iscsi_initiatorname
);
1778 my ($filename, $fd) = @_;
1782 my $raw = defined($fd) ?
<$fd> : '';
1787 my @tokens = split(/\s+/, $raw);
1792 while (defined(my $tok = shift @tokens)) {
1794 $machine = shift @tokens if $tok eq 'machine';
1796 $data->{$machine} = {} if !$data->{$machine};
1798 $data->{$machine}->{login
} = shift @tokens if $tok eq 'login';
1799 $data->{$machine}->{password
} = shift @tokens if $tok eq 'password';
1805 my $format_apt_auth_data = sub {
1810 # sort longer entries first, so machine definitions with higher granularity are preferred
1811 for my $machine (sort { length($b) <=> length($a) || $a cmp $b} keys %$data) {
1812 my $d = $data->{$machine};
1813 next if !defined($d); # allow "deleting" set entries
1815 $raw .= "machine $machine\n";
1816 $raw .= " login $d->{login}\n" if $d->{login
};
1817 $raw .= " password $d->{password}\n" if $d->{password
};
1824 sub write_apt_auth
{
1825 my ($filename, $fh, $data) = @_;
1827 my $raw = $format_apt_auth_data->($data);
1829 die "write failed: $!" unless print $fh "$raw\n";
1834 sub update_apt_auth
{
1835 my ($filename, $fh, $data) = @_;
1837 my $orig = read_apt_auth
($filename, $fh);
1839 foreach my $machine (keys %$data) {
1840 $orig->{$machine} = $data->{$machine};
1843 return $format_apt_auth_data->($orig);
1848 "/etc/apt/auth.conf",