3 # todo: maybe we do not need update_file() ?
13 use Fcntl
qw(:DEFAULT :flock);
15 use PVE
::Exception
qw(raise_param_exc);
17 use Storable
qw(dclone);
22 our @EXPORT_OK = qw(read_file write_file register_file);
31 '/etc/network/interfaces' => '/etc/network/interfaces.new',
34 # to enable cached operation, you need to call 'inotify_init'
35 # inotify handles are a limited resource, so use with care (only
36 # enable the cache if you really need it)
38 # Note: please close the inotify handle after you fork
40 sub ccache_default_writer
{
41 my ($filename, $data) = @_;
43 die "undefined config writer for '$filename' :ERROR";
46 sub ccache_default_parser
{
47 my ($filename, $srcfd) = @_;
49 die "undefined config reader for '$filename' :ERROR";
52 sub ccache_compute_diff
{
53 my ($filename, $shadow) = @_;
57 open (TMP
, "diff -b -N -u '$filename' '$shadow'|");
59 while (my $line = <TMP
>) {
65 $diff = undef if !$diff;
73 foreach my $uid (keys %$ccacheregex) {
74 my $ccinfo = $ccacheregex->{$uid};
75 my $dir = $ccinfo->{dir
};
76 my $regex = $ccinfo->{regex
};
77 if ($filename =~ m
|^$dir/+$regex$|) {
78 if (!$ccache->{$filename}) {
80 while (my ($k, $v) = each %$ccinfo) {
83 $ccache->{$filename} = $cp;
85 return ($ccache->{$filename}, $filename);
89 $filename = $ccachemap->{$filename} if defined ($ccachemap->{$filename});
91 die "file '$filename' not added :ERROR" if !defined ($ccache->{$filename});
93 return ($ccache->{$filename}, $filename);
97 my ($fileid, $data, $full) = @_;
99 my ($ccinfo, $filename) = ccache_info
($fileid);
101 my $writer = $ccinfo->{writer
};
103 my $realname = $filename;
106 if ($shadow = $shadowfiles->{$filename}) {
110 my $perm = $ccinfo->{perm
} || 0644;
112 my $tmpname = "$realname.tmp.$$";
116 my $fh = IO
::File-
>new($tmpname, O_WRONLY
|O_CREAT
, $perm);
117 die "unable to open file '$tmpname' - $!\n" if !$fh;
119 $res = &$writer($filename, $fh, $data);
121 die "closing file '$tmpname' failed - $!\n" unless close $fh;
125 $ccinfo->{version
} = undef;
132 if (!rename($tmpname, $realname)) {
133 my $msg = "close (rename) atomic file '$filename' failed: $!\n";
139 if ($shadow && $full) {
140 $diff = ccache_compute_diff
($filename, $shadow);
144 return { data
=> $res, changes
=> $diff };
151 my ($fileid, $data, @args) = @_;
153 my ($ccinfo, $filename) = ccache_info
($fileid);
155 my $update = $ccinfo->{update
};
157 die "unable to update/merge data" if !$update;
159 my $lkfn = "$filename.lock";
167 $fd = IO
::File-
>new ($filename, "r");
169 my $new = &$update($filename, $fd, $data, @args);
172 PVE
::Tools
::file_set_contents
($filename, $new, $ccinfo->{perm
});
178 PVE
::Tools
::lock_file
($lkfn, $timeout, $code);
181 close($fd) if defined($fd);
188 sub discard_changes
{
189 my ($fileid, $full) = @_;
191 my ($ccinfo, $filename) = ccache_info
($fileid);
193 if (my $copy = $shadowfiles->{$filename}) {
197 return read_file
($filename, $full);
201 my ($fileid, $full) = @_;
205 my ($ccinfo, $filename) = ccache_info
($fileid);
207 $parser = $ccinfo->{parser
};
212 poll
() if $inotify; # read new inotify events
214 $versions->{$filename} = 0 if !defined ($versions->{$filename});
216 my $cver = $versions->{$filename};
218 if (my $copy = $shadowfiles->{$filename}) {
219 if ($fd = IO
::File-
>new ($copy, "r")) {
222 $fd = IO
::File-
>new ($filename, "r");
225 $fd = IO
::File-
>new ($filename, "r");
228 my $acp = $ccinfo->{always_call_parser
};
231 $ccinfo->{version
} = undef;
232 $ccinfo->{data
} = undef;
233 $ccinfo->{diff
} = undef;
234 return undef if !$acp;
237 my $noclone = $ccinfo->{noclone
};
240 if (!$ccinfo->{nocache
} &&
241 $inotify && $versions->{$filename} &&
242 defined ($ccinfo->{data
}) &&
243 defined ($ccinfo->{version
}) &&
244 ($ccinfo->{readonce
} ||
245 ($ccinfo->{version
} == $versions->{$filename}))) {
248 if (!$noclone && ref ($ccinfo->{data
})) {
249 $ret->{data
} = dclone
($ccinfo->{data
});
251 $ret->{data
} = $ccinfo->{data
};
253 $ret->{changes
} = $ccinfo->{diff
};
255 return $full ?
$ret : $ret->{data
};
261 $diff = ccache_compute_diff
($filename, $shadow);
264 my $res = &$parser($filename, $fd);
266 if (!$ccinfo->{nocache
}) {
267 $ccinfo->{version
} = $cver;
270 # we cache data with references, so we always need to
271 # dclone this data. Else the original data may get
273 $ccinfo->{data
} = $res;
276 $ccinfo->{diff
} = $diff;
279 if (!$noclone && ref ($ccinfo->{data
})) {
280 $ret->{data
} = dclone
($ccinfo->{data
});
282 $ret->{data
} = $ccinfo->{data
};
284 $ret->{changes
} = $ccinfo->{diff
};
286 return $full ?
$ret : $ret->{data
};
289 sub parse_ccache_options
{
290 my ($ccinfo, %options) = @_;
292 foreach my $opt (keys %options) {
293 my $v = $options{$opt};
294 if ($opt eq 'readonce') {
295 $ccinfo->{$opt} = $v;
296 } elsif ($opt eq 'nocache') {
297 $ccinfo->{$opt} = $v;
298 } elsif ($opt eq 'shadow') {
299 $ccinfo->{$opt} = $v;
300 } elsif ($opt eq 'perm') {
301 $ccinfo->{$opt} = $v;
302 } elsif ($opt eq 'noclone') {
303 # noclone flag for large read-only data chunks like aplinfo
304 $ccinfo->{$opt} = $v;
305 } elsif ($opt eq 'always_call_parser') {
306 # when set, we call parser even when the file does not exists.
307 # this allows the parser to return some default
308 $ccinfo->{$opt} = $v;
310 die "internal error - unsupported option '$opt'";
316 my ($id, $filename, $parser, $writer, $update, %options) = @_;
318 die "can't register file after initify_init" if $inotify;
320 die "file '$filename' already added :ERROR" if defined ($ccache->{$filename});
321 die "ID '$id' already used :ERROR" if defined ($ccachemap->{$id});
326 $ccinfo->{parser
} = $parser || \
&ccache_default_parser
;
327 $ccinfo->{writer
} = $writer || \
&ccache_default_writer
;
328 $ccinfo->{update
} = $update;
330 parse_ccache_options
($ccinfo, %options);
332 if ($options{shadow
}) {
333 $shadowfiles->{$filename} = $options{shadow
};
336 $ccachemap->{$id} = $filename;
337 $ccache->{$filename} = $ccinfo;
341 my ($dir, $regex, $parser, $writer, $update, %options) = @_;
343 die "can't register regex after initify_init" if $inotify;
345 my $uid = "$dir/$regex";
346 die "regular expression '$uid' already added :ERROR" if defined ($ccacheregex->{$uid});
350 $ccinfo->{dir
} = $dir;
351 $ccinfo->{regex
} = $regex;
352 $ccinfo->{parser
} = $parser || \
&ccache_default_parser
;
353 $ccinfo->{writer
} = $writer || \
&ccache_default_writer
;
354 $ccinfo->{update
} = $update;
356 parse_ccache_options
($ccinfo, %options);
358 $ccacheregex->{$uid} = $ccinfo;
364 if ($inotify_pid != $$) {
365 syslog
('err', "got inotify poll request in wrong process - disabling inotify");
368 1 while $inotify && $inotify->poll;
373 foreach my $filename (keys %$ccache) {
374 $ccache->{$filename}->{version
} = undef;
375 $ccache->{$filename}->{data
} = undef;
376 $ccache->{$filename}->{diff
} = undef;
386 die "only one inotify instance allowed" if $inotify;
388 $inotify = Linux
::Inotify2-
>new()
389 || die "Unable to create new inotify object: $!";
391 $inotify->blocking (0);
396 foreach my $fn (keys %$ccache) {
397 my $dir = dirname
($fn);
398 my $base = basename
($fn);
400 $dirhash->{$dir}->{$base} = $fn;
402 if (my $sf = $shadowfiles->{$fn}) {
403 $base = basename
($sf);
404 $dir = dirname
($sf);
405 $dirhash->{$dir}->{$base} = $fn; # change version of original file!
409 foreach my $uid (keys %$ccacheregex) {
410 my $ccinfo = $ccacheregex->{$uid};
411 $dirhash->{$ccinfo->{dir
}}->{_regex
} = 1;
416 foreach my $dir (keys %$dirhash) {
418 my $evlist = IN_MODIFY
|IN_ATTRIB
|IN_MOVED_FROM
|IN_MOVED_TO
|IN_DELETE
|IN_CREATE
;
419 $inotify->watch ($dir, $evlist, sub {
423 if ($inotify_pid != $$) {
424 syslog
('err', "got inotify event in wrong process");
427 if ($e->IN_ISDIR || !$name) {
431 if ($e->IN_Q_OVERFLOW) {
432 syslog
('info', "got inotify overflow - flushing cache");
437 if ($e->IN_UNMOUNT) {
438 syslog
('err', "got 'unmount' event on '$name' - disabling inotify");
441 if ($e->IN_IGNORED) {
442 syslog
('err', "got 'ignored' event on '$name' - disabling inotify");
446 if ($dirhash->{$dir}->{_regex
}) {
447 foreach my $uid (keys %$ccacheregex) {
448 my $ccinfo = $ccacheregex->{$uid};
449 next if $dir ne $ccinfo->{dir
};
450 my $regex = $ccinfo->{regex
};
451 if ($regex && ($name =~ m
|^$regex$|)) {
453 my $fn = "$dir/$name";
455 #print "VERSION:$fn:$versions->{$fn}\n";
458 } elsif (my $fn = $dirhash->{$dir}->{$name}) {
461 #print "VERSION:$fn:$versions->{$fn}\n";
466 foreach my $dir (keys %$dirhash) {
467 foreach my $name (keys %{$dirhash->{$dir}}) {
468 if ($name eq '_regex') {
469 foreach my $uid (keys %$ccacheregex) {
470 my $ccinfo = $ccacheregex->{$uid};
471 next if $dir ne $ccinfo->{dir
};
472 my $re = $ccinfo->{regex
};
473 if (my $fd = IO
::Dir-
>new ($dir)) {
474 while (defined(my $de = $fd->read)) {
475 if ($de =~ m/^$re$/) {
477 $versions->{$fn}++; # init with version
478 #print "init:$fn:$versions->{$fn}\n";
484 my $fn = $dirhash->{$dir}->{$name};
485 $versions->{$fn}++; # init with version
486 #print "init:$fn:$versions->{$fn}\n";
496 return $cached_nodename if $cached_nodename;
498 my ($sysname, $nodename) = POSIX
::uname
();
500 $nodename =~ s/\..*$//; # strip domain part, if any
502 die "unable to read node name\n" if !$nodename;
504 $cached_nodename = $nodename;
506 return $cached_nodename;
509 sub read_etc_hostname
{
510 my ($filename, $fd) = @_;
512 my $hostname = <$fd>;
516 $hostname =~ s/\..*$//; # strip domain part, if any
521 sub write_etc_hostname
{
522 my ($filename, $fh, $hostname) = @_;
524 die "write failed: $!" unless print $fh "$hostname\n";
529 register_file
('hostname', "/etc/hostname",
531 \
&write_etc_hostname
);
533 sub read_etc_resolv_conf
{
534 my ($filename, $fh) = @_;
539 while (my $line = <$fh>) {
541 if ($line =~ m/^(search|domain)\s+(\S+)\s*/) {
543 } elsif ($line =~ m/^nameserver\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*/) {
546 $res->{"dns$nscount"} = $1;
554 sub update_etc_resolv_conf
{
555 my ($filename, $fh, $resolv, @args) = @_;
559 $data = "search $resolv->{search}\n"
560 if $resolv->{search
};
563 foreach my $k ("dns1", "dns2", "dns3") {
564 my $ns = $resolv->{$k};
565 if ($ns && $ns ne '0.0.0.0' && !$written->{$ns}) {
567 $data .= "nameserver $ns\n";
571 while (my $line = <$fh>) {
572 next if $line =~ m/^(search|domain|nameserver)\s+/;
579 register_file
('resolvconf', "/etc/resolv.conf",
580 \
&read_etc_resolv_conf
, undef,
581 \
&update_etc_resolv_conf
);
583 sub read_etc_timezone
{
584 my ($filename, $fd) = @_;
586 my $timezone = <$fd>;
593 sub write_etc_timezone
{
594 my ($filename, $fh, $timezone) = @_;
596 my $tzinfo = "/usr/share/zoneinfo/$timezone";
598 raise_param_exc
({ 'timezone' => "No such timezone" })
601 ($timezone) = $timezone =~ m/^(.*)$/; # untaint
603 print $fh "$timezone\n";
605 unlink ("/etc/localtime");
606 symlink ("/usr/share/zoneinfo/$timezone", "/etc/localtime");
610 register_file
('timezone', "/etc/timezone",
612 \
&write_etc_timezone
);
614 sub read_active_workers
{
615 my ($filename, $fh) = @_;
620 while (defined (my $line = <$fh>)) {
621 if ($line =~ m/^(\S+)\s(0|1)(\s([0-9A-Za-z]{8})(\s(\s*\S.*))?)?$/) {
626 if ((my $task = PVE
::Tools
::upid_decode
($upid, 1))) {
627 $task->{upid
} = $upid;
628 $task->{saved
} = $saved;
629 $task->{endtime
} = hex($endtime) if $endtime;
630 $task->{status
} = $status if $status;
634 warn "unable to parse line: $line";
642 sub write_active_workers
{
643 my ($filename, $fh, $tasklist) = @_;
646 foreach my $task (@$tasklist) {
647 my $upid = $task->{upid
};
648 my $saved = $task->{saved
} ?
1 : 0;
649 if ($task->{endtime
}) {
650 if ($task->{status
}) {
651 $raw .= sprintf("$upid $saved %08X $task->{status}\n", $task->{endtime
});
653 $raw .= sprintf("$upid $saved %08X\n", $task->{endtime
});
656 $raw .= "$upid $saved\n";
660 PVE
::Tools
::safe_print
($filename, $fh, $raw) if $raw;
663 register_file
('active', "/var/log/pve/tasks/active",
664 \
&read_active_workers
,
665 \
&write_active_workers
);
668 my $bond_modes = { 'balance-rr' => 0,
669 'active-backup' => 1,
677 my $ovs_bond_modes = {
678 'active-backup' => 1,
683 #sub get_bond_modes {
684 # return $bond_modes;
687 my $parse_ovs_option = sub {
691 foreach my $kv (split (/\s+/, $data || '')) {
692 my ($k, $v) = split('=', $kv, 2);
693 $opts->{$k} = $v if $k && $v;
698 my $set_ovs_option = sub {
699 my ($d, %params) = @_;
701 my $opts = &$parse_ovs_option($d->{ovs_options
});
703 foreach my $k (keys %params) {
713 foreach my $k (keys %$opts) {
714 push @$res, "$k=$opts->{$k}";
717 if (my $new = join(' ', @$res)) {
718 $d->{ovs_options
} = $new;
719 return $d->{ovs_options
};
721 delete $d->{ovs_options
};
726 my $extract_ovs_option = sub {
729 my $opts = &$parse_ovs_option($d->{ovs_options
});
731 my $v = delete $opts->{$name};
734 foreach my $k (keys %$opts) {
735 push @$res, "$k=$opts->{$k}";
738 if (my $new = join(' ', @$res)) {
739 $d->{ovs_options
} = $new;
741 delete $d->{ovs_options
};
747 sub read_etc_network_interfaces
{
748 my ($filename, $fh) = @_;
754 if (my $fd2 = IO
::File-
>new("/proc/net/dev", "r")) {
755 while (defined ($line = <$fd2>)) {
756 if ($line =~ m/^\s*(eth\d+):.*/) {
757 $ifaces->{$1}->{exists} = 1;
763 # we try to keep order inside the file
764 my $priority = 2; # 1 is reserved for lo
766 # always add the vmbr0 bridge device
767 $ifaces->{vmbr0
}->{exists} = 1;
771 while (defined ($line = <$fh>)) {
773 next if $line =~ m/^#/;
775 if ($line =~ m/^auto\s+(.*)$/) {
776 my @aa = split (/\s+/, $1);
778 foreach my $a (@aa) {
779 $ifaces->{$a}->{autostart
} = 1;
782 } elsif ($line =~ m/^iface\s+(\S+)\s+inet\s+(\S+)\s*$/) {
784 $ifaces->{$i}->{method} = $2;
785 $ifaces->{$i}->{priority
} = $priority++;
787 my $d = $ifaces->{$i};
788 while (defined ($line = <$fh>)) {
789 if ($line =~ m/^\s*#(.*)\s*$/) {
790 # NOTE: we use 'comments' instead of 'comment' to
791 # avoid automatic utf8 conversion
792 $d->{comments
} = '' if !$d->{comments
};
793 $d->{comments
} .= "$1\n";
794 } elsif ($line =~ m/^\s+((\S+)\s+(.+))$/) {
796 my ($id, $value) = ($2, $3);
797 if (($id eq 'address') || ($id eq 'netmask') || ($id eq 'broadcast')) {
799 } elsif ($id eq 'gateway') {
802 } elsif ($id eq 'ovs_type' || $id eq 'ovs_options'|| $id eq 'ovs_bridge' ||
803 $id eq 'ovs_bonds' || $id eq 'ovs_ports') {
805 } elsif ($id eq 'slaves' || $id eq 'bridge_ports') {
807 foreach my $p (split (/\s+/, $value)) {
808 next if $p eq 'none';
811 my $str = join (' ', sort keys %{$devs});
812 $d->{$id} = $str || '';
813 } elsif ($id eq 'bridge_stp') {
814 if ($value =~ m/^\s*(on|yes)\s*$/i) {
819 } elsif ($id eq 'bridge_fd') {
821 } elsif ($id eq 'bond_miimon') {
823 } elsif ($id eq 'bond_mode') {
825 foreach my $bm (keys %$bond_modes) {
826 my $id = $bond_modes->{$bm};
834 push @{$d->{options
}}, $option;
845 $ifaces->{vmbr0
}->{gateway
} = '';
848 if (!$ifaces->{lo
}) {
849 $ifaces->{lo
}->{priority
} = 1;
850 $ifaces->{lo
}->{method} = 'loopback';
851 $ifaces->{lo
}->{type
} = 'loopback';
852 $ifaces->{lo
}->{autostart
} = 1;
855 foreach my $iface (keys %$ifaces) {
856 my $d = $ifaces->{$iface};
857 if ($iface =~ m/^bond\d+$/) {
858 if (!$d->{ovs_type
}) {
860 } elsif ($d->{ovs_type
} eq 'OVSBond') {
861 $d->{type
} = $d->{ovs_type
};
862 # translate: ovs_options => bond_mode
863 $d->{'bond_mode'} = &$extract_ovs_option($d, 'bond_mode');
865 $d->{type
} = 'unknown';
867 } elsif ($iface =~ m/^vmbr\d+$/) {
868 if (!$d->{ovs_type
}) {
869 $d->{type
} = 'bridge';
871 if (!defined ($d->{bridge_fd
})) {
874 if (!defined ($d->{bridge_stp
})) {
875 $d->{bridge_stp
} = 'off';
877 } elsif ($d->{ovs_type
} eq 'OVSBridge') {
878 $d->{type
} = $d->{ovs_type
};
880 $d->{type
} = 'unknown';
882 } elsif ($iface =~ m/^(\S+):\d+$/) {
883 $d->{type
} = 'alias';
884 if (defined ($ifaces->{$1})) {
885 $d->{exists} = $ifaces->{$1}->{exists};
887 $ifaces->{$1}->{exists} = 0;
890 } elsif ($iface =~ m/^eth\d+$/) {
891 if (!$d->{ovs_type
}) {
893 } elsif ($d->{ovs_type
} eq 'OVSPort') {
894 $d->{type
} = $d->{ovs_type
};
896 $d->{type
} = 'unknown';
898 } elsif ($iface =~ m/^lo$/) {
899 $d->{type
} = 'loopback';
901 if (!$d->{ovs_type
}) {
902 $d->{type
} = 'unknown';
903 } elsif ($d->{ovs_type
} eq 'OVSIntPort') {
904 $d->{type
} = $d->{ovs_type
};
908 $d->{method} = 'manual' if !$d->{method};
911 if (my $fd2 = IO
::File-
>new("/proc/net/if_inet6", "r")) {
912 while (defined ($line = <$fd2>)) {
913 if ($line =~ m/^[a-f0-9]{32}\s+[a-f0-9]{2}\s+[a-f0-9]{2}\s+[a-f0-9]{2}\s+[a-f0-9]{2}\s+(\S+)$/) {
914 $ifaces->{$1}->{active
} = 1 if defined($ifaces->{$1});
923 sub __interface_to_string
{
924 my ($iface, $d) = @_;
926 return '' if !($d && $d->{method});
930 $raw .= "iface $iface inet $d->{method}\n";
931 $raw .= "\taddress $d->{address}\n" if $d->{address
};
932 $raw .= "\tnetmask $d->{netmask}\n" if $d->{netmask
};
933 $raw .= "\tgateway $d->{gateway}\n" if $d->{gateway
};
934 $raw .= "\tbroadcast $d->{broadcast}\n" if $d->{broadcast
};
936 my $done = { type
=> 1, priority
=> 1, method => 1, active
=> 1, exists => 1,
937 comments
=> 1, autostart
=> 1, options
=> 1,
938 address
=> 1, netmask
=> 1, gateway
=> 1, broadcast
=> 1 };
940 if ($d->{type
} eq 'bridge') {
942 my $ports = $d->{bridge_ports
} || 'none';
943 $raw .= "\tbridge_ports $ports\n";
944 $done->{bridge_ports
} = 1;
946 my $v = defined($d->{bridge_stp
}) ?
$d->{bridge_stp
} : 'off';
947 $raw .= "\tbridge_stp $v\n";
948 $done->{bridge_stp
} = 1;
950 $v = defined($d->{bridge_fd
}) ?
$d->{bridge_fd
} : 0;
951 $raw .= "\tbridge_fd $v\n";
952 $done->{bridge_fd
} = 1;
954 } elsif ($d->{type
} eq 'bond') {
956 my $slaves = $d->{slaves
} || 'none';
957 $raw .= "\tslaves $slaves\n";
960 my $v = defined ($d->{'bond_miimon'}) ?
$d->{'bond_miimon'} : 100;
961 $raw .= "\tbond_miimon $v\n";
962 $done->{'bond_miimon'} = 1;
964 $v = defined ($d->{'bond_mode'}) ?
$d->{'bond_mode'} : 'balance-rr';
965 $raw .= "\tbond_mode $v\n";
966 $done->{'bond_mode'} = 1;
968 } elsif ($d->{type
} eq 'OVSBridge') {
970 $raw .= "\tovs_type $d->{type}\n";
971 $done->{ovs_type
} = 1;
973 $raw .= "\tovs_ports $d->{ovs_ports}\n" if $d->{ovs_ports
};
974 $done->{ovs_ports
} = 1;
976 } elsif ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' ||
977 $d->{type
} eq 'OVSBond') {
979 $d->{autostart
} = 0; # started by the bridge
981 if ($d->{type
} eq 'OVSBond') {
983 $d->{bond_mode
} = 'active-backup' if !$d->{bond_mode
};
985 $ovs_bond_modes->{$d->{bond_mode
}} ||
986 die "OVS does not support bond mode '$d->{bond_mode}\n";
988 &$set_ovs_option($d, bond_mode
=> $d->{bond_mode
});
989 $done->{bond_mode
} = 1;
991 $raw .= "\tovs_bonds $d->{ovs_bonds}\n" if $d->{ovs_bonds
};
992 $done->{ovs_bonds
} = 1;
995 if ($d->{ovs_bridge
}) {
996 $raw = "allow-$d->{ovs_bridge} $iface\n$raw";
999 $raw .= "\tovs_type $d->{type}\n";
1000 $done->{ovs_type
} = 1;
1002 if ($d->{ovs_bridge
}) {
1003 $raw .= "\tovs_bridge $d->{ovs_bridge}\n";
1004 $done->{ovs_bridge
} = 1;
1006 # fixme: use Data::Dumper; print Dumper($d);
1009 # print other settings
1010 foreach my $k (keys %$d) {
1011 next if $done->{$k};
1013 $raw .= "\t$k $d->{$k}\n";
1016 foreach my $option (@{$d->{options
}}) {
1017 $raw .= "\t$option\n";
1021 my $comments = $d->{comments
} || '';
1022 foreach my $cl (split(/\n/, $comments)) {
1026 if ($d->{autostart
}) {
1027 $raw = "auto $iface\n$raw";
1035 sub write_etc_network_interfaces
{
1036 my ($filename, $fh, $ifaces) = @_;
1038 my $used_ports = {};
1040 foreach my $iface (keys %$ifaces) {
1041 my $d = $ifaces->{$iface};
1044 foreach my $k (qw(bridge_ports ovs_ports slaves ovs_bonds)) {
1045 $ports .= " $d->{$k}" if $d->{$k};
1048 foreach my $p (PVE
::Tools
::split_list
($ports)) {
1049 die "port '$p' is already used on interface '$used_ports->{$p}'\n"
1050 if $used_ports->{$p} && $used_ports->{$p} ne $iface;
1051 $used_ports->{$p} = $iface;
1055 # delete unused OVS ports
1056 foreach my $iface (keys %$ifaces) {
1057 my $d = $ifaces->{$iface};
1058 if ($d->{type
} eq 'OVSPort' || $d->{type
} eq 'OVSIntPort' ||
1059 $d->{type
} eq 'OVSBond') {
1060 my $brname = $used_ports->{$iface};
1061 if (!$brname || !$ifaces->{$brname}) {
1062 delete $ifaces->{$iface};
1065 my $bd = $ifaces->{$brname};
1066 if ($bd->{type
} ne 'OVSBridge') {
1067 delete $ifaces->{$iface};
1073 # create OVS bridge ports
1074 foreach my $iface (keys %$ifaces) {
1075 my $d = $ifaces->{$iface};
1076 if ($d->{type
} eq 'OVSBridge' && $d->{ovs_ports
}) {
1077 foreach my $p (split (/\s+/, $d->{ovs_ports
})) {
1078 my $n = $ifaces->{$p};
1079 die "OVS bridge '$iface' - unable to find port '$p'\n"
1081 if ($n->{type
} eq 'eth') {
1082 $n->{type
} = 'OVSPort';
1083 $n->{ovs_bridge
} = $iface;
1084 } elsif ($n->{type
} eq 'OVSBond' || $n->{type
} eq 'OVSPort' ||
1085 $n->{type
} eq 'OVSIntPort') {
1086 $n->{ovs_bridge
} = $iface;
1088 die "interface '$p' is not defined as OVS port/bond\n";
1094 # check OVS bond ports
1095 foreach my $iface (keys %$ifaces) {
1096 my $d = $ifaces->{$iface};
1097 if ($d->{type
} eq 'OVSBond' && $d->{ovs_bonds
}) {
1098 foreach my $p (split (/\s+/, $d->{ovs_bonds
})) {
1099 my $n = $ifaces->{$p};
1100 die "OVS bond '$iface' - unable to find slave '$p'\n"
1102 die "OVS bond '$iface' - wrong interface type on slave '$p' " .
1103 "('$n->{type}' != 'eth')\n" if $n->{type
} ne 'eth';
1108 my $raw = "# network interface settings\n";
1112 my $if_type_hash = {
1120 my $lookup_type_prio = sub {
1124 if ($iface =~ m/^(\S+):\d+$/) {
1130 if ($iface eq 'lo') {
1131 $pri = $if_type_hash->{loopback
};
1132 } elsif ($iface =~ m/^eth\d+$/) {
1133 $pri = $if_type_hash->{eth
} + $alias;
1134 } elsif ($iface =~ m/^bond\d+$/) {
1135 $pri = $if_type_hash->{bond
} + $alias;
1136 } elsif ($iface =~ m/^vmbr\d+$/) {
1137 $pri = $if_type_hash->{bridge
} + $alias;
1140 return $pri || ($if_type_hash->{unknown
} + $alias);
1143 foreach my $iface (sort {
1144 my $ref1 = $ifaces->{$a};
1145 my $ref2 = $ifaces->{$b};
1146 my $p1 = &$lookup_type_prio($a);
1147 my $p2 = &$lookup_type_prio($b);
1149 return $p1 <=> $p2 if $p1 != $p2;
1151 $p1 = $ref1->{priority
} || 100000;
1152 $p2 = $ref2->{priority
} || 100000;
1154 return $p1 <=> $p2 if $p1 != $p2;
1159 my $d = $ifaces->{$iface};
1161 next if $printed->{$iface};
1163 $printed->{$iface} = 1;
1164 $raw .= __interface_to_string
($iface, $d);
1167 PVE
::Tools
::safe_print
($filename, $fh, $raw);
1170 register_file
('interfaces', "/etc/network/interfaces",
1171 \
&read_etc_network_interfaces
,
1172 \
&write_etc_network_interfaces
);
1175 sub read_iscsi_initiatorname
{
1176 my ($filename, $fd) = @_;
1178 while (defined(my $line = <$fd>)) {
1179 if ($line =~ m/^InitiatorName=(\S+)$/) {
1187 register_file
('initiatorname', "/etc/iscsi/initiatorname.iscsi",
1188 \
&read_iscsi_initiatorname
);
1191 my ($filename, $fd) = @_;
1195 my $raw = defined($fd) ?
<$fd> : '';
1200 my @tokens = split(/\s+/, $raw);
1205 while (defined(my $tok = shift @tokens)) {
1207 $machine = shift @tokens if $tok eq 'machine';
1209 $data->{$machine} = {} if !$data->{$machine};
1211 $data->{$machine}->{login
} = shift @tokens if $tok eq 'login';
1212 $data->{$machine}->{password
} = shift @tokens if $tok eq 'password';
1218 my $format_apt_auth_data = sub {
1223 foreach my $machine (sort keys %$data) {
1224 my $d = $data->{$machine};
1225 $raw .= "machine $machine\n";
1226 $raw .= " login $d->{login}\n" if $d->{login
};
1227 $raw .= " password $d->{password}\n" if $d->{password
};
1234 sub write_apt_auth
{
1235 my ($filename, $fh, $data) = @_;
1237 my $raw = &$format_apt_auth_data($data);
1239 die "write failed: $!" unless print $fh "$raw\n";
1244 sub update_apt_auth
{
1245 my ($filename, $fh, $data) = @_;
1247 my $orig = read_apt_auth
($filename, $fh);
1249 foreach my $machine (keys %$data) {
1250 $orig->{$machine} = $data->{$machine};
1253 return &$format_apt_auth_data($orig);
1256 register_file
('apt-auth', "/etc/apt/auth.conf",
1257 \
&read_apt_auth
, \
&write_apt_auth
,
1258 \
&update_apt_auth
, perm
=> 0640);