[pve-devel] [PATCH container 0/5] update & cleanup for our hooks

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Nov 5 13:58:00 CET 2019


This should deduplicate some code in our hooks and add support for
`lxc.hook.version`. That way users can use newer hook versions without
breaking the PVE hooks in the process.

Patches 2 & 4 are best viewed with -w. I'm attaching -w of 2 below, 4
isn't that tragic.

Wolfgang Bumiller (5):
  add PVE::LXC::Tools
  change hooks to use new helper
  tools: add device file iterator, and cgroup_do_write
  autodev hook: use new helpers and whitespace fixup
  config: whitelist lxc.hook.version

 src/PVE/LXC/Config.pm     |   1 +
 src/PVE/LXC/Makefile      |   7 +-
 src/PVE/LXC/Tools.pm      | 133 +++++++++++++++++++++++++++++++
 src/lxc-pve-autodev-hook  | 115 ++++++++-------------------
 src/lxc-pve-poststop-hook | 154 ++++++++++++------------------------
 src/lxc-pve-prestart-hook | 161 ++++++++++++--------------------------
 6 files changed, 276 insertions(+), 295 deletions(-)
 create mode 100644 src/PVE/LXC/Tools.pm

-- 
2.20.1


commit 2be78e2a0fc97014422e6eacfc64916d7a755813
Author: Wolfgang Bumiller <w.bumiller at proxmox.com>
Date:   Fri Jul 12 13:46:42 2019 +0200

    change hooks to use new helper
    
    We now get rid of all the PVE::CLIHandler baggage which
    reduces the code a lot. It is also not compatible with the
    new lxc.hook.version=1 method of hooks!
    
    The new helper is specific to lxc hooks and supports both
    current `lxc.hook.version`s.
    
    Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>

diff --git a/src/lxc-pve-autodev-hook b/src/lxc-pve-autodev-hook
index c934bfd..4913870 100755
--- a/src/lxc-pve-autodev-hook
+++ b/src/lxc-pve-autodev-hook
@@ -3,23 +3,16 @@
 use strict;
 use warnings;
 
-exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/;
-
 use File::Path;
 use File::Basename;
 
+use PVE::LXC::Tools;
 use PVE::Tools;
 
-my $vmid = $ENV{LXC_NAME};
-my $root = $ENV{LXC_ROOTFS_MOUNT};
+PVE::LXC::Tools::lxc_hook('autodev', 'lxc', sub {
+    my ($vmid, $vars, undef, undef) = @_;
 
-if (@ARGV != 3 || $ARGV[1] ne 'lxc' || $ARGV[2] ne 'autodev') {
-    die "invalid usage, this is an LXC autodev hook\n";
-}
-
-if ($vmid ne $ARGV[0]) {
-    die "got wrong name: $ARGV[0] while LXC_NAME=$vmid\n";
-}
+    my $root = $vars->{ROOTFS_MOUNT};
 
     my $devlist_file = "/var/lib/lxc/$vmid/devices";
     my $fd;
@@ -87,5 +80,4 @@ while (defined(my $line = <$fd>)) {
         }
     }
     close $fd;
-
-exit 0;
+});
diff --git a/src/lxc-pve-poststop-hook b/src/lxc-pve-poststop-hook
index 00bd0b3..19d0b52 100755
--- a/src/lxc-pve-poststop-hook
+++ b/src/lxc-pve-poststop-hook
@@ -1,53 +1,21 @@
 #!/usr/bin/perl
 
-package lxc_pve_poststop_hook;
-
 use strict;
 use warnings;
 
-exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/;
-
 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::GuestHelpers;
+use PVE::LXC::Config;
+use PVE::LXC::Tools;
+use PVE::LXC;
+use PVE::Network;
+use PVE::Storage;
+use PVE::Tools;
 
-use base qw(PVE::CLIHandler);
-
-__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>.conf (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+$/;
-
-	my $vmid = $param->{name};
+PVE::LXC::Tools::lxc_hook('post-stop', 'lxc', sub {
+    my ($vmid, $vars, undef, undef) = @_;
 
     return undef if ! -f PVE::LXC::Config->config_file($vmid);
 
@@ -57,9 +25,7 @@ __PACKAGE__->register_method ({
 
     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]);
+    PVE::Tools::run_command(['umount', '--recursive', $vars->{ROOTFS_PATH}]);
 
     # Because netlink is not a reliable protocol it can happen that lxc's
     # link-deletion messages get lost (or end up being too early?)
@@ -72,7 +38,7 @@ __PACKAGE__->register_method ({
 	PVE::Network::veth_delete("veth${vmid}i$ind");
     }
 
-	my $target = $ENV{LXC_TARGET};
+    my $target = $vars->{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
@@ -91,24 +57,4 @@ __PACKAGE__->register_method ({
     }
 
     PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'post-stop');
-
-	return undef;
-    }});
-
-
-push @ARGV, 'help' if !scalar(@ARGV);
-
-my $param = {};
-
-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];
-
-    @ARGV = ();
-} else {
-    @ARGV = ('help');
-}
-
-our $cmddef = [ __PACKAGE__, 'lxc-pve-poststop-hook', [], $param];
-
-__PACKAGE__->run_cli_handler();
+});
diff --git a/src/lxc-pve-prestart-hook b/src/lxc-pve-prestart-hook
index 18b60cf..c0965ab 100755
--- a/src/lxc-pve-prestart-hook
+++ b/src/lxc-pve-prestart-hook
@@ -5,57 +5,21 @@ package lxc_pve_prestart_hook;
 use strict;
 use warnings;
 
-exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/;
-
 use POSIX;
 use File::Path;
 use Fcntl ':mode';
 
-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::LXC;
+use PVE::LXC::Config;
 use PVE::LXC::Setup;
+use PVE::LXC::Tools;
+use PVE::LXC;
+use PVE::Storage;
+use PVE::Tools;
+
+PVE::LXC::Tools::lxc_hook('pre-start', 'lxc', sub {
+    my ($vmid, $vars, undef, undef) = @_;
 
-use base qw(PVE::CLIHandler);
-
-__PACKAGE__->register_method ({
-    name => 'lxc-pve-prestart-hook',
-    path => 'lxc-pve-prestart-hook',
-    method => 'GET',
-    description => "Create a new container root directory.",
-    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>.conf (else it is just a NOP).",
-		type => 'string',
-		pattern => '\S+',
-		maxLength => 64,
-	    },
-	    path => {
-		description => "The path to the container configuration directory (LXC internal argument - do not pass manually!).",
-		type => 'string',
-	    },
-	    rootfs => {
-		description => "The path to the container's rootfs (LXC internal argument - do not pass manually!)",
-		type => 'string',
-	    },
-	},
-    },
-    returns => { type => 'null' },
-
-    code => sub {
-	my ($param) = @_;
-
-	return undef if $param->{name} !~ m/^\d+$/;
-
-	my $vmid = $param->{name};
     my $skiplock_flag_fn = "/run/lxc/skiplock-$vmid";
     my $skiplock = 1 if -e $skiplock_flag_fn;
     unlink $skiplock_flag_fn if $skiplock;
@@ -76,7 +40,7 @@ __PACKAGE__->register_method ({
 
     PVE::Storage::activate_volumes($storage_cfg, $vollist);
 
-	my $rootdir = $param->{rootfs};
+    my $rootdir = $vars->{ROOTFS_PATH};
 
     # Delete any leftover reboot-trigger file
     unlink("/var/lib/lxc/$vmid/reboot");
@@ -116,25 +80,4 @@ __PACKAGE__->register_method ({
 	}
 	PVE::Tools::file_set_contents($devlist_file, $devlist);
     }
-	return undef;
-    }});
-
-
-push @ARGV, 'help' if !scalar(@ARGV);
-
-my $param = {};
-
-if ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'pre-start')) {
-    $param->{name} = $ENV{'LXC_NAME'};
-    die "got wrong name" if $param->{name} ne $ARGV[0];
-
-    $param->{path} = $ENV{'LXC_CONFIG_FILE'};
-    $param->{rootfs} = $ENV{'LXC_ROOTFS_PATH'};
-    @ARGV = ();
-} else {
-    @ARGV = ('help');
-}
-
-our $cmddef = [ __PACKAGE__, 'lxc-pve-prestart-hook', [], $param];
-
-__PACKAGE__->run_cli_handler();
+});




More information about the pve-devel mailing list