3 # todo: maybe we do not need update_file() ?
13 use Fcntl
qw(:DEFAULT :flock);
15 use PVE
::Exception
qw(raise_param_exc);
23 use Encode
qw(encode decode);
25 our @EXPORT_OK = qw(read_file write_file register_file);
34 '/etc/network/interfaces' => '/etc/network/interfaces.new',
37 # to enable cached operation, you need to call 'inotify_init'
38 # inotify handles are a limited resource, so use with care (only
39 # enable the cache if you really need it)
41 # Note: please close the inotify handle after you fork
43 sub ccache_default_writer
{
44 my ($filename, $data) = @_;
46 die "undefined config writer for '$filename' :ERROR";
49 sub ccache_default_parser
{
50 my ($filename, $srcfd) = @_;
52 die "undefined config reader for '$filename' :ERROR";
55 sub ccache_compute_diff
{
56 my ($filename, $shadow) = @_;
60 my $cmd = ['/usr/bin/diff', '-b', '-N', '-u', $filename, $shadow];
61 PVE
::Tools
::run_command
($cmd, noerr
=> 1, outfunc
=> sub {
63 $diff .= decode
('UTF-8', $line) . "\n";
66 $diff = undef if !$diff;
74 foreach my $uid (keys %$ccacheregex) {
75 my $ccinfo = $ccacheregex->{$uid};
76 my $dir = $ccinfo->{dir
};
77 my $regex = $ccinfo->{regex
};
78 if ($filename =~ m
|^$dir/+$regex$|) {
79 if (!$ccache->{$filename}) {
81 while (my ($k, $v) = each %$ccinfo) {
84 $ccache->{$filename} = $cp;
86 return ($ccache->{$filename}, $filename);
90 $filename = $ccachemap->{$filename} if defined ($ccachemap->{$filename});
92 die "file '$filename' not added :ERROR" if !defined ($ccache->{$filename});
94 return ($ccache->{$filename}, $filename);
98 my ($fileid, $data, $full) = @_;
100 my ($ccinfo, $filename) = ccache_info
($fileid);
102 my $writer = $ccinfo->{writer
};
104 my $realname = $filename;
107 if ($shadow = $shadowfiles->{$filename}) {
111 my $perm = $ccinfo->{perm
} || 0644;
113 my $tmpname = "$realname.tmp.$$";
117 my $fh = IO
::File-
>new($tmpname, O_WRONLY
|O_CREAT
, $perm);
118 die "unable to open file '$tmpname' - $!\n" if !$fh;
120 $res = &$writer($filename, $fh, $data);
122 die "closing file '$tmpname' failed - $!\n" unless close $fh;
126 $ccinfo->{version
} = undef;
133 if (!rename($tmpname, $realname)) {
134 my $msg = "close (rename) atomic file '$filename' failed: $!\n";
140 if ($shadow && $full) {
141 $diff = ccache_compute_diff
($filename, $shadow);
145 return { data
=> $res, changes
=> $diff };
152 my ($fileid, $data, @args) = @_;
154 my ($ccinfo, $filename) = ccache_info
($fileid);
156 my $update = $ccinfo->{update
};
158 die "unable to update/merge data" if !$update;
160 my $lkfn = "$filename.lock";
168 $fd = IO
::File-
>new ($filename, "r");
170 my $new = &$update($filename, $fd, $data, @args);
173 PVE
::Tools
::file_set_contents
($filename, $new, $ccinfo->{perm
});
179 PVE
::Tools
::lock_file
($lkfn, $timeout, $code);
182 close($fd) if defined($fd);
189 sub discard_changes
{
190 my ($fileid, $full) = @_;
192 my ($ccinfo, $filename) = ccache_info
($fileid);
194 if (my $copy = $shadowfiles->{$filename}) {
198 return read_file
($filename, $full);
204 poll
() if $inotify; # read new inotify events
206 $versions->{$filename} = 0 if !defined ($versions->{$filename});
208 return $versions->{$filename};
212 my ($fileid, $full) = @_;
216 my ($ccinfo, $filename) = ccache_info
($fileid);
218 $parser = $ccinfo->{parser
};
223 my $cver = poll_changes
($filename);
225 if (my $copy = $shadowfiles->{$filename}) {
226 if ($fd = IO
::File-
>new ($copy, "r")) {
229 $fd = IO
::File-
>new ($filename, "r");
232 $fd = IO
::File-
>new ($filename, "r");
235 my $acp = $ccinfo->{always_call_parser
};
238 $ccinfo->{version
} = undef;
239 $ccinfo->{data
} = undef;
240 $ccinfo->{diff
} = undef;
241 return undef if !$acp;
244 my $noclone = $ccinfo->{noclone
};
247 if (!$ccinfo->{nocache
} &&
249 defined ($ccinfo->{data
}) &&
250 defined ($ccinfo->{version
}) &&
251 ($ccinfo->{readonce
} ||
252 ($ccinfo->{version
} == $cver))) {
255 if (!$noclone && ref ($ccinfo->{data
})) {
256 $ret->{data
} = clone
($ccinfo->{data
});
258 $ret->{data
} = $ccinfo->{data
};
260 $ret->{changes
} = $ccinfo->{diff
};
262 return $full ?
$ret : $ret->{data
};
268 $diff = ccache_compute_diff
($filename, $shadow);
271 my $res = &$parser($filename, $fd);
273 if (!$ccinfo->{nocache
}) {
274 $ccinfo->{version
} = $cver;
277 # we cache data with references, so we always need to
278 # clone this data. Else the original data may get
280 $ccinfo->{data
} = $res;
283 $ccinfo->{diff
} = $diff;
286 if (!$noclone && ref ($ccinfo->{data
})) {
287 $ret->{data
} = clone
($ccinfo->{data
});
289 $ret->{data
} = $ccinfo->{data
};
291 $ret->{changes
} = $ccinfo->{diff
};
293 return $full ?
$ret : $ret->{data
};
296 sub parse_ccache_options
{
297 my ($ccinfo, %options) = @_;
299 foreach my $opt (keys %options) {
300 my $v = $options{$opt};
301 if ($opt eq 'readonce') {
302 $ccinfo->{$opt} = $v;
303 } elsif ($opt eq 'nocache') {
304 $ccinfo->{$opt} = $v;
305 } elsif ($opt eq 'shadow') {
306 $ccinfo->{$opt} = $v;
307 } elsif ($opt eq 'perm') {
308 $ccinfo->{$opt} = $v;
309 } elsif ($opt eq 'noclone') {
310 # noclone flag for large read-only data chunks like aplinfo
311 $ccinfo->{$opt} = $v;
312 } elsif ($opt eq 'always_call_parser') {
313 # when set, we call parser even when the file does not exists.
314 # this allows the parser to return some default
315 $ccinfo->{$opt} = $v;
317 die "internal error - unsupported option '$opt'";
323 my ($id, $filename, $parser, $writer, $update, %options) = @_;
325 die "can't register file '$filename' after inotify_init" if $inotify;
327 die "file '$filename' already added :ERROR" if defined ($ccache->{$filename});
328 die "ID '$id' already used :ERROR" if defined ($ccachemap->{$id});
333 $ccinfo->{parser
} = $parser || \
&ccache_default_parser
;
334 $ccinfo->{writer
} = $writer || \
&ccache_default_writer
;
335 $ccinfo->{update
} = $update;
337 parse_ccache_options
($ccinfo, %options);
339 if ($options{shadow
}) {
340 $shadowfiles->{$filename} = $options{shadow
};
343 $ccachemap->{$id} = $filename;
344 $ccache->{$filename} = $ccinfo;
348 my ($dir, $regex, $parser, $writer, $update, %options) = @_;
350 die "can't register regex after inotify_init" if $inotify;
352 my $uid = "$dir/$regex";
353 die "regular expression '$uid' already added :ERROR" if defined ($ccacheregex->{$uid});
357 $ccinfo->{dir
} = $dir;
358 $ccinfo->{regex
} = $regex;
359 $ccinfo->{parser
} = $parser || \
&ccache_default_parser
;
360 $ccinfo->{writer
} = $writer || \
&ccache_default_writer
;
361 $ccinfo->{update
} = $update;
363 parse_ccache_options
($ccinfo, %options);
365 $ccacheregex->{$uid} = $ccinfo;
371 if ($inotify_pid != $$) {
372 syslog
('err', "got inotify poll request in wrong process - disabling inotify");
375 1 while $inotify && $inotify->poll;
380 foreach my $filename (keys %$ccache) {
381 $ccache->{$filename}->{version
} = undef;
382 $ccache->{$filename}->{data
} = undef;
383 $ccache->{$filename}->{diff
} = undef;
393 die "only one inotify instance allowed" if $inotify;
395 $inotify = Linux
::Inotify2-
>new()
396 || die "Unable to create new inotify object: $!";
398 $inotify->blocking (0);
403 foreach my $fn (keys %$ccache) {
404 my $dir = dirname
($fn);
405 my $base = basename
($fn);
407 $dirhash->{$dir}->{$base} = $fn;
409 if (my $sf = $shadowfiles->{$fn}) {
410 $base = basename
($sf);
411 $dir = dirname
($sf);
412 $dirhash->{$dir}->{$base} = $fn; # change version of original file!
416 foreach my $uid (keys %$ccacheregex) {
417 my $ccinfo = $ccacheregex->{$uid};
418 $dirhash->{$ccinfo->{dir
}}->{_regex
} = 1;
423 foreach my $dir (keys %$dirhash) {
425 my $evlist = IN_MODIFY
|IN_ATTRIB
|IN_MOVED_FROM
|IN_MOVED_TO
|IN_DELETE
|IN_CREATE
;
426 $inotify->watch ($dir, $evlist, sub {
430 if ($inotify_pid != $$) {
431 syslog
('err', "got inotify event in wrong process");
434 if ($e->IN_ISDIR || !$name) {
438 if ($e->IN_Q_OVERFLOW) {
439 syslog
('info', "got inotify overflow - flushing cache");
444 if ($e->IN_UNMOUNT) {
445 syslog
('err', "got 'unmount' event on '$name' - disabling inotify");
448 if ($e->IN_IGNORED) {
449 syslog
('err', "got 'ignored' event on '$name' - disabling inotify");
453 if ($dirhash->{$dir}->{_regex
}) {
454 foreach my $uid (keys %$ccacheregex) {
455 my $ccinfo = $ccacheregex->{$uid};
456 next if $dir ne $ccinfo->{dir
};
457 my $regex = $ccinfo->{regex
};
458 if ($regex && ($name =~ m
|^$regex$|)) {
460 my $fn = "$dir/$name";
462 #print "VERSION:$fn:$versions->{$fn}\n";
465 } elsif (my $fn = $dirhash->{$dir}->{$name}) {
468 #print "VERSION:$fn:$versions->{$fn}\n";
473 foreach my $dir (keys %$dirhash) {
474 foreach my $name (keys %{$dirhash->{$dir}}) {
475 if ($name eq '_regex') {
476 foreach my $uid (keys %$ccacheregex) {
477 my $ccinfo = $ccacheregex->{$uid};
478 next if $dir ne $ccinfo->{dir
};
479 my $re = $ccinfo->{regex
};
480 if (my $fd = IO
::Dir-
>new ($dir)) {
481 while (defined(my $de = $fd->read)) {
482 if ($de =~ m/^$re$/) {
484 $versions->{$fn}++; # init with version
485 #print "init:$fn:$versions->{$fn}\n";
491 my $fn = $dirhash->{$dir}->{$name};
492 $versions->{$fn}++; # init with version
493 #print "init:$fn:$versions->{$fn}\n";
503 return $cached_nodename if $cached_nodename;
505 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
);
540 sub read_etc_resolv_conf
{
541 my ($filename, $fh) = @_;
546 while (my $line = <$fh>) {
548 if ($line =~ m/^(search|domain)\s+(\S+)\s*/) {
550 } elsif ($line =~ m/^\s*nameserver\s+($PVE::Tools::IPRE)\s*/) {
553 $res->{"dns$nscount"} = $1;
561 sub update_etc_resolv_conf
{
562 my ($filename, $fh, $resolv, @args) = @_;
566 $data = "search $resolv->{search}\n"
567 if $resolv->{search
};
570 foreach my $k ("dns1", "dns2", "dns3") {
571 my $ns = $resolv->{$k};
572 if ($ns && $ns ne '0.0.0.0' && !$written->{$ns}) {
574 $data .= "nameserver $ns\n";
578 while (my $line = <$fh>) {
579 next if $line =~ m/^(search|domain|nameserver)\s+/;
586 register_file
('resolvconf', "/etc/resolv.conf",
587 \
&read_etc_resolv_conf
, undef,
588 \
&update_etc_resolv_conf
);
590 sub read_etc_timezone
{
591 my ($filename, $fd) = @_;
593 my $timezone = <$fd>;
600 sub write_etc_timezone
{
601 my ($filename, $fh, $timezone) = @_;
603 my $tzinfo = "/usr/share/zoneinfo/$timezone";
605 raise_param_exc
({ 'timezone' => "No such timezone" })
608 ($timezone) = $timezone =~ m/^(.*)$/; # untaint
610 print $fh "$timezone\n";
612 unlink ("/etc/localtime");
613 symlink ("/usr/share/zoneinfo/$timezone", "/etc/localtime");
617 register_file
('timezone', "/etc/timezone",
619 \
&write_etc_timezone
);
621 sub read_active_workers
{
622 my ($filename, $fh) = @_;
627 while (defined (my $line = <$fh>)) {
628 if ($line =~ m/^(\S+)\s(0|1)(\s([0-9A-Za-z]{8})(\s(\s*\S.*))?)?$/) {
633 if ((my $task = PVE
::Tools
::upid_decode
($upid, 1))) {
634 $task->{upid
} = $upid;
635 $task->{saved
} = $saved;
636 $task->{endtime
} = hex($endtime) if $endtime;
637 $task->{status
} = $status if $status;
641 warn "unable to parse line: $line";
649 sub write_active_workers
{
650 my ($filename, $fh, $tasklist) = @_;
653 foreach my $task (@$tasklist) {
654 my $upid = $task->{upid
};
655 my $saved = $task->{saved
} ?
1 : 0;
656 if ($task->{endtime
}) {
657 if ($task->{status
}) {
658 $raw .= sprintf("%s %s %08X %s\n", $upid, $saved, $task->{endtime
}, $task->{status
});
660 $raw .= sprintf("%s %s %08X\n", $upid, $saved, $task->{endtime
});
663 $raw .= "$upid $saved\n";
667 PVE
::Tools
::safe_print
($filename, $fh, $raw) if $raw;
670 register_file
('active', "/var/log/pve/tasks/active",
671 \
&read_active_workers
,
672 \
&write_active_workers
);
675 our $bond_modes = { 'balance-rr' => 0,
676 'active-backup' => 1,
684 my $ovs_bond_modes = {
685 'active-backup' => 1,
687 'lacp-balance-slb' => 1,
688 'lacp-balance-tcp' => 1,
691 #sub get_bond_modes {
692 # return $bond_modes;
695 my $parse_ovs_option = sub {
699 foreach my $kv (split (/\s+/, $data || '')) {
700 my ($k, $v) = split('=', $kv, 2);
701 $opts->{$k} = $v if $k && $v;
706 my $set_ovs_option = sub {
707 my ($d, %params) = @_;
709 my $opts = &$parse_ovs_option($d->{ovs_options
});
711 foreach my $k (keys %params) {
721 foreach my $k (keys %$opts) {
722 push @$res, "$k=$opts->{$k}";
725 if (my $new = join(' ', @$res)) {
726 $d->{ovs_options
} = $new;
727 return $d->{ovs_options
};
729 delete $d->{ovs_options
};
734 my $extract_ovs_option = sub {
737 my $opts = &$parse_ovs_option($d->{ovs_options
});
739 my $v = delete $opts->{$name};
742 foreach my $k (keys %$opts) {
743 push @$res, "$k=$opts->{$k}";
746 if (my $new = join(' ', @$res)) {
747 $d->{ovs_options
} = $new;
749 delete $d->{ovs_options
};
755 my $check_mtu = sub {
756 my ($ifaces, $parent, $child) = @_;
758 die "check mtu - missing parent interface\n" if !$parent;
759 die "check mtu - missing child interface\n" if !$child;
761 my $cmtu = $ifaces->{$child}->{mtu
};
764 my $parentdata = $ifaces->{$parent};
765 my $pmtu = $parentdata->{mtu
};
766 $pmtu = $cmtu if $parentdata->{type
} eq 'bond' && !$pmtu;
767 $pmtu = 1500 if !$pmtu;
769 die "interface '$parent' - mtu $pmtu is lower than '$child' - mtu $cmtu\n"
776 # <optional> exists => BOOL,
777 # <optional> active => BOOL,
778 # <optional> autostart => BOOL,
779 # <auto> priority => INT,
781 # type => "eth" | "bridge" | "bond" | "loopback" | "OVS*" | ... ,
783 # families => ["inet", "inet6", ...],
785 # method => "manual" | "static" | "dhcp" | ... ,
790 # comments => [ "..." ],
792 # method6 => "manual" | "static" | "dhcp" | ... ,
794 # netmask6 => SUBNET,
796 # comments6 => [ "..." ],
798 # <known options>, # like bridge_ports, ovs_*
800 # # extra/unknown options stored by-family:
801 # options => { <inet options>... }
802 # options6 => { <inet6 options>... }
806 # # mappings end up here as well, as we don't need to understand them
810 sub read_etc_network_interfaces
{
811 my ($filename, $fh) = @_;
812 my $proc_net_dev = IO
::File-
>new('/proc/net/dev', 'r');
813 my $active = PVE
::ProcFSTools
::get_active_network_interfaces
();
814 return __read_etc_network_interfaces
($fh, $proc_net_dev, $active);
817 sub __read_etc_network_interfaces
{
818 my ($fh, $proc_net_dev, $active_ifaces) = @_;
821 my $ifaces = $config->{ifaces
} = {};
822 my $options = $config->{options
} = [];
824 my $options_alternatives = {
825 'bond-slaves' => 'slaves',
826 'bond_slaves' => 'slaves',
827 'bond-xmit-hash-policy' => 'bond_xmit_hash_policy',
828 'bond-mode' => 'bond_mode',
829 'bond-miimon' =>'bond_miimon',
830 'bridge-vlan-aware' => 'bridge_vlan_aware',
831 'bridge-fd' => 'bridge_fd',
832 'bridge-stp' => 'bridge_stp',
833 'bridge-ports' => 'bridge_ports',
834 'bridge-vids' => 'bridge_vids'
840 while (defined ($line = <$proc_net_dev>)) {
841 if ($line =~ m/^\s*($PVE::Network::PHYSICAL_NIC_RE):.*/) {
842 $ifaces->{$1}->{exists} = 1;
845 close($proc_net_dev);
848 # we try to keep order inside the file
849 my $priority = 2; # 1 is reserved for lo
851 SECTION
: while (defined ($line = <$fh>)) {
853 next if $line =~ m/^\s*#/;
855 if ($line =~ m/^\s*auto\s+(.*)$/) {
856 my @aa = split (/\s+/, $1);
858 foreach my $a (@aa) {
859 $ifaces->{$a}->{autostart
} = 1;
862 } elsif ($line =~ m/^\s*iface\s+(\S+)\s+(inet6?)\s+(\S+)\s*$/) {
865 my $f = { method => $3 }; # by family, merged to $d with a $suffix
866 (my $suffix = $family) =~ s/^inet//;
868 my $d = $ifaces->{$i} ||= {};
869 $d->{priority
} = $priority++ if !$d->{priority
};
870 push @{$d->{families
}}, $family;
872 while (defined ($line = <$fh>)) {
874 if ($line =~ m/^\s*#(.*?)\s*$/) {
875 $f->{comments
} = '' if !$f->{comments
};
876 my $comment = decode
('UTF-8', $1);
877 $f->{comments
} .= "$comment\n";
878 } elsif ($line =~ m
/^\s
*(?
:iface\s
886 } elsif ($line =~ m/^\s*((\S+)\s+(.+))$/) {
888 my ($id, $value) = ($2, $3);
890 $id = $options_alternatives->{$id} if $options_alternatives->{$id};
892 my $simple_options = {
901 'bridge-access' => 1,
902 'bridge-learning' => 1,
903 'bridge-arp-nd-suppress' => 1,
904 'bridge-unicast-flood' => 1,
905 'bridge-multicast-flood' => 1,
907 'bond_xmit_hash_policy' => 1,
909 'vxlan-svcnodeip' => 1,
910 'vxlan-physdev' => 1,
911 'vxlan-local-tunnelip' => 1 };
913 if (($id eq 'address') || ($id eq 'netmask') || ($id eq 'broadcast') || ($id eq 'gateway')) {
915 } elsif ($simple_options->{$id}) {
917 } elsif ($id eq 'slaves' || $id eq 'bridge_ports') {
919 foreach my $p (split (/\s+/, $value)) {
920 next if $p eq 'none';
923 my $str = join (' ', sort keys %{$devs});
925 $d->{$id} .= ' ' . $str if $str;
927 $d->{$id} = $str || '';
929 } elsif ($id eq 'bridge_stp') {
930 if ($value =~ m/^\s*(on|yes)\s*$/i) {
935 } elsif ($id eq 'bridge_vlan_aware') {
937 } elsif ($id eq 'bond_mode') {
939 foreach my $bm (keys %$bond_modes) {
940 my $id = $bond_modes->{$bm};
947 } elsif ($id eq 'vxlan-remoteip') {
948 push @{$d->{$id}}, $value;
950 push @{$f->{options
}}, $option;
956 $d->{"$_$suffix"} = $f->{$_} foreach (keys %$f);
957 last SECTION
if !defined($line);
959 } elsif ($line =~ /\w/) {
960 push @$options, [$priority++, $line];
964 foreach my $ifname (@$active_ifaces) {
965 if (my $iface = $ifaces->{$ifname}) {
966 $iface->{active
} = 1;
970 if (!$ifaces->{lo
}) {
971 $ifaces->{lo
}->{priority
} = 1;
972 $ifaces->{lo
}->{method} = 'loopback';
973 $ifaces->{lo
}->{type
} = 'loopback';
974 $ifaces->{lo
}->{autostart
} = 1;
977 foreach my $iface (keys %$ifaces) {
978 my $d = $ifaces->{$iface};
979 if ($iface =~ m/^bond\d+$/) {
980 if (!$d->{ovs_type
}) {
982 } elsif ($d->{ovs_type
} eq 'OVSBond') {
983 $d->{type
} = $d->{ovs_type
};
984 # translate: ovs_options => bond_mode
985 $d->{'bond_mode'} = &$extract_ovs_option($d, 'bond_mode');
986 my $lacp = &$extract_ovs_option($d, 'lacp');
987 if ($lacp && $lacp eq 'active') {
988 if ($d->{'bond_mode'} eq 'balance-slb') {
989 $d->{'bond_mode'} = 'lacp-balance-slb';
992 # Note: balance-tcp needs lacp
993 if ($d->{'bond_mode'} eq 'balance-tcp') {
994 $d->{'bond_mode'} = 'lacp-balance-tcp';
996 my $tag = &$extract_ovs_option($d, 'tag');
997 $d->{ovs_tag
} = $tag if defined($tag);
999 $d->{type
} = 'unknown';
1001 } elsif ($iface =~ m/^vmbr\d+$/) {
1002 if (!$d->{ovs_type
}) {
1003 $d->{type
} = 'bridge';
1005 if (!defined ($d->{bridge_fd
})) {
1006 $d->{bridge_fd
} = 0;
1008 if (!defined ($d->{bridge_stp
})) {
1009 $d->{bridge_stp
} = 'off';
1011 } elsif ($d->{ovs_type
} eq 'OVSBridge') {
1012 $d->{type
} = $d->{ovs_type
};
1014 $d->{type
} = 'unknown';
1016 } elsif ($iface =~ m/^(\S+):\d+$/) {
1017 $d->{type
} = 'alias';
1018 if (defined ($ifaces->{$1})) {
1019 $d->{exists} = $ifaces->{$1}->{exists};
1021 $ifaces->{$1}->{exists} = 0;
1024 } elsif ($iface =~ m/^(\S+)\.\d+$/) {
1025 $d->{type
} = 'vlan';
1026 if (defined ($ifaces->{$1})) {
1027 $d->{exists} = $ifaces->{$1}->{exists};
1029 $ifaces->{$1}->{exists} = 0;
1032 } elsif ($iface =~ m/^$PVE::Network::PHYSICAL_NIC_RE$/) {
1033 if (!$d->{ovs_type
}) {
1035 } elsif ($d->{ovs_type
} eq 'OVSPort') {
1036 $d->{type
} = $d->{ovs_type
};
1037 my $tag = &$extract_ovs_option($d, 'tag');
1038 $d->{ovs_tag
} = $tag if defined($tag);
1040 $d->{type
} = 'unknown';
1042 } elsif ($iface =~ m/^lo$/) {
1043 $d->{type
} = 'loopback';
1045 if ($d->{'vxlan-id'}) {
1046 $d->{type
} = 'vxlan';
1047 } elsif (!$d->{ovs_type
}) {
1048 $d->{type
} = 'unknown';
1049 } elsif ($d->{ovs_type
} eq 'OVSIntPort') {
1050 $d->{type
} = $d->{ovs_type
};
1051 my $tag = &$extract_ovs_option($d, 'tag');
1052 $d->{ovs_tag
} = $tag if defined($tag);
1056 $d->{method} = 'manual' if !$d->{method};
1057 $d->{method6
} = 'manual' if !$d->{method6
};
1059 $d->{families
} ||= ['inet'];
1062 # OVS bridges create "allow-$BRIDGE $IFACE" lines which we need to remove
1063 # from the {options} hash for them to be removed correctly.
1064 @$options = grep {defined($_)} map {
1065 my ($pri, $line) = @$_;
1066 if ($line =~ /^allow-(\S+)\s+(.*)$/) {
1068 my @ports = split(/\s+/, $2);
1069 if (defined(my $br = $ifaces->{$bridge})) {
1070 # if this port is part of a bridge, remove it
1071 my %in_ovs_ports = map {$_=>1} split(/\s+/, $br->{ovs_ports
});
1072 @ports = grep { not $in_ovs_ports{$_} } @ports;
1074 # create the allow line for the remaining ports, or delete if empty
1076 [$pri, "allow-$bridge " . join(' ', @ports)];
1081 # don't modify other lines
1089 sub __interface_to_string
{
1090 my ($iface, $d, $family, $first_block, $ifupdown2) = @_;
1092 (my $suffix = $family) =~ s/^inet//;
1094 return '' if !($d && $d->{"method$suffix"});
1098 $raw .= "iface $iface $family " . $d->{"method$suffix"} . "\n";
1099 $raw .= "\taddress " . $d->{"address$suffix"} . "\n" if $d->{"address$suffix"};
1100 $raw .= "\tnetmask " . $d->{"netmask$suffix"} . "\n" if $d->{"netmask$suffix"};
1101 $raw .= "\tgateway " . $d->{"gateway$suffix"} . "\n" if $d->{"gateway$suffix"};
1102 $raw .= "\tbroadcast " . $d->{"broadcast$suffix"} . "\n" if $d->{"broadcast$suffix"};
1104 my $done = { type
=> 1, priority
=> 1, method => 1, active
=> 1, exists => 1,
1105 comments
=> 1, autostart
=> 1, options
=> 1,
1106 address
=> 1, netmask
=> 1, gateway
=> 1, broadcast
=> 1,
1107 method6
=> 1, families
=> 1, options6
=> 1,
1108 address6
=> 1, netmask6
=> 1, gateway6
=> 1, broadcast6
=> 1 };
1110 if (!$first_block) {
1111 # not printing out options
1112 } elsif ($d->{type
} eq 'bridge') {
1114 $d->{bridge_ports
} =~ s/[;,\s]+/ /g;
1115 my $ports = $d->{bridge_ports
} || 'none';
1116 $raw .= "\tbridge-ports $ports\n";
1117 $done->{bridge_ports
} = 1;
1119 my $v = defined($d->{bridge_stp
}) ?
$d->{bridge_stp
} : 'off';
1120 $raw .= "\tbridge-stp $v\n";
1121 $done->{bridge_stp
} = 1;
1123 $v = defined($d->{bridge_fd
}) ?
$d->{bridge_fd
} : 0;
1124 $raw .= "\tbridge-fd $v\n";
1125 $done->{bridge_fd
} = 1;
1127 if( defined($d->{bridge_vlan_aware
})) {
1128 $raw .= "\tbridge-vlan-aware yes\n";
1129 $v = defined($d->{bridge_vids
}) ?
$d->{bridge_vids
} : "2-4094";
1130 $raw .= "\tbridge-vids $v\n";
1132 $done->{bridge_vlan_aware
} = 1;
1133 $done->{bridge_vids
} = 1;
1135 } elsif ($d->{type
} eq 'bond') {
1137 $d->{slaves
} =~ s/[;,\s]+/ /g;
1138 my $slaves = $d->{slaves
} || 'none';
1139 $raw .= "\tbond-slaves $slaves\n";
1140 $done->{slaves
} = 1;
1142 my $v = defined ($d->{'bond_miimon'}) ?
$d->{'bond_miimon'} : 100;
1143 $raw .= "\tbond-miimon $v\n";
1144 $done->{'bond_miimon'} = 1;
1146 $v = defined ($d->{'bond_mode'}) ?
$d->{'bond_mode'} : 'balance-rr';
1147 $raw .= "\tbond-mode $v\n";
1148 $done->{'bond_mode'} = 1;
1150 if ($d->{'bond_mode'} && $d->{'bond_xmit_hash_policy'} &&
1151 ($d->{'bond_mode'} eq 'balance-xor' || $d->{'bond_mode'} eq '802.3ad')) {
1152 $raw .= "\tbond-xmit-hash-policy $d->{'bond_xmit_hash_policy'}\n";
1154 $done->{'bond_xmit_hash_policy'} = 1;
1155 } elsif ($d->{type
} eq 'vxlan') {
1157 foreach my $k (qw(vxlan-id vxlan-svcnodeip vxlan-physdev vxlan-local-tunnelip)) {
1158 $raw .= "\t$k $d->{$k}\n" if defined $d->{$k};
1162 if ($d->{'vxlan-remoteip'}) {
1163 foreach my $remoteip (@{$d->{'vxlan-remoteip'}}) {
1164 $raw .= "\tvxlan-remoteip $remoteip\n";
1166 $done->{'vxlan-remoteip'} = 1;
1168 } elsif ($d->{type
} eq 'OVSBridge') {
1170 $raw .= "\tovs_type $d->{type}\n";
1171 $done->{ovs_type
} = 1;
1173 $raw .= "\tovs_ports $d->{ovs_ports}\n" if $d->{ovs_ports
};
1174 $done->{ovs_ports
} = 1;
1175 } elsif ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' ||
1176 $d->{type
} eq 'OVSBond') {
1178 $d->{autostart
} = 0; # started by the bridge
1180 if (defined($d->{ovs_tag
})) {
1181 &$set_ovs_option($d, tag
=> $d->{ovs_tag
});
1183 $done->{ovs_tag
} = 1;
1185 if ($d->{type
} eq 'OVSBond') {
1187 $d->{bond_mode
} = 'active-backup' if !$d->{bond_mode
};
1189 $ovs_bond_modes->{$d->{bond_mode
}} ||
1190 die "OVS does not support bond mode '$d->{bond_mode}\n";
1192 if ($d->{bond_mode
} eq 'lacp-balance-slb') {
1193 &$set_ovs_option($d, lacp
=> 'active');
1194 &$set_ovs_option($d, bond_mode
=> 'balance-slb');
1195 } elsif ($d->{bond_mode
} eq 'lacp-balance-tcp') {
1196 &$set_ovs_option($d, lacp
=> 'active');
1197 &$set_ovs_option($d, bond_mode
=> 'balance-tcp');
1199 &$set_ovs_option($d, lacp
=> undef);
1200 &$set_ovs_option($d, bond_mode
=> $d->{bond_mode
});
1202 $done->{bond_mode
} = 1;
1204 $raw .= "\tovs_bonds $d->{ovs_bonds}\n" if $d->{ovs_bonds
};
1205 $done->{ovs_bonds
} = 1;
1208 $raw .= "\tovs_type $d->{type}\n";
1209 $done->{ovs_type
} = 1;
1211 if ($d->{ovs_bridge
}) {
1214 $raw = "auto $iface\n$raw";
1216 $raw = "allow-$d->{ovs_bridge} $iface\n$raw";
1219 $raw .= "\tovs_bridge $d->{ovs_bridge}\n";
1220 $done->{ovs_bridge
} = 1;
1225 # print other settings
1226 foreach my $k (sort keys %$d) {
1227 next if $done->{$k};
1229 $raw .= "\t$k $d->{$k}\n";
1233 foreach my $option (@{$d->{"options$suffix"}}) {
1234 $raw .= "\t$option\n";
1238 my $comments = $d->{"comments$suffix"} || '';
1239 foreach my $cl (split(/\n/, $comments)) {
1249 sub write_etc_network_interfaces
{
1250 my ($filename, $fh, $config) = @_;
1251 my $ifupdown2 = -e
'/usr/share/ifupdown2';
1252 my $raw = __write_etc_network_interfaces
($config, $ifupdown2);
1253 PVE
::Tools
::safe_print
($filename, $fh, encode
('UTF-8', $raw));
1255 sub __write_etc_network_interfaces
{
1256 my ($config, $ifupdown2) = @_;
1258 my $ifaces = $config->{ifaces
};
1259 my @options = @{$config->{options
}};
1261 my $used_ports = {};
1263 foreach my $iface (keys %$ifaces) {
1264 my $d = $ifaces->{$iface};
1267 foreach my $k (qw(bridge_ports ovs_ports slaves ovs_bonds)) {
1268 $ports .= " $d->{$k}" if $d->{$k};
1271 foreach my $p (PVE
::Tools
::split_list
($ports)) {
1272 die "port '$p' is already used on interface '$used_ports->{$p}'\n"
1273 if $used_ports->{$p} && $used_ports->{$p} ne $iface;
1274 $used_ports->{$p} = $iface;
1278 # delete unused OVS ports
1279 foreach my $iface (keys %$ifaces) {
1280 my $d = $ifaces->{$iface};
1281 if ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' ||
1282 $d->{type
} eq 'OVSBond') {
1283 my $brname = $used_ports->{$iface};
1284 if (!$brname || !$ifaces->{$brname}) {
1285 if ($iface =~ /^$PVE::Network::PHYSICAL_NIC_RE/) {
1286 $ifaces->{$iface} = { type
=> 'eth',
1289 families
=> ['inet'] };
1291 delete $ifaces->{$iface};
1295 my $bd = $ifaces->{$brname};
1296 if ($bd->{type
} ne 'OVSBridge') {
1297 delete $ifaces->{$iface};
1303 # create OVS bridge ports
1304 foreach my $iface (keys %$ifaces) {
1305 my $d = $ifaces->{$iface};
1306 if ($d->{type
} eq 'OVSBridge' && $d->{ovs_ports
}) {
1307 foreach my $p (split (/\s+/, $d->{ovs_ports
})) {
1308 my $n = $ifaces->{$p};
1309 die "OVS bridge '$iface' - unable to find port '$p'\n"
1311 $n->{autostart
} = 0;
1312 if ($n->{type
} eq 'eth') {
1313 $n->{type
} = 'OVSPort';
1314 $n->{ovs_bridge
} = $iface;
1315 } elsif ($n->{type
} eq 'OVSBond' || $n->{type
} eq 'OVSPort' ||
1316 $n->{type
} eq 'OVSIntPort') {
1317 $n->{ovs_bridge
} = $iface;
1319 die "interface '$p' is not defined as OVS port/bond\n";
1322 &$check_mtu($ifaces, $iface, $p);
1327 # check OVS bond ports
1328 foreach my $iface (keys %$ifaces) {
1329 my $d = $ifaces->{$iface};
1330 if ($d->{type
} eq 'OVSBond' && $d->{ovs_bonds
}) {
1331 foreach my $p (split (/\s+/, $d->{ovs_bonds
})) {
1332 my $n = $ifaces->{$p};
1333 die "OVS bond '$iface' - unable to find slave '$p'\n"
1335 die "OVS bond '$iface' - wrong interface type on slave '$p' " .
1336 "('$n->{type}' != 'eth')\n" if $n->{type
} ne 'eth';
1337 &$check_mtu($ifaces, $iface, $p);
1343 foreach my $iface (keys %$ifaces) {
1344 my $d = $ifaces->{$iface};
1345 if ($d->{type
} eq 'bond' && $d->{slaves
}) {
1346 foreach my $p (split (/\s+/, $d->{slaves
})) {
1347 my $n = $ifaces->{$p};
1349 die "bond '$iface' - unable to find slave '$p'\n"
1351 die "bond '$iface' - wrong interface type on slave '$p' " .
1352 "('$n->{type}' != 'eth')\n" if $n->{type
} ne 'eth';
1353 &$check_mtu($ifaces, $iface, $p);
1360 foreach my $iface (keys %$ifaces) {
1361 my $d = $ifaces->{$iface};
1363 if ($d->{type
} eq 'vxlan' && $d->{'vxlan-id'}) {
1364 my $vxlanid = $d->{'vxlan-id'};
1365 die "iface $iface - duplicate vxlan-id $vxlanid already used in $vxlans->{$vxlanid}\n" if $vxlans->{$vxlanid};
1366 $vxlans->{$vxlanid} = $iface;
1370 ++$ips if defined $d->{'vxlan-svcnodeip'};
1371 ++$ips if defined $d->{'vxlan-remoteip'};
1372 ++$ips if defined $d->{'vxlan-local-tunnelip'};
1374 die "iface $iface - vxlan-svcnodeip, vxlan-remoteip and vxlan-localtunnelip are mutually exclusive\n";
1377 if (defined($d->{'vxlan-svcnodeip'}) != defined($d->{'vxlan-physdev'})) {
1378 die "iface $iface - vxlan-svcnodeip and vxlan-physdev must be define together\n";
1380 #fixme : check if vxlan mtu is lower than 50bytes than physical interface where tunnel is going out
1384 foreach my $iface (keys %$ifaces) {
1385 my $d = $ifaces->{$iface};
1386 if ($d->{type
} eq 'vlan' && $iface =~ m/^(\S+)\.\d+$/) {
1388 my $n = $ifaces->{$p};
1390 die "vlan '$iface' - unable to find parent '$p'\n"
1393 if ($n->{type
} eq 'bridge' && !$n->{bridge_vlan_aware
}) {
1394 die "vlan '$iface' - bridge vlan aware is not enabled on parent '$p'\n";
1395 } elsif ($n->{type
} ne 'eth' && $n->{type
} ne 'bridge' && $n->{type
} ne 'bond') {
1396 die "vlan '$iface' - wrong interface type on parent '$p' " .
1397 "('$n->{type}' != 'eth|bond|bridge' )\n";
1399 &$check_mtu($ifaces, $p, $iface);
1403 # check bridgeport option
1404 my $bridgeports = {};
1406 foreach my $iface (keys %$ifaces) {
1407 my $d = $ifaces->{$iface};
1408 if ($d->{type
} eq 'bridge') {
1409 foreach my $p (split (/\s+/, $d->{bridge_ports
})) {
1411 my $n = $ifaces->{$p};
1412 die "bridge '$iface' - unable to find bridge port '$p'\n"
1414 &$check_mtu($ifaces, $iface, $p);
1415 $bridgeports->{$p} = $iface;
1417 $bridges->{$iface} = $d;
1421 foreach my $iface (keys %$ifaces) {
1422 my $d = $ifaces->{$iface};
1424 foreach my $k (qw(bridge-learning bridge-arp-nd-suppress bridge-unicast-flood bridge-multicast-flood bridge-access)) {
1425 die "iface $iface - $k: bridge port specific options can be used only on interfaces attached to a bridge\n"
1426 if $d->{$k} && !$bridgeports->{$iface};
1429 if ($d->{'bridge-access'} && !$bridges->{$bridgeports->{$iface}}->{bridge_vlan_aware
}) {
1430 die "iface $iface - bridge-access option can be only used if interface is in a vlan aware bridge\n";
1434 my $raw = <<'NETWORKDOC';
1435 # network interface settings; autogenerated
1436 # Please do NOT modify this file directly, unless you know what
1439 # If you want to manage part of the network configuration manually,
1440 # please utilize the 'source' or 'source-directory' directives to do
1442 # PVE will preserve these directives, but will NOT its network
1443 # configuration from sourced files, so do not attempt to move any of
1444 # the PVE managed interfaces into external files!
1450 my $if_type_hash = {
1458 my $lookup_type_prio = sub {
1462 if ($iface =~ m/^(\S+)(\.|:)\d+$/) {
1468 if ($iface eq 'lo') {
1469 $pri = $if_type_hash->{loopback
};
1470 } elsif ($iface =~ m/^$PVE::Network::PHYSICAL_NIC_RE$/) {
1471 $pri = $if_type_hash->{eth
} + $child;
1472 } elsif ($iface =~ m/^bond\d+$/) {
1473 $pri = $if_type_hash->{bond
} + $child;
1474 } elsif ($iface =~ m/^vmbr\d+$/) {
1475 $pri = $if_type_hash->{bridge
} + $child;
1481 foreach my $iface (sort {
1482 my $ref1 = $ifaces->{$a};
1483 my $ref2 = $ifaces->{$b};
1484 my $tp1 = &$lookup_type_prio($a);
1485 my $tp2 = &$lookup_type_prio($b);
1487 # Only recognized types are in relation to each other. If one type
1488 # is unknown then only consider the interfaces' priority attributes.
1489 $tp1 = $tp2 = 0 if !defined($tp1) || !defined($tp2);
1491 my $p1 = $tp1 + ($ref1->{priority
} // 50000);
1492 my $p2 = $tp2 + ($ref2->{priority
} // 50000);
1494 return $p1 <=> $p2 if $p1 != $p2;
1498 next if $printed->{$iface};
1500 my $d = $ifaces->{$iface};
1501 my $pri = $d->{priority
} // 0;
1502 if (@options && $options[0]->[0] < $pri) {
1504 $raw .= (shift @options)->[1] . "\n";
1505 } while (@options && $options[0]->[0] < $pri);
1509 $printed->{$iface} = 1;
1510 $raw .= "auto $iface\n" if $d->{autostart
};
1511 my $i = 0; # some options should be printed only once
1512 $raw .= __interface_to_string
($iface, $d, $_, !$i++, $ifupdown2) foreach @{$d->{families
}};
1515 $raw .= $_->[1] . "\n" foreach @options;
1519 register_file
('interfaces', "/etc/network/interfaces",
1520 \
&read_etc_network_interfaces
,
1521 \
&write_etc_network_interfaces
);
1524 sub read_iscsi_initiatorname
{
1525 my ($filename, $fd) = @_;
1527 while (defined(my $line = <$fd>)) {
1528 if ($line =~ m/^InitiatorName=(\S+)$/) {
1536 register_file
('initiatorname', "/etc/iscsi/initiatorname.iscsi",
1537 \
&read_iscsi_initiatorname
);
1540 my ($filename, $fd) = @_;
1544 my $raw = defined($fd) ?
<$fd> : '';
1549 my @tokens = split(/\s+/, $raw);
1554 while (defined(my $tok = shift @tokens)) {
1556 $machine = shift @tokens if $tok eq 'machine';
1558 $data->{$machine} = {} if !$data->{$machine};
1560 $data->{$machine}->{login
} = shift @tokens if $tok eq 'login';
1561 $data->{$machine}->{password
} = shift @tokens if $tok eq 'password';
1567 my $format_apt_auth_data = sub {
1572 foreach my $machine (sort keys %$data) {
1573 my $d = $data->{$machine};
1574 $raw .= "machine $machine\n";
1575 $raw .= " login $d->{login}\n" if $d->{login
};
1576 $raw .= " password $d->{password}\n" if $d->{password
};
1583 sub write_apt_auth
{
1584 my ($filename, $fh, $data) = @_;
1586 my $raw = &$format_apt_auth_data($data);
1588 die "write failed: $!" unless print $fh "$raw\n";
1593 sub update_apt_auth
{
1594 my ($filename, $fh, $data) = @_;
1596 my $orig = read_apt_auth
($filename, $fh);
1598 foreach my $machine (keys %$data) {
1599 $orig->{$machine} = $data->{$machine};
1602 return &$format_apt_auth_data($orig);
1605 register_file
('apt-auth', "/etc/apt/auth.conf",
1606 \
&read_apt_auth
, \
&write_apt_auth
,
1607 \
&update_apt_auth
, perm
=> 0640);