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 nodename);
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";
504 return $cached_nodename if $cached_nodename;
506 my ($sysname, $nodename) = POSIX
::uname
();
507 $nodename =~ s/\..*$//; # strip domain part, if any
509 die "unable to read node name\n" if !$nodename;
511 $cached_nodename = $nodename;
513 return $cached_nodename;
516 sub read_etc_hostname
{
517 my ($filename, $fd) = @_;
519 my $hostname = <$fd>;
523 $hostname =~ s/\..*$//; # strip domain part, if any
528 sub write_etc_hostname
{
529 my ($filename, $fh, $hostname) = @_;
531 die "write failed: $!" unless print $fh "$hostname\n";
536 register_file
('hostname', "/etc/hostname",
538 \
&write_etc_hostname
);
541 my ($filename, $fh) = @_;
546 while (my $line = <$fh>) {
548 if ($line =~ m/^\s*#/) {
549 $line = decode
('UTF-8', $line);
555 digest
=> Digest
::SHA
::sha1_hex
($raw),
560 sub write_etc_hosts
{
561 my ($filename, $fh, $hosts, @args) = @_;
563 # check validity of ips/names
564 for my $line (split("\n", $hosts)) {
565 next if $line =~ m/^\s*#/; # comments
566 next if $line =~ m/^\s*$/; # whitespace/empty lines
568 my ($ip, @names) = split(/\s+/, $line);
570 raise_param_exc
({ 'data' => "Invalid IP '$ip'" })
571 if $ip !~ m/^$PVE::Tools::IPRE$/;
573 for my $name (@names) {
574 raise_param_exc
({ 'data' => "Invalid Hostname '$name'" })
575 if $name !~ m/^[.\-a-zA-Z0-9]+$/;
579 die "write failed: $!" if !print $fh encode
('UTF-8', $hosts);
584 register_file
('etchosts', "/etc/hosts",
588 sub read_etc_resolv_conf
{
589 my ($filename, $fh) = @_;
594 while (my $line = <$fh>) {
596 if ($line =~ m/^(search|domain)\s+(\S+)\s*/) {
598 } elsif ($line =~ m/^\s*nameserver\s+($PVE::Tools::IPRE)\s*/) {
601 $res->{"dns$nscount"} = $1;
609 sub update_etc_resolv_conf
{
610 my ($filename, $fh, $resolv, @args) = @_;
614 $data = "search $resolv->{search}\n"
615 if $resolv->{search
};
618 foreach my $k ("dns1", "dns2", "dns3") {
619 my $ns = $resolv->{$k};
620 if ($ns && $ns ne '0.0.0.0' && !$written->{$ns}) {
622 $data .= "nameserver $ns\n";
626 while (my $line = <$fh>) {
627 next if $line =~ m/^(search|domain|nameserver)\s+/;
634 register_file
('resolvconf', "/etc/resolv.conf",
635 \
&read_etc_resolv_conf
, undef,
636 \
&update_etc_resolv_conf
);
638 sub read_etc_timezone
{
639 my ($filename, $fd) = @_;
641 my $timezone = <$fd>;
648 sub write_etc_timezone
{
649 my ($filename, $fh, $timezone) = @_;
651 my $tzinfo = "/usr/share/zoneinfo/$timezone";
653 raise_param_exc
({ 'timezone' => "No such timezone" })
656 ($timezone) = $timezone =~ m/^(.*)$/; # untaint
658 print $fh "$timezone\n";
660 unlink ("/etc/localtime");
661 symlink ("/usr/share/zoneinfo/$timezone", "/etc/localtime");
665 register_file
('timezone', "/etc/timezone",
667 \
&write_etc_timezone
);
669 sub read_active_workers
{
670 my ($filename, $fh) = @_;
675 while (defined (my $line = <$fh>)) {
676 if ($line =~ m/^(\S+)\s(0|1)(\s([0-9A-Za-z]{8})(\s(\s*\S.*))?)?$/) {
681 if ((my $task = PVE
::Tools
::upid_decode
($upid, 1))) {
682 $task->{upid
} = $upid;
683 $task->{saved
} = $saved;
684 $task->{endtime
} = hex($endtime) if $endtime;
685 $task->{status
} = $status if $status;
689 warn "unable to parse line: $line";
697 sub write_active_workers
{
698 my ($filename, $fh, $tasklist) = @_;
701 foreach my $task (@$tasklist) {
702 my $upid = $task->{upid
};
703 my $saved = $task->{saved
} ?
1 : 0;
704 if ($task->{endtime
}) {
705 if ($task->{status
}) {
706 $raw .= sprintf("%s %s %08X %s\n", $upid, $saved, $task->{endtime
}, $task->{status
});
708 $raw .= sprintf("%s %s %08X\n", $upid, $saved, $task->{endtime
});
711 $raw .= "$upid $saved\n";
715 PVE
::Tools
::safe_print
($filename, $fh, $raw) if $raw;
718 register_file
('active', "/var/log/pve/tasks/active",
719 \
&read_active_workers
,
720 \
&write_active_workers
);
725 'active-backup' => 1,
733 my $ovs_bond_modes = {
734 'active-backup' => 1,
736 'lacp-balance-slb' => 1,
737 'lacp-balance-tcp' => 1,
740 #sub get_bond_modes {
741 # return $bond_modes;
744 my $parse_ovs_option = sub {
748 foreach my $kv (split (/\s+/, $data || '')) {
749 my ($k, $v) = split('=', $kv, 2);
750 $opts->{$k} = $v if $k && $v;
755 my $set_ovs_option = sub {
756 my ($d, %params) = @_;
758 my $opts = &$parse_ovs_option($d->{ovs_options
});
760 foreach my $k (keys %params) {
770 foreach my $k (keys %$opts) {
771 push @$res, "$k=$opts->{$k}";
774 if (my $new = join(' ', @$res)) {
775 $d->{ovs_options
} = $new;
776 return $d->{ovs_options
};
778 delete $d->{ovs_options
};
783 my $extract_ovs_option = sub {
786 my $opts = &$parse_ovs_option($d->{ovs_options
});
788 my $v = delete $opts->{$name};
791 foreach my $k (keys %$opts) {
792 push @$res, "$k=$opts->{$k}";
795 if (my $new = join(' ', @$res)) {
796 $d->{ovs_options
} = $new;
798 delete $d->{ovs_options
};
804 my $check_mtu = sub {
805 my ($ifaces, $parent, $child) = @_;
807 die "check mtu - missing parent interface\n" if !$parent;
808 die "check mtu - missing child interface\n" if !$child;
810 my $cmtu = $ifaces->{$child}->{mtu
};
813 my $parentdata = $ifaces->{$parent};
814 my $pmtu = $parentdata->{mtu
};
815 $pmtu = $cmtu if $parentdata->{type
} eq 'bond' && !$pmtu;
816 $pmtu = 1500 if !$pmtu;
818 die "interface '$parent' - mtu $pmtu is lower than '$child' - mtu $cmtu\n"
825 # <optional> exists => BOOL,
826 # <optional> active => BOOL,
827 # <optional> autostart => BOOL,
828 # <auto> priority => INT,
830 # type => "eth" | "bridge" | "bond" | "loopback" | "OVS*" | ... ,
832 # families => ["inet", "inet6", ...],
834 # method => "manual" | "static" | "dhcp" | ... ,
839 # comments => [ "..." ],
841 # method6 => "manual" | "static" | "dhcp" | ... ,
843 # netmask6 => SUBNET,
845 # comments6 => [ "..." ],
847 # <known options>, # like bridge_ports, ovs_*
849 # # extra/unknown options stored by-family:
850 # options => { <inet options>... }
851 # options6 => { <inet6 options>... }
855 # # mappings end up here as well, as we don't need to understand them
859 sub read_etc_network_interfaces
{
860 my ($filename, $fh) = @_;
861 my $proc_net_dev = IO
::File-
>new('/proc/net/dev', 'r');
862 my $active = PVE
::ProcFSTools
::get_active_network_interfaces
();
863 return __read_etc_network_interfaces
($fh, $proc_net_dev, $active);
866 sub __read_etc_network_interfaces
{
867 my ($fh, $proc_net_dev, $active_ifaces) = @_;
870 my $ifaces = $config->{ifaces
} = {};
871 my $options = $config->{options
} = [];
873 my $options_alternatives = {
875 'bond-slaves' => 'slaves',
876 'bond_slaves' => 'slaves',
877 'bond-xmit-hash-policy' => 'bond_xmit_hash_policy',
878 'bond-mode' => 'bond_mode',
879 'bond-miimon' =>'bond_miimon',
880 'bridge-vlan-aware' => 'bridge_vlan_aware',
881 'bridge-fd' => 'bridge_fd',
882 'bridge-stp' => 'bridge_stp',
883 'bridge-ports' => 'bridge_ports',
884 'bridge-vids' => 'bridge_vids',
890 while (defined ($line = <$proc_net_dev>)) {
891 if ($line =~ m/^\s*($PVE::Network::PHYSICAL_NIC_RE):.*/) {
892 $ifaces->{$1}->{exists} = 1;
895 close($proc_net_dev);
898 # we try to keep order inside the file
899 my $priority = 2; # 1 is reserved for lo
901 SECTION
: while (defined ($line = <$fh>)) {
903 next if $line =~ m/^\s*#/;
905 if ($line =~ m/^\s*(allow-auto|auto|allow-ovs)\s+(.*)$/) {
907 $ifaces->{$_}->{autostart
} = 1 for split (/\s+/, $2);
909 } elsif ($line =~ m/^\s*(allow-hotplug)\s+(.*)$/) {
911 # FIXME: handle those differently? auto makes it required on-boot, vs. best-effort
912 $ifaces->{$_}->{autostart
} = 1 for split (/\s+/, $2);
914 } elsif ($line =~ m/^\s*iface\s+(\S+)\s+(inet6?)\s+(\S+)\s*$/) {
917 my $f = { method => $3 }; # by family, merged to $d with a $suffix
918 (my $suffix = $family) =~ s/^inet//;
920 my $d = $ifaces->{$i} ||= {};
921 $d->{priority
} = $priority++ if !$d->{priority
};
922 push @{$d->{families
}}, $family;
924 while (defined ($line = <$fh>)) {
925 $line =~ s/\s+$//; # drop trailing whitespaces
927 if ($line =~ m/^\s*#(.*?)\s*$/) {
928 $f->{comments
} = '' if !$f->{comments
};
929 my $comment = decode
('UTF-8', $1);
930 $f->{comments
} .= "$comment\n";
931 } elsif ($line =~ m/^\s*(?:(?:iface|mapping|auto|source|source-directory)\s|allow-)/) {
933 } elsif ($line =~ m/^\s*((\S+)\s+(.+))$/) {
935 my ($id, $value) = ($2, $3);
937 $id = $options_alternatives->{$id} if $options_alternatives->{$id};
939 my $simple_options = {
948 'bridge-access' => 1,
949 'bridge-learning' => 1,
950 'bridge-arp-nd-suppress' => 1,
951 'bridge-unicast-flood' => 1,
952 'bridge-multicast-flood' => 1,
953 'bridge-disable-mac-learning' => 1,
955 'bond_xmit_hash_policy' => 1,
959 'vlan-protocol' => 1,
960 'vlan-raw-device' => 1,
963 'vxlan-svcnodeip' => 1,
964 'vxlan-physdev' => 1,
965 'vxlan-local-tunnelip' => 1,
968 if ($id eq 'address' || $id eq 'netmask' || $id eq 'broadcast' || $id eq 'gateway') {
970 } elsif ($simple_options->{$id}) {
972 } elsif ($id eq 'slaves' || $id eq 'bridge_ports') {
974 foreach my $p (split (/\s+/, $value)) {
975 next if $p eq 'none';
978 my $str = join (' ', sort keys %{$devs});
980 $d->{$id} .= ' ' . $str if $str;
982 $d->{$id} = $str || '';
984 } elsif ($id eq 'bridge_stp') {
985 if ($value =~ m/^\s*(on|yes)\s*$/i) {
990 } elsif ($id eq 'bridge_vlan_aware') {
992 } elsif ($id eq 'bond_mode') {
994 foreach my $bm (keys %$bond_modes) {
995 if ($bond_modes->{$bm} eq $value) {
1001 } elsif ($id eq 'vxlan-remoteip') {
1002 push @{$d->{$id}}, $value;
1004 push @{$f->{options
}}, $option;
1010 $d->{"$_$suffix"} = $f->{$_} for keys $f->%*;
1011 last SECTION
if !defined($line);
1013 } elsif ($line =~ /\w/) {
1014 push @$options, [$priority++, $line];
1018 foreach my $ifname (@$active_ifaces) {
1019 if (my $iface = $ifaces->{$ifname}) {
1020 $iface->{active
} = 1;
1024 if (!$ifaces->{lo
}) {
1027 method => 'loopback',
1033 foreach my $iface (keys %$ifaces) {
1034 my $d = $ifaces->{$iface};
1035 $d->{type
} = 'unknown';
1036 if ($iface =~ m/^bond\d+$/) {
1037 if (!$d->{ovs_type
}) {
1038 $d->{type
} = 'bond';
1039 } elsif ($d->{ovs_type
} eq 'OVSBond') {
1040 $d->{type
} = $d->{ovs_type
};
1041 # translate: ovs_options => bond_mode
1042 $d->{'bond_mode'} = &$extract_ovs_option($d, 'bond_mode');
1043 my $lacp = &$extract_ovs_option($d, 'lacp');
1044 if ($lacp && $lacp eq 'active') {
1045 if ($d->{'bond_mode'} eq 'balance-slb') {
1046 $d->{'bond_mode'} = 'lacp-balance-slb';
1049 # Note: balance-tcp needs lacp
1050 if ($d->{'bond_mode'} eq 'balance-tcp') {
1051 $d->{'bond_mode'} = 'lacp-balance-tcp';
1053 my $tag = &$extract_ovs_option($d, 'tag');
1054 $d->{ovs_tag
} = $tag if defined($tag);
1056 } elsif ($iface =~ m/^vmbr\d+$/) {
1057 if (!$d->{ovs_type
}) {
1058 $d->{type
} = 'bridge';
1059 if (!defined ($d->{bridge_stp
})) {
1060 $d->{bridge_stp
} = 'off';
1062 if (!defined($d->{bridge_fd
}) && $d->{bridge_stp
} eq 'off') {
1063 $d->{bridge_fd
} = 0;
1065 } elsif ($d->{ovs_type
} eq 'OVSBridge') {
1066 $d->{type
} = $d->{ovs_type
};
1068 } elsif ($iface =~ m/^(\S+):\d+$/) {
1069 $d->{type
} = 'alias';
1070 if (defined ($ifaces->{$1})) {
1071 $d->{exists} = $ifaces->{$1}->{exists};
1073 $ifaces->{$1}->{exists} = 0;
1076 } elsif ($iface =~ m/^(\S+)\.(\d+)$/ || $d->{'vlan-raw-device'}) {
1077 $d->{type
} = 'vlan';
1079 my ($dev, $id) = ($1, $2);
1080 $d->{'vlan-raw-device'} = $dev if defined($dev) && !$d->{'vlan-raw-device'};
1082 if (!$id && $iface =~ m/^vlan(\d+)$/) { # VLAN id 0 is not valid, so truthy check it is
1085 $d->{'vlan-id'} = $id if $id;
1087 my $raw_iface = $d->{'vlan-raw-device'};
1089 if (defined ($ifaces->{$raw_iface})) {
1090 $d->{exists} = $ifaces->{$raw_iface}->{exists};
1092 $ifaces->{$raw_iface}->{exists} = 0;
1095 } elsif ($iface =~ m/^$PVE::Network::PHYSICAL_NIC_RE$/) {
1096 if (!$d->{ovs_type
}) {
1098 } elsif ($d->{ovs_type
} eq 'OVSPort') {
1099 $d->{type
} = $d->{ovs_type
};
1100 my $tag = &$extract_ovs_option($d, 'tag');
1101 $d->{ovs_tag
} = $tag if defined($tag);
1103 } elsif ($iface =~ m/^lo$/) {
1104 $d->{type
} = 'loopback';
1106 if ($d->{'vxlan-id'}) {
1107 $d->{type
} = 'vxlan';
1108 } elsif (defined($d->{ovs_type
})) {
1109 if ($d->{ovs_type
} eq 'OVSIntPort') {
1110 $d->{type
} = $d->{ovs_type
};
1111 my $tag = &$extract_ovs_option($d, 'tag');
1112 $d->{ovs_tag
} = $tag if defined($tag);
1114 } elsif (defined($d->{'link-type'})) {
1115 $d->{type
} = $d->{'link-type'} if $d->{'link-type'} eq 'dummy';
1119 # map address and netmask to cidr
1120 if (my $addr = $d->{address
}) {
1121 if (_address_is_cidr
($addr)) {
1123 my ($baseaddr, $mask) = _cidr_split
($addr);
1124 $d->{address
} = $baseaddr;
1125 $d->{netmask
} = $mask;
1126 } elsif (my $cidr = _get_cidr
($d->{address
}, $d->{netmask
})) {
1128 (undef, $d->{netmask
}) = _cidr_split
($cidr);
1130 # no mask, else we'd got a cidr above
1131 $d->{cidr
} = $addr ."/32";
1135 # map address6 and netmask6 to cidr6
1136 if (my $addr6 = $d->{address6
}) {
1137 if (_address_is_cidr
($addr6)) {
1138 $d->{cidr6
} = $addr6;
1139 my ($baseaddr, $mask) = _cidr_split
($addr6);
1140 $d->{address6
} = $baseaddr;
1141 $d->{netmask6
} = $mask;
1142 } elsif (my $cidr6 = _get_cidr
($d->{address6
}, $d->{netmask6
})) {
1143 $d->{cidr6
} = $cidr6;
1145 # no mask, else we'd got a cidr above
1146 $d->{cidr6
} = $addr6 ."/128";
1150 $d->{method} = 'manual' if !$d->{method};
1151 $d->{method6
} = 'manual' if !$d->{method6
};
1153 if (my $comments6 = delete $d->{comments6
}) {
1154 $d->{comments
} = ($d->{comments
} // '') . $comments6;
1157 $d->{families
} ||= ['inet'];
1160 # OVS bridges create "allow-$BRIDGE $IFACE" lines which we need to remove
1161 # from the {options} hash for them to be removed correctly.
1162 @$options = grep {defined($_)} map {
1163 my ($pri, $line) = @$_;
1164 if ($line =~ /^allow-ovs\s+(.*)$/) {
1166 } elsif ($line =~ /^allow-(\S+)\s+(.*)$/) {
1168 my @ports = split(/\s+/, $2);
1169 if (defined(my $br = $ifaces->{$bridge})) {
1170 # if this port is part of a bridge, remove it
1171 my %in_ovs_ports = map {$_=>1} split(/\s+/, $br->{ovs_ports
});
1172 @ports = grep { not $in_ovs_ports{$_} } @ports;
1174 # create the allow line for the remaining ports, or delete if empty
1176 [$pri, "allow-$bridge " . join(' ', @ports)];
1181 # don't modify other lines
1189 sub _address_is_cidr
{
1191 return $addr =~ /\/\d
+$/ ?
1 : 0;
1196 $cidr =~ /^(.+)\/(\d
+)$/;
1197 return ($1, $2); # (address, mask)
1201 my ($addr, $mask) = @_;
1203 return $addr if _address_is_cidr
($addr);
1204 return undef if !$mask;
1206 if ($mask =~ m/^\d+$/) { # cidr notation
1207 return $addr . "/" . $mask;
1208 } elsif (my $cidrmask = PVE
::JSONSchema
::get_netmask_bits
($mask)) {
1209 return $addr . "/" . $cidrmask;
1214 sub __interface_to_string
{
1215 my ($iface, $d, $family, $first_block, $ifupdown2) = @_;
1217 (my $suffix = $family) =~ s/^inet//;
1219 return '' if !($d && $d->{"method$suffix"});
1221 my $raw = "iface $iface $family " . $d->{"method$suffix"} . "\n";
1223 if (my $addr = $d->{"address$suffix"}) {
1224 if ($addr !~ /\/\d
+$/ && $d->{"netmask$suffix"}) {
1225 if ($d->{"netmask$suffix"} =~ m/^\d+$/) {
1226 $addr .= "/" . $d->{"netmask$suffix"};
1227 } elsif (my $mask = PVE
::JSONSchema
::get_netmask_bits
($d->{"netmask$suffix"})) {
1228 $addr .= "/" . $mask;
1231 $raw .= "\taddress ${addr}\n";
1234 $raw .= "\tgateway " . $d->{"gateway$suffix"} . "\n" if $d->{"gateway$suffix"};
1237 type
=> 1, priority
=> 1, method => 1, active
=> 1, exists => 1, comments
=> 1,
1238 autostart
=> 1, options
=> 1, address
=> 1, netmask
=> 1, gateway
=> 1, broadcast
=> 1,
1239 method6
=> 1, families
=> 1, options6
=> 1, comments6
=> 1, address6
=> 1,
1240 netmask6
=> 1, gateway6
=> 1, broadcast6
=> 1, 'uplink-id' => 1,
1243 if (!$first_block) {
1244 # not printing out options
1245 } elsif ($d->{type
} eq 'bridge') {
1247 my $ports = $d->{bridge_ports
} || 'none';
1248 $ports =~ s/[;,\s]+/ /g;
1249 $raw .= "\tbridge-ports $ports\n";
1250 $done->{bridge_ports
} = 1;
1252 my $br_stp = defined($d->{bridge_stp
}) ?
$d->{bridge_stp
} : 'off';
1253 my $no_stp = $br_stp eq 'off';
1255 $raw .= "\tbridge-stp $br_stp\n";
1256 $done->{bridge_stp
} = 1;
1258 # NOTE: forwarding delay must be 2 <= FD <= 30 if STP is enabled
1259 if (defined(my $br_fd = $d->{bridge_fd
})) {
1260 if ($no_stp || ($br_fd >= 2 && $br_fd <= 30)) {
1261 $raw .= "\tbridge-fd $br_fd\n";
1263 # only complain if the user actually set a value, but not for default fallback below
1264 warn "'$iface': ignoring 'bridge_fd' value '$br_fd', outside of allowed range 2-30\n";
1267 $raw .= "\tbridge-fd 0\n";
1269 $done->{bridge_fd
} = 1;
1271 if (defined($d->{bridge_vlan_aware
})) {
1272 $raw .= "\tbridge-vlan-aware yes\n";
1273 my $vlans = defined($d->{bridge_vids
}) ?
$d->{bridge_vids
} : "2-4094";
1274 $raw .= "\tbridge-vids $vlans\n";
1276 $done->{bridge_vlan_aware
} = 1;
1277 $done->{bridge_vids
} = 1;
1279 $raw .= "\tmtu $d->{mtu}\n" if $d->{mtu
};
1281 $done->{'bridge-disable-mac-learning'} = 1;
1283 } elsif ($d->{type
} eq 'bond') {
1285 $d->{slaves
} =~ s/[;,\s]+/ /g;
1286 my $slaves = $d->{slaves
} || 'none';
1287 $raw .= "\tbond-slaves $slaves\n";
1288 $done->{slaves
} = 1;
1290 my $v = defined ($d->{'bond_miimon'}) ?
$d->{'bond_miimon'} : 100;
1291 $raw .= "\tbond-miimon $v\n";
1292 $done->{'bond_miimon'} = 1;
1294 $v = defined ($d->{'bond_mode'}) ?
$d->{'bond_mode'} : 'balance-rr';
1295 $raw .= "\tbond-mode $v\n";
1296 $done->{'bond_mode'} = 1;
1298 if ($d->{'bond_mode'} && $d->{'bond_xmit_hash_policy'} &&
1299 ($d->{'bond_mode'} eq 'balance-xor' || $d->{'bond_mode'} eq '802.3ad')) {
1300 $raw .= "\tbond-xmit-hash-policy $d->{'bond_xmit_hash_policy'}\n";
1302 $done->{'bond_xmit_hash_policy'} = 1;
1304 if ($d->{'bond_mode'} && $d->{'bond_mode'} eq 'active-backup' && $d->{'bond-primary'}) {
1305 $raw .= "\tbond-primary $d->{'bond-primary'}\n";
1307 $done->{'bond-primary'} = 1;
1309 $raw .= "\tmtu $d->{mtu}\n" if $d->{mtu
};
1312 } elsif ($d->{type
} eq 'vlan') {
1313 die "$iface: wrong vlan-protocol $d->{'vlan-protocol'}\n"
1314 if $d->{'vlan-protocol'} && $d->{'vlan-protocol'} ne '802.1ad' && $d->{'vlan-protocol'} ne '802.1q';
1316 } elsif ($d->{type
} eq 'vxlan') {
1318 foreach my $k (qw(vxlan-id vxlan-svcnodeip vxlan-physdev vxlan-local-tunnelip)) {
1319 $raw .= "\t$k $d->{$k}\n" if defined $d->{$k};
1323 if ($d->{'vxlan-remoteip'}) {
1324 foreach my $remoteip (@{$d->{'vxlan-remoteip'}}) {
1325 $raw .= "\tvxlan-remoteip $remoteip\n";
1327 $done->{'vxlan-remoteip'} = 1;
1330 $raw .= "\tmtu $d->{mtu}\n" if $d->{mtu
};
1333 } elsif ($d->{type
} eq 'OVSBridge') {
1335 $raw .= "\tovs_type $d->{type}\n";
1336 $done->{ovs_type
} = 1;
1338 $raw .= "\tovs_ports $d->{ovs_ports}\n" if $d->{ovs_ports
};
1339 $done->{ovs_ports
} = 1;
1341 $raw .= "\tovs_mtu $d->{mtu}\n" if $d->{mtu
};
1344 } elsif ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' || $d->{type
} eq 'OVSBond') {
1346 $d->{autostart
} = 0; # started by the bridge
1348 if (defined($d->{ovs_tag
})) {
1349 &$set_ovs_option($d, tag
=> $d->{ovs_tag
});
1351 $done->{ovs_tag
} = 1;
1353 if ($d->{type
} eq 'OVSBond') {
1355 $d->{bond_mode
} = 'active-backup' if !$d->{bond_mode
};
1357 $ovs_bond_modes->{$d->{bond_mode
}} ||
1358 die "OVS does not support bond mode '$d->{bond_mode}\n";
1360 if ($d->{bond_mode
} eq 'lacp-balance-slb') {
1361 &$set_ovs_option($d, lacp
=> 'active');
1362 &$set_ovs_option($d, bond_mode
=> 'balance-slb');
1363 } elsif ($d->{bond_mode
} eq 'lacp-balance-tcp') {
1364 &$set_ovs_option($d, lacp
=> 'active');
1365 &$set_ovs_option($d, bond_mode
=> 'balance-tcp');
1367 &$set_ovs_option($d, lacp
=> undef);
1368 &$set_ovs_option($d, bond_mode
=> $d->{bond_mode
});
1370 $done->{bond_mode
} = 1;
1372 $raw .= "\tovs_bonds $d->{ovs_bonds}\n" if $d->{ovs_bonds
};
1373 $done->{ovs_bonds
} = 1;
1376 $raw .= "\tovs_type $d->{type}\n";
1377 $done->{ovs_type
} = 1;
1379 if (my $bridge = $d->{ovs_bridge
}) {
1381 $raw = "auto $iface\n$raw";
1383 $raw = "allow-$bridge $iface\n$raw";
1386 $raw .= "\tovs_bridge $bridge\n";
1387 $done->{ovs_bridge
} = 1;
1390 $raw .= "\tovs_mtu $d->{mtu}\n" if $d->{mtu
};
1395 # print other settings
1396 foreach my $k (sort keys %$d) {
1397 next if $done->{$k};
1399 $raw .= "\t$k $d->{$k}\n";
1403 foreach my $option (@{$d->{"options$suffix"}}) {
1404 $raw .= "\t$option\n";
1408 my $comments = $d->{"comments$suffix"} || '';
1409 foreach my $cl (split(/\n/, $comments)) {
1419 sub write_etc_network_interfaces
{
1420 my ($filename, $fh, $config) = @_;
1421 my $ifupdown2 = -e
'/usr/share/ifupdown2/ifupdown2';
1422 my $raw = __write_etc_network_interfaces
($config, $ifupdown2);
1423 PVE
::Tools
::safe_print
($filename, $fh, encode
('UTF-8', $raw));
1425 sub __write_etc_network_interfaces
{
1426 my ($config, $ifupdown2) = @_;
1428 my $ifaces = $config->{ifaces
};
1429 my @options = @{$config->{options
}};
1431 my $used_ports = {};
1433 foreach my $iface (keys %$ifaces) {
1434 my $d = $ifaces->{$iface};
1436 my ($cidr, $cidr6) = (delete $d->{cidr
}, delete $d->{cidr6
});
1437 $d->{address
} //= $cidr;
1438 $d->{address6
} //= $cidr6;
1441 foreach my $k (qw(bridge_ports ovs_ports slaves ovs_bonds)) {
1442 $ports .= " $d->{$k}" if $d->{$k};
1445 foreach my $p (PVE
::Tools
::split_list
($ports)) {
1446 die "port '$p' is already used on interface '$used_ports->{$p}'\n"
1447 if $used_ports->{$p} && $used_ports->{$p} ne $iface;
1448 $used_ports->{$p} = $iface;
1452 # delete unused OVS ports
1453 foreach my $iface (keys %$ifaces) {
1454 my $d = $ifaces->{$iface};
1455 if ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' || $d->{type
} eq 'OVSBond') {
1456 my $brname = $used_ports->{$iface};
1457 if (!$brname || !$ifaces->{$brname}) {
1458 if ($iface =~ /^$PVE::Network::PHYSICAL_NIC_RE/) {
1459 $ifaces->{$iface} = {
1463 families
=> ['inet'],
1466 delete $ifaces->{$iface};
1470 my $bd = $ifaces->{$brname};
1471 if ($bd->{type
} ne 'OVSBridge') {
1472 delete $ifaces->{$iface};
1478 # create OVS bridge ports
1479 foreach my $iface (keys %$ifaces) {
1480 my $d = $ifaces->{$iface};
1481 if ($d->{type
} eq 'OVSBridge' && $d->{ovs_ports
}) {
1482 foreach my $p (split (/\s+/, $d->{ovs_ports
})) {
1483 my $n = $ifaces->{$p};
1484 die "OVS bridge '$iface' - unable to find port '$p'\n" if !$n;
1485 $n->{autostart
} = 0;
1486 if ($n->{type
} eq 'eth') {
1487 $n->{type
} = 'OVSPort';
1488 $n->{ovs_bridge
} = $iface;
1489 } elsif ($n->{type
} eq 'OVSBond' || $n->{type
} eq 'OVSPort' ||
1490 $n->{type
} eq 'OVSIntPort') {
1491 $n->{ovs_bridge
} = $iface;
1493 die "interface '$p' is not defined as OVS port/bond\n";
1496 &$check_mtu($ifaces, $iface, $p);
1501 # check OVS bond ports
1502 foreach my $iface (keys %$ifaces) {
1503 my $d = $ifaces->{$iface};
1504 if ($d->{type
} eq 'OVSBond' && $d->{ovs_bonds
}) {
1505 foreach my $p (split (/\s+/, $d->{ovs_bonds
})) {
1506 my $n = $ifaces->{$p};
1507 $n->{autostart
} = 1;
1508 die "OVS bond '$iface' - unable to find slave '$p'\n" if !$n;
1509 die "OVS bond '$iface' - wrong interface type on slave '$p' ('$n->{type}' != 'eth')\n"
1510 if $n->{type
} ne 'eth';
1511 &$check_mtu($ifaces, $iface, $p);
1517 foreach my $iface (keys %$ifaces) {
1518 my $d = $ifaces->{$iface};
1519 next if !($d->{type
} eq 'bond' && $d->{slaves
});
1521 my $bond_primary_is_slave = undef;
1522 foreach my $p (split (/\s+/, $d->{slaves
})) {
1523 my $n = $ifaces->{$p};
1524 $n->{autostart
} = 1;
1526 die "bond '$iface' - unable to find slave '$p'\n" if !$n;
1527 die "bond '$iface' - wrong interface type on slave '$p' ('$n->{type}' != 'eth or bond')\n"
1528 if ($n->{type
} ne 'eth' && $n->{type
} ne 'bond');
1530 $check_mtu->($ifaces, $iface, $p);
1531 $bond_primary_is_slave = 1 if $d->{'bond-primary'} && $d->{'bond-primary'} eq $p;
1533 die "bond '$iface' - bond-primary interface is not a slave" if $d->{'bond-primary'} && !$bond_primary_is_slave;
1538 foreach my $iface (keys %$ifaces) {
1539 my $d = $ifaces->{$iface};
1541 if ($d->{type
} eq 'vxlan' && $d->{'vxlan-id'}) {
1542 my $vxlanid = $d->{'vxlan-id'};
1543 die "iface $iface - duplicate vxlan-id $vxlanid already used in $vxlans->{$vxlanid}\n" if $vxlans->{$vxlanid};
1544 $vxlans->{$vxlanid} = $iface;
1548 ++$ips if defined $d->{'vxlan-svcnodeip'};
1549 ++$ips if defined $d->{'vxlan-remoteip'};
1550 ++$ips if defined $d->{'vxlan-local-tunnelip'};
1552 die "iface $iface - vxlan-svcnodeip, vxlan-remoteip and vxlan-localtunnelip are mutually exclusive\n";
1555 if (defined($d->{'vxlan-svcnodeip'}) != defined($d->{'vxlan-physdev'})) {
1556 die "iface $iface - vxlan-svcnodeip and vxlan-physdev must be define together\n";
1558 #fixme : check if vxlan mtu is lower than 50bytes than physical interface where tunnel is going out
1562 foreach my $iface (keys %$ifaces) {
1563 my $d = $ifaces->{$iface};
1564 if ($d->{type
} eq 'vlan') {
1569 if ($iface =~ m/^(\S+)\.(\d+)$/) {
1572 delete $d->{'vlan-raw-device'} if $d->{'vlan-raw-device'};
1573 delete $d->{'vlan-id'} if $d->{'vlan-id'};
1576 die "missing vlan-raw-device option" if !$d->{'vlan-raw-device'};
1577 $p = $d->{'vlan-raw-device'};
1579 if ($iface =~ m/^vlan(\d+)$/) {
1581 delete $d->{'vlan-id'} if $d->{'vlan-id'};
1583 die "custom vlan interface name need ifupdown2" if !$ifupdown2;
1584 die "missing vlan-id option" if !$d->{'vlan-id'};
1585 $vlanid = $d->{'vlan-id'};
1588 my $n = $ifaces->{$p};
1590 die "vlan '$iface' - vlan-id $vlanid should be <= 4094\n" if $vlanid > 4094;
1591 die "vlan '$iface' - unable to find parent '$p'\n"
1594 if ($n->{type
} ne 'eth' && $n->{type
} ne 'bridge' && $n->{type
} ne 'bond' && $n->{type
} ne 'vlan') {
1595 die "vlan '$iface' - wrong interface type on parent '$p' " .
1596 "('$n->{type}' != 'eth|bond|bridge|vlan' )\n";
1599 &$check_mtu($ifaces, $p, $iface);
1606 foreach my $iface (keys %$ifaces) {
1607 my $d = $ifaces->{$iface};
1608 if (my $uplinkid = $d->{'uplink-id'}) {
1609 die "iface '$iface' - uplink-id $uplinkid is only allowed on physical and linux bond interfaces\n"
1610 if $d->{type
} ne 'eth' && $d->{type
} ne 'bond';
1612 die "iface '$iface' - uplink-id $uplinkid is already assigned on '$uplinks->{$uplinkid}'\n"
1613 if $uplinks->{$uplinkid};
1615 $uplinks->{$uplinkid} = $iface;
1619 # check bridgeport option
1620 my $bridgeports = {};
1622 my $ifaces_copy = { %$ifaces };
1623 foreach my $iface (keys %$ifaces_copy) {
1624 my $d = $ifaces_copy->{$iface};
1625 if ($d->{type
} eq 'bridge') {
1626 foreach my $p (split (/\s+/, $d->{bridge_ports
} // '')) {
1627 if($p =~ m/(\S+)\.(\d+)$/) {
1628 my $vlanparent = $1;
1629 if (!defined($ifaces_copy->{$p})) {
1630 $ifaces_copy->{$p}->{type
} = 'vlan';
1631 $ifaces_copy->{$p}->{method} = 'manual';
1632 $ifaces_copy->{$p}->{method6
} = 'manual';
1633 $ifaces_copy->{$p}->{mtu
} = $ifaces_copy->{$vlanparent}->{mtu
} if defined($ifaces_copy->{$1}->{mtu
});
1636 my $n = $ifaces_copy->{$p};
1637 die "bridge '$iface' - unable to find bridge port '$p'\n" if !$n;
1638 die "iface $p - ip address can't be set on interface if bridged in $iface\n"
1639 if ($n->{method} && $n->{method} eq 'static' && $n->{address
} ne '0.0.0.0') ||
1640 ($n->{method6
} && $n->{method6
} eq 'static' && $n->{address6
} ne '::');
1641 &$check_mtu($ifaces_copy, $p, $iface);
1642 $bridgeports->{$p} = $iface;
1644 $bridges->{$iface} = $d;
1648 foreach my $iface (keys %$ifaces) {
1649 my $d = $ifaces->{$iface};
1651 foreach my $k (qw(bridge-learning bridge-arp-nd-suppress bridge-unicast-flood bridge-multicast-flood bridge-access)) {
1652 die "iface $iface - $k: bridge port specific options can be used only on interfaces attached to a bridge\n"
1653 if $d->{$k} && !$bridgeports->{$iface};
1656 if ($d->{'bridge-access'} && !$bridges->{$bridgeports->{$iface}}->{bridge_vlan_aware
}) {
1657 die "iface $iface - bridge-access option can be only used if interface is in a vlan aware bridge\n";
1661 my $raw = <<'NETWORKDOC';
1662 # network interface settings; autogenerated
1663 # Please do NOT modify this file directly, unless you know what
1666 # If you want to manage parts of the network configuration manually,
1667 # please utilize the 'source' or 'source-directory' directives to do
1669 # PVE will preserve these directives, but will NOT read its network
1670 # configuration from sourced files, so do not attempt to move any of
1671 # the PVE managed interfaces into external files!
1677 my $if_type_hash = {
1682 OVSIntPort
=> 300000,
1686 OVSBridge
=> 500000,
1691 my $lookup_type_prio = sub {
1692 my ($iface, $ifaces) = @_;
1694 my ($rootiface, @rest) = split(/[.:]/, $iface);
1695 my $childlevel = scalar(@rest);
1696 my $type = $ifaces->{$rootiface}->{type
};
1697 return if !$type || $type eq 'unknown';
1699 return $if_type_hash->{$type} + $childlevel
1702 foreach my $iface (sort {
1703 my $ref1 = $ifaces->{$a};
1704 my $ref2 = $ifaces->{$b};
1705 my $tp1 = &$lookup_type_prio($a, $ifaces);
1706 my $tp2 = &$lookup_type_prio($b, $ifaces);
1708 # Only recognized types are in relation to each other. If one type
1709 # is unknown then only consider the interfaces' priority attributes.
1710 $tp1 = $tp2 = 0 if !defined($tp1) || !defined($tp2);
1712 my $p1 = $tp1 + ($ref1->{priority
} // 50000);
1713 my $p2 = $tp2 + ($ref2->{priority
} // 50000);
1715 return $p1 <=> $p2 if $p1 != $p2;
1719 next if $printed->{$iface};
1720 my $d = $ifaces->{$iface};
1721 my $pri = $d->{priority
} // 0;
1722 if (@options && $options[0]->[0] < $pri) {
1724 $raw .= (shift @options)->[1] . "\n";
1725 } while (@options && $options[0]->[0] < $pri);
1729 $printed->{$iface} = 1;
1730 if ($d->{autostart
}) {
1731 if ($d->{type
} eq 'OVSBridge' && !$ifupdown2) {
1732 # cannot use 'auto' for OVS, would add race with systemd ifup@.service
1733 $raw .= "allow-ovs $iface\n";
1735 $raw .= "auto $iface\n";
1739 # if 'inet6' is the only family
1740 if (scalar($d->{families
}->@*) == 1 && $d->{families
}[0] eq 'inet6') {
1741 $d->{comments6
} = delete $d->{comments
};
1744 my $i = 0; # some options should be printed only once
1745 $raw .= __interface_to_string
($iface, $d, $_, !$i++, $ifupdown2) foreach @{$d->{families
}};
1748 $raw .= $_->[1] . "\n" foreach @options;
1752 register_file
('interfaces', "/etc/network/interfaces",
1753 \
&read_etc_network_interfaces
,
1754 \
&write_etc_network_interfaces
);
1757 sub read_iscsi_initiatorname
{
1758 my ($filename, $fd) = @_;
1760 while (defined(my $line = <$fd>)) {
1761 if ($line =~ m/^InitiatorName=(\S+)$/) {
1769 register_file
('initiatorname', "/etc/iscsi/initiatorname.iscsi",
1770 \
&read_iscsi_initiatorname
);