]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Tools.pm
1 # Module for lxc related functionality used mostly by our hooks.
3 package PVE
::LXC
::Tools
;
7 # LXC introduced an `lxc.hook.version` property which allows hooks to be executed in different
8 # manners. The old way passes a lot of stuff as command line parameter, the new way passes
9 # environment variables.
11 # This is supposed to be a common entry point for hooks, consuming the parameters passed by lxc and
12 # passing them on to a subroutine in a defined way.
14 my ($expected_type, $expected_section, $code) = @_;
16 my ($ct_name, $section, $type);
20 my $version = $ENV{LXC_HOOK_VERSION
} // '0';
21 if ($version eq '0') {
23 $ct_name = shift @ARGV;
24 $section = shift @ARGV;
27 if (!defined($ct_name) || !defined($section) || !defined($type)) {
28 die "missing hook parameters, expected to be called by lxc as hook\n";
31 if ($ct_name !~ /^\d+$/ || $section ne $expected_section || $type ne $expected_type) {
35 if ($type eq 'stop') {
36 foreach my $ns (@ARGV) {
37 if ($ns =~ /^([^:]+):(.+)$/) {
38 $namespaces->{$1} = $2;
40 die "unrecognized 'stop' hook parameter: $ns\n";
43 } elsif ($type eq 'clone') {
46 } elsif ($version eq '1') {
47 $ct_name = $ENV{LXC_NAME
}
48 or die "missing LXC_NAME environment variable\n";
49 $section = $ENV{LXC_HOOK_SECTION
}
50 or die "missing LXC_HOOK_SECTION environment variable\n";
51 $type = $ENV{LXC_HOOK_TYPE
}
52 or die "missing LXC_HOOK_TYPE environment variable\n";
54 if ($ct_name !~ /^\d+$/ || $section ne $expected_section || $type ne $expected_type) {
58 foreach my $var (keys %$ENV) {
59 if ($var =~ /^LXC_([A-Z]+)_NS$/) {
60 $namespaces->{lc($1)} = $ENV{$1};
64 die "lxc.hook.version $version not supported!\n";
67 my $logid = $ENV{PVE_LOG_ID
} || "pve-lxc-hook-$section-$type";
71 ROOTFS_MOUNT
=> ($ENV{LXC_ROOTFS_MOUNT
} or die "missing LXC_ROOTFS_MOUNT env var\n"),
72 ROOTFS_PATH
=> ($ENV{LXC_ROOTFS_PATH
} or die "missing LXC_ROOTFS_PATH env var\n"),
73 CONFIG_FILE
=> ($ENV{LXC_CONFIG_FILE
} or die "missing LXC_CONFIG_FILE env var\n"),
75 if (defined(my $target = $ENV{LXC_TARGET
})) {
76 $common_vars->{TARGET
} = $target;
79 $code->($ct_name, $common_vars, $namespaces, $args);