4 use PVE
::Cluster
qw(cfs_read_file);
6 use PVE
::Network
::Network
::Plugin
;
7 use PVE
::Network
::Network
::VnetPlugin
;
8 use PVE
::Network
::Network
::VlanPlugin
;
9 use PVE
::Network
::Network
::VxlanMulticastPlugin
;
11 PVE
::Network
::Network
::VnetPlugin-
>register();
12 PVE
::Network
::Network
::VlanPlugin-
>register();
13 PVE
::Network
::Network
::VxlanMulticastPlugin-
>register();
14 PVE
::Network
::Network
::Plugin-
>init();
17 my $rawconfig = generate_network_config
();
19 verify_merged_config
($rawconfig);
20 write_final_config
($rawconfig);
22 sub generate_network_config
{
24 #only support ifupdown2
25 die "you need ifupdown2 to reload networking\n" if !-e
'/usr/share/ifupdown2';
27 #read main config for physical interfaces
28 my $current_config_file = "/etc/network/interfaces";
29 my $fh = IO
::File-
>new($current_config_file);
30 my $interfaces_config = PVE
::INotify
::read_etc_network_interfaces
(1,$fh);
35 foreach my $id (keys %{$interfaces_config->{ifaces
}}) {
36 my $interface = $interfaces_config->{ifaces
}->{$id};
37 if (my $uplink = $interface->{'uplink-id'}) {
38 die "uplink-id $uplink is already defined on $uplinks->{$uplink}" if $uplinks->{$uplink};
39 $uplinks->{$interface->{'uplink-id'}} = $id;
43 my $network_cfg = PVE
::Cluster
::cfs_read_file
('networks.cfg');
45 my $transport_cfg = undef;
47 foreach my $id (keys %{$network_cfg->{ids
}}) {
48 if ($network_cfg->{ids
}->{$id}->{type
} eq 'vnet') {
49 $vnet_cfg->{ids
}->{$id} = $network_cfg->{ids
}->{$id};
51 $transport_cfg->{ids
}->{$id} = $network_cfg->{ids
}->{$id};
55 #generate configuration
57 foreach my $id (keys %{$vnet_cfg->{ids
}}) {
58 my $vnet = $vnet_cfg->{ids
}->{$id};
59 my $zone = $vnet->{transportzone
};
61 die "zone $zone don't exist" if !$zone;
62 my $plugin_config = $transport_cfg->{ids
}->{$zone};
63 die "zone $zone don't exist" if !defined($plugin_config);
64 my $plugin = PVE
::Network
::Network
::Plugin-
>lookup($plugin_config->{type
});
65 $rawconfig .= $plugin->generate_network_config($plugin_config, $zone, $id, $vnet, $uplinks);
71 #implement reload (split and reuse code from API2/Network.pm for bridge delete verification)
73 sub verify_merged_config
{
76 #merge main network intefaces and vnet file for possible conflict verification
77 my $tmp_merged_network_interfaces = "/var/tmp/pve-merged_network_interfaces";
78 copy
("/etc/network/interfaces", $tmp_merged_network_interfaces);
80 my $writefh = IO
::File-
>new($tmp_merged_network_interfaces, '>>');
81 print $writefh $rawconfig;
84 my $readfh = IO
::File-
>new($tmp_merged_network_interfaces);
85 my $merged_interfaces_config = PVE
::INotify
::read_etc_network_interfaces
(1,$readfh);
87 unlink $tmp_merged_network_interfaces;
88 PVE
::INotify
::__write_etc_network_interfaces
($merged_interfaces_config, 1);
92 sub write_final_config
{
94 #now write final separate filename
95 my $tmp_file = "/var/tmp/pve-vnet.cfg";
97 my $vnet_interfaces_file = "/etc/network/interfaces.d/vnet";
99 my $writefh = IO
::File-
>new($vnet_interfaces_file,">");
100 print $writefh $rawconfig;