]> git.proxmox.com Git - pve-container.git/blob - src/lxc-pve-poststop-hook
1dba48c02f83ad48bb86c0e9b4deff47a1905ce3
[pve-container.git] / src / lxc-pve-poststop-hook
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use File::Path;
7 use IO::Dir;
8 use POSIX;
9
10 use PVE::GuestHelpers;
11 use PVE::LXC::Config;
12 use PVE::LXC::Tools;
13 use PVE::LXC;
14 use PVE::Network;
15 use PVE::Storage;
16 use PVE::Tools;
17
18 PVE::LXC::Tools::lxc_hook('post-stop', 'lxc', sub {
19 my ($vmid, $vars, undef, undef) = @_;
20
21 return undef if ! -f PVE::LXC::Config->config_file($vmid);
22
23 my $conf = PVE::LXC::Config->load_config($vmid);
24
25 my $storage_cfg = PVE::Storage::config();
26
27 PVE::Tools::run_command(['umount', '--recursive', '--', $vars->{ROOTFS_PATH}]);
28 my $staging_dir = PVE::LXC::get_staging_tempfs();
29 if (my $dh = IO::Dir->new($staging_dir)) {
30 while (defined(my $dir = $dh->read)) {
31 next if $dir eq '.' || $dir eq '..';
32 eval {
33 PVE::Tools::run_command(['umount', '--', "$staging_dir/$dir"]);
34 };
35 warn $@ if $@;
36 }
37 }
38
39 PVE::LXC::vm_stop_cleanup($storage_cfg, $vmid, $conf);
40
41 # Because netlink is not a reliable protocol it can happen that lxc's
42 # link-deletion messages get lost (or end up being too early?)
43 for my $k (keys %$conf) {
44 next if $k !~ /^net(\d+)/;
45 my $ind = $1;
46 my $net = PVE::LXC::Config->parse_lxc_network($conf->{$k});
47 next if $net->{type} ne 'veth';
48 # veth_delete tests with '-d /sys/class/net/$name' before running the command
49 PVE::Network::veth_delete("veth${vmid}i$ind");
50 }
51
52 my $config_updated = 0;
53 if ($conf->{pending}) {
54 eval { PVE::LXC::Config->vmconfig_apply_pending($vmid, $conf, $storage_cfg) };
55 warn "$@" if $@;
56 PVE::LXC::update_lxc_config($vmid, $conf);
57 $config_updated = 1;
58 }
59
60
61 my $target = $vars->{TARGET};
62 if ($target && $target eq 'reboot') {
63 # In order to make sure hot-plugged config changes aren't reverted
64 # to what the monitor initially loaded we need to stop the container
65 # and restart it.
66 # Update the config and queue a restart of the pve-container@$vmid
67 # task, note that we must not block because we're part of the
68 # service cgroup systemd waits for to die before issuing the new
69 # lxc-start command.
70 PVE::LXC::update_lxc_config($vmid, $conf) if !$config_updated;
71 # Tell the post-stop hook we want to be restarted.
72 open(my $fh, '>', "/var/lib/lxc/$vmid/reboot")
73 or die "failed to create reboot trigger file: $!\n";
74 close($fh);
75 # cause lxc to stop instead of rebooting
76 exit(1);
77 }
78
79 PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'post-stop');
80 });