]> git.proxmox.com Git - pve-container.git/blame - src/lxc-pve-poststop-hook
Move volume activation to vm_start
[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;
15use PVE::Storage;
16use PVE::Tools;
32e6d659 17
0a49c44e
WB
18PVE::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
407c7294
WB
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 }
0a49c44e 38
dc06f461
WB
39 PVE::LXC::vm_stop_cleanup($storage_cfg, $vmid, $conf);
40
0a49c44e
WB
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
1b54db95
OB
52 my $config_updated = 0;
53 if ($conf->{pending}) {
87c36547
FE
54 eval {
55 PVE::LXC::Config->vmconfig_apply_pending($vmid, $conf, $storage_cfg);
56 PVE::LXC::Config->write_config($vmid, $conf);
57 };
6a1cf3b9 58 warn "$@" if $@;
1b54db95
OB
59 PVE::LXC::update_lxc_config($vmid, $conf);
60 $config_updated = 1;
61 }
62
63
0a49c44e
WB
64 my $target = $vars->{TARGET};
65 if ($target && $target eq 'reboot') {
66 # In order to make sure hot-plugged config changes aren't reverted
67 # to what the monitor initially loaded we need to stop the container
68 # and restart it.
69 # Update the config and queue a restart of the pve-container@$vmid
70 # task, note that we must not block because we're part of the
71 # service cgroup systemd waits for to die before issuing the new
72 # lxc-start command.
1b54db95 73 PVE::LXC::update_lxc_config($vmid, $conf) if !$config_updated;
0a49c44e
WB
74 # Tell the post-stop hook we want to be restarted.
75 open(my $fh, '>', "/var/lib/lxc/$vmid/reboot")
76 or die "failed to create reboot trigger file: $!\n";
77 close($fh);
e8bb92bd
SI
78
79 # activate all volumes of the container in case pending changes added
80 # a not yet activated volume
81 my $vollist = PVE::LXC::Config->get_vm_volumes($conf);
82 PVE::Storage::activate_volumes($storage_cfg, $vollist);
83
0a49c44e
WB
84 # cause lxc to stop instead of rebooting
85 exit(1);
86 }
87
88 PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'post-stop');
89});