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