[pve-devel] [PATCH 4/6] add pve-prestart-hook and move activate volumes inside it

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


Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 src/Makefile              |   6 +-
 src/PVE/API2/LXC.pm       |  17 ------
 src/lxc-pve-prestart-hook | 153 ++++++++++++++++++++++++++++++++++++++++++++++
 src/lxc-pve.conf          |   1 +
 4 files changed, 159 insertions(+), 18 deletions(-)
 create mode 100755 src/lxc-pve-prestart-hook

diff --git a/src/Makefile b/src/Makefile
index 244c31d..49f0613 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -30,6 +30,9 @@ pct.1.pod: pct
 pct.conf.5.pod: gen-pct-conf-pod.pl PVE/LXC.pm
 	perl -I. ./gen-pct-conf-pod.pl >$@
 
+lxc-pve-prestart-hook.1.pod: lxc-pve-prestart-hook
+	perl -I. -T ./lxc-pve-prestart-hook printmanpod >$@
+
 lxc-pve-mount-hook.1.pod: lxc-pve-mount-hook
 	perl -I. -T ./lxc-pve-mount-hook printmanpod >$@
 
@@ -37,7 +40,7 @@ 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 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
+install: pct lxc-pve.conf lxc-pve-prestart-hook lxc-pve-mount-hook lxc-pve-poststop-hook lxcnetaddbr lxc-pve-prestart-hook.1.pod lxc-pve-prestart-hook.1.gz 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}
@@ -45,6 +48,7 @@ install: pct lxc-pve.conf lxc-pve-mount-hook lxc-pve-poststop-hook lxcnetaddbr l
 	install -d ${LXC_SCRIPT_DIR}
 	install -m 0755 lxcnetaddbr ${LXC_SCRIPT_DIR}
 	install -d ${LXC_HOOK_DIR}
+	install -m 0755 lxc-pve-prestart-hook ${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}
diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index b6c6b0f..0d373c7 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -1005,23 +1005,6 @@ __PACKAGE__->register_method({
 
 		my $storage_cfg = cfs_read_file("storage.cfg");
 
-		PVE::LXC::foreach_mountpoint($conf, sub {
-		    my ($ms, $mountpoint) = @_;
-
-		    my $volid = $mountpoint->{volume};
-		    PVE::Storage::activate_volumes($storage_cfg, [$volid]);
-
-		    my ($storage, $volname) = PVE::Storage::parse_volume_id($volid);
-		    my $scfg = PVE::Storage::storage_config($storage_cfg, $storage);
-		    my ($vtype, undef, undef, undef, undef, $isBase, $format) =
-			PVE::Storage::parse_volname($storage_cfg, $volid);
-
-		    if($ms ne 'rootfs' && $format ne 'subvol' && ($scfg->{type} eq 'dir' || $scfg->{type} eq 'nfs')) {
-			my $path = PVE::Storage::path($storage_cfg, $volid);
-			PVE::Tools::run_command(['losetup', '--find', '--show', $path]);
-		    }
-		});
-		
 		PVE::LXC::update_lxc_config($storage_cfg, $vmid, $conf);
 
 		my $cmd = ['lxc-start', '-n', $vmid];
diff --git a/src/lxc-pve-prestart-hook b/src/lxc-pve-prestart-hook
new file mode 100755
index 0000000..0d61d92
--- /dev/null
+++ b/src/lxc-pve-prestart-hook
@@ -0,0 +1,153 @@
+#!/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::LXC;
+use PVE::LXCSetup;
+use Data::Dumper;
+
+use base qw(PVE::CLIHandler);
+
+$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
+
+initlog ('lxc-pve-prestart-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-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>/config (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 $rootdir = $ENV{LXC_ROOTFS_MOUNT};
+
+	# 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 $fn = "/etc/pve/storage.cfg";
+
+	return if ! -f $fn;
+
+	my $raw_storagecfg = PVE::Tools::file_get_contents($fn);
+	my $storage_cfg = PVE::Storage::Plugin->parse_config($fn, $raw_storagecfg);
+
+	PVE::LXC::foreach_mountpoint($conf, sub {
+	    my ($ms, $mountpoint) = @_;
+
+	    my $volid = $mountpoint->{volume};
+	    PVE::Storage::activate_volumes($storage_cfg, [$volid]);
+
+	    my ($storage, $volname) = PVE::Storage::parse_volume_id($volid);
+	    my $scfg = PVE::Storage::storage_config($storage_cfg, $storage);
+	    my ($vtype, undef, undef, undef, undef, $isBase, $format) =
+		PVE::Storage::parse_volname($storage_cfg, $volid);
+
+	    if($ms ne 'rootfs' && $format ne 'subvol' && ($scfg->{type} eq 'dir' || $scfg->{type} eq 'nfs')) {
+		my $path = PVE::Storage::path($storage_cfg, $volid);
+		PVE::Tools::run_command(['losetup', '--find', '--show', $path]);
+	    }
+	});
+
+	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 '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');
+}
+
+my $cmddef = [ __PACKAGE__, 'lxc-pve-prestart-hook', [], $param];
+
+PVE::CLIHandler::handle_simple_cmd($cmddef, \@ARGV, undef, $0);
+
+exit 0;
+
+__END__
+
+=head1 NAME
+
+lxc-pve - LXC pre-start hook for Proxmox VE
+
+=head1 SYNOPSIS
+
+=include synopsis
+
+=head1 DESCRIPTION
+
+This mount hook activate storage and 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 2a4acde..14dae1a 100644
--- a/src/lxc-pve.conf
+++ b/src/lxc-pve.conf
@@ -1,3 +1,4 @@
+lxc.hook.pre-start = /usr/share/lxc/hooks/lxc-pve-prestart-hook
 lxc.hook.mount = /usr/share/lxc/hooks/lxc-pve-mount-hook
 lxc.hook.post-stop = /usr/share/lxc/hooks/lxc-pve-poststop-hook
 x
-- 
2.1.4




More information about the pve-devel mailing list