3 # todo: maybe we do not need update_file() ?
12 use Fcntl
qw(:DEFAULT :flock);
14 use PVE
::Exception
qw(raise_param_exc);
24 use Encode
qw(encode decode);
26 our @EXPORT_OK = qw(read_file write_file register_file);
35 '/etc/network/interfaces' => '/etc/network/interfaces.new',
38 # to enable cached operation, you need to call 'inotify_init'
39 # inotify handles are a limited resource, so use with care (only
40 # enable the cache if you really need it)
42 # Note: please close the inotify handle after you fork
44 sub ccache_default_writer
{
45 my ($filename, $data) = @_;
47 die "undefined config writer for '$filename' :ERROR";
50 sub ccache_default_parser
{
51 my ($filename, $srcfd) = @_;
53 die "undefined config reader for '$filename' :ERROR";
56 sub ccache_compute_diff
{
57 my ($filename, $shadow) = @_;
61 my $cmd = ['/usr/bin/diff', '-b', '-N', '-u', $filename, $shadow];
62 PVE
::Tools
::run_command
($cmd, noerr
=> 1, outfunc
=> sub {
64 $diff .= decode
('UTF-8', $line) . "\n";
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 exist.
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
);
542 my ($filename, $fh) = @_;
547 while (my $line = <$fh>) {
549 if ($line =~ m/^\s*#/) {
550 $line = decode
('UTF-8', $line);
556 digest
=> Digest
::SHA
::sha1_hex
($raw),
561 sub write_etc_hosts
{
562 my ($filename, $fh, $hosts, @args) = @_;
564 # check validity of ips/names
565 for my $line (split("\n", $hosts)) {
566 next if $line =~ m/^\s*#/; # comments
567 next if $line =~ m/^\s*$/; # whitespace/empty lines
569 my ($ip, @names) = split(/\s+/, $line);
571 raise_param_exc
({ 'data' => "Invalid IP '$ip'" })
572 if $ip !~ m/^$PVE::Tools::IPRE$/;
574 for my $name (@names) {
575 raise_param_exc
({ 'data' => "Invalid Hostname '$name'" })
576 if $name !~ m/^[.\-a-zA-Z0-9]+$/;
580 die "write failed: $!" if !print $fh encode
('UTF-8', $hosts);
585 register_file
('etchosts', "/etc/hosts",
589 sub read_etc_resolv_conf
{
590 my ($filename, $fh) = @_;
595 while (my $line = <$fh>) {
597 if ($line =~ m/^(search|domain)\s+(\S+)\s*/) {
599 } elsif ($line =~ m/^\s*nameserver\s+($PVE::Tools::IPRE)\s*/) {
602 $res->{"dns$nscount"} = $1;
610 sub update_etc_resolv_conf
{
611 my ($filename, $fh, $resolv, @args) = @_;
615 $data = "search $resolv->{search}\n"
616 if $resolv->{search
};
619 foreach my $k ("dns1", "dns2", "dns3") {
620 my $ns = $resolv->{$k};
621 if ($ns && $ns ne '0.0.0.0' && !$written->{$ns}) {
623 $data .= "nameserver $ns\n";
627 while (my $line = <$fh>) {
628 next if $line =~ m/^(search|domain|nameserver)\s+/;
635 register_file
('resolvconf', "/etc/resolv.conf",
636 \
&read_etc_resolv_conf
, undef,
637 \
&update_etc_resolv_conf
);
639 sub read_etc_timezone
{
640 my ($filename, $fd) = @_;
642 my $timezone = <$fd>;
649 sub write_etc_timezone
{
650 my ($filename, $fh, $timezone) = @_;
652 my $tzinfo = "/usr/share/zoneinfo/$timezone";
654 raise_param_exc
({ 'timezone' => "No such timezone" })
657 ($timezone) = $timezone =~ m/^(.*)$/; # untaint
659 print $fh "$timezone\n";
661 unlink ("/etc/localtime");
662 symlink ("/usr/share/zoneinfo/$timezone", "/etc/localtime");
666 register_file
('timezone', "/etc/timezone",
668 \
&write_etc_timezone
);
670 sub read_active_workers
{
671 my ($filename, $fh) = @_;
676 while (defined (my $line = <$fh>)) {
677 if ($line =~ m/^(\S+)\s(0|1)(\s([0-9A-Za-z]{8})(\s(\s*\S.*))?)?$/) {
682 if ((my $task = PVE
::Tools
::upid_decode
($upid, 1))) {
683 $task->{upid
} = $upid;
684 $task->{saved
} = $saved;
685 $task->{endtime
} = hex($endtime) if $endtime;
686 $task->{status
} = $status if $status;
690 warn "unable to parse line: $line";
698 sub write_active_workers
{
699 my ($filename, $fh, $tasklist) = @_;
702 foreach my $task (@$tasklist) {
703 my $upid = $task->{upid
};
704 my $saved = $task->{saved
} ?
1 : 0;
705 if ($task->{endtime
}) {
706 if ($task->{status
}) {
707 $raw .= sprintf("%s %s %08X %s\n", $upid, $saved, $task->{endtime
}, $task->{status
});
709 $raw .= sprintf("%s %s %08X\n", $upid, $saved, $task->{endtime
});
712 $raw .= "$upid $saved\n";
716 PVE
::Tools
::safe_print
($filename, $fh, $raw) if $raw;
719 register_file
('active', "/var/log/pve/tasks/active",
720 \
&read_active_workers
,
721 \
&write_active_workers
);
724 our $bond_modes = { 'balance-rr' => 0,
725 'active-backup' => 1,
733 my $ovs_bond_modes = {
734 'active-backup' => 1,
736 'lacp-balance-slb' => 1,
737 'lacp-balance-tcp' => 1,
740 #sub get_bond_modes {
741 # return $bond_modes;
744 my $parse_ovs_option = sub {
748 foreach my $kv (split (/\s+/, $data || '')) {
749 my ($k, $v) = split('=', $kv, 2);
750 $opts->{$k} = $v if $k && $v;
755 my $set_ovs_option = sub {
756 my ($d, %params) = @_;
758 my $opts = &$parse_ovs_option($d->{ovs_options
});
760 foreach my $k (keys %params) {
770 foreach my $k (keys %$opts) {
771 push @$res, "$k=$opts->{$k}";
774 if (my $new = join(' ', @$res)) {
775 $d->{ovs_options
} = $new;
776 return $d->{ovs_options
};
778 delete $d->{ovs_options
};
783 my $extract_ovs_option = sub {
786 my $opts = &$parse_ovs_option($d->{ovs_options
});
788 my $v = delete $opts->{$name};
791 foreach my $k (keys %$opts) {
792 push @$res, "$k=$opts->{$k}";
795 if (my $new = join(' ', @$res)) {
796 $d->{ovs_options
} = $new;
798 delete $d->{ovs_options
};
804 my $check_mtu = sub {
805 my ($ifaces, $parent, $child) = @_;
807 die "check mtu - missing parent interface\n" if !$parent;
808 die "check mtu - missing child interface\n" if !$child;
810 my $cmtu = $ifaces->{$child}->{mtu
};
813 my $parentdata = $ifaces->{$parent};
814 my $pmtu = $parentdata->{mtu
};
815 $pmtu = $cmtu if $parentdata->{type
} eq 'bond' && !$pmtu;
816 $pmtu = 1500 if !$pmtu;
818 die "interface '$parent' - mtu $pmtu is lower than '$child' - mtu $cmtu\n"
825 # <optional> exists => BOOL,
826 # <optional> active => BOOL,
827 # <optional> autostart => BOOL,
828 # <auto> priority => INT,
830 # type => "eth" | "bridge" | "bond" | "loopback" | "OVS*" | ... ,
832 # families => ["inet", "inet6", ...],
834 # method => "manual" | "static" | "dhcp" | ... ,
839 # comments => [ "..." ],
841 # method6 => "manual" | "static" | "dhcp" | ... ,
843 # netmask6 => SUBNET,
845 # comments6 => [ "..." ],
847 # <known options>, # like bridge_ports, ovs_*
849 # # extra/unknown options stored by-family:
850 # options => { <inet options>... }
851 # options6 => { <inet6 options>... }
855 # # mappings end up here as well, as we don't need to understand them
859 sub read_etc_network_interfaces
{
860 my ($filename, $fh) = @_;
861 my $proc_net_dev = IO
::File-
>new('/proc/net/dev', 'r');
862 my $active = PVE
::ProcFSTools
::get_active_network_interfaces
();
863 return __read_etc_network_interfaces
($fh, $proc_net_dev, $active);
866 sub __read_etc_network_interfaces
{
867 my ($fh, $proc_net_dev, $active_ifaces) = @_;
870 my $ifaces = $config->{ifaces
} = {};
871 my $options = $config->{options
} = [];
873 my $options_alternatives = {
875 'bond-slaves' => 'slaves',
876 'bond_slaves' => 'slaves',
877 'bond-xmit-hash-policy' => 'bond_xmit_hash_policy',
878 'bond-mode' => 'bond_mode',
879 'bond-miimon' =>'bond_miimon',
880 'bridge-vlan-aware' => 'bridge_vlan_aware',
881 'bridge-fd' => 'bridge_fd',
882 'bridge-stp' => 'bridge_stp',
883 'bridge-ports' => 'bridge_ports',
884 'bridge-vids' => 'bridge_vids'
890 while (defined ($line = <$proc_net_dev>)) {
891 if ($line =~ m/^\s*($PVE::Network::PHYSICAL_NIC_RE):.*/) {
892 $ifaces->{$1}->{exists} = 1;
895 close($proc_net_dev);
898 # we try to keep order inside the file
899 my $priority = 2; # 1 is reserved for lo
901 SECTION
: while (defined ($line = <$fh>)) {
903 next if $line =~ m/^\s*#/;
904 next if $line =~ m/^\s*(allow-hotplug)\s+(.*)$/;
906 if ($line =~ m/^\s*(auto|allow-ovs)\s+(.*)$/) {
907 my @aa = split (/\s+/, $2);
909 foreach my $a (@aa) {
910 $ifaces->{$a}->{autostart
} = 1;
913 } elsif ($line =~ m/^\s*iface\s+(\S+)\s+(inet6?)\s+(\S+)\s*$/) {
916 my $f = { method => $3 }; # by family, merged to $d with a $suffix
917 (my $suffix = $family) =~ s/^inet//;
919 my $d = $ifaces->{$i} ||= {};
920 $d->{priority
} = $priority++ if !$d->{priority
};
921 push @{$d->{families
}}, $family;
923 while (defined ($line = <$fh>)) {
925 if ($line =~ m/^\s*#(.*?)\s*$/) {
926 $f->{comments
} = '' if !$f->{comments
};
927 my $comment = decode
('UTF-8', $1);
928 $f->{comments
} .= "$comment\n";
929 } elsif ($line =~ m
/^\s
*(?
:iface\s
937 } elsif ($line =~ m/^\s*((\S+)\s+(.+))$/) {
939 my ($id, $value) = ($2, $3);
941 $id = $options_alternatives->{$id} if $options_alternatives->{$id};
943 my $simple_options = {
952 'bridge-access' => 1,
953 'bridge-learning' => 1,
954 'bridge-arp-nd-suppress' => 1,
955 'bridge-unicast-flood' => 1,
956 'bridge-multicast-flood' => 1,
958 'bond_xmit_hash_policy' => 1,
961 'vlan-protocol' => 1,
962 'vlan-raw-device' => 1,
965 'vxlan-svcnodeip' => 1,
966 'vxlan-physdev' => 1,
967 'vxlan-local-tunnelip' => 1 };
969 if (($id eq 'address') || ($id eq 'netmask') || ($id eq 'broadcast') || ($id eq 'gateway')) {
971 } elsif ($simple_options->{$id}) {
973 } elsif ($id eq 'slaves' || $id eq 'bridge_ports') {
975 foreach my $p (split (/\s+/, $value)) {
976 next if $p eq 'none';
979 my $str = join (' ', sort keys %{$devs});
981 $d->{$id} .= ' ' . $str if $str;
983 $d->{$id} = $str || '';
985 } elsif ($id eq 'bridge_stp') {
986 if ($value =~ m/^\s*(on|yes)\s*$/i) {
991 } elsif ($id eq 'bridge_vlan_aware') {
993 } elsif ($id eq 'bond_mode') {
995 foreach my $bm (keys %$bond_modes) {
996 my $id = $bond_modes->{$bm};
1003 } elsif ($id eq 'vxlan-remoteip') {
1004 push @{$d->{$id}}, $value;
1006 push @{$f->{options
}}, $option;
1012 $d->{"$_$suffix"} = $f->{$_} foreach (keys %$f);
1013 last SECTION
if !defined($line);
1015 } elsif ($line =~ /\w/) {
1016 push @$options, [$priority++, $line];
1020 foreach my $ifname (@$active_ifaces) {
1021 if (my $iface = $ifaces->{$ifname}) {
1022 $iface->{active
} = 1;
1026 if (!$ifaces->{lo
}) {
1027 $ifaces->{lo
}->{priority
} = 1;
1028 $ifaces->{lo
}->{method} = 'loopback';
1029 $ifaces->{lo
}->{type
} = 'loopback';
1030 $ifaces->{lo
}->{autostart
} = 1;
1033 foreach my $iface (keys %$ifaces) {
1034 my $d = $ifaces->{$iface};
1035 $d->{type
} = 'unknown';
1036 if ($iface =~ m/^bond\d+$/) {
1037 if (!$d->{ovs_type
}) {
1038 $d->{type
} = 'bond';
1039 } elsif ($d->{ovs_type
} eq 'OVSBond') {
1040 $d->{type
} = $d->{ovs_type
};
1041 # translate: ovs_options => bond_mode
1042 $d->{'bond_mode'} = &$extract_ovs_option($d, 'bond_mode');
1043 my $lacp = &$extract_ovs_option($d, 'lacp');
1044 if ($lacp && $lacp eq 'active') {
1045 if ($d->{'bond_mode'} eq 'balance-slb') {
1046 $d->{'bond_mode'} = 'lacp-balance-slb';
1049 # Note: balance-tcp needs lacp
1050 if ($d->{'bond_mode'} eq 'balance-tcp') {
1051 $d->{'bond_mode'} = 'lacp-balance-tcp';
1053 my $tag = &$extract_ovs_option($d, 'tag');
1054 $d->{ovs_tag
} = $tag if defined($tag);
1056 } elsif ($iface =~ m/^vmbr\d+$/) {
1057 if (!$d->{ovs_type
}) {
1058 $d->{type
} = 'bridge';
1060 if (!defined ($d->{bridge_fd
})) {
1061 $d->{bridge_fd
} = 0;
1063 if (!defined ($d->{bridge_stp
})) {
1064 $d->{bridge_stp
} = 'off';
1066 } elsif ($d->{ovs_type
} eq 'OVSBridge') {
1067 $d->{type
} = $d->{ovs_type
};
1069 } elsif ($iface =~ m/^(\S+):\d+$/) {
1070 $d->{type
} = 'alias';
1071 if (defined ($ifaces->{$1})) {
1072 $d->{exists} = $ifaces->{$1}->{exists};
1074 $ifaces->{$1}->{exists} = 0;
1077 } elsif ($iface =~ m/^(\S+)\.\d+$/ || $d->{'vlan-raw-device'}) {
1078 $d->{type
} = 'vlan';
1080 my $raw_iface = $d->{'vlan-raw-device'} ?
$d->{'vlan-raw-device'} : $1;
1081 if (defined ($ifaces->{$raw_iface})) {
1082 $d->{exists} = $ifaces->{$raw_iface}->{exists};
1084 $ifaces->{$raw_iface}->{exists} = 0;
1087 } elsif ($iface =~ m/^$PVE::Network::PHYSICAL_NIC_RE$/) {
1088 if (!$d->{ovs_type
}) {
1090 } elsif ($d->{ovs_type
} eq 'OVSPort') {
1091 $d->{type
} = $d->{ovs_type
};
1092 my $tag = &$extract_ovs_option($d, 'tag');
1093 $d->{ovs_tag
} = $tag if defined($tag);
1095 } elsif ($iface =~ m/^lo$/) {
1096 $d->{type
} = 'loopback';
1098 if ($d->{'vxlan-id'}) {
1099 $d->{type
} = 'vxlan';
1100 } elsif ($d->{ovs_type
} eq 'OVSIntPort') {
1101 $d->{type
} = $d->{ovs_type
};
1102 my $tag = &$extract_ovs_option($d, 'tag');
1103 $d->{ovs_tag
} = $tag if defined($tag);
1107 # map address and netmask to cidr
1108 if ($d->{address
}) {
1109 if ($d->{netmask
}) {
1110 if ($d->{netmask
} =~ m/^\d+$/) { # e.g. netmask 20
1111 $d->{address
} = $d->{address
} . "/" . $d->{netmask
};
1112 } elsif (my $mask = PVE
::JSONSchema
::get_netmask_bits
($d->{netmask
})) {
1113 $d->{address
} = $d->{address
} . "/" . $mask;
1116 # for api compatibility
1117 $d->{cidr
} = $d->{address
};
1120 # map address6 and netmask6 to cidr6
1121 if ($d->{address6
}) {
1122 $d->{address6
} .= "/" . $d->{netmask6
} if $d->{address6
} !~ m
!^(.*)/(\d
+)$! && $d->{netmask6
};
1123 # for api compatibility
1124 $d->{cidr6
} = $d->{address6
};
1127 $d->{method} = 'manual' if !$d->{method};
1128 $d->{method6
} = 'manual' if !$d->{method6
};
1130 $d->{families
} ||= ['inet'];
1133 # OVS bridges create "allow-$BRIDGE $IFACE" lines which we need to remove
1134 # from the {options} hash for them to be removed correctly.
1135 @$options = grep {defined($_)} map {
1136 my ($pri, $line) = @$_;
1137 if ($line =~ /^allow-ovs\s+(.*)$/) {
1139 } elsif ($line =~ /^allow-(\S+)\s+(.*)$/) {
1141 my @ports = split(/\s+/, $2);
1142 if (defined(my $br = $ifaces->{$bridge})) {
1143 # if this port is part of a bridge, remove it
1144 my %in_ovs_ports = map {$_=>1} split(/\s+/, $br->{ovs_ports
});
1145 @ports = grep { not $in_ovs_ports{$_} } @ports;
1147 # create the allow line for the remaining ports, or delete if empty
1149 [$pri, "allow-$bridge " . join(' ', @ports)];
1154 # don't modify other lines
1162 sub __interface_to_string
{
1163 my ($iface, $d, $family, $first_block, $ifupdown2) = @_;
1165 (my $suffix = $family) =~ s/^inet//;
1167 return '' if !($d && $d->{"method$suffix"});
1171 $raw .= "iface $iface $family " . $d->{"method$suffix"} . "\n";
1172 $raw .= "\taddress " . $d->{"address$suffix"} . "\n" if $d->{"address$suffix"};
1173 $raw .= "\tgateway " . $d->{"gateway$suffix"} . "\n" if $d->{"gateway$suffix"};
1175 my $done = { type
=> 1, priority
=> 1, method => 1, active
=> 1, exists => 1,
1176 comments
=> 1, autostart
=> 1, options
=> 1,
1177 address
=> 1, netmask
=> 1, gateway
=> 1, broadcast
=> 1,
1178 method6
=> 1, families
=> 1, options6
=> 1,
1179 address6
=> 1, netmask6
=> 1, gateway6
=> 1, broadcast6
=> 1, 'uplink-id' => 1 };
1181 if (!$first_block) {
1182 # not printing out options
1183 } elsif ($d->{type
} eq 'bridge') {
1185 $d->{bridge_ports
} =~ s/[;,\s]+/ /g;
1186 my $ports = $d->{bridge_ports
} || 'none';
1187 $raw .= "\tbridge-ports $ports\n";
1188 $done->{bridge_ports
} = 1;
1190 my $v = defined($d->{bridge_stp
}) ?
$d->{bridge_stp
} : 'off';
1191 $raw .= "\tbridge-stp $v\n";
1192 $done->{bridge_stp
} = 1;
1194 $v = defined($d->{bridge_fd
}) ?
$d->{bridge_fd
} : 0;
1195 $raw .= "\tbridge-fd $v\n";
1196 $done->{bridge_fd
} = 1;
1198 if( defined($d->{bridge_vlan_aware
})) {
1199 $raw .= "\tbridge-vlan-aware yes\n";
1200 $v = defined($d->{bridge_vids
}) ?
$d->{bridge_vids
} : "2-4094";
1201 $raw .= "\tbridge-vids $v\n";
1203 $done->{bridge_vlan_aware
} = 1;
1204 $done->{bridge_vids
} = 1;
1206 $raw .= "\tmtu $d->{mtu}\n" if $d->{mtu
};
1209 } elsif ($d->{type
} eq 'bond') {
1211 $d->{slaves
} =~ s/[;,\s]+/ /g;
1212 my $slaves = $d->{slaves
} || 'none';
1213 $raw .= "\tbond-slaves $slaves\n";
1214 $done->{slaves
} = 1;
1216 my $v = defined ($d->{'bond_miimon'}) ?
$d->{'bond_miimon'} : 100;
1217 $raw .= "\tbond-miimon $v\n";
1218 $done->{'bond_miimon'} = 1;
1220 $v = defined ($d->{'bond_mode'}) ?
$d->{'bond_mode'} : 'balance-rr';
1221 $raw .= "\tbond-mode $v\n";
1222 $done->{'bond_mode'} = 1;
1224 if ($d->{'bond_mode'} && $d->{'bond_xmit_hash_policy'} &&
1225 ($d->{'bond_mode'} eq 'balance-xor' || $d->{'bond_mode'} eq '802.3ad')) {
1226 $raw .= "\tbond-xmit-hash-policy $d->{'bond_xmit_hash_policy'}\n";
1228 $done->{'bond_xmit_hash_policy'} = 1;
1230 if ($d->{'bond_mode'} && $d->{'bond_mode'} eq 'active-backup' && $d->{'bond-primary'}) {
1231 $raw .= "\tbond-primary $d->{'bond-primary'}\n";
1233 $done->{'bond-primary'} = 1;
1235 $raw .= "\tmtu $d->{mtu}\n" if $d->{mtu
};
1238 } elsif ($d->{type
} eq 'vlan') {
1239 die "$iface: wrong vlan-protocol $d->{'vlan-protocol'}\n"
1240 if $d->{'vlan-protocol'} && $d->{'vlan-protocol'} ne '802.1ad' && $d->{'vlan-protocol'} ne '802.1q';
1242 } elsif ($d->{type
} eq 'vxlan') {
1244 foreach my $k (qw(vxlan-id vxlan-svcnodeip vxlan-physdev vxlan-local-tunnelip)) {
1245 $raw .= "\t$k $d->{$k}\n" if defined $d->{$k};
1249 if ($d->{'vxlan-remoteip'}) {
1250 foreach my $remoteip (@{$d->{'vxlan-remoteip'}}) {
1251 $raw .= "\tvxlan-remoteip $remoteip\n";
1253 $done->{'vxlan-remoteip'} = 1;
1256 $raw .= "\tmtu $d->{mtu}\n" if $d->{mtu
};
1259 } elsif ($d->{type
} eq 'OVSBridge') {
1261 $raw .= "\tovs_type $d->{type}\n";
1262 $done->{ovs_type
} = 1;
1264 $raw .= "\tovs_ports $d->{ovs_ports}\n" if $d->{ovs_ports
};
1265 $done->{ovs_ports
} = 1;
1267 $raw .= "\tovs_mtu $d->{mtu}\n" if $d->{mtu
};
1270 } elsif ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' ||
1271 $d->{type
} eq 'OVSBond') {
1273 $d->{autostart
} = 0; # started by the bridge
1275 if (defined($d->{ovs_tag
})) {
1276 &$set_ovs_option($d, tag
=> $d->{ovs_tag
});
1278 $done->{ovs_tag
} = 1;
1280 if ($d->{type
} eq 'OVSBond') {
1282 $d->{bond_mode
} = 'active-backup' if !$d->{bond_mode
};
1284 $ovs_bond_modes->{$d->{bond_mode
}} ||
1285 die "OVS does not support bond mode '$d->{bond_mode}\n";
1287 if ($d->{bond_mode
} eq 'lacp-balance-slb') {
1288 &$set_ovs_option($d, lacp
=> 'active');
1289 &$set_ovs_option($d, bond_mode
=> 'balance-slb');
1290 } elsif ($d->{bond_mode
} eq 'lacp-balance-tcp') {
1291 &$set_ovs_option($d, lacp
=> 'active');
1292 &$set_ovs_option($d, bond_mode
=> 'balance-tcp');
1294 &$set_ovs_option($d, lacp
=> undef);
1295 &$set_ovs_option($d, bond_mode
=> $d->{bond_mode
});
1297 $done->{bond_mode
} = 1;
1299 $raw .= "\tovs_bonds $d->{ovs_bonds}\n" if $d->{ovs_bonds
};
1300 $done->{ovs_bonds
} = 1;
1303 $raw .= "\tovs_type $d->{type}\n";
1304 $done->{ovs_type
} = 1;
1306 if (my $bridge = $d->{ovs_bridge
}) {
1308 $raw = "auto $iface\n$raw";
1310 $raw = "allow-$bridge $iface\n$raw";
1313 $raw .= "\tovs_bridge $bridge\n";
1314 $done->{ovs_bridge
} = 1;
1317 $raw .= "\tovs_mtu $d->{mtu}\n" if $d->{mtu
};
1322 # print other settings
1323 foreach my $k (sort keys %$d) {
1324 next if $done->{$k};
1326 $raw .= "\t$k $d->{$k}\n";
1330 foreach my $option (@{$d->{"options$suffix"}}) {
1331 $raw .= "\t$option\n";
1335 my $comments = $d->{"comments$suffix"} || '';
1336 foreach my $cl (split(/\n/, $comments)) {
1346 sub write_etc_network_interfaces
{
1347 my ($filename, $fh, $config) = @_;
1348 my $ifupdown2 = -e
'/usr/share/ifupdown2/ifupdown2';
1349 my $raw = __write_etc_network_interfaces
($config, $ifupdown2);
1350 PVE
::Tools
::safe_print
($filename, $fh, encode
('UTF-8', $raw));
1352 sub __write_etc_network_interfaces
{
1353 my ($config, $ifupdown2) = @_;
1355 my $ifaces = $config->{ifaces
};
1356 my @options = @{$config->{options
}};
1358 my $used_ports = {};
1360 foreach my $iface (keys %$ifaces) {
1361 my $d = $ifaces->{$iface};
1367 foreach my $k (qw(bridge_ports ovs_ports slaves ovs_bonds)) {
1368 $ports .= " $d->{$k}" if $d->{$k};
1371 foreach my $p (PVE
::Tools
::split_list
($ports)) {
1372 die "port '$p' is already used on interface '$used_ports->{$p}'\n"
1373 if $used_ports->{$p} && $used_ports->{$p} ne $iface;
1374 $used_ports->{$p} = $iface;
1378 # delete unused OVS ports
1379 foreach my $iface (keys %$ifaces) {
1380 my $d = $ifaces->{$iface};
1381 if ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' ||
1382 $d->{type
} eq 'OVSBond') {
1383 my $brname = $used_ports->{$iface};
1384 if (!$brname || !$ifaces->{$brname}) {
1385 if ($iface =~ /^$PVE::Network::PHYSICAL_NIC_RE/) {
1386 $ifaces->{$iface} = { type
=> 'eth',
1389 families
=> ['inet'] };
1391 delete $ifaces->{$iface};
1395 my $bd = $ifaces->{$brname};
1396 if ($bd->{type
} ne 'OVSBridge') {
1397 delete $ifaces->{$iface};
1403 # create OVS bridge ports
1404 foreach my $iface (keys %$ifaces) {
1405 my $d = $ifaces->{$iface};
1406 if ($d->{type
} eq 'OVSBridge' && $d->{ovs_ports
}) {
1407 foreach my $p (split (/\s+/, $d->{ovs_ports
})) {
1408 my $n = $ifaces->{$p};
1409 die "OVS bridge '$iface' - unable to find port '$p'\n"
1411 $n->{autostart
} = 0;
1412 if ($n->{type
} eq 'eth') {
1413 $n->{type
} = 'OVSPort';
1414 $n->{ovs_bridge
} = $iface;
1415 } elsif ($n->{type
} eq 'OVSBond' || $n->{type
} eq 'OVSPort' ||
1416 $n->{type
} eq 'OVSIntPort') {
1417 $n->{ovs_bridge
} = $iface;
1419 die "interface '$p' is not defined as OVS port/bond\n";
1422 &$check_mtu($ifaces, $iface, $p);
1427 # check OVS bond ports
1428 foreach my $iface (keys %$ifaces) {
1429 my $d = $ifaces->{$iface};
1430 if ($d->{type
} eq 'OVSBond' && $d->{ovs_bonds
}) {
1431 foreach my $p (split (/\s+/, $d->{ovs_bonds
})) {
1432 my $n = $ifaces->{$p};
1433 die "OVS bond '$iface' - unable to find slave '$p'\n"
1435 die "OVS bond '$iface' - wrong interface type on slave '$p' " .
1436 "('$n->{type}' != 'eth')\n" if $n->{type
} ne 'eth';
1437 &$check_mtu($ifaces, $iface, $p);
1443 foreach my $iface (keys %$ifaces) {
1444 my $d = $ifaces->{$iface};
1445 if ($d->{type
} eq 'bond' && $d->{slaves
}) {
1446 my $bond_primary_is_slave = undef;
1447 foreach my $p (split (/\s+/, $d->{slaves
})) {
1448 my $n = $ifaces->{$p};
1450 die "bond '$iface' - unable to find slave '$p'\n"
1452 die "bond '$iface' - wrong interface type on slave '$p' " .
1453 "('$n->{type}' != 'eth')\n" if $n->{type
} ne 'eth';
1454 &$check_mtu($ifaces, $iface, $p);
1455 $bond_primary_is_slave = 1 if $d->{'bond-primary'} && $d->{'bond-primary'} eq $p;
1457 die "bond '$iface' - bond-primary interface is not a slave" if $d->{'bond-primary'} && !$bond_primary_is_slave;
1463 foreach my $iface (keys %$ifaces) {
1464 my $d = $ifaces->{$iface};
1466 if ($d->{type
} eq 'vxlan' && $d->{'vxlan-id'}) {
1467 my $vxlanid = $d->{'vxlan-id'};
1468 die "iface $iface - duplicate vxlan-id $vxlanid already used in $vxlans->{$vxlanid}\n" if $vxlans->{$vxlanid};
1469 $vxlans->{$vxlanid} = $iface;
1473 ++$ips if defined $d->{'vxlan-svcnodeip'};
1474 ++$ips if defined $d->{'vxlan-remoteip'};
1475 ++$ips if defined $d->{'vxlan-local-tunnelip'};
1477 die "iface $iface - vxlan-svcnodeip, vxlan-remoteip and vxlan-localtunnelip are mutually exclusive\n";
1480 if (defined($d->{'vxlan-svcnodeip'}) != defined($d->{'vxlan-physdev'})) {
1481 die "iface $iface - vxlan-svcnodeip and vxlan-physdev must be define together\n";
1483 #fixme : check if vxlan mtu is lower than 50bytes than physical interface where tunnel is going out
1487 foreach my $iface (keys %$ifaces) {
1488 my $d = $ifaces->{$iface};
1489 if ($d->{type
} eq 'vlan') {
1494 if ($iface =~ m/^(\S+)\.(\d+)$/) {
1497 delete $d->{'vlan-raw-device'} if $d->{'vlan-raw-device'};
1499 die "missing vlan-raw-device option" if !$d->{'vlan-raw-device'};
1500 $p = $d->{'vlan-raw-device'};
1502 if ($iface =~ m/^vlan(\d+)$/) {
1504 delete $d->{'vlan-id'} if $d->{'vlan-id'};
1506 die "custom vlan interface name need ifupdown2" if !$ifupdown2;
1507 die "missing vlan-id option" if !$d->{'vlan-id'};
1508 $vlanid = $d->{'vlan-id'};
1511 my $n = $ifaces->{$p};
1513 die "vlan '$iface' - vlan-id $vlanid should be <= 4094\n" if $vlanid > 4094;
1514 die "vlan '$iface' - unable to find parent '$p'\n"
1517 if ($n->{type
} ne 'eth' && $n->{type
} ne 'bridge' && $n->{type
} ne 'bond' && $n->{type
} ne 'vlan') {
1518 die "vlan '$iface' - wrong interface type on parent '$p' " .
1519 "('$n->{type}' != 'eth|bond|bridge|vlan' )\n";
1522 &$check_mtu($ifaces, $p, $iface);
1529 foreach my $iface (keys %$ifaces) {
1530 my $d = $ifaces->{$iface};
1531 if (my $uplinkid = $d->{'uplink-id'}) {
1532 die "iface '$iface' - uplink-id $uplinkid is only allowed on physical and linux bond interfaces\n"
1533 if $d->{type
} ne 'eth' && $d->{type
} ne 'bond';
1535 die "iface '$iface' - uplink-id $uplinkid is already assigned on '$uplinks->{$uplinkid}'\n"
1536 if $uplinks->{$uplinkid};
1538 $uplinks->{$uplinkid} = $iface;
1542 # check bridgeport option
1543 my $bridgeports = {};
1545 my $ifaces_copy = { %$ifaces };
1546 foreach my $iface (keys %$ifaces_copy) {
1547 my $d = $ifaces_copy->{$iface};
1548 if ($d->{type
} eq 'bridge') {
1549 foreach my $p (split (/\s+/, $d->{bridge_ports
})) {
1550 if($p =~ m/(\S+)\.(\d+)$/) {
1551 my $vlanparent = $1;
1552 if (!defined($ifaces_copy->{$p})) {
1553 $ifaces_copy->{$p}->{type
} = 'vlan';
1554 $ifaces_copy->{$p}->{method} = 'manual';
1555 $ifaces_copy->{$p}->{method6
} = 'manual';
1556 $ifaces_copy->{$p}->{mtu
} = $ifaces_copy->{$vlanparent}->{mtu
} if defined($ifaces_copy->{$1}->{mtu
});
1559 my $n = $ifaces_copy->{$p};
1560 die "bridge '$iface' - unable to find bridge port '$p'\n" if !$n;
1561 die "iface $p - ip address can't be set on interface if bridged in $iface\n"
1562 if ($n->{method} && $n->{method} eq 'static' && $n->{address
} ne '0.0.0.0') ||
1563 ($n->{method6
} && $n->{method6
} eq 'static' && $n->{address
} ne '::');
1564 &$check_mtu($ifaces_copy, $p, $iface);
1565 $bridgeports->{$p} = $iface;
1567 $bridges->{$iface} = $d;
1571 foreach my $iface (keys %$ifaces) {
1572 my $d = $ifaces->{$iface};
1574 foreach my $k (qw(bridge-learning bridge-arp-nd-suppress bridge-unicast-flood bridge-multicast-flood bridge-access)) {
1575 die "iface $iface - $k: bridge port specific options can be used only on interfaces attached to a bridge\n"
1576 if $d->{$k} && !$bridgeports->{$iface};
1579 if ($d->{'bridge-access'} && !$bridges->{$bridgeports->{$iface}}->{bridge_vlan_aware
}) {
1580 die "iface $iface - bridge-access option can be only used if interface is in a vlan aware bridge\n";
1584 my $raw = <<'NETWORKDOC';
1585 # network interface settings; autogenerated
1586 # Please do NOT modify this file directly, unless you know what
1589 # If you want to manage parts of the network configuration manually,
1590 # please utilize the 'source' or 'source-directory' directives to do
1592 # PVE will preserve these directives, but will NOT read its network
1593 # configuration from sourced files, so do not attempt to move any of
1594 # the PVE managed interfaces into external files!
1600 my $if_type_hash = {
1604 OVSIntPort
=> 200000,
1607 OVSBridge
=> 400000,
1612 my $lookup_type_prio = sub {
1613 my ($iface, $ifaces) = @_;
1615 my ($rootiface, @rest) = split(/[.:]/, $iface);
1616 my $childlevel = scalar(@rest);
1617 my $n = $ifaces->{$rootiface};
1619 my $pri = $if_type_hash->{$n->{type
}} + $childlevel
1620 if $n->{type
} && $n->{type
} ne 'unknown';
1625 foreach my $iface (sort {
1626 my $ref1 = $ifaces->{$a};
1627 my $ref2 = $ifaces->{$b};
1628 my $tp1 = &$lookup_type_prio($a, $ifaces);
1629 my $tp2 = &$lookup_type_prio($b, $ifaces);
1631 # Only recognized types are in relation to each other. If one type
1632 # is unknown then only consider the interfaces' priority attributes.
1633 $tp1 = $tp2 = 0 if !defined($tp1) || !defined($tp2);
1635 my $p1 = $tp1 + ($ref1->{priority
} // 50000);
1636 my $p2 = $tp2 + ($ref2->{priority
} // 50000);
1638 return $p1 <=> $p2 if $p1 != $p2;
1642 next if $printed->{$iface};
1643 my $d = $ifaces->{$iface};
1644 my $pri = $d->{priority
} // 0;
1645 if (@options && $options[0]->[0] < $pri) {
1647 $raw .= (shift @options)->[1] . "\n";
1648 } while (@options && $options[0]->[0] < $pri);
1652 $printed->{$iface} = 1;
1653 if ($d->{autostart
}) {
1654 if ($d->{type
} eq 'OVSBridge' && !$ifupdown2) {
1655 # cannot use 'auto' for OVS, would add race with systemd ifup@.service
1656 $raw .= "allow-ovs $iface\n";
1658 $raw .= "auto $iface\n";
1662 my $i = 0; # some options should be printed only once
1663 $raw .= __interface_to_string
($iface, $d, $_, !$i++, $ifupdown2) foreach @{$d->{families
}};
1666 $raw .= $_->[1] . "\n" foreach @options;
1670 register_file
('interfaces', "/etc/network/interfaces",
1671 \
&read_etc_network_interfaces
,
1672 \
&write_etc_network_interfaces
);
1675 sub read_iscsi_initiatorname
{
1676 my ($filename, $fd) = @_;
1678 while (defined(my $line = <$fd>)) {
1679 if ($line =~ m/^InitiatorName=(\S+)$/) {
1687 register_file
('initiatorname', "/etc/iscsi/initiatorname.iscsi",
1688 \
&read_iscsi_initiatorname
);
1691 my ($filename, $fd) = @_;
1695 my $raw = defined($fd) ?
<$fd> : '';
1700 my @tokens = split(/\s+/, $raw);
1705 while (defined(my $tok = shift @tokens)) {
1707 $machine = shift @tokens if $tok eq 'machine';
1709 $data->{$machine} = {} if !$data->{$machine};
1711 $data->{$machine}->{login
} = shift @tokens if $tok eq 'login';
1712 $data->{$machine}->{password
} = shift @tokens if $tok eq 'password';
1718 my $format_apt_auth_data = sub {
1723 foreach my $machine (sort keys %$data) {
1724 my $d = $data->{$machine};
1725 $raw .= "machine $machine\n";
1726 $raw .= " login $d->{login}\n" if $d->{login
};
1727 $raw .= " password $d->{password}\n" if $d->{password
};
1734 sub write_apt_auth
{
1735 my ($filename, $fh, $data) = @_;
1737 my $raw = &$format_apt_auth_data($data);
1739 die "write failed: $!" unless print $fh "$raw\n";
1744 sub update_apt_auth
{
1745 my ($filename, $fh, $data) = @_;
1747 my $orig = read_apt_auth
($filename, $fh);
1749 foreach my $machine (keys %$data) {
1750 $orig->{$machine} = $data->{$machine};
1753 return &$format_apt_auth_data($orig);
1756 register_file
('apt-auth', "/etc/apt/auth.conf",
1757 \
&read_apt_auth
, \
&write_apt_auth
,
1758 \
&update_apt_auth
, perm
=> 0640);