]> git.proxmox.com Git - pve-container.git/blobdiff - src/lxc-pve-poststop-hook
destroy_config: die if unlink fails
[pve-container.git] / src / lxc-pve-poststop-hook
index 17463e881ffa10e9818f925d121aababc7d3473f..654aa268780c977a9e37b83c9b299162fa1d91a9 100755 (executable)
@@ -1,9 +1,11 @@
 #!/usr/bin/perl
 
+package lxc_pve_poststop_hook;
+
 use strict;
 use warnings;
 
-exit 0 if $ENV{'LXC_NAME'} !~ /^\d+$/;
+exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/;
 
 use POSIX;
 use File::Path;
@@ -22,23 +24,6 @@ use Data::Dumper;
 
 use base qw(PVE::CLIHandler);
 
-$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
-
-initlog ('lxc-pve-poststop-hook');
-
-die "please run as root\n" if $> != 0;
-
-PVE::INotify::inotify_init();
-
-my $rpcenv = PVE::RPCEnvironment->init('cli');
-$rpcenv->set_language($ENV{LANG});
-$rpcenv->set_user('root@pam');
-
-# we cannot use cfs_read here (permission problem)
-#$rpcenv->init_request();
-
-my $nodename = PVE::INotify::nodename();
-
 __PACKAGE__->register_method ({
     name => 'lxc-pve-poststop-hook',
     path => 'lxc-pve-poststop-hook',
@@ -64,14 +49,47 @@ __PACKAGE__->register_method ({
 
        my $vmid = $param->{name};
 
-       return undef if ! -f PVE::LXC::config_file($vmid);
+       return undef if ! -f PVE::LXC::Config->config_file($vmid);
        
-       my $conf = PVE::LXC::load_config($vmid);
+       my $conf = PVE::LXC::Config->load_config($vmid);
 
        my $storage_cfg = PVE::Storage::config();
 
         PVE::LXC::vm_stop_cleanup($storage_cfg, $vmid, $conf);
-       
+
+       my $rootfs = $ENV{LXC_ROOTFS_PATH};
+       die "Missing container root directory!\n" if !$rootfs;
+       PVE::Tools::run_command(['umount', '--recursive', $rootfs]);
+
+       # Because netlink is not a reliable protocol it can happen that lxc's
+       # link-deletion messages get lost (or end up being too early?)
+       for my $k (keys %$conf) {
+           next if $k !~ /^net(\d+)/;
+           my $ind = $1;
+           my $net = PVE::LXC::Config->parse_lxc_network($conf->{$k});
+           next if $net->{type} ne 'veth';
+           # veth_delete tests with '-d /sys/class/net/$name' before running the command
+           PVE::Network::veth_delete("veth${vmid}i$ind");
+       }
+
+       my $target = $ENV{LXC_TARGET};
+       if ($target && $target eq 'reboot') {
+           # In order to make sure hot-plugged config changes aren't reverted
+           # to what the monitor initially loaded we need to stop the container
+           # and restart it.
+           # Update the config and queue a restart of the pve-container@$vmid
+           # task, note that we must not block because we're part of the
+           # service cgroup systemd waits for to die before issuing the new
+           # lxc-start command.
+           PVE::LXC::update_lxc_config($vmid, $conf);
+           # Tell the post-stop hook we want to be restarted.
+           open(my $fh, '>', "/var/lib/lxc/$vmid/reboot")
+               or die "failed to create reboot trigger file: $!\n";
+           close($fh);
+           # cause lxc to stop instead of rebooting
+           exit(1);
+       }
+
        return undef;
     }});
 
@@ -80,10 +98,7 @@ push @ARGV, 'help' if !scalar(@ARGV);
 
 my $param = {};
 
-if ((scalar(@ARGV) == 1) && ($ARGV[0] eq 'printmanpod') ||
-    ($ARGV[0] eq 'verifyapi')) {
-    # OK
-} elsif ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'post-stop')) {
+if ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'post-stop')) {
     $param->{name} = $ENV{'LXC_NAME'};
     die "got wrong name" if $param->{name} ne $ARGV[0];
 
@@ -92,28 +107,6 @@ if ((scalar(@ARGV) == 1) && ($ARGV[0] eq 'printmanpod') ||
     @ARGV = ('help');
 }
 
-my $cmddef = [ __PACKAGE__, 'lxc-pve-poststop-hook', [], $param];
-
-PVE::CLIHandler::handle_simple_cmd($cmddef, \@ARGV, undef, $0);
-
-exit 0;
-
-__END__
-
-=head1 NAME
-
-lxc-pve - LXC post stop hook for Proxmox VE
-
-=head1 SYNOPSIS
-
-=include synopsis
-
-=head1 DESCRIPTION
-
-This post stop hook detach loop devices and deactivate volumes for pve container.
-
-=head1 SEE ALSO
-
-lct(1)
+our $cmddef = [ __PACKAGE__, 'lxc-pve-poststop-hook', [], $param];
 
-=include pve_copyright
+__PACKAGE__->run_cli_handler();