]> git.proxmox.com Git - pve-container.git/blame - src/lxcnetaddbr
net: Pass network config directly to net_tap_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);
8a04b6c7 10use PVE::ProcFSTools;
a1ff8c37 11
8a04b6c7
DM
12die "got unexpected argument count\n" if scalar(@ARGV) != 5;
13
14my ($vmid, $arg2, $arg3, $type, $iface) = @ARGV;
15
16die "got unexpected argument ($arg2 != net)\n" if $arg2 ne 'net';
17die "got unexpected argument ($arg3 != up)\n" if $arg3 ne 'up';
18
19die "got unexpected argument ($type != veth)\n" if $type ne 'veth';
a1ff8c37 20
8a04b6c7
DM
21die "got unexpected environment" if $vmid ne $ENV{LXC_NAME};
22
23die "missing vmid parameter\n" if !$vmid;
24die "missing iface parameter\n" if !$iface;
25
67afe46e 26my $conf = PVE::LXC::Config->load_config($vmid);
8a04b6c7 27
27916659 28my $netconf;
18862537 29if ($iface =~ m/^veth(\d+)i(\d+)$/) {
27916659
DM
30 die "got unexpected interface name '$iface'\n" if $1 ne $vmid;
31 $netconf = $conf->{"net$2"};
8a04b6c7
DM
32}
33
34die "unable to find network definition for interface '$iface'\n"
27916659
DM
35 if !defined($netconf);
36
1b4cf758 37my $net = PVE::LXC::Config->parse_lxc_network($netconf);
a1ff8c37 38
8a04b6c7 39my $bridge = $net->{bridge};
8a04b6c7
DM
40die "missing bridge configuration" if !$bridge;
41
42if (-d "/sys/class/net/$iface") {
43
44 my $bridgemtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu");
45 die "bridge '$bridge' does not exist\n" if !$bridgemtu;
46 #avoid insecure dependency;
47 ($bridgemtu) = $bridgemtu =~ /(\d+)/;
48
49 PVE::Tools::run_command("/sbin/ip link set dev $iface up mtu $bridgemtu");
50 PVE::Tools::run_command("/sbin/ip addr add 0.0.0.0/0 dev $iface");
83ec30ad 51 PVE::LXC::net_tap_plug($iface, $net);
8a04b6c7
DM
52}
53
54exit 0;