]> git.proxmox.com Git - pve-container.git/blame - src/lxcnetaddbr
lxc: add sdn veth_create|plug
[pve-container.git] / src / lxcnetaddbr
CommitLineData
8a04b6c7
DM
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
d6811d3f 6exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/;
4ed2b825 7
8a04b6c7
DM
8use PVE::LXC;
9use PVE::Tools qw(run_command);
10use PVE::Network;
11use PVE::ProcFSTools;
ab2ec461
AD
12my $have_sdn;
13eval {
14 require PVE::Network::SDN::Zones;
15 $have_sdn = 1;
16};
8a04b6c7
DM
17
18die "got unexpected argument count\n" if scalar(@ARGV) != 5;
19
20my ($vmid, $arg2, $arg3, $type, $iface) = @ARGV;
21
22die "got unexpected argument ($arg2 != net)\n" if $arg2 ne 'net';
23die "got unexpected argument ($arg3 != up)\n" if $arg3 ne 'up';
24
25die "got unexpected argument ($type != veth)\n" if $type ne 'veth';
26
27die "got unexpected environment" if $vmid ne $ENV{LXC_NAME};
28
29die "missing vmid parameter\n" if !$vmid;
30die "missing iface parameter\n" if !$iface;
31
67afe46e 32my $conf = PVE::LXC::Config->load_config($vmid);
8a04b6c7 33
27916659 34my $netconf;
18862537 35if ($iface =~ m/^veth(\d+)i(\d+)$/) {
27916659
DM
36 die "got unexpected interface name '$iface'\n" if $1 ne $vmid;
37 $netconf = $conf->{"net$2"};
8a04b6c7
DM
38}
39
40die "unable to find network definition for interface '$iface'\n"
27916659
DM
41 if !defined($netconf);
42
1b4cf758 43my $net = PVE::LXC::Config->parse_lxc_network($netconf);
8a04b6c7
DM
44
45my $tag = $net->{tag};
46my $firewall = $net->{firewall};
47my $bridge = $net->{bridge};
23eb2244 48my $trunks = $net->{trunks};
380962c7 49my $rate = $net->{rate};
8a04b6c7
DM
50
51die "missing bridge configuration" if !$bridge;
52
53if (-d "/sys/class/net/$iface") {
54
ab2ec461
AD
55 if($have_sdn) {
56 ($bridge, undef) = PVE::Network::SDN::Zones::get_bridge_vlan($bridge);
57 }
58
8a04b6c7
DM
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");
ab2ec461
AD
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 }
8a04b6c7
DM
72}
73
74exit 0;