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
);
726 our $bond_modes = { 'balance-rr' => 0,
727 'active-backup' => 1,
735 my $ovs_bond_modes = {
736 'active-backup' => 1,
738 'lacp-balance-slb' => 1,
739 'lacp-balance-tcp' => 1,
742 #sub get_bond_modes {
743 # return $bond_modes;
746 my $parse_ovs_option = sub {
750 foreach my $kv (split (/\s+/, $data || '')) {
751 my ($k, $v) = split('=', $kv, 2);
752 $opts->{$k} = $v if $k && $v;
757 my $set_ovs_option = sub {
758 my ($d, %params) = @_;
760 my $opts = &$parse_ovs_option($d->{ovs_options
});
762 foreach my $k (keys %params) {
772 foreach my $k (keys %$opts) {
773 push @$res, "$k=$opts->{$k}";
776 if (my $new = join(' ', @$res)) {
777 $d->{ovs_options
} = $new;
778 return $d->{ovs_options
};
780 delete $d->{ovs_options
};
785 my $extract_ovs_option = sub {
788 my $opts = &$parse_ovs_option($d->{ovs_options
});
790 my $v = delete $opts->{$name};
793 foreach my $k (keys %$opts) {
794 push @$res, "$k=$opts->{$k}";
797 if (my $new = join(' ', @$res)) {
798 $d->{ovs_options
} = $new;
800 delete $d->{ovs_options
};
806 my $check_mtu = sub {
807 my ($ifaces, $parent, $child) = @_;
809 die "check mtu - missing parent interface\n" if !$parent;
810 die "check mtu - missing child interface\n" if !$child;
812 my $cmtu = $ifaces->{$child}->{mtu
};
815 my $parentdata = $ifaces->{$parent};
816 my $pmtu = $parentdata->{mtu
};
817 $pmtu = $cmtu if $parentdata->{type
} eq 'bond' && !$pmtu;
818 $pmtu = 1500 if !$pmtu;
820 die "interface '$parent' - mtu $pmtu is lower than '$child' - mtu $cmtu\n"
827 # <optional> exists => BOOL,
828 # <optional> active => BOOL,
829 # <optional> autostart => BOOL,
830 # <auto> priority => INT,
832 # type => "eth" | "bridge" | "bond" | "loopback" | "OVS*" | ... ,
834 # families => ["inet", "inet6", ...],
836 # method => "manual" | "static" | "dhcp" | ... ,
841 # comments => [ "..." ],
843 # method6 => "manual" | "static" | "dhcp" | ... ,
845 # netmask6 => SUBNET,
847 # comments6 => [ "..." ],
849 # <known options>, # like bridge_ports, ovs_*
851 # # extra/unknown options stored by-family:
852 # options => { <inet options>... }
853 # options6 => { <inet6 options>... }
857 # # mappings end up here as well, as we don't need to understand them
861 sub read_etc_network_interfaces
{
862 my ($filename, $fh) = @_;
863 my $proc_net_dev = IO
::File-
>new('/proc/net/dev', 'r');
864 my $active = PVE
::ProcFSTools
::get_active_network_interfaces
();
865 return __read_etc_network_interfaces
($fh, $proc_net_dev, $active);
868 sub __read_etc_network_interfaces
{
869 my ($fh, $proc_net_dev, $active_ifaces) = @_;
872 my $ifaces = $config->{ifaces
} = {};
873 my $options = $config->{options
} = [];
875 my $options_alternatives = {
877 'bond-slaves' => 'slaves',
878 'bond_slaves' => 'slaves',
879 'bond-xmit-hash-policy' => 'bond_xmit_hash_policy',
880 'bond-mode' => 'bond_mode',
881 'bond-miimon' =>'bond_miimon',
882 'bridge-vlan-aware' => 'bridge_vlan_aware',
883 'bridge-fd' => 'bridge_fd',
884 'bridge-stp' => 'bridge_stp',
885 'bridge-ports' => 'bridge_ports',
886 'bridge-vids' => 'bridge_vids'
892 while (defined ($line = <$proc_net_dev>)) {
893 if ($line =~ m/^\s*($PVE::Network::PHYSICAL_NIC_RE):.*/) {
894 $ifaces->{$1}->{exists} = 1;
897 close($proc_net_dev);
900 # we try to keep order inside the file
901 my $priority = 2; # 1 is reserved for lo
903 SECTION
: while (defined ($line = <$fh>)) {
905 next if $line =~ m/^\s*#/;
906 next if $line =~ m/^\s*(allow-hotplug)\s+(.*)$/;
908 if ($line =~ m/^\s*(auto|allow-ovs)\s+(.*)$/) {
909 my @aa = split (/\s+/, $2);
911 foreach my $a (@aa) {
912 $ifaces->{$a}->{autostart
} = 1;
915 } elsif ($line =~ m/^\s*iface\s+(\S+)\s+(inet6?)\s+(\S+)\s*$/) {
918 my $f = { method => $3 }; # by family, merged to $d with a $suffix
919 (my $suffix = $family) =~ s/^inet//;
921 my $d = $ifaces->{$i} ||= {};
922 $d->{priority
} = $priority++ if !$d->{priority
};
923 push @{$d->{families
}}, $family;
925 while (defined ($line = <$fh>)) {
926 $line =~ s/\s+$//; # drop trailing whitespaces
928 if ($line =~ m/^\s*#(.*?)\s*$/) {
929 $f->{comments
} = '' if !$f->{comments
};
930 my $comment = decode
('UTF-8', $1);
931 $f->{comments
} .= "$comment\n";
932 } elsif ($line =~ m
/^\s
*(?
:iface\s
940 } elsif ($line =~ m/^\s*((\S+)\s+(.+))$/) {
942 my ($id, $value) = ($2, $3);
944 $id = $options_alternatives->{$id} if $options_alternatives->{$id};
946 my $simple_options = {
955 'bridge-access' => 1,
956 'bridge-learning' => 1,
957 'bridge-arp-nd-suppress' => 1,
958 'bridge-unicast-flood' => 1,
959 'bridge-multicast-flood' => 1,
961 'bond_xmit_hash_policy' => 1,
964 'vlan-protocol' => 1,
965 'vlan-raw-device' => 1,
968 'vxlan-svcnodeip' => 1,
969 'vxlan-physdev' => 1,
970 'vxlan-local-tunnelip' => 1 };
972 if (($id eq 'address') || ($id eq 'netmask') || ($id eq 'broadcast') || ($id eq 'gateway')) {
974 } elsif ($simple_options->{$id}) {
976 } elsif ($id eq 'slaves' || $id eq 'bridge_ports') {
978 foreach my $p (split (/\s+/, $value)) {
979 next if $p eq 'none';
982 my $str = join (' ', sort keys %{$devs});
984 $d->{$id} .= ' ' . $str if $str;
986 $d->{$id} = $str || '';
988 } elsif ($id eq 'bridge_stp') {
989 if ($value =~ m/^\s*(on|yes)\s*$/i) {
994 } elsif ($id eq 'bridge_vlan_aware') {
996 } elsif ($id eq 'bond_mode') {
998 foreach my $bm (keys %$bond_modes) {
999 my $id = $bond_modes->{$bm};
1000 if ($id eq $value) {
1006 } elsif ($id eq 'vxlan-remoteip') {
1007 push @{$d->{$id}}, $value;
1009 push @{$f->{options
}}, $option;
1015 $d->{"$_$suffix"} = $f->{$_} foreach (keys %$f);
1016 last SECTION
if !defined($line);
1018 } elsif ($line =~ /\w/) {
1019 push @$options, [$priority++, $line];
1023 foreach my $ifname (@$active_ifaces) {
1024 if (my $iface = $ifaces->{$ifname}) {
1025 $iface->{active
} = 1;
1029 if (!$ifaces->{lo
}) {
1030 $ifaces->{lo
}->{priority
} = 1;
1031 $ifaces->{lo
}->{method} = 'loopback';
1032 $ifaces->{lo
}->{type
} = 'loopback';
1033 $ifaces->{lo
}->{autostart
} = 1;
1036 foreach my $iface (keys %$ifaces) {
1037 my $d = $ifaces->{$iface};
1038 $d->{type
} = 'unknown';
1039 if ($iface =~ m/^bond\d+$/) {
1040 if (!$d->{ovs_type
}) {
1041 $d->{type
} = 'bond';
1042 } elsif ($d->{ovs_type
} eq 'OVSBond') {
1043 $d->{type
} = $d->{ovs_type
};
1044 # translate: ovs_options => bond_mode
1045 $d->{'bond_mode'} = &$extract_ovs_option($d, 'bond_mode');
1046 my $lacp = &$extract_ovs_option($d, 'lacp');
1047 if ($lacp && $lacp eq 'active') {
1048 if ($d->{'bond_mode'} eq 'balance-slb') {
1049 $d->{'bond_mode'} = 'lacp-balance-slb';
1052 # Note: balance-tcp needs lacp
1053 if ($d->{'bond_mode'} eq 'balance-tcp') {
1054 $d->{'bond_mode'} = 'lacp-balance-tcp';
1056 my $tag = &$extract_ovs_option($d, 'tag');
1057 $d->{ovs_tag
} = $tag if defined($tag);
1059 } elsif ($iface =~ m/^vmbr\d+$/) {
1060 if (!$d->{ovs_type
}) {
1061 $d->{type
} = 'bridge';
1063 if (!defined ($d->{bridge_fd
})) {
1064 $d->{bridge_fd
} = 0;
1066 if (!defined ($d->{bridge_stp
})) {
1067 $d->{bridge_stp
} = 'off';
1069 } elsif ($d->{ovs_type
} eq 'OVSBridge') {
1070 $d->{type
} = $d->{ovs_type
};
1072 } elsif ($iface =~ m/^(\S+):\d+$/) {
1073 $d->{type
} = 'alias';
1074 if (defined ($ifaces->{$1})) {
1075 $d->{exists} = $ifaces->{$1}->{exists};
1077 $ifaces->{$1}->{exists} = 0;
1080 } elsif ($iface =~ m/^(\S+)\.\d+$/ || $d->{'vlan-raw-device'}) {
1081 $d->{type
} = 'vlan';
1083 my $raw_iface = $d->{'vlan-raw-device'} ?
$d->{'vlan-raw-device'} : $1;
1084 if (defined ($ifaces->{$raw_iface})) {
1085 $d->{exists} = $ifaces->{$raw_iface}->{exists};
1087 $ifaces->{$raw_iface}->{exists} = 0;
1090 } elsif ($iface =~ m/^$PVE::Network::PHYSICAL_NIC_RE$/) {
1091 if (!$d->{ovs_type
}) {
1093 } elsif ($d->{ovs_type
} eq 'OVSPort') {
1094 $d->{type
} = $d->{ovs_type
};
1095 my $tag = &$extract_ovs_option($d, 'tag');
1096 $d->{ovs_tag
} = $tag if defined($tag);
1098 } elsif ($iface =~ m/^lo$/) {
1099 $d->{type
} = 'loopback';
1101 if ($d->{'vxlan-id'}) {
1102 $d->{type
} = 'vxlan';
1103 } elsif (defined($d->{ovs_type
})) {
1104 if ($d->{ovs_type
} eq 'OVSIntPort') {
1105 $d->{type
} = $d->{ovs_type
};
1106 my $tag = &$extract_ovs_option($d, 'tag');
1107 $d->{ovs_tag
} = $tag if defined($tag);
1112 # map address and netmask to cidr
1113 if (my $addr = $d->{address
}) {
1114 if (_address_is_cidr
($addr)) {
1116 my ($baseaddr, $mask) = _cidr_split
($addr);
1117 $d->{address
} = $baseaddr;
1118 $d->{netmask
} = $mask;
1119 } elsif (my $cidr = _get_cidr
($d->{address
}, $d->{netmask
})) {
1121 (undef, $d->{netmask
}) = _cidr_split
($cidr);
1123 # no mask, else we'd got a cidr above
1124 $d->{cidr
} = $addr ."/32";
1128 # map address6 and netmask6 to cidr6
1129 if (my $addr6 = $d->{address6
}) {
1130 if (_address_is_cidr
($addr6)) {
1131 $d->{cidr6
} = $addr6;
1132 my ($baseaddr, $mask) = _cidr_split
($addr6);
1133 $d->{address6
} = $baseaddr;
1134 $d->{netmask6
} = $mask;
1135 } elsif (my $cidr6 = _get_cidr
($d->{address6
}, $d->{netmask6
})) {
1136 $d->{cidr6
} = $cidr6;
1138 # no mask, else we'd got a cidr above
1139 $d->{cidr6
} = $addr6 ."/128";
1143 $d->{method} = 'manual' if !$d->{method};
1144 $d->{method6
} = 'manual' if !$d->{method6
};
1146 $d->{families
} ||= ['inet'];
1149 # OVS bridges create "allow-$BRIDGE $IFACE" lines which we need to remove
1150 # from the {options} hash for them to be removed correctly.
1151 @$options = grep {defined($_)} map {
1152 my ($pri, $line) = @$_;
1153 if ($line =~ /^allow-ovs\s+(.*)$/) {
1155 } elsif ($line =~ /^allow-(\S+)\s+(.*)$/) {
1157 my @ports = split(/\s+/, $2);
1158 if (defined(my $br = $ifaces->{$bridge})) {
1159 # if this port is part of a bridge, remove it
1160 my %in_ovs_ports = map {$_=>1} split(/\s+/, $br->{ovs_ports
});
1161 @ports = grep { not $in_ovs_ports{$_} } @ports;
1163 # create the allow line for the remaining ports, or delete if empty
1165 [$pri, "allow-$bridge " . join(' ', @ports)];
1170 # don't modify other lines
1178 sub _address_is_cidr
{
1180 return $addr =~ /\/\d
+$/ ?
1 : 0;
1185 $cidr =~ /^(.+)\/(\d
+)$/;
1186 return ($1, $2); # (address, mask)
1190 my ($addr, $mask) = @_;
1192 return $addr if _address_is_cidr
($addr);
1193 return undef if !$mask;
1195 if ($mask =~ m/^\d+$/) { # cidr notation
1196 return $addr . "/" . $mask;
1197 } elsif (my $cidrmask = PVE
::JSONSchema
::get_netmask_bits
($mask)) {
1198 return $addr . "/" . $cidrmask;
1203 sub __interface_to_string
{
1204 my ($iface, $d, $family, $first_block, $ifupdown2) = @_;
1206 (my $suffix = $family) =~ s/^inet//;
1208 return '' if !($d && $d->{"method$suffix"});
1212 $raw .= "iface $iface $family " . $d->{"method$suffix"} . "\n";
1214 if (my $addr = $d->{"address$suffix"}) {
1216 if ($addr !~ /\/\d
+$/ && $d->{"netmask$suffix"}) {
1217 if ($d->{"netmask$suffix"} =~ m/^\d+$/) {
1218 $addr .= "/" . $d->{"netmask$suffix"};
1219 } elsif (my $mask = PVE
::JSONSchema
::get_netmask_bits
($d->{"netmask$suffix"})) {
1220 $addr .= "/" . $mask;
1224 $raw .= "\taddress " . $addr . "\n";
1227 $raw .= "\tgateway " . $d->{"gateway$suffix"} . "\n" if $d->{"gateway$suffix"};
1229 my $done = { type
=> 1, priority
=> 1, method => 1, active
=> 1, exists => 1,
1230 comments
=> 1, autostart
=> 1, options
=> 1,
1231 address
=> 1, netmask
=> 1, gateway
=> 1, broadcast
=> 1,
1232 method6
=> 1, families
=> 1, options6
=> 1,
1233 address6
=> 1, netmask6
=> 1, gateway6
=> 1, broadcast6
=> 1, 'uplink-id' => 1 };
1235 if (!$first_block) {
1236 # not printing out options
1237 } elsif ($d->{type
} eq 'bridge') {
1239 my $ports = $d->{bridge_ports
} || 'none';
1240 $ports =~ s/[;,\s]+/ /g;
1241 $raw .= "\tbridge-ports $ports\n";
1242 $done->{bridge_ports
} = 1;
1244 my $v = defined($d->{bridge_stp
}) ?
$d->{bridge_stp
} : 'off';
1245 $raw .= "\tbridge-stp $v\n";
1246 $done->{bridge_stp
} = 1;
1248 $v = defined($d->{bridge_fd
}) ?
$d->{bridge_fd
} : 0;
1249 $raw .= "\tbridge-fd $v\n";
1250 $done->{bridge_fd
} = 1;
1252 if( defined($d->{bridge_vlan_aware
})) {
1253 $raw .= "\tbridge-vlan-aware yes\n";
1254 $v = defined($d->{bridge_vids
}) ?
$d->{bridge_vids
} : "2-4094";
1255 $raw .= "\tbridge-vids $v\n";
1257 $done->{bridge_vlan_aware
} = 1;
1258 $done->{bridge_vids
} = 1;
1260 $raw .= "\tmtu $d->{mtu}\n" if $d->{mtu
};
1263 } elsif ($d->{type
} eq 'bond') {
1265 $d->{slaves
} =~ s/[;,\s]+/ /g;
1266 my $slaves = $d->{slaves
} || 'none';
1267 $raw .= "\tbond-slaves $slaves\n";
1268 $done->{slaves
} = 1;
1270 my $v = defined ($d->{'bond_miimon'}) ?
$d->{'bond_miimon'} : 100;
1271 $raw .= "\tbond-miimon $v\n";
1272 $done->{'bond_miimon'} = 1;
1274 $v = defined ($d->{'bond_mode'}) ?
$d->{'bond_mode'} : 'balance-rr';
1275 $raw .= "\tbond-mode $v\n";
1276 $done->{'bond_mode'} = 1;
1278 if ($d->{'bond_mode'} && $d->{'bond_xmit_hash_policy'} &&
1279 ($d->{'bond_mode'} eq 'balance-xor' || $d->{'bond_mode'} eq '802.3ad')) {
1280 $raw .= "\tbond-xmit-hash-policy $d->{'bond_xmit_hash_policy'}\n";
1282 $done->{'bond_xmit_hash_policy'} = 1;
1284 if ($d->{'bond_mode'} && $d->{'bond_mode'} eq 'active-backup' && $d->{'bond-primary'}) {
1285 $raw .= "\tbond-primary $d->{'bond-primary'}\n";
1287 $done->{'bond-primary'} = 1;
1289 $raw .= "\tmtu $d->{mtu}\n" if $d->{mtu
};
1292 } elsif ($d->{type
} eq 'vlan') {
1293 die "$iface: wrong vlan-protocol $d->{'vlan-protocol'}\n"
1294 if $d->{'vlan-protocol'} && $d->{'vlan-protocol'} ne '802.1ad' && $d->{'vlan-protocol'} ne '802.1q';
1296 } elsif ($d->{type
} eq 'vxlan') {
1298 foreach my $k (qw(vxlan-id vxlan-svcnodeip vxlan-physdev vxlan-local-tunnelip)) {
1299 $raw .= "\t$k $d->{$k}\n" if defined $d->{$k};
1303 if ($d->{'vxlan-remoteip'}) {
1304 foreach my $remoteip (@{$d->{'vxlan-remoteip'}}) {
1305 $raw .= "\tvxlan-remoteip $remoteip\n";
1307 $done->{'vxlan-remoteip'} = 1;
1310 $raw .= "\tmtu $d->{mtu}\n" if $d->{mtu
};
1313 } elsif ($d->{type
} eq 'OVSBridge') {
1315 $raw .= "\tovs_type $d->{type}\n";
1316 $done->{ovs_type
} = 1;
1318 $raw .= "\tovs_ports $d->{ovs_ports}\n" if $d->{ovs_ports
};
1319 $done->{ovs_ports
} = 1;
1321 $raw .= "\tovs_mtu $d->{mtu}\n" if $d->{mtu
};
1324 } elsif ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' ||
1325 $d->{type
} eq 'OVSBond') {
1327 $d->{autostart
} = 0; # started by the bridge
1329 if (defined($d->{ovs_tag
})) {
1330 &$set_ovs_option($d, tag
=> $d->{ovs_tag
});
1332 $done->{ovs_tag
} = 1;
1334 if ($d->{type
} eq 'OVSBond') {
1336 $d->{bond_mode
} = 'active-backup' if !$d->{bond_mode
};
1338 $ovs_bond_modes->{$d->{bond_mode
}} ||
1339 die "OVS does not support bond mode '$d->{bond_mode}\n";
1341 if ($d->{bond_mode
} eq 'lacp-balance-slb') {
1342 &$set_ovs_option($d, lacp
=> 'active');
1343 &$set_ovs_option($d, bond_mode
=> 'balance-slb');
1344 } elsif ($d->{bond_mode
} eq 'lacp-balance-tcp') {
1345 &$set_ovs_option($d, lacp
=> 'active');
1346 &$set_ovs_option($d, bond_mode
=> 'balance-tcp');
1348 &$set_ovs_option($d, lacp
=> undef);
1349 &$set_ovs_option($d, bond_mode
=> $d->{bond_mode
});
1351 $done->{bond_mode
} = 1;
1353 $raw .= "\tovs_bonds $d->{ovs_bonds}\n" if $d->{ovs_bonds
};
1354 $done->{ovs_bonds
} = 1;
1357 $raw .= "\tovs_type $d->{type}\n";
1358 $done->{ovs_type
} = 1;
1360 if (my $bridge = $d->{ovs_bridge
}) {
1362 $raw = "auto $iface\n$raw";
1364 $raw = "allow-$bridge $iface\n$raw";
1367 $raw .= "\tovs_bridge $bridge\n";
1368 $done->{ovs_bridge
} = 1;
1371 $raw .= "\tovs_mtu $d->{mtu}\n" if $d->{mtu
};
1376 # print other settings
1377 foreach my $k (sort keys %$d) {
1378 next if $done->{$k};
1380 $raw .= "\t$k $d->{$k}\n";
1384 foreach my $option (@{$d->{"options$suffix"}}) {
1385 $raw .= "\t$option\n";
1389 my $comments = $d->{"comments$suffix"} || '';
1390 foreach my $cl (split(/\n/, $comments)) {
1400 sub write_etc_network_interfaces
{
1401 my ($filename, $fh, $config) = @_;
1402 my $ifupdown2 = -e
'/usr/share/ifupdown2/ifupdown2';
1403 my $raw = __write_etc_network_interfaces
($config, $ifupdown2);
1404 PVE
::Tools
::safe_print
($filename, $fh, encode
('UTF-8', $raw));
1406 sub __write_etc_network_interfaces
{
1407 my ($config, $ifupdown2) = @_;
1409 my $ifaces = $config->{ifaces
};
1410 my @options = @{$config->{options
}};
1412 my $used_ports = {};
1414 foreach my $iface (keys %$ifaces) {
1415 my $d = $ifaces->{$iface};
1417 my ($cidr, $cidr6) = (delete $d->{cidr
}, delete $d->{cidr6
});
1418 $d->{address
} //= $cidr;
1419 $d->{address6
} //= $cidr6;
1422 foreach my $k (qw(bridge_ports ovs_ports slaves ovs_bonds)) {
1423 $ports .= " $d->{$k}" if $d->{$k};
1426 foreach my $p (PVE
::Tools
::split_list
($ports)) {
1427 die "port '$p' is already used on interface '$used_ports->{$p}'\n"
1428 if $used_ports->{$p} && $used_ports->{$p} ne $iface;
1429 $used_ports->{$p} = $iface;
1433 # delete unused OVS ports
1434 foreach my $iface (keys %$ifaces) {
1435 my $d = $ifaces->{$iface};
1436 if ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' ||
1437 $d->{type
} eq 'OVSBond') {
1438 my $brname = $used_ports->{$iface};
1439 if (!$brname || !$ifaces->{$brname}) {
1440 if ($iface =~ /^$PVE::Network::PHYSICAL_NIC_RE/) {
1441 $ifaces->{$iface} = {
1445 families
=> ['inet'],
1448 delete $ifaces->{$iface};
1452 my $bd = $ifaces->{$brname};
1453 if ($bd->{type
} ne 'OVSBridge') {
1454 delete $ifaces->{$iface};
1460 # create OVS bridge ports
1461 foreach my $iface (keys %$ifaces) {
1462 my $d = $ifaces->{$iface};
1463 if ($d->{type
} eq 'OVSBridge' && $d->{ovs_ports
}) {
1464 foreach my $p (split (/\s+/, $d->{ovs_ports
})) {
1465 my $n = $ifaces->{$p};
1466 die "OVS bridge '$iface' - unable to find port '$p'\n"
1468 $n->{autostart
} = 0;
1469 if ($n->{type
} eq 'eth') {
1470 $n->{type
} = 'OVSPort';
1471 $n->{ovs_bridge
} = $iface;
1472 } elsif ($n->{type
} eq 'OVSBond' || $n->{type
} eq 'OVSPort' ||
1473 $n->{type
} eq 'OVSIntPort') {
1474 $n->{ovs_bridge
} = $iface;
1476 die "interface '$p' is not defined as OVS port/bond\n";
1479 &$check_mtu($ifaces, $iface, $p);
1484 # check OVS bond ports
1485 foreach my $iface (keys %$ifaces) {
1486 my $d = $ifaces->{$iface};
1487 if ($d->{type
} eq 'OVSBond' && $d->{ovs_bonds
}) {
1488 foreach my $p (split (/\s+/, $d->{ovs_bonds
})) {
1489 my $n = $ifaces->{$p};
1490 $n->{autostart
} = 1;
1491 die "OVS bond '$iface' - unable to find slave '$p'\n"
1493 die "OVS bond '$iface' - wrong interface type on slave '$p' " .
1494 "('$n->{type}' != 'eth')\n" if $n->{type
} ne 'eth';
1495 &$check_mtu($ifaces, $iface, $p);
1501 foreach my $iface (keys %$ifaces) {
1502 my $d = $ifaces->{$iface};
1503 next if !($d->{type
} eq 'bond' && $d->{slaves
});
1505 my $bond_primary_is_slave = undef;
1506 foreach my $p (split (/\s+/, $d->{slaves
})) {
1507 my $n = $ifaces->{$p};
1508 $n->{autostart
} = 1;
1510 die "bond '$iface' - unable to find slave '$p'\n" if !$n;
1511 die "bond '$iface' - wrong interface type on slave '$p' ('$n->{type}' != 'eth or bond')\n"
1512 if ($n->{type
} ne 'eth' && $n->{type
} ne 'bond');
1514 $check_mtu->($ifaces, $iface, $p);
1515 $bond_primary_is_slave = 1 if $d->{'bond-primary'} && $d->{'bond-primary'} eq $p;
1517 die "bond '$iface' - bond-primary interface is not a slave" if $d->{'bond-primary'} && !$bond_primary_is_slave;
1522 foreach my $iface (keys %$ifaces) {
1523 my $d = $ifaces->{$iface};
1525 if ($d->{type
} eq 'vxlan' && $d->{'vxlan-id'}) {
1526 my $vxlanid = $d->{'vxlan-id'};
1527 die "iface $iface - duplicate vxlan-id $vxlanid already used in $vxlans->{$vxlanid}\n" if $vxlans->{$vxlanid};
1528 $vxlans->{$vxlanid} = $iface;
1532 ++$ips if defined $d->{'vxlan-svcnodeip'};
1533 ++$ips if defined $d->{'vxlan-remoteip'};
1534 ++$ips if defined $d->{'vxlan-local-tunnelip'};
1536 die "iface $iface - vxlan-svcnodeip, vxlan-remoteip and vxlan-localtunnelip are mutually exclusive\n";
1539 if (defined($d->{'vxlan-svcnodeip'}) != defined($d->{'vxlan-physdev'})) {
1540 die "iface $iface - vxlan-svcnodeip and vxlan-physdev must be define together\n";
1542 #fixme : check if vxlan mtu is lower than 50bytes than physical interface where tunnel is going out
1546 foreach my $iface (keys %$ifaces) {
1547 my $d = $ifaces->{$iface};
1548 if ($d->{type
} eq 'vlan') {
1553 if ($iface =~ m/^(\S+)\.(\d+)$/) {
1556 delete $d->{'vlan-raw-device'} if $d->{'vlan-raw-device'};
1558 die "missing vlan-raw-device option" if !$d->{'vlan-raw-device'};
1559 $p = $d->{'vlan-raw-device'};
1561 if ($iface =~ m/^vlan(\d+)$/) {
1563 delete $d->{'vlan-id'} if $d->{'vlan-id'};
1565 die "custom vlan interface name need ifupdown2" if !$ifupdown2;
1566 die "missing vlan-id option" if !$d->{'vlan-id'};
1567 $vlanid = $d->{'vlan-id'};
1570 my $n = $ifaces->{$p};
1572 die "vlan '$iface' - vlan-id $vlanid should be <= 4094\n" if $vlanid > 4094;
1573 die "vlan '$iface' - unable to find parent '$p'\n"
1576 if ($n->{type
} ne 'eth' && $n->{type
} ne 'bridge' && $n->{type
} ne 'bond' && $n->{type
} ne 'vlan') {
1577 die "vlan '$iface' - wrong interface type on parent '$p' " .
1578 "('$n->{type}' != 'eth|bond|bridge|vlan' )\n";
1581 &$check_mtu($ifaces, $p, $iface);
1588 foreach my $iface (keys %$ifaces) {
1589 my $d = $ifaces->{$iface};
1590 if (my $uplinkid = $d->{'uplink-id'}) {
1591 die "iface '$iface' - uplink-id $uplinkid is only allowed on physical and linux bond interfaces\n"
1592 if $d->{type
} ne 'eth' && $d->{type
} ne 'bond';
1594 die "iface '$iface' - uplink-id $uplinkid is already assigned on '$uplinks->{$uplinkid}'\n"
1595 if $uplinks->{$uplinkid};
1597 $uplinks->{$uplinkid} = $iface;
1601 # check bridgeport option
1602 my $bridgeports = {};
1604 my $ifaces_copy = { %$ifaces };
1605 foreach my $iface (keys %$ifaces_copy) {
1606 my $d = $ifaces_copy->{$iface};
1607 if ($d->{type
} eq 'bridge') {
1608 foreach my $p (split (/\s+/, $d->{bridge_ports
} // '')) {
1609 if($p =~ m/(\S+)\.(\d+)$/) {
1610 my $vlanparent = $1;
1611 if (!defined($ifaces_copy->{$p})) {
1612 $ifaces_copy->{$p}->{type
} = 'vlan';
1613 $ifaces_copy->{$p}->{method} = 'manual';
1614 $ifaces_copy->{$p}->{method6
} = 'manual';
1615 $ifaces_copy->{$p}->{mtu
} = $ifaces_copy->{$vlanparent}->{mtu
} if defined($ifaces_copy->{$1}->{mtu
});
1618 my $n = $ifaces_copy->{$p};
1619 die "bridge '$iface' - unable to find bridge port '$p'\n" if !$n;
1620 die "iface $p - ip address can't be set on interface if bridged in $iface\n"
1621 if ($n->{method} && $n->{method} eq 'static' && $n->{address
} ne '0.0.0.0') ||
1622 ($n->{method6
} && $n->{method6
} eq 'static' && $n->{address6
} ne '::');
1623 &$check_mtu($ifaces_copy, $p, $iface);
1624 $bridgeports->{$p} = $iface;
1626 $bridges->{$iface} = $d;
1630 foreach my $iface (keys %$ifaces) {
1631 my $d = $ifaces->{$iface};
1633 foreach my $k (qw(bridge-learning bridge-arp-nd-suppress bridge-unicast-flood bridge-multicast-flood bridge-access)) {
1634 die "iface $iface - $k: bridge port specific options can be used only on interfaces attached to a bridge\n"
1635 if $d->{$k} && !$bridgeports->{$iface};
1638 if ($d->{'bridge-access'} && !$bridges->{$bridgeports->{$iface}}->{bridge_vlan_aware
}) {
1639 die "iface $iface - bridge-access option can be only used if interface is in a vlan aware bridge\n";
1643 my $raw = <<'NETWORKDOC';
1644 # network interface settings; autogenerated
1645 # Please do NOT modify this file directly, unless you know what
1648 # If you want to manage parts of the network configuration manually,
1649 # please utilize the 'source' or 'source-directory' directives to do
1651 # PVE will preserve these directives, but will NOT read its network
1652 # configuration from sourced files, so do not attempt to move any of
1653 # the PVE managed interfaces into external files!
1659 my $if_type_hash = {
1663 OVSIntPort
=> 300000,
1667 OVSBridge
=> 500000,
1672 my $lookup_type_prio = sub {
1673 my ($iface, $ifaces) = @_;
1675 my ($rootiface, @rest) = split(/[.:]/, $iface);
1676 my $childlevel = scalar(@rest);
1677 my $type = $ifaces->{$rootiface}->{type
};
1678 return if !$type || $type eq 'unknown';
1680 return $if_type_hash->{$type} + $childlevel
1683 foreach my $iface (sort {
1684 my $ref1 = $ifaces->{$a};
1685 my $ref2 = $ifaces->{$b};
1686 my $tp1 = &$lookup_type_prio($a, $ifaces);
1687 my $tp2 = &$lookup_type_prio($b, $ifaces);
1689 # Only recognized types are in relation to each other. If one type
1690 # is unknown then only consider the interfaces' priority attributes.
1691 $tp1 = $tp2 = 0 if !defined($tp1) || !defined($tp2);
1693 my $p1 = $tp1 + ($ref1->{priority
} // 50000);
1694 my $p2 = $tp2 + ($ref2->{priority
} // 50000);
1696 return $p1 <=> $p2 if $p1 != $p2;
1700 next if $printed->{$iface};
1701 my $d = $ifaces->{$iface};
1702 my $pri = $d->{priority
} // 0;
1703 if (@options && $options[0]->[0] < $pri) {
1705 $raw .= (shift @options)->[1] . "\n";
1706 } while (@options && $options[0]->[0] < $pri);
1710 $printed->{$iface} = 1;
1711 if ($d->{autostart
}) {
1712 if ($d->{type
} eq 'OVSBridge' && !$ifupdown2) {
1713 # cannot use 'auto' for OVS, would add race with systemd ifup@.service
1714 $raw .= "allow-ovs $iface\n";
1716 $raw .= "auto $iface\n";
1720 my $i = 0; # some options should be printed only once
1721 $raw .= __interface_to_string
($iface, $d, $_, !$i++, $ifupdown2) foreach @{$d->{families
}};
1724 $raw .= $_->[1] . "\n" foreach @options;
1728 register_file
('interfaces', "/etc/network/interfaces",
1729 \
&read_etc_network_interfaces
,
1730 \
&write_etc_network_interfaces
);
1733 sub read_iscsi_initiatorname
{
1734 my ($filename, $fd) = @_;
1736 while (defined(my $line = <$fd>)) {
1737 if ($line =~ m/^InitiatorName=(\S+)$/) {
1745 register_file
('initiatorname', "/etc/iscsi/initiatorname.iscsi",
1746 \
&read_iscsi_initiatorname
);
1749 my ($filename, $fd) = @_;
1753 my $raw = defined($fd) ?
<$fd> : '';
1758 my @tokens = split(/\s+/, $raw);
1763 while (defined(my $tok = shift @tokens)) {
1765 $machine = shift @tokens if $tok eq 'machine';
1767 $data->{$machine} = {} if !$data->{$machine};
1769 $data->{$machine}->{login
} = shift @tokens if $tok eq 'login';
1770 $data->{$machine}->{password
} = shift @tokens if $tok eq 'password';
1776 my $format_apt_auth_data = sub {
1781 foreach my $machine (sort keys %$data) {
1782 my $d = $data->{$machine};
1783 $raw .= "machine $machine\n";
1784 $raw .= " login $d->{login}\n" if $d->{login
};
1785 $raw .= " password $d->{password}\n" if $d->{password
};
1792 sub write_apt_auth
{
1793 my ($filename, $fh, $data) = @_;
1795 my $raw = &$format_apt_auth_data($data);
1797 die "write failed: $!" unless print $fh "$raw\n";
1802 sub update_apt_auth
{
1803 my ($filename, $fh, $data) = @_;
1805 my $orig = read_apt_auth
($filename, $fh);
1807 foreach my $machine (keys %$data) {
1808 $orig->{$machine} = $data->{$machine};
1811 return &$format_apt_auth_data($orig);
1814 register_file
('apt-auth', "/etc/apt/auth.conf",
1815 \
&read_apt_auth
, \
&write_apt_auth
,
1816 \
&update_apt_auth
, perm
=> 0640);