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