[pve-devel] [PATCH 3/6] add lxc-pve-poststop-hook and move vm_stop_cleanup

Alexandre Derumier aderumier at odiso.com
Tue Aug 18 05:56:06 CEST 2015


It's better to vm_stop_cleanup inside poststop hook,
in case of CT failure (wrong start for example)

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 src/Makefile              |   6 ++-
 src/PVE/API2/LXC.pm       |   4 --
 src/lxc-pve-poststop-hook | 128 ++++++++++++++++++++++++++++++++++++++++++++++
 src/lxc-pve.conf          |   3 +-
 4 files changed, 135 insertions(+), 6 deletions(-)
 create mode 100755 src/lxc-pve-poststop-hook

diff --git a/src/Makefile b/src/Makefile
index 75a5eaa..244c31d 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -33,8 +33,11 @@ pct.conf.5.pod: gen-pct-conf-pod.pl PVE/LXC.pm
 lxc-pve-mount-hook.1.pod: lxc-pve-mount-hook
 	perl -I. -T ./lxc-pve-mount-hook printmanpod >$@
 
+lxc-pve-poststop-hook.1.pod: lxc-pve-poststop-hook
+	perl -I. -T ./lxc-pve-poststop-hook printmanpod >$@
+
 .PHONY: install
-install: pct lxc-pve.conf lxc-pve-mount-hook lxcnetaddbr lxc-pve-mount-hook.1.pod lxc-pve-mount-hook.1.gz pct.1.pod pct.1.gz pct.conf.5.pod pct.conf.5.gz pve-update-lxc-config
+install: pct lxc-pve.conf lxc-pve-mount-hook lxc-pve-poststop-hook lxcnetaddbr lxc-pve-mount-hook.1.pod lxc-pve-mount-hook.1.gz lxc-pve-poststop-hook.1.pod lxc-pve-poststop-hook.1.gz pct.1.pod pct.1.gz pct.conf.5.pod pct.conf.5.gz pve-update-lxc-config
 	perl -T -I. ./pct verifyapi
 	install -d ${SBINDIR}
 	install -m 0755 pct ${SBINDIR}
@@ -43,6 +46,7 @@ install: pct lxc-pve.conf lxc-pve-mount-hook lxcnetaddbr lxc-pve-mount-hook.1.po
 	install -m 0755 lxcnetaddbr ${LXC_SCRIPT_DIR}
 	install -d ${LXC_HOOK_DIR}
 	install -m 0755 lxc-pve-mount-hook ${LXC_HOOK_DIR}
+	install -m 0755 lxc-pve-poststop-hook ${LXC_HOOK_DIR}
 	install -d ${LXC_COMMON_CONFIG_DIR}
 	install -m 0644 lxc-pve.conf ${LXC_COMMON_CONFIG_DIR}/01-pve.conf
 	make -C PVE install
diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index b395af3..b6c6b0f 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -1101,8 +1101,6 @@ __PACKAGE__->register_method({
 
 		run_command($cmd);
 
-		PVE::LXC::vm_stop_cleanup($storage_cfg, $vmid, $conf);
-
 		return;
 	    };
 
@@ -1184,8 +1182,6 @@ __PACKAGE__->register_method({
 		
 	    }
 
-	    PVE::LXC::vm_stop_cleanup($storage_cfg, $vmid, $conf);
-	    
 	    die $err if !$err;
 
 	    return;
diff --git a/src/lxc-pve-poststop-hook b/src/lxc-pve-poststop-hook
new file mode 100755
index 0000000..b42b780
--- /dev/null
+++ b/src/lxc-pve-poststop-hook
@@ -0,0 +1,128 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use POSIX;
+use File::Path;
+
+use PVE::SafeSyslog;
+use PVE::Tools;
+use PVE::Cluster;
+use PVE::INotify;
+use PVE::RPCEnvironment;
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::CLIHandler;
+use PVE::Storage;
+use PVE::Storage::Plugin;
+use PVE::LXC;
+use PVE::LXCSetup;
+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 at 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',
+    method => 'GET',
+    description => "vm_stop_cleanup.",
+    parameters => {
+    	additionalProperties => 0,
+	properties => {
+	    name => {
+		description => "The container name. This hook is only active for containers using numeric IDs, where configuration is stored on /etc/pve/lxc/<name>/config (else it is just a NOP).",
+		type => 'string',
+		pattern => '\S+',
+		maxLength => 64,
+	    }
+	},
+    },
+    returns => { type => 'null' },
+
+    code => sub {
+	my ($param) = @_;
+
+
+	return undef if $param->{name} !~ m/^\d+$/;
+
+	# Note: PVE::INotify::nodename() returns wrong value when run
+	# inside container mount hook, so we cannot simply 
+	# use PVE::LXC::load_conf().
+
+	my $config_filename = "/etc/pve/lxc/$param->{name}.conf";
+
+	return undef if ! -f $config_filename;
+	
+	my $raw = PVE::Tools::file_get_contents($config_filename);
+	my $conf = PVE::LXC::parse_pct_config($config_filename, $raw);
+
+	my $storagecfg_filename = "/etc/pve/storage.cfg";
+
+	return undef if ! -f $storagecfg_filename;
+
+	my $storage_raw = PVE::Tools::file_get_contents($storagecfg_filename);
+	my $storage_cfg = PVE::Storage::Plugin->parse_config($storagecfg_filename, $storage_raw);
+
+        PVE::LXC::vm_stop_cleanup($storage_cfg, $param->{name}, $conf);
+	
+	return undef;
+    }});
+
+
+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')) {
+    $param->{name} = $ENV{'LXC_NAME'};
+    die "got wrong name" if $param->{name} ne $ARGV[0];
+
+    @ARGV = ();
+} else {
+    @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)
+
+=include pve_copyright
diff --git a/src/lxc-pve.conf b/src/lxc-pve.conf
index bb0d941..2a4acde 100644
--- a/src/lxc-pve.conf
+++ b/src/lxc-pve.conf
@@ -1,2 +1,3 @@
 lxc.hook.mount = /usr/share/lxc/hooks/lxc-pve-mount-hook
-x
\ No newline at end of file
+lxc.hook.post-stop = /usr/share/lxc/hooks/lxc-pve-poststop-hook
+x
-- 
2.1.4




More information about the pve-devel mailing list