]> git.proxmox.com Git - pve-container.git/blob - src/lxcnetaddbr
lxc: add sdn veth_create|plug
[pve-container.git] / src / lxcnetaddbr
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/;
7
8 use PVE::LXC;
9 use PVE::Tools qw(run_command);
10 use PVE::Network;
11 use PVE::ProcFSTools;
12 my $have_sdn;
13 eval {
14 require PVE::Network::SDN::Zones;
15 $have_sdn = 1;
16 };
17
18 die "got unexpected argument count\n" if scalar(@ARGV) != 5;
19
20 my ($vmid, $arg2, $arg3, $type, $iface) = @ARGV;
21
22 die "got unexpected argument ($arg2 != net)\n" if $arg2 ne 'net';
23 die "got unexpected argument ($arg3 != up)\n" if $arg3 ne 'up';
24
25 die "got unexpected argument ($type != veth)\n" if $type ne 'veth';
26
27 die "got unexpected environment" if $vmid ne $ENV{LXC_NAME};
28
29 die "missing vmid parameter\n" if !$vmid;
30 die "missing iface parameter\n" if !$iface;
31
32 my $conf = PVE::LXC::Config->load_config($vmid);
33
34 my $netconf;
35 if ($iface =~ m/^veth(\d+)i(\d+)$/) {
36 die "got unexpected interface name '$iface'\n" if $1 ne $vmid;
37 $netconf = $conf->{"net$2"};
38 }
39
40 die "unable to find network definition for interface '$iface'\n"
41 if !defined($netconf);
42
43 my $net = PVE::LXC::Config->parse_lxc_network($netconf);
44
45 my $tag = $net->{tag};
46 my $firewall = $net->{firewall};
47 my $bridge = $net->{bridge};
48 my $trunks = $net->{trunks};
49 my $rate = $net->{rate};
50
51 die "missing bridge configuration" if !$bridge;
52
53 if (-d "/sys/class/net/$iface") {
54
55 if($have_sdn) {
56 ($bridge, undef) = PVE::Network::SDN::Zones::get_bridge_vlan($bridge);
57 }
58
59 my $bridgemtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu");
60 die "bridge '$bridge' does not exist\n" if !$bridgemtu;
61 #avoid insecure dependency;
62 ($bridgemtu) = $bridgemtu =~ /(\d+)/;
63
64 PVE::Tools::run_command("/sbin/ip link set dev $iface up mtu $bridgemtu");
65 PVE::Tools::run_command("/sbin/ip addr add 0.0.0.0/0 dev $iface");
66
67 if($have_sdn) {
68 PVE::Network::SDN::Zones::tap_plug($iface, $net->{bridge}, $tag, $firewall, $trunks, $rate);
69 } else {
70 PVE::Network::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate);
71 }
72 }
73
74 exit 0;