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