]>
Commit | Line | Data |
---|---|---|
32e6d659 AD |
1 | #!/usr/bin/perl |
2 | ||
b056d074 DM |
3 | package lxc_pve_poststop_hook; |
4 | ||
32e6d659 AD |
5 | use strict; |
6 | use warnings; | |
4ed2b825 | 7 | |
d6811d3f | 8 | exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/; |
4ed2b825 | 9 | |
32e6d659 AD |
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; | |
1a416433 | 23 | use PVE::GuestHelpers; |
32e6d659 AD |
24 | use Data::Dumper; |
25 | ||
26 | use base qw(PVE::CLIHandler); | |
27 | ||
32e6d659 AD |
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 => { | |
c0c77476 | 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).", |
32e6d659 AD |
38 | type => 'string', |
39 | pattern => '\S+', | |
40 | maxLength => 64, | |
41 | } | |
42 | }, | |
43 | }, | |
44 | returns => { type => 'null' }, | |
45 | ||
46 | code => sub { | |
47 | my ($param) = @_; | |
48 | ||
32e6d659 AD |
49 | return undef if $param->{name} !~ m/^\d+$/; |
50 | ||
2914ad9b | 51 | my $vmid = $param->{name}; |
32e6d659 | 52 | |
67afe46e | 53 | return undef if ! -f PVE::LXC::Config->config_file($vmid); |
32e6d659 | 54 | |
67afe46e | 55 | my $conf = PVE::LXC::Config->load_config($vmid); |
32e6d659 | 56 | |
2914ad9b | 57 | my $storage_cfg = PVE::Storage::config(); |
32e6d659 | 58 | |
2914ad9b | 59 | PVE::LXC::vm_stop_cleanup($storage_cfg, $vmid, $conf); |
c9a5774b WB |
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]); | |
9d8b1f8c WB |
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'; | |
9d8b1f8c WB |
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 | ||
fbeeb88f WB |
76 | my $target = $ENV{LXC_TARGET}; |
77 | if ($target && $target eq 'reboot') { | |
510e21f8 | 78 | # In order to make sure hot-plugged config changes aren't reverted |
fbeeb88f | 79 | # to what the monitor initially loaded we need to stop the container |
510e21f8 | 80 | # and restart it. |
be8ec1b0 FG |
81 | # Update the config and queue a restart of the pve-container@$vmid |
82 | # task, note that we must not block because we're part of the | |
83 | # service cgroup systemd waits for to die before issuing the new | |
84 | # lxc-start command. | |
510e21f8 | 85 | PVE::LXC::update_lxc_config($vmid, $conf); |
1e1fad99 WB |
86 | # Tell the post-stop hook we want to be restarted. |
87 | open(my $fh, '>', "/var/lib/lxc/$vmid/reboot") | |
88 | or die "failed to create reboot trigger file: $!\n"; | |
89 | close($fh); | |
fbeeb88f | 90 | # cause lxc to stop instead of rebooting |
1e1fad99 | 91 | exit(1); |
fbeeb88f WB |
92 | } |
93 | ||
1a416433 DC |
94 | PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'post-stop'); |
95 | ||
32e6d659 AD |
96 | return undef; |
97 | }}); | |
98 | ||
99 | ||
100 | push @ARGV, 'help' if !scalar(@ARGV); | |
101 | ||
102 | my $param = {}; | |
103 | ||
b056d074 | 104 | if ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'post-stop')) { |
32e6d659 AD |
105 | $param->{name} = $ENV{'LXC_NAME'}; |
106 | die "got wrong name" if $param->{name} ne $ARGV[0]; | |
107 | ||
108 | @ARGV = (); | |
109 | } else { | |
110 | @ARGV = ('help'); | |
111 | } | |
112 | ||
b056d074 | 113 | our $cmddef = [ __PACKAGE__, 'lxc-pve-poststop-hook', [], $param]; |
32e6d659 | 114 | |
b056d074 | 115 | __PACKAGE__->run_cli_handler(); |