]> git.proxmox.com Git - pve-container.git/blob - src/lxc-pve-poststop-hook
Fix #929: delete veths in the post-stop hook
[pve-container.git] / src / lxc-pve-poststop-hook
1 #!/usr/bin/perl
2
3 package lxc_pve_poststop_hook;
4
5 use strict;
6 use warnings;
7
8 exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/;
9
10 use POSIX;
11 use File::Path;
12
13 use PVE::SafeSyslog;
14 use PVE::Tools;
15 use PVE::Cluster;
16 use PVE::INotify;
17 use PVE::RPCEnvironment;
18 use PVE::JSONSchema qw(get_standard_option);
19 use PVE::CLIHandler;
20 use PVE::Storage;
21 use PVE::Storage::Plugin;
22 use PVE::LXC;
23 use Data::Dumper;
24
25 use base qw(PVE::CLIHandler);
26
27 __PACKAGE__->register_method ({
28 name => 'lxc-pve-poststop-hook',
29 path => 'lxc-pve-poststop-hook',
30 method => 'GET',
31 description => "vm_stop_cleanup.",
32 parameters => {
33 additionalProperties => 0,
34 properties => {
35 name => {
36 description => "The container name. This hook is only active for containers using numeric IDs, where configuration is stored on /etc/pve/lxc/<name>.conf (else it is just a NOP).",
37 type => 'string',
38 pattern => '\S+',
39 maxLength => 64,
40 }
41 },
42 },
43 returns => { type => 'null' },
44
45 code => sub {
46 my ($param) = @_;
47
48 return undef if $param->{name} !~ m/^\d+$/;
49
50 my $vmid = $param->{name};
51
52 return undef if ! -f PVE::LXC::Config->config_file($vmid);
53
54 my $conf = PVE::LXC::Config->load_config($vmid);
55
56 my $storage_cfg = PVE::Storage::config();
57
58 PVE::LXC::vm_stop_cleanup($storage_cfg, $vmid, $conf);
59
60 my $rootfs = $ENV{LXC_ROOTFS_PATH};
61 die "Missing container root directory!\n" if !$rootfs;
62 PVE::Tools::run_command(['umount', '--recursive', $rootfs]);
63
64 # Because netlink is not a reliable protocol it can happen that lxc's
65 # link-deletion messages get lost (or end up being too early?)
66 for my $k (keys %$conf) {
67 next if $k !~ /^net(\d+)/;
68 my $ind = $1;
69 my $net = PVE::LXC::Config->parse_lxc_network($conf->{$k});
70 next if $net->{type} ne 'veth';
71 my $name = "veth${vmid}i${ind}";
72 # veth_delete tests with '-d /sys/class/net/$name' before running the command
73 PVE::Network::veth_delete("veth${vmid}i$ind");
74 }
75
76 return undef;
77 }});
78
79
80 push @ARGV, 'help' if !scalar(@ARGV);
81
82 my $param = {};
83
84 if ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'post-stop')) {
85 $param->{name} = $ENV{'LXC_NAME'};
86 die "got wrong name" if $param->{name} ne $ARGV[0];
87
88 @ARGV = ();
89 } else {
90 @ARGV = ('help');
91 }
92
93 our $cmddef = [ __PACKAGE__, 'lxc-pve-poststop-hook', [], $param];
94
95 __PACKAGE__->run_cli_handler();