]> git.proxmox.com Git - pve-container.git/blame - src/lxc-pve-poststop-hook
bump version to 2.0-3
[pve-container.git] / src / lxc-pve-poststop-hook
CommitLineData
32e6d659
AD
1#!/usr/bin/perl
2
b056d074
DM
3package lxc_pve_poststop_hook;
4
32e6d659
AD
5use strict;
6use warnings;
4ed2b825 7
d6811d3f 8exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/;
4ed2b825 9
32e6d659
AD
10use POSIX;
11use File::Path;
12
13use PVE::SafeSyslog;
14use PVE::Tools;
15use PVE::Cluster;
16use PVE::INotify;
17use PVE::RPCEnvironment;
18use PVE::JSONSchema qw(get_standard_option);
19use PVE::CLIHandler;
20use PVE::Storage;
21use PVE::Storage::Plugin;
22use PVE::LXC;
32e6d659
AD
23use Data::Dumper;
24
25use base qw(PVE::CLIHandler);
26
32e6d659
AD
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 => {
c0c77476 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).",
32e6d659
AD
37 type => 'string',
38 pattern => '\S+',
39 maxLength => 64,
40 }
41 },
42 },
43 returns => { type => 'null' },
44
45 code => sub {
46 my ($param) = @_;
47
32e6d659
AD
48 return undef if $param->{name} !~ m/^\d+$/;
49
2914ad9b 50 my $vmid = $param->{name};
32e6d659 51
67afe46e 52 return undef if ! -f PVE::LXC::Config->config_file($vmid);
32e6d659 53
67afe46e 54 my $conf = PVE::LXC::Config->load_config($vmid);
32e6d659 55
2914ad9b 56 my $storage_cfg = PVE::Storage::config();
32e6d659 57
2914ad9b 58 PVE::LXC::vm_stop_cleanup($storage_cfg, $vmid, $conf);
c9a5774b
WB
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]);
9d8b1f8c
WB
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';
9d8b1f8c
WB
71 # veth_delete tests with '-d /sys/class/net/$name' before running the command
72 PVE::Network::veth_delete("veth${vmid}i$ind");
73 }
74
fbeeb88f
WB
75 my $target = $ENV{LXC_TARGET};
76 if ($target && $target eq 'reboot') {
510e21f8 77 # In order to make sure hot-plugged config changes aren't reverted
fbeeb88f 78 # to what the monitor initially loaded we need to stop the container
510e21f8
WB
79 # and restart it.
80 # Update the config and queue a restart of the lxc@$vmid task, note
81 # that we must not block because we're part of the service cgroup
82 # systemd waits for to die before issuing the new lxc-start command.
83 PVE::LXC::update_lxc_config($vmid, $conf);
1e1fad99
WB
84 # Tell the post-stop hook we want to be restarted.
85 open(my $fh, '>', "/var/lib/lxc/$vmid/reboot")
86 or die "failed to create reboot trigger file: $!\n";
87 close($fh);
fbeeb88f 88 # cause lxc to stop instead of rebooting
1e1fad99 89 exit(1);
fbeeb88f
WB
90 }
91
32e6d659
AD
92 return undef;
93 }});
94
95
96push @ARGV, 'help' if !scalar(@ARGV);
97
98my $param = {};
99
b056d074 100if ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'post-stop')) {
32e6d659
AD
101 $param->{name} = $ENV{'LXC_NAME'};
102 die "got wrong name" if $param->{name} ne $ARGV[0];
103
104 @ARGV = ();
105} else {
106 @ARGV = ('help');
107}
108
b056d074 109our $cmddef = [ __PACKAGE__, 'lxc-pve-poststop-hook', [], $param];
32e6d659 110
b056d074 111__PACKAGE__->run_cli_handler();