3 # todo: maybe we do not need update_file() ?
13 use Fcntl
qw(:DEFAULT :flock);
15 use PVE
::Exception
qw(raise_param_exc);
24 our @EXPORT_OK = qw(read_file write_file register_file);
33 '/etc/network/interfaces' => '/etc/network/interfaces.new',
36 # to enable cached operation, you need to call 'inotify_init'
37 # inotify handles are a limited resource, so use with care (only
38 # enable the cache if you really need it)
40 # Note: please close the inotify handle after you fork
42 sub ccache_default_writer
{
43 my ($filename, $data) = @_;
45 die "undefined config writer for '$filename' :ERROR";
48 sub ccache_default_parser
{
49 my ($filename, $srcfd) = @_;
51 die "undefined config reader for '$filename' :ERROR";
54 sub ccache_compute_diff
{
55 my ($filename, $shadow) = @_;
59 open (TMP
, "diff -b -N -u '$filename' '$shadow'|");
61 while (my $line = <TMP
>) {
67 $diff = undef if !$diff;
75 foreach my $uid (keys %$ccacheregex) {
76 my $ccinfo = $ccacheregex->{$uid};
77 my $dir = $ccinfo->{dir
};
78 my $regex = $ccinfo->{regex
};
79 if ($filename =~ m
|^$dir/+$regex$|) {
80 if (!$ccache->{$filename}) {
82 while (my ($k, $v) = each %$ccinfo) {
85 $ccache->{$filename} = $cp;
87 return ($ccache->{$filename}, $filename);
91 $filename = $ccachemap->{$filename} if defined ($ccachemap->{$filename});
93 die "file '$filename' not added :ERROR" if !defined ($ccache->{$filename});
95 return ($ccache->{$filename}, $filename);
99 my ($fileid, $data, $full) = @_;
101 my ($ccinfo, $filename) = ccache_info
($fileid);
103 my $writer = $ccinfo->{writer
};
105 my $realname = $filename;
108 if ($shadow = $shadowfiles->{$filename}) {
112 my $perm = $ccinfo->{perm
} || 0644;
114 my $tmpname = "$realname.tmp.$$";
118 my $fh = IO
::File-
>new($tmpname, O_WRONLY
|O_CREAT
, $perm);
119 die "unable to open file '$tmpname' - $!\n" if !$fh;
121 $res = &$writer($filename, $fh, $data);
123 die "closing file '$tmpname' failed - $!\n" unless close $fh;
127 $ccinfo->{version
} = undef;
134 if (!rename($tmpname, $realname)) {
135 my $msg = "close (rename) atomic file '$filename' failed: $!\n";
141 if ($shadow && $full) {
142 $diff = ccache_compute_diff
($filename, $shadow);
146 return { data
=> $res, changes
=> $diff };
153 my ($fileid, $data, @args) = @_;
155 my ($ccinfo, $filename) = ccache_info
($fileid);
157 my $update = $ccinfo->{update
};
159 die "unable to update/merge data" if !$update;
161 my $lkfn = "$filename.lock";
169 $fd = IO
::File-
>new ($filename, "r");
171 my $new = &$update($filename, $fd, $data, @args);
174 PVE
::Tools
::file_set_contents
($filename, $new, $ccinfo->{perm
});
180 PVE
::Tools
::lock_file
($lkfn, $timeout, $code);
183 close($fd) if defined($fd);
190 sub discard_changes
{
191 my ($fileid, $full) = @_;
193 my ($ccinfo, $filename) = ccache_info
($fileid);
195 if (my $copy = $shadowfiles->{$filename}) {
199 return read_file
($filename, $full);
205 poll
() if $inotify; # read new inotify events
207 $versions->{$filename} = 0 if !defined ($versions->{$filename});
209 return $versions->{$filename};
213 my ($fileid, $full) = @_;
217 my ($ccinfo, $filename) = ccache_info
($fileid);
219 $parser = $ccinfo->{parser
};
224 my $cver = poll_changes
($filename);
226 if (my $copy = $shadowfiles->{$filename}) {
227 if ($fd = IO
::File-
>new ($copy, "r")) {
230 $fd = IO
::File-
>new ($filename, "r");
233 $fd = IO
::File-
>new ($filename, "r");
236 my $acp = $ccinfo->{always_call_parser
};
239 $ccinfo->{version
} = undef;
240 $ccinfo->{data
} = undef;
241 $ccinfo->{diff
} = undef;
242 return undef if !$acp;
245 my $noclone = $ccinfo->{noclone
};
248 if (!$ccinfo->{nocache
} &&
250 defined ($ccinfo->{data
}) &&
251 defined ($ccinfo->{version
}) &&
252 ($ccinfo->{readonce
} ||
253 ($ccinfo->{version
} == $cver))) {
256 if (!$noclone && ref ($ccinfo->{data
})) {
257 $ret->{data
} = clone
($ccinfo->{data
});
259 $ret->{data
} = $ccinfo->{data
};
261 $ret->{changes
} = $ccinfo->{diff
};
263 return $full ?
$ret : $ret->{data
};
269 $diff = ccache_compute_diff
($filename, $shadow);
272 my $res = &$parser($filename, $fd);
274 if (!$ccinfo->{nocache
}) {
275 $ccinfo->{version
} = $cver;
278 # we cache data with references, so we always need to
279 # clone this data. Else the original data may get
281 $ccinfo->{data
} = $res;
284 $ccinfo->{diff
} = $diff;
287 if (!$noclone && ref ($ccinfo->{data
})) {
288 $ret->{data
} = clone
($ccinfo->{data
});
290 $ret->{data
} = $ccinfo->{data
};
292 $ret->{changes
} = $ccinfo->{diff
};
294 return $full ?
$ret : $ret->{data
};
297 sub parse_ccache_options
{
298 my ($ccinfo, %options) = @_;
300 foreach my $opt (keys %options) {
301 my $v = $options{$opt};
302 if ($opt eq 'readonce') {
303 $ccinfo->{$opt} = $v;
304 } elsif ($opt eq 'nocache') {
305 $ccinfo->{$opt} = $v;
306 } elsif ($opt eq 'shadow') {
307 $ccinfo->{$opt} = $v;
308 } elsif ($opt eq 'perm') {
309 $ccinfo->{$opt} = $v;
310 } elsif ($opt eq 'noclone') {
311 # noclone flag for large read-only data chunks like aplinfo
312 $ccinfo->{$opt} = $v;
313 } elsif ($opt eq 'always_call_parser') {
314 # when set, we call parser even when the file does not exists.
315 # this allows the parser to return some default
316 $ccinfo->{$opt} = $v;
318 die "internal error - unsupported option '$opt'";
324 my ($id, $filename, $parser, $writer, $update, %options) = @_;
326 die "can't register file '$filename' after inotify_init" if $inotify;
328 die "file '$filename' already added :ERROR" if defined ($ccache->{$filename});
329 die "ID '$id' already used :ERROR" if defined ($ccachemap->{$id});
334 $ccinfo->{parser
} = $parser || \
&ccache_default_parser
;
335 $ccinfo->{writer
} = $writer || \
&ccache_default_writer
;
336 $ccinfo->{update
} = $update;
338 parse_ccache_options
($ccinfo, %options);
340 if ($options{shadow
}) {
341 $shadowfiles->{$filename} = $options{shadow
};
344 $ccachemap->{$id} = $filename;
345 $ccache->{$filename} = $ccinfo;
349 my ($dir, $regex, $parser, $writer, $update, %options) = @_;
351 die "can't register regex after inotify_init" if $inotify;
353 my $uid = "$dir/$regex";
354 die "regular expression '$uid' already added :ERROR" if defined ($ccacheregex->{$uid});
358 $ccinfo->{dir
} = $dir;
359 $ccinfo->{regex
} = $regex;
360 $ccinfo->{parser
} = $parser || \
&ccache_default_parser
;
361 $ccinfo->{writer
} = $writer || \
&ccache_default_writer
;
362 $ccinfo->{update
} = $update;
364 parse_ccache_options
($ccinfo, %options);
366 $ccacheregex->{$uid} = $ccinfo;
372 if ($inotify_pid != $$) {
373 syslog
('err', "got inotify poll request in wrong process - disabling inotify");
376 1 while $inotify && $inotify->poll;
381 foreach my $filename (keys %$ccache) {
382 $ccache->{$filename}->{version
} = undef;
383 $ccache->{$filename}->{data
} = undef;
384 $ccache->{$filename}->{diff
} = undef;
394 die "only one inotify instance allowed" if $inotify;
396 $inotify = Linux
::Inotify2-
>new()
397 || die "Unable to create new inotify object: $!";
399 $inotify->blocking (0);
404 foreach my $fn (keys %$ccache) {
405 my $dir = dirname
($fn);
406 my $base = basename
($fn);
408 $dirhash->{$dir}->{$base} = $fn;
410 if (my $sf = $shadowfiles->{$fn}) {
411 $base = basename
($sf);
412 $dir = dirname
($sf);
413 $dirhash->{$dir}->{$base} = $fn; # change version of original file!
417 foreach my $uid (keys %$ccacheregex) {
418 my $ccinfo = $ccacheregex->{$uid};
419 $dirhash->{$ccinfo->{dir
}}->{_regex
} = 1;
424 foreach my $dir (keys %$dirhash) {
426 my $evlist = IN_MODIFY
|IN_ATTRIB
|IN_MOVED_FROM
|IN_MOVED_TO
|IN_DELETE
|IN_CREATE
;
427 $inotify->watch ($dir, $evlist, sub {
431 if ($inotify_pid != $$) {
432 syslog
('err', "got inotify event in wrong process");
435 if ($e->IN_ISDIR || !$name) {
439 if ($e->IN_Q_OVERFLOW) {
440 syslog
('info', "got inotify overflow - flushing cache");
445 if ($e->IN_UNMOUNT) {
446 syslog
('err', "got 'unmount' event on '$name' - disabling inotify");
449 if ($e->IN_IGNORED) {
450 syslog
('err', "got 'ignored' event on '$name' - disabling inotify");
454 if ($dirhash->{$dir}->{_regex
}) {
455 foreach my $uid (keys %$ccacheregex) {
456 my $ccinfo = $ccacheregex->{$uid};
457 next if $dir ne $ccinfo->{dir
};
458 my $regex = $ccinfo->{regex
};
459 if ($regex && ($name =~ m
|^$regex$|)) {
461 my $fn = "$dir/$name";
463 #print "VERSION:$fn:$versions->{$fn}\n";
466 } elsif (my $fn = $dirhash->{$dir}->{$name}) {
469 #print "VERSION:$fn:$versions->{$fn}\n";
474 foreach my $dir (keys %$dirhash) {
475 foreach my $name (keys %{$dirhash->{$dir}}) {
476 if ($name eq '_regex') {
477 foreach my $uid (keys %$ccacheregex) {
478 my $ccinfo = $ccacheregex->{$uid};
479 next if $dir ne $ccinfo->{dir
};
480 my $re = $ccinfo->{regex
};
481 if (my $fd = IO
::Dir-
>new ($dir)) {
482 while (defined(my $de = $fd->read)) {
483 if ($de =~ m/^$re$/) {
485 $versions->{$fn}++; # init with version
486 #print "init:$fn:$versions->{$fn}\n";
492 my $fn = $dirhash->{$dir}->{$name};
493 $versions->{$fn}++; # init with version
494 #print "init:$fn:$versions->{$fn}\n";
504 return $cached_nodename if $cached_nodename;
506 my ($sysname, $nodename) = POSIX
::uname
();
508 $nodename =~ s/\..*$//; # strip domain part, if any
510 die "unable to read node name\n" if !$nodename;
512 $cached_nodename = $nodename;
514 return $cached_nodename;
517 sub read_etc_hostname
{
518 my ($filename, $fd) = @_;
520 my $hostname = <$fd>;
524 $hostname =~ s/\..*$//; # strip domain part, if any
529 sub write_etc_hostname
{
530 my ($filename, $fh, $hostname) = @_;
532 die "write failed: $!" unless print $fh "$hostname\n";
537 register_file
('hostname', "/etc/hostname",
539 \
&write_etc_hostname
);
541 sub read_etc_resolv_conf
{
542 my ($filename, $fh) = @_;
547 while (my $line = <$fh>) {
549 if ($line =~ m/^(search|domain)\s+(\S+)\s*/) {
551 } elsif ($line =~ m/^\s*nameserver\s+($PVE::Tools::IPRE)\s*/) {
554 $res->{"dns$nscount"} = $1;
562 sub update_etc_resolv_conf
{
563 my ($filename, $fh, $resolv, @args) = @_;
567 $data = "search $resolv->{search}\n"
568 if $resolv->{search
};
571 foreach my $k ("dns1", "dns2", "dns3") {
572 my $ns = $resolv->{$k};
573 if ($ns && $ns ne '0.0.0.0' && !$written->{$ns}) {
575 $data .= "nameserver $ns\n";
579 while (my $line = <$fh>) {
580 next if $line =~ m/^(search|domain|nameserver)\s+/;
587 register_file
('resolvconf', "/etc/resolv.conf",
588 \
&read_etc_resolv_conf
, undef,
589 \
&update_etc_resolv_conf
);
591 sub read_etc_timezone
{
592 my ($filename, $fd) = @_;
594 my $timezone = <$fd>;
601 sub write_etc_timezone
{
602 my ($filename, $fh, $timezone) = @_;
604 my $tzinfo = "/usr/share/zoneinfo/$timezone";
606 raise_param_exc
({ 'timezone' => "No such timezone" })
609 ($timezone) = $timezone =~ m/^(.*)$/; # untaint
611 print $fh "$timezone\n";
613 unlink ("/etc/localtime");
614 symlink ("/usr/share/zoneinfo/$timezone", "/etc/localtime");
618 register_file
('timezone', "/etc/timezone",
620 \
&write_etc_timezone
);
622 sub read_active_workers
{
623 my ($filename, $fh) = @_;
628 while (defined (my $line = <$fh>)) {
629 if ($line =~ m/^(\S+)\s(0|1)(\s([0-9A-Za-z]{8})(\s(\s*\S.*))?)?$/) {
634 if ((my $task = PVE
::Tools
::upid_decode
($upid, 1))) {
635 $task->{upid
} = $upid;
636 $task->{saved
} = $saved;
637 $task->{endtime
} = hex($endtime) if $endtime;
638 $task->{status
} = $status if $status;
642 warn "unable to parse line: $line";
650 sub write_active_workers
{
651 my ($filename, $fh, $tasklist) = @_;
654 foreach my $task (@$tasklist) {
655 my $upid = $task->{upid
};
656 my $saved = $task->{saved
} ?
1 : 0;
657 if ($task->{endtime
}) {
658 if ($task->{status
}) {
659 $raw .= sprintf("%s %s %08X %s\n", $upid, $saved, $task->{endtime
}, $task->{status
});
661 $raw .= sprintf("%s %s %08X\n", $upid, $saved, $task->{endtime
});
664 $raw .= "$upid $saved\n";
668 PVE
::Tools
::safe_print
($filename, $fh, $raw) if $raw;
671 register_file
('active', "/var/log/pve/tasks/active",
672 \
&read_active_workers
,
673 \
&write_active_workers
);
676 our $bond_modes = { 'balance-rr' => 0,
677 'active-backup' => 1,
685 my $ovs_bond_modes = {
686 'active-backup' => 1,
688 'lacp-balance-slb' => 1,
689 'lacp-balance-tcp' => 1,
692 #sub get_bond_modes {
693 # return $bond_modes;
696 my $parse_ovs_option = sub {
700 foreach my $kv (split (/\s+/, $data || '')) {
701 my ($k, $v) = split('=', $kv, 2);
702 $opts->{$k} = $v if $k && $v;
707 my $set_ovs_option = sub {
708 my ($d, %params) = @_;
710 my $opts = &$parse_ovs_option($d->{ovs_options
});
712 foreach my $k (keys %params) {
722 foreach my $k (keys %$opts) {
723 push @$res, "$k=$opts->{$k}";
726 if (my $new = join(' ', @$res)) {
727 $d->{ovs_options
} = $new;
728 return $d->{ovs_options
};
730 delete $d->{ovs_options
};
735 my $extract_ovs_option = sub {
738 my $opts = &$parse_ovs_option($d->{ovs_options
});
740 my $v = delete $opts->{$name};
743 foreach my $k (keys %$opts) {
744 push @$res, "$k=$opts->{$k}";
747 if (my $new = join(' ', @$res)) {
748 $d->{ovs_options
} = $new;
750 delete $d->{ovs_options
};
756 my $check_mtu = sub {
757 my ($ifaces, $parent, $child) = @_;
759 die "check mtu - missing parent interface\n" if !$parent;
760 die "check mtu - missing child interface\n" if !$child;
762 my $pmtu = $ifaces->{$parent}->{mtu
} ?
$ifaces->{$parent}->{mtu
} : 1500;
763 my $cmtu = $ifaces->{$child}->{mtu
} ?
$ifaces->{$child}->{mtu
} : 1500;
765 die "interface '$parent' - mtu $pmtu is bigger than '$child' - mtu $cmtu\n"
772 # <optional> exists => BOOL,
773 # <optional> active => BOOL,
774 # <optional> autostart => BOOL,
775 # <auto> priority => INT,
777 # type => "eth" | "bridge" | "bond" | "loopback" | "OVS*" | ... ,
779 # families => ["inet", "inet6", ...],
781 # method => "manual" | "static" | "dhcp" | ... ,
786 # comments => [ "..." ],
788 # method6 => "manual" | "static" | "dhcp" | ... ,
790 # netmask6 => SUBNET,
792 # comments6 => [ "..." ],
794 # <known options>, # like bridge_ports, ovs_*
796 # # extra/unknown options stored by-family:
797 # options => { <inet options>... }
798 # options6 => { <inet6 options>... }
802 # # mappings end up here as well, as we don't need to understand them
806 sub read_etc_network_interfaces
{
807 my ($filename, $fh) = @_;
808 my $proc_net_dev = IO
::File-
>new('/proc/net/dev', 'r');
809 my $active = PVE
::ProcFSTools
::get_active_network_interfaces
();
810 return __read_etc_network_interfaces
($fh, $proc_net_dev, $active);
813 sub __read_etc_network_interfaces
{
814 my ($fh, $proc_net_dev, $active_ifaces) = @_;
817 my $ifaces = $config->{ifaces
} = {};
818 my $options = $config->{options
} = [];
820 my $options_alternatives = {
821 'bond-slaves' => 'slaves',
822 'bond_slaves' => 'slaves',
823 'bond-xmit-hash-policy' => 'bond_xmit_hash_policy',
824 'bond-mode' => 'bond_mode',
825 'bond-miimon' =>'bond_miimon',
826 'bridge-vlan-aware' => 'bridge_vlan_aware',
827 'bridge-fd' => 'bridge_fd',
828 'bridge-stp' => 'bridge_stp',
829 'bridge-ports' => 'bridge_ports',
830 'bridge-vids' => 'bridge_vids'
836 while (defined ($line = <$proc_net_dev>)) {
837 if ($line =~ m/^\s*($PVE::Network::PHYSICAL_NIC_RE):.*/) {
838 $ifaces->{$1}->{exists} = 1;
841 close($proc_net_dev);
844 # we try to keep order inside the file
845 my $priority = 2; # 1 is reserved for lo
847 SECTION
: while (defined ($line = <$fh>)) {
849 next if $line =~ m/^\s*#/;
851 if ($line =~ m/^\s*auto\s+(.*)$/) {
852 my @aa = split (/\s+/, $1);
854 foreach my $a (@aa) {
855 $ifaces->{$a}->{autostart
} = 1;
858 } elsif ($line =~ m/^\s*iface\s+(\S+)\s+(inet6?)\s+(\S+)\s*$/) {
861 my $f = { method => $3 }; # by family, merged to $d with a $suffix
862 (my $suffix = $family) =~ s/^inet//;
864 my $d = $ifaces->{$i} ||= {};
865 $d->{priority
} = $priority++ if !$d->{priority
};
866 push @{$d->{families
}}, $family;
868 while (defined ($line = <$fh>)) {
870 if ($line =~ m/^\s*#(.*?)\s*$/) {
871 # NOTE: we use 'comments' instead of 'comment' to
872 # avoid automatic utf8 conversion
873 $f->{comments
} = '' if !$f->{comments
};
874 $f->{comments
} .= "$1\n";
875 } elsif ($line =~ m
/^\s
*(?
:iface\s
883 } elsif ($line =~ m/^\s*((\S+)\s+(.+))$/) {
885 my ($id, $value) = ($2, $3);
887 $id = $options_alternatives->{$id} if $options_alternatives->{$id};
889 my $simple_options = {
898 'bridge-access' => 1,
899 'bridge-learning' => 1,
900 'bridge-arp-nd-suppress' => 1,
901 'bridge-unicast-flood' => 1,
902 'bridge-multicast-flood' => 1,
904 'bond_xmit_hash_policy' => 1,
906 'vxlan-svcnodeip' => 1,
907 'vxlan-physdev' => 1,
908 'vxlan-local-tunnelip' => 1 };
910 if (($id eq 'address') || ($id eq 'netmask') || ($id eq 'broadcast') || ($id eq 'gateway')) {
912 } elsif ($simple_options->{$id}) {
914 } elsif ($id eq 'slaves' || $id eq 'bridge_ports') {
916 foreach my $p (split (/\s+/, $value)) {
917 next if $p eq 'none';
920 my $str = join (' ', sort keys %{$devs});
922 $d->{$id} .= ' ' . $str if $str;
924 $d->{$id} = $str || '';
926 } elsif ($id eq 'bridge_stp') {
927 if ($value =~ m/^\s*(on|yes)\s*$/i) {
932 } elsif ($id eq 'bridge_vlan_aware') {
934 } elsif ($id eq 'bond_mode') {
936 foreach my $bm (keys %$bond_modes) {
937 my $id = $bond_modes->{$bm};
944 } elsif ($id eq 'vxlan-remoteip') {
945 push @{$d->{$id}}, $value;
947 push @{$f->{options
}}, $option;
953 $d->{"$_$suffix"} = $f->{$_} foreach (keys %$f);
954 last SECTION
if !defined($line);
956 } elsif ($line =~ /\w/) {
957 push @$options, [$priority++, $line];
961 foreach my $ifname (@$active_ifaces) {
962 if (my $iface = $ifaces->{$ifname}) {
963 $iface->{active
} = 1;
967 if (!$ifaces->{lo
}) {
968 $ifaces->{lo
}->{priority
} = 1;
969 $ifaces->{lo
}->{method} = 'loopback';
970 $ifaces->{lo
}->{type
} = 'loopback';
971 $ifaces->{lo
}->{autostart
} = 1;
974 foreach my $iface (keys %$ifaces) {
975 my $d = $ifaces->{$iface};
976 if ($iface =~ m/^bond\d+$/) {
977 if (!$d->{ovs_type
}) {
979 } elsif ($d->{ovs_type
} eq 'OVSBond') {
980 $d->{type
} = $d->{ovs_type
};
981 # translate: ovs_options => bond_mode
982 $d->{'bond_mode'} = &$extract_ovs_option($d, 'bond_mode');
983 my $lacp = &$extract_ovs_option($d, 'lacp');
984 if ($lacp && $lacp eq 'active') {
985 if ($d->{'bond_mode'} eq 'balance-slb') {
986 $d->{'bond_mode'} = 'lacp-balance-slb';
989 # Note: balance-tcp needs lacp
990 if ($d->{'bond_mode'} eq 'balance-tcp') {
991 $d->{'bond_mode'} = 'lacp-balance-tcp';
993 my $tag = &$extract_ovs_option($d, 'tag');
994 $d->{ovs_tag
} = $tag if defined($tag);
996 $d->{type
} = 'unknown';
998 } elsif ($iface =~ m/^vmbr\d+$/) {
999 if (!$d->{ovs_type
}) {
1000 $d->{type
} = 'bridge';
1002 if (!defined ($d->{bridge_fd
})) {
1003 $d->{bridge_fd
} = 0;
1005 if (!defined ($d->{bridge_stp
})) {
1006 $d->{bridge_stp
} = 'off';
1008 } elsif ($d->{ovs_type
} eq 'OVSBridge') {
1009 $d->{type
} = $d->{ovs_type
};
1011 $d->{type
} = 'unknown';
1013 } elsif ($iface =~ m/^(\S+):\d+$/) {
1014 $d->{type
} = 'alias';
1015 if (defined ($ifaces->{$1})) {
1016 $d->{exists} = $ifaces->{$1}->{exists};
1018 $ifaces->{$1}->{exists} = 0;
1021 } elsif ($iface =~ m/^(\S+)\.\d+$/) {
1022 $d->{type
} = 'vlan';
1023 if (defined ($ifaces->{$1})) {
1024 $d->{exists} = $ifaces->{$1}->{exists};
1026 $ifaces->{$1}->{exists} = 0;
1029 } elsif ($iface =~ m/^$PVE::Network::PHYSICAL_NIC_RE$/) {
1030 if (!$d->{ovs_type
}) {
1032 } elsif ($d->{ovs_type
} eq 'OVSPort') {
1033 $d->{type
} = $d->{ovs_type
};
1034 my $tag = &$extract_ovs_option($d, 'tag');
1035 $d->{ovs_tag
} = $tag if defined($tag);
1037 $d->{type
} = 'unknown';
1039 } elsif ($iface =~ m/^lo$/) {
1040 $d->{type
} = 'loopback';
1042 if ($d->{'vxlan-id'}) {
1043 $d->{type
} = 'vxlan';
1044 } elsif (!$d->{ovs_type
}) {
1045 $d->{type
} = 'unknown';
1046 } elsif ($d->{ovs_type
} eq 'OVSIntPort') {
1047 $d->{type
} = $d->{ovs_type
};
1048 my $tag = &$extract_ovs_option($d, 'tag');
1049 $d->{ovs_tag
} = $tag if defined($tag);
1053 $d->{method} = 'manual' if !$d->{method};
1054 $d->{method6
} = 'manual' if !$d->{method6
};
1056 $d->{families
} ||= ['inet'];
1059 # OVS bridges create "allow-$BRIDGE $IFACE" lines which we need to remove
1060 # from the {options} hash for them to be removed correctly.
1061 @$options = grep {defined($_)} map {
1062 my ($pri, $line) = @$_;
1063 if ($line =~ /^allow-(\S+)\s+(.*)$/) {
1065 my @ports = split(/\s+/, $2);
1066 if (defined(my $br = $ifaces->{$bridge})) {
1067 # if this port is part of a bridge, remove it
1068 my %in_ovs_ports = map {$_=>1} split(/\s+/, $br->{ovs_ports
});
1069 @ports = grep { not $in_ovs_ports{$_} } @ports;
1071 # create the allow line for the remaining ports, or delete if empty
1073 [$pri, "allow-$bridge " . join(' ', @ports)];
1078 # don't modify other lines
1086 sub __interface_to_string
{
1087 my ($iface, $d, $family, $first_block, $ifupdown2) = @_;
1089 (my $suffix = $family) =~ s/^inet//;
1091 return '' if !($d && $d->{"method$suffix"});
1095 $raw .= "iface $iface $family " . $d->{"method$suffix"} . "\n";
1096 $raw .= "\taddress " . $d->{"address$suffix"} . "\n" if $d->{"address$suffix"};
1097 $raw .= "\tnetmask " . $d->{"netmask$suffix"} . "\n" if $d->{"netmask$suffix"};
1098 $raw .= "\tgateway " . $d->{"gateway$suffix"} . "\n" if $d->{"gateway$suffix"};
1099 $raw .= "\tbroadcast " . $d->{"broadcast$suffix"} . "\n" if $d->{"broadcast$suffix"};
1101 my $done = { type
=> 1, priority
=> 1, method => 1, active
=> 1, exists => 1,
1102 comments
=> 1, autostart
=> 1, options
=> 1,
1103 address
=> 1, netmask
=> 1, gateway
=> 1, broadcast
=> 1,
1104 method6
=> 1, families
=> 1, options6
=> 1,
1105 address6
=> 1, netmask6
=> 1, gateway6
=> 1, broadcast6
=> 1 };
1107 if (!$first_block) {
1108 # not printing out options
1109 } elsif ($d->{type
} eq 'bridge') {
1111 $d->{bridge_ports
} =~ s/[;,\s]+/ /g;
1112 my $ports = $d->{bridge_ports
} || 'none';
1113 $raw .= "\tbridge-ports $ports\n";
1114 $done->{bridge_ports
} = 1;
1116 my $v = defined($d->{bridge_stp
}) ?
$d->{bridge_stp
} : 'off';
1117 $raw .= "\tbridge-stp $v\n";
1118 $done->{bridge_stp
} = 1;
1120 $v = defined($d->{bridge_fd
}) ?
$d->{bridge_fd
} : 0;
1121 $raw .= "\tbridge-fd $v\n";
1122 $done->{bridge_fd
} = 1;
1124 if( defined($d->{bridge_vlan_aware
})) {
1125 $raw .= "\tbridge-vlan-aware yes\n";
1126 $v = defined($d->{bridge_vids
}) ?
$d->{bridge_vids
} : "2-4094";
1127 $raw .= "\tbridge-vids $v\n";
1129 $done->{bridge_vlan_aware
} = 1;
1130 $done->{bridge_vids
} = 1;
1132 } elsif ($d->{type
} eq 'bond') {
1134 $d->{slaves
} =~ s/[;,\s]+/ /g;
1135 my $slaves = $d->{slaves
} || 'none';
1136 $raw .= "\tbond-slaves $slaves\n";
1137 $done->{slaves
} = 1;
1139 my $v = defined ($d->{'bond_miimon'}) ?
$d->{'bond_miimon'} : 100;
1140 $raw .= "\tbond-miimon $v\n";
1141 $done->{'bond_miimon'} = 1;
1143 $v = defined ($d->{'bond_mode'}) ?
$d->{'bond_mode'} : 'balance-rr';
1144 $raw .= "\tbond-mode $v\n";
1145 $done->{'bond_mode'} = 1;
1147 if ($d->{'bond_mode'} && $d->{'bond_xmit_hash_policy'} &&
1148 ($d->{'bond_mode'} eq 'balance-xor' || $d->{'bond_mode'} eq '802.3ad')) {
1149 $raw .= "\tbond-xmit-hash-policy $d->{'bond_xmit_hash_policy'}\n";
1151 $done->{'bond_xmit_hash_policy'} = 1;
1152 } elsif ($d->{type
} eq 'vxlan') {
1154 foreach my $k (qw(vxlan-id vxlan-svcnodeip vxlan-physdev vxlan-local-tunnelip)) {
1155 $raw .= "\t$k $d->{$k}\n" if defined $d->{$k};
1159 if ($d->{'vxlan-remoteip'}) {
1160 foreach my $remoteip (@{$d->{'vxlan-remoteip'}}) {
1161 $raw .= "\tvxlan-remoteip $remoteip\n";
1163 $done->{'vxlan-remoteip'} = 1;
1165 } elsif ($d->{type
} eq 'OVSBridge') {
1167 $raw .= "\tovs_type $d->{type}\n";
1168 $done->{ovs_type
} = 1;
1170 $raw .= "\tovs_ports $d->{ovs_ports}\n" if $d->{ovs_ports
};
1171 $done->{ovs_ports
} = 1;
1172 } elsif ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' ||
1173 $d->{type
} eq 'OVSBond') {
1175 $d->{autostart
} = 0; # started by the bridge
1177 if (defined($d->{ovs_tag
})) {
1178 &$set_ovs_option($d, tag
=> $d->{ovs_tag
});
1180 $done->{ovs_tag
} = 1;
1182 if ($d->{type
} eq 'OVSBond') {
1184 $d->{bond_mode
} = 'active-backup' if !$d->{bond_mode
};
1186 $ovs_bond_modes->{$d->{bond_mode
}} ||
1187 die "OVS does not support bond mode '$d->{bond_mode}\n";
1189 if ($d->{bond_mode
} eq 'lacp-balance-slb') {
1190 &$set_ovs_option($d, lacp
=> 'active');
1191 &$set_ovs_option($d, bond_mode
=> 'balance-slb');
1192 } elsif ($d->{bond_mode
} eq 'lacp-balance-tcp') {
1193 &$set_ovs_option($d, lacp
=> 'active');
1194 &$set_ovs_option($d, bond_mode
=> 'balance-tcp');
1196 &$set_ovs_option($d, lacp
=> undef);
1197 &$set_ovs_option($d, bond_mode
=> $d->{bond_mode
});
1199 $done->{bond_mode
} = 1;
1201 $raw .= "\tovs_bonds $d->{ovs_bonds}\n" if $d->{ovs_bonds
};
1202 $done->{ovs_bonds
} = 1;
1205 $raw .= "\tovs_type $d->{type}\n";
1206 $done->{ovs_type
} = 1;
1208 if ($d->{ovs_bridge
}) {
1211 $raw = "auto $iface\n$raw";
1213 $raw = "allow-$d->{ovs_bridge} $iface\n$raw";
1216 $raw .= "\tovs_bridge $d->{ovs_bridge}\n";
1217 $done->{ovs_bridge
} = 1;
1222 # print other settings
1223 foreach my $k (sort keys %$d) {
1224 next if $done->{$k};
1226 $raw .= "\t$k $d->{$k}\n";
1230 foreach my $option (@{$d->{"options$suffix"}}) {
1231 $raw .= "\t$option\n";
1235 my $comments = $d->{"comments$suffix"} || '';
1236 foreach my $cl (split(/\n/, $comments)) {
1246 sub write_etc_network_interfaces
{
1247 my ($filename, $fh, $config) = @_;
1248 my $ifupdown2 = -e
'/usr/share/ifupdown2';
1249 my $raw = __write_etc_network_interfaces
($config, $ifupdown2);
1250 PVE
::Tools
::safe_print
($filename, $fh, $raw);
1252 sub __write_etc_network_interfaces
{
1253 my ($config, $ifupdown2) = @_;
1255 my $ifaces = $config->{ifaces
};
1256 my @options = @{$config->{options
}};
1258 my $used_ports = {};
1260 foreach my $iface (keys %$ifaces) {
1261 my $d = $ifaces->{$iface};
1264 foreach my $k (qw(bridge_ports ovs_ports slaves ovs_bonds)) {
1265 $ports .= " $d->{$k}" if $d->{$k};
1268 foreach my $p (PVE
::Tools
::split_list
($ports)) {
1269 die "port '$p' is already used on interface '$used_ports->{$p}'\n"
1270 if $used_ports->{$p} && $used_ports->{$p} ne $iface;
1271 $used_ports->{$p} = $iface;
1275 # delete unused OVS ports
1276 foreach my $iface (keys %$ifaces) {
1277 my $d = $ifaces->{$iface};
1278 if ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' ||
1279 $d->{type
} eq 'OVSBond') {
1280 my $brname = $used_ports->{$iface};
1281 if (!$brname || !$ifaces->{$brname}) {
1282 if ($iface =~ /^$PVE::Network::PHYSICAL_NIC_RE/) {
1283 $ifaces->{$iface} = { type
=> 'eth',
1286 families
=> ['inet'] };
1288 delete $ifaces->{$iface};
1292 my $bd = $ifaces->{$brname};
1293 if ($bd->{type
} ne 'OVSBridge') {
1294 delete $ifaces->{$iface};
1300 # create OVS bridge ports
1301 foreach my $iface (keys %$ifaces) {
1302 my $d = $ifaces->{$iface};
1303 if ($d->{type
} eq 'OVSBridge' && $d->{ovs_ports
}) {
1304 foreach my $p (split (/\s+/, $d->{ovs_ports
})) {
1305 my $n = $ifaces->{$p};
1306 die "OVS bridge '$iface' - unable to find port '$p'\n"
1308 $n->{autostart
} = 0;
1309 if ($n->{type
} eq 'eth') {
1310 $n->{type
} = 'OVSPort';
1311 $n->{ovs_bridge
} = $iface;
1312 } elsif ($n->{type
} eq 'OVSBond' || $n->{type
} eq 'OVSPort' ||
1313 $n->{type
} eq 'OVSIntPort') {
1314 $n->{ovs_bridge
} = $iface;
1316 die "interface '$p' is not defined as OVS port/bond\n";
1319 &$check_mtu($ifaces, $iface, $p);
1324 # check OVS bond ports
1325 foreach my $iface (keys %$ifaces) {
1326 my $d = $ifaces->{$iface};
1327 if ($d->{type
} eq 'OVSBond' && $d->{ovs_bonds
}) {
1328 foreach my $p (split (/\s+/, $d->{ovs_bonds
})) {
1329 my $n = $ifaces->{$p};
1330 die "OVS bond '$iface' - unable to find slave '$p'\n"
1332 die "OVS bond '$iface' - wrong interface type on slave '$p' " .
1333 "('$n->{type}' != 'eth')\n" if $n->{type
} ne 'eth';
1334 &$check_mtu($ifaces, $iface, $p);
1340 foreach my $iface (keys %$ifaces) {
1341 my $d = $ifaces->{$iface};
1342 if ($d->{type
} eq 'bond' && $d->{slaves
}) {
1343 foreach my $p (split (/\s+/, $d->{slaves
})) {
1344 my $n = $ifaces->{$p};
1346 die "bond '$iface' - unable to find slave '$p'\n"
1348 die "bond '$iface' - wrong interface type on slave '$p' " .
1349 "('$n->{type}' != 'eth')\n" if $n->{type
} ne 'eth';
1350 &$check_mtu($ifaces, $iface, $p);
1357 foreach my $iface (keys %$ifaces) {
1358 my $d = $ifaces->{$iface};
1360 if ($d->{type
} eq 'vxlan' && $d->{'vxlan-id'}) {
1361 my $vxlanid = $d->{'vxlan-id'};
1362 die "iface $iface - duplicate vxlan-id $vxlanid already used in $vxlans->{$vxlanid}\n" if $vxlans->{$vxlanid};
1363 $vxlans->{$vxlanid} = $iface;
1367 ++$ips if defined $d->{'vxlan-svcnodeip'};
1368 ++$ips if defined $d->{'vxlan-remoteip'};
1369 ++$ips if defined $d->{'vxlan-local-tunnelip'};
1371 die "iface $iface - vxlan-svcnodeip, vxlan-remoteip and vxlan-localtunnelip are mutually exclusive\n";
1374 if (defined($d->{'vxlan-svcnodeip'}) != defined($d->{'vxlan-physdev'})) {
1375 die "iface $iface - vxlan-svcnodeip and vxlan-physdev must be define together\n";
1377 #fixme : check if vxlan mtu is lower than 50bytes than physical interface where tunnel is going out
1381 foreach my $iface (keys %$ifaces) {
1382 my $d = $ifaces->{$iface};
1383 if ($d->{type
} eq 'vlan' && $iface =~ m/^(\S+)\.\d+$/) {
1385 my $n = $ifaces->{$p};
1387 die "vlan '$iface' - unable to find parent '$p'\n"
1390 if ($n->{type
} eq 'bridge' && !$n->{bridge_vlan_aware
}) {
1391 die "vlan '$iface' - bridge vlan aware is not enabled on parent '$p'\n";
1392 } elsif ($n->{type
} ne 'eth' && $n->{type
} ne 'bridge' && $n->{type
} ne 'bond') {
1393 die "vlan '$iface' - wrong interface type on parent '$p' " .
1394 "('$n->{type}' != 'eth|bond|bridge' )\n";
1396 &$check_mtu($ifaces, $iface, $p);
1400 # check bridgeport option
1401 my $bridgeports = {};
1403 foreach my $iface (keys %$ifaces) {
1404 my $d = $ifaces->{$iface};
1405 if ($d->{type
} eq 'bridge') {
1406 foreach my $p (split (/\s+/, $d->{bridge_ports
})) {
1407 my $n = $ifaces->{$p};
1408 die "bridge '$iface' - unable to find bridge port '$p'\n"
1410 &$check_mtu($ifaces, $iface, $p);
1411 $bridgeports->{$p} = $iface;
1413 $bridges->{$iface} = $d;
1417 foreach my $iface (keys %$ifaces) {
1418 my $d = $ifaces->{$iface};
1420 foreach my $k (qw(bridge-learning bridge-arp-nd-suppress bridge-unicast-flood bridge-multicast-flood bridge-access)) {
1421 die "iface $iface - $k: bridge port specific options can be used only on interfaces attached to a bridge\n"
1422 if $d->{$k} && !$bridgeports->{$iface};
1425 if ($d->{'bridge-access'} && !$bridges->{$bridgeports->{$iface}}->{bridge_vlan_aware
}) {
1426 die "iface $iface - bridge-access option can be only used if interface is in a vlan aware bridge\n";
1430 my $raw = <<'NETWORKDOC';
1431 # network interface settings; autogenerated
1432 # Please do NOT modify this file directly, unless you know what
1435 # If you want to manage part of the network configuration manually,
1436 # please utilize the 'source' or 'source-directory' directives to do
1438 # PVE will preserve these directives, but will NOT its network
1439 # configuration from sourced files, so do not attempt to move any of
1440 # the PVE managed interfaces into external files!
1446 my $if_type_hash = {
1454 my $lookup_type_prio = sub {
1458 if ($iface =~ m/^(\S+)(\.|:)\d+$/) {
1464 if ($iface eq 'lo') {
1465 $pri = $if_type_hash->{loopback
};
1466 } elsif ($iface =~ m/^$PVE::Network::PHYSICAL_NIC_RE$/) {
1467 $pri = $if_type_hash->{eth
} + $child;
1468 } elsif ($iface =~ m/^bond\d+$/) {
1469 $pri = $if_type_hash->{bond
} + $child;
1470 } elsif ($iface =~ m/^vmbr\d+$/) {
1471 $pri = $if_type_hash->{bridge
} + $child;
1477 foreach my $iface (sort {
1478 my $ref1 = $ifaces->{$a};
1479 my $ref2 = $ifaces->{$b};
1480 my $tp1 = &$lookup_type_prio($a);
1481 my $tp2 = &$lookup_type_prio($b);
1483 # Only recognized types are in relation to each other. If one type
1484 # is unknown then only consider the interfaces' priority attributes.
1485 $tp1 = $tp2 = 0 if !defined($tp1) || !defined($tp2);
1487 my $p1 = $tp1 + ($ref1->{priority
} // 50000);
1488 my $p2 = $tp2 + ($ref2->{priority
} // 50000);
1490 return $p1 <=> $p2 if $p1 != $p2;
1494 next if $printed->{$iface};
1496 my $d = $ifaces->{$iface};
1497 my $pri = $d->{priority
} // 0;
1498 if (@options && $options[0]->[0] < $pri) {
1500 $raw .= (shift @options)->[1] . "\n";
1501 } while (@options && $options[0]->[0] < $pri);
1505 $printed->{$iface} = 1;
1506 $raw .= "auto $iface\n" if $d->{autostart
};
1507 my $i = 0; # some options should be printed only once
1508 $raw .= __interface_to_string
($iface, $d, $_, !$i++, $ifupdown2) foreach @{$d->{families
}};
1511 $raw .= $_->[1] . "\n" foreach @options;
1515 register_file
('interfaces', "/etc/network/interfaces",
1516 \
&read_etc_network_interfaces
,
1517 \
&write_etc_network_interfaces
);
1520 sub read_iscsi_initiatorname
{
1521 my ($filename, $fd) = @_;
1523 while (defined(my $line = <$fd>)) {
1524 if ($line =~ m/^InitiatorName=(\S+)$/) {
1532 register_file
('initiatorname', "/etc/iscsi/initiatorname.iscsi",
1533 \
&read_iscsi_initiatorname
);
1536 my ($filename, $fd) = @_;
1540 my $raw = defined($fd) ?
<$fd> : '';
1545 my @tokens = split(/\s+/, $raw);
1550 while (defined(my $tok = shift @tokens)) {
1552 $machine = shift @tokens if $tok eq 'machine';
1554 $data->{$machine} = {} if !$data->{$machine};
1556 $data->{$machine}->{login
} = shift @tokens if $tok eq 'login';
1557 $data->{$machine}->{password
} = shift @tokens if $tok eq 'password';
1563 my $format_apt_auth_data = sub {
1568 foreach my $machine (sort keys %$data) {
1569 my $d = $data->{$machine};
1570 $raw .= "machine $machine\n";
1571 $raw .= " login $d->{login}\n" if $d->{login
};
1572 $raw .= " password $d->{password}\n" if $d->{password
};
1579 sub write_apt_auth
{
1580 my ($filename, $fh, $data) = @_;
1582 my $raw = &$format_apt_auth_data($data);
1584 die "write failed: $!" unless print $fh "$raw\n";
1589 sub update_apt_auth
{
1590 my ($filename, $fh, $data) = @_;
1592 my $orig = read_apt_auth
($filename, $fh);
1594 foreach my $machine (keys %$data) {
1595 $orig->{$machine} = $data->{$machine};
1598 return &$format_apt_auth_data($orig);
1601 register_file
('apt-auth', "/etc/apt/auth.conf",
1602 \
&read_apt_auth
, \
&write_apt_auth
,
1603 \
&update_apt_auth
, perm
=> 0640);