]>
Commit | Line | Data |
---|---|---|
8df9fab5 AD |
1 | use strict; |
2 | use warnings; | |
3 | use File::Copy; | |
4 | use PVE::Cluster qw(cfs_read_file); | |
5 | ||
6 | use PVE::Network::Vnet; | |
7 | use PVE::Network::Plugin; | |
8 | use PVE::Network::VlanPlugin; | |
9 | use PVE::Network::VxlanMulticastPlugin; | |
10 | ||
11 | PVE::Network::VlanPlugin->register(); | |
12 | PVE::Network::VxlanMulticastPlugin->register(); | |
13 | PVE::Network::Plugin->init(); | |
14 | ||
15 | ||
16 | my $rawconfig = generate_network_config(); | |
02abc721 | 17 | print $rawconfig; |
8df9fab5 AD |
18 | verify_merged_config($rawconfig); |
19 | write_final_config($rawconfig); | |
8df9fab5 AD |
20 | |
21 | sub generate_network_config { | |
22 | ||
23 | #only support ifupdown2 | |
24 | die "you need ifupdown2 to reload networking\n" if !-e '/usr/share/ifupdown2'; | |
25 | ||
26 | #read main config for physical interfaces | |
27 | my $current_config_file = "/etc/network/interfaces"; | |
28 | my $fh = IO::File->new($current_config_file); | |
29 | my $interfaces_config = PVE::INotify::read_etc_network_interfaces(1,$fh); | |
30 | $fh->close(); | |
31 | ||
32 | #check uplinks | |
33 | my $uplinks = {}; | |
34 | foreach my $id (keys %{$interfaces_config->{ifaces}}) { | |
35 | my $interface = $interfaces_config->{ifaces}->{$id}; | |
36 | if (my $uplink = $interface->{'uplink-id'}) { | |
37 | die "uplink-id $uplink is already defined on $uplinks->{$uplink}" if $uplinks->{$uplink}; | |
38 | $uplinks->{$interface->{'uplink-id'}} = $id; | |
39 | } | |
40 | } | |
41 | ||
42 | my $vnet_cfg = PVE::Cluster::cfs_read_file('network/vnet.cfg'); | |
43 | my $transport_cfg = PVE::Cluster::cfs_read_file('network/transports.cfg'); | |
44 | ||
45 | #generate configuration | |
46 | my $rawconfig = ""; | |
47 | foreach my $id (keys %{$vnet_cfg->{ids}}) { | |
48 | my $vnet = $vnet_cfg->{ids}->{$id}; | |
49 | my $zone = $vnet->{transportzone}; | |
50 | ||
51 | my $plugin_config = $transport_cfg->{ids}->{$zone}; | |
52 | die "zone $zone don't exist" if !defined($plugin_config); | |
53 | my $plugin = PVE::Network::Plugin->lookup($plugin_config->{type}); | |
bad3d113 | 54 | $rawconfig .= $plugin->generate_network_config($plugin_config, $zone, $id, $vnet, $uplinks); |
8df9fab5 AD |
55 | } |
56 | ||
57 | return $rawconfig; | |
58 | } | |
59 | ||
60 | #implement reload (split and reuse code from API2/Network.pm for bridge delete verification) | |
61 | ||
62 | sub verify_merged_config { | |
63 | my ($rawconfig) = @_; | |
64 | ||
65 | #merge main network intefaces and vnet file for possible conflict verification | |
66 | my $tmp_merged_network_interfaces = "/var/tmp/pve-merged_network_interfaces"; | |
67 | copy("/etc/network/interfaces", $tmp_merged_network_interfaces); | |
68 | ||
69 | my $writefh = IO::File->new($tmp_merged_network_interfaces, '>>'); | |
70 | print $writefh $rawconfig; | |
71 | $writefh->close(); | |
72 | ||
73 | my $readfh = IO::File->new($tmp_merged_network_interfaces); | |
74 | my $merged_interfaces_config = PVE::INotify::read_etc_network_interfaces(1,$readfh); | |
75 | $readfh->close(); | |
76 | unlink $tmp_merged_network_interfaces; | |
77 | PVE::INotify::__write_etc_network_interfaces($merged_interfaces_config, 1); | |
78 | ||
79 | } | |
80 | ||
81 | sub write_final_config { | |
82 | my ($rawconfig) = @_; | |
83 | #now write final separate filename | |
84 | my $tmp_file = "/var/tmp/pve-vnet.cfg"; | |
85 | ||
86 | my $vnet_interfaces_file = "/etc/network/interfaces.d/vnet"; | |
87 | ||
88 | my $writefh = IO::File->new($vnet_interfaces_file,">"); | |
89 | print $writefh $rawconfig; | |
90 | $writefh->close(); | |
91 | } | |
92 | ||
93 | ||
94 | ||
95 |