]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/Network/VnetPlugin.pm
vnetplugin: on_delete_hook : verify if vnet exist in vm && ct
[pve-network.git] / PVE / Network / Network / VnetPlugin.pm
1 package PVE::Network::Network::VnetPlugin;
2
3 use strict;
4 use warnings;
5 use PVE::Network::Network::Plugin;
6
7 use base('PVE::Network::Network::Plugin');
8
9 use PVE::Cluster;
10 use PVE::LXC;
11 use PVE::LXC::Config;
12 use PVE::QemuServer;
13 use PVE::QemuConfig;
14
15 sub type {
16 return 'vnet';
17 }
18
19
20
21 sub properties {
22 return {
23 transportzone => {
24 type => 'string',
25 description => "transportzone id",
26 },
27 tag => {
28 type => 'integer',
29 description => "vlan or vxlan id",
30 },
31 alias => {
32 type => 'string',
33 description => "alias name of the vnet",
34 optional => 1,
35 },
36 mtu => {
37 type => 'integer',
38 description => "mtu",
39 optional => 1,
40 },
41 ipv4 => {
42 description => "Anycast router ipv4 address.",
43 type => 'string', format => 'ipv4',
44 optional => 1,
45 },
46 ipv6 => {
47 description => "Anycast router ipv6 address.",
48 type => 'string', format => 'ipv6',
49 optional => 1,
50 },
51 mac => {
52 type => 'boolean',
53 description => "Anycast router mac address",
54 optional => 1,
55 }
56 };
57 }
58
59 sub options {
60 return {
61 transportzone => { optional => 0},
62 tag => { optional => 0},
63 alias => { optional => 1 },
64 ipv4 => { optional => 1 },
65 ipv6 => { optional => 1 },
66 mtu => { optional => 1 },
67 };
68 }
69
70 sub on_delete_hook {
71 my ($class, $networkid, $scfg) = @_;
72
73 # verify than no vm or ct have interfaces in this bridge
74 my $vmdata = read_local_vm_config();
75
76 foreach my $vmid (sort keys %{$vmdata->{qemu}}) {
77 my $conf = $vmdata->{qemu}->{$vmid};
78 foreach my $netid (sort keys %$conf) {
79 next if $netid !~ m/^net(\d+)$/;
80 my $net = PVE::QemuServer::parse_net($conf->{$netid});
81 die "vnet $networkid is used by vm $vmid" if $net->{bridge} eq $networkid;
82 }
83 }
84
85 foreach my $vmid (sort keys %{$vmdata->{lxc}}) {
86 my $conf = $vmdata->{lxc}->{$vmid};
87 foreach my $netid (sort keys %$conf) {
88 next if $netid !~ m/^net(\d+)$/;
89 my $net = PVE::LXC::Config->parse_lxc_network($conf->{$netid});
90 die "vnet $networkid is used by ct $vmid" if $net->{bridge} eq $networkid;
91 }
92 }
93
94 }
95
96 sub on_update_hook {
97 my ($class, $networkid, $scfg) = @_;
98
99 # verify that tag is not already defined in another vnet
100
101 }
102
103 sub read_local_vm_config {
104
105 my $qemu = {};
106 my $lxc = {};
107
108 my $vmdata = { qemu => $qemu, lxc => $lxc };
109
110 my $vmlist = PVE::Cluster::get_vmlist();
111 return $vmdata if !$vmlist || !$vmlist->{ids};
112 my $ids = $vmlist->{ids};
113
114 foreach my $vmid (keys %$ids) {
115 next if !$vmid;
116 my $d = $ids->{$vmid};
117 next if !$d->{node};
118 next if !$d->{type};
119 if ($d->{type} eq 'qemu') {
120 my $cfspath = PVE::QemuConfig->cfs_config_path($vmid);
121 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
122 $qemu->{$vmid} = $conf;
123 }
124 } elsif ($d->{type} eq 'lxc') {
125 my $cfspath = PVE::LXC::Config->cfs_config_path($vmid);
126 if (my $conf = PVE::Cluster::cfs_read_file($cfspath)) {
127 $lxc->{$vmid} = $conf;
128 }
129 }
130 }
131
132 return $vmdata;
133 };
134
135 1;