]> git.proxmox.com Git - pve-container.git/blob - src/lxc-pve-poststop-hook
e3a8956ed1b644b8e3222924dfd343650af15644
[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 use IO::Pipe;
13
14 use PVE::SafeSyslog;
15 use PVE::Tools;
16 use PVE::Cluster;
17 use PVE::INotify;
18 use PVE::RPCEnvironment;
19 use PVE::JSONSchema qw(get_standard_option);
20 use PVE::CLIHandler;
21 use PVE::Storage;
22 use PVE::Storage::Plugin;
23 use PVE::LXC;
24 use Data::Dumper;
25
26 use base qw(PVE::CLIHandler);
27
28 __PACKAGE__->register_method ({
29 name => 'lxc-pve-poststop-hook',
30 path => 'lxc-pve-poststop-hook',
31 method => 'GET',
32 description => "vm_stop_cleanup.",
33 parameters => {
34 additionalProperties => 0,
35 properties => {
36 name => {
37 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).",
38 type => 'string',
39 pattern => '\S+',
40 maxLength => 64,
41 }
42 },
43 },
44 returns => { type => 'null' },
45
46 code => sub {
47 my ($param) = @_;
48
49 return undef if $param->{name} !~ m/^\d+$/;
50
51 my $vmid = $param->{name};
52
53 return undef if ! -f PVE::LXC::Config->config_file($vmid);
54
55 my $conf = PVE::LXC::Config->load_config($vmid);
56
57 my $storage_cfg = PVE::Storage::config();
58
59 PVE::LXC::vm_stop_cleanup($storage_cfg, $vmid, $conf);
60
61 my $rootfs = $ENV{LXC_ROOTFS_PATH};
62 die "Missing container root directory!\n" if !$rootfs;
63 PVE::Tools::run_command(['umount', '--recursive', $rootfs]);
64
65 # Because netlink is not a reliable protocol it can happen that lxc's
66 # link-deletion messages get lost (or end up being too early?)
67 for my $k (keys %$conf) {
68 next if $k !~ /^net(\d+)/;
69 my $ind = $1;
70 my $net = PVE::LXC::Config->parse_lxc_network($conf->{$k});
71 next if $net->{type} ne 'veth';
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 my $target = $ENV{LXC_TARGET};
77 if ($target && $target eq 'reboot') {
78 # In order to make sure hot-plugged config changes aren't reverted
79 # to what the monitor initially loaded we need to stop the container
80 # and restart it.
81 # Update the config and queue a restart of the lxc@$vmid task, note
82 # that we must not block because we're part of the service cgroup
83 # systemd waits for to die before issuing the new lxc-start command.
84 PVE::LXC::update_lxc_config($vmid, $conf);
85 $SIG{HUP} = 'IGNORE';
86 # Synchronization pipe
87 my $scope = IO::Pipe->new() or die "pipe failed: $!\n";
88 my $pid = fork();
89 die "fork failed: $!\n" if !defined($pid);
90 if (!$pid) {
91 $scope->writer();
92 # We inherit a pipe from LXC, replace it with stderr otherwise
93 # lxc will keep waiting for us...
94 POSIX::dup2(2, 1);
95 POSIX::setsid();
96 eval {
97 # Change scope otherwise we're part of lxc@.service and then
98 # if lxc finishes cleaning up before we restart it systemd
99 # might clean US up as well (read: kill us) => race
100 PVE::Tools::enter_systemd_scope("restart-$vmid", "Restarter for Proxmox VE CT $vmid",
101 Slice => 'lxc.slice',
102 KillMode => 'none');
103 # Tell the main stop hook we're "in the clear":
104 close($scope);
105 # Wait for the container to clean up everything...
106 PVE::Tools::run_command(['lxc-wait', "--name=$vmid", '--state=STOPPED']);
107 # ... before finally triggering a restart:
108 PVE::Tools::run_command(['systemctl', 'restart', "lxc\@$vmid"]);
109 };
110 warn "$@" if $@;
111 POSIX::_exit(0);
112 }
113 # Wait for the restarter scope to make sure systemd doesn't kill it before it started...
114 $scope->reader();
115 <$scope>;
116 # cause lxc to stop instead of rebooting
117 exit(1);
118 }
119
120 return undef;
121 }});
122
123
124 push @ARGV, 'help' if !scalar(@ARGV);
125
126 my $param = {};
127
128 if ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'post-stop')) {
129 $param->{name} = $ENV{'LXC_NAME'};
130 die "got wrong name" if $param->{name} ne $ARGV[0];
131
132 @ARGV = ();
133 } else {
134 @ARGV = ('help');
135 }
136
137 our $cmddef = [ __PACKAGE__, 'lxc-pve-poststop-hook', [], $param];
138
139 __PACKAGE__->run_cli_handler();