]> git.proxmox.com Git - pve-container.git/blob - src/lxc-pve-prestart-hook
lxc hooks: use run_cli_handler(), remove stale docs
[pve-container.git] / src / lxc-pve-prestart-hook
1 #!/usr/bin/perl
2
3 package lxc_pve_prestart_hook;
4
5 use strict;
6 use warnings;
7
8 exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/;
9
10 use POSIX;
11 use File::Path;
12
13 use PVE::SafeSyslog;
14 use PVE::Tools;
15 use PVE::Cluster;
16 use PVE::INotify;
17 use PVE::RPCEnvironment;
18 use PVE::JSONSchema qw(get_standard_option);
19 use PVE::CLIHandler;
20 use PVE::Storage;
21 use PVE::LXC;
22 use Data::Dumper;
23
24 use base qw(PVE::CLIHandler);
25
26 __PACKAGE__->register_method ({
27 name => 'lxc-pve-prestart-hook',
28 path => 'lxc-pve-prestart-hook',
29 method => 'GET',
30 description => "Create a new container root directory.",
31 parameters => {
32 additionalProperties => 0,
33 properties => {
34 name => {
35 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).",
36 type => 'string',
37 pattern => '\S+',
38 maxLength => 64,
39 },
40 path => {
41 description => "The path to the container configuration directory (LXC internal argument - do not pass manually!).",
42 type => 'string',
43 },
44 rootfs => {
45 description => "The path to the container's rootfs (LXC internal argument - do not pass manually!)",
46 type => 'string',
47 },
48 },
49 },
50 returns => { type => 'null' },
51
52 code => sub {
53 my ($param) = @_;
54
55 return undef if $param->{name} !~ m/^\d+$/;
56
57 my $rootdir = $ENV{LXC_ROOTFS_MOUNT};
58
59 my $vmid = $param->{name};
60
61 PVE::Cluster::check_cfs_quorum(); # only start if we have quorum
62
63 return undef if ! -f PVE::LXC::config_file($vmid);
64
65 my $conf = PVE::LXC::load_config($vmid);
66
67 my $storage_cfg = PVE::Storage::config();
68
69 my $vollist = PVE::LXC::get_vm_volumes($conf);
70 my $loopdevlist = PVE::LXC::get_vm_volumes($conf, 'rootfs');
71
72 PVE::Storage::activate_volumes($storage_cfg, $vollist);
73 return undef;
74 }});
75
76
77 push @ARGV, 'help' if !scalar(@ARGV);
78
79 my $param = {};
80
81 if ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'pre-start')) {
82 $param->{name} = $ENV{'LXC_NAME'};
83 die "got wrong name" if $param->{name} ne $ARGV[0];
84
85 $param->{path} = $ENV{'LXC_CONFIG_FILE'};
86 $param->{rootfs} = $ENV{'LXC_ROOTFS_PATH'};
87 @ARGV = ();
88 } else {
89 @ARGV = ('help');
90 }
91
92 our $cmddef = [ __PACKAGE__, 'lxc-pve-prestart-hook', [], $param];
93
94 __PACKAGE__->run_cli_handler();