]> git.proxmox.com Git - pve-container.git/blame - src/lxcnetaddbr
consistent interface names and live network update
[pve-container.git] / src / lxcnetaddbr
CommitLineData
8a04b6c7
DM
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use PVE::LXC;
7use PVE::Tools qw(run_command);
8use PVE::Network;
9use PVE::ProcFSTools;
10
11die "got unexpected argument count\n" if scalar(@ARGV) != 5;
12
13my ($vmid, $arg2, $arg3, $type, $iface) = @ARGV;
14
15die "got unexpected argument ($arg2 != net)\n" if $arg2 ne 'net';
16die "got unexpected argument ($arg3 != up)\n" if $arg3 ne 'up';
17
18die "got unexpected argument ($type != veth)\n" if $type ne 'veth';
19
20die "got unexpected environment" if $vmid ne $ENV{LXC_NAME};
21
22die "missing vmid parameter\n" if !$vmid;
23die "missing iface parameter\n" if !$iface;
24
25my $conf = PVE::LXC::load_config($vmid);
26
27916659 27my $netconf;
18862537 28if ($iface =~ m/^veth(\d+)i(\d+)$/) {
27916659
DM
29 die "got unexpected interface name '$iface'\n" if $1 ne $vmid;
30 $netconf = $conf->{"net$2"};
8a04b6c7
DM
31}
32
33die "unable to find network definition for interface '$iface'\n"
27916659
DM
34 if !defined($netconf);
35
36my $net = PVE::LXC::parse_lxc_network($netconf);
8a04b6c7
DM
37
38my $tag = $net->{tag};
39my $firewall = $net->{firewall};
40my $bridge = $net->{bridge};
41
42die "missing bridge configuration" if !$bridge;
43
44if (-d "/sys/class/net/$iface") {
45
46 my $bridgemtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu");
47 die "bridge '$bridge' does not exist\n" if !$bridgemtu;
48 #avoid insecure dependency;
49 ($bridgemtu) = $bridgemtu =~ /(\d+)/;
50
51 PVE::Tools::run_command("/sbin/ip link set dev $iface up mtu $bridgemtu");
52 PVE::Tools::run_command("/sbin/ip addr add 0.0.0.0/0 dev $iface");
53 PVE::ProcFSTools::write_proc_entry("/proc/sys/net/ipv4/conf/$iface/proxy_arp", "1");
54 PVE::ProcFSTools::write_proc_entry("/proc/sys/net/ipv4/conf/$iface/forwarding", "1");
55 PVE::Network::tap_plug($iface, $bridge, $tag, $firewall);
56}
57
58exit 0;