]> git.proxmox.com Git - pve-container.git/blob - src/lxc-pve-prestart-hook
8ff46721f5033a53966e360648a7073012b4996d
[pve-container.git] / src / lxc-pve-prestart-hook
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use POSIX;
6 use File::Path;
7
8 use PVE::SafeSyslog;
9 use PVE::Tools;
10 use PVE::Cluster;
11 use PVE::INotify;
12 use PVE::RPCEnvironment;
13 use PVE::JSONSchema qw(get_standard_option);
14 use PVE::CLIHandler;
15 use PVE::Storage;
16 use PVE::LXC;
17 use Data::Dumper;
18
19 use base qw(PVE::CLIHandler);
20
21 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
22
23 initlog ('lxc-pve-prestart-hook');
24
25 die "please run as root\n" if $> != 0;
26
27 PVE::INotify::inotify_init();
28
29 my $rpcenv = PVE::RPCEnvironment->init('cli');
30 $rpcenv->set_language($ENV{LANG});
31 $rpcenv->set_user('root@pam');
32
33 # we cannot use cfs_read here (permission problem)
34 #$rpcenv->init_request();
35
36 my $nodename = PVE::INotify::nodename();
37
38 __PACKAGE__->register_method ({
39 name => 'lxc-pve-prestart-hook',
40 path => 'lxc-pve-prestart-hook',
41 method => 'GET',
42 description => "Create a new container root directory.",
43 parameters => {
44 additionalProperties => 0,
45 properties => {
46 name => {
47 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).",
48 type => 'string',
49 pattern => '\S+',
50 maxLength => 64,
51 },
52 path => {
53 description => "The path to the container configuration directory (LXC internal argument - do not pass manually!).",
54 type => 'string',
55 },
56 rootfs => {
57 description => "The path to the container's rootfs (LXC internal argument - do not pass manually!)",
58 type => 'string',
59 },
60 },
61 },
62 returns => { type => 'null' },
63
64 code => sub {
65 my ($param) = @_;
66
67 return undef if $param->{name} !~ m/^\d+$/;
68
69 my $rootdir = $ENV{LXC_ROOTFS_MOUNT};
70
71 my $vmid = $param->{name};
72
73 PVE::Cluster::check_cfs_quorum(); # only start if we have quorum
74
75 return undef if ! -f PVE::LXC::config_file($vmid);
76
77 my $conf = PVE::LXC::load_config($vmid);
78
79 my $storage_cfg = PVE::Storage::config();
80
81 my $vollist = [];
82 my $loopdevlist = [];
83
84 PVE::LXC::foreach_mountpoint($conf, sub {
85 my ($ms, $mountpoint) = @_;
86
87 my $volid = $mountpoint->{volume};
88 return if !$volid || $volid =~ m|^/dev/.+|;
89 push @$vollist, $volid;
90 push @$loopdevlist, $volid if $ms ne 'rootfs';
91 });
92
93 PVE::Storage::activate_volumes($storage_cfg, $vollist);
94 PVE::LXC::attach_loops($storage_cfg, $loopdevlist);
95 return undef;
96 }});
97
98
99 push @ARGV, 'help' if !scalar(@ARGV);
100
101 my $param = {};
102
103 if ((scalar(@ARGV) == 1) && ($ARGV[0] eq 'printmanpod') ||
104 ($ARGV[0] eq 'verifyapi')) {
105 # OK
106 } elsif ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'pre-start')) {
107 $param->{name} = $ENV{'LXC_NAME'};
108 die "got wrong name" if $param->{name} ne $ARGV[0];
109
110 $param->{path} = $ENV{'LXC_CONFIG_FILE'};
111 $param->{rootfs} = $ENV{'LXC_ROOTFS_PATH'};
112 @ARGV = ();
113 } else {
114 @ARGV = ('help');
115 }
116
117 my $cmddef = [ __PACKAGE__, 'lxc-pve-prestart-hook', [], $param];
118
119 PVE::CLIHandler::handle_simple_cmd($cmddef, \@ARGV, undef, $0);
120
121 exit 0;
122
123 __END__
124
125 =head1 NAME
126
127 lxc-pve - LXC pre-start hook for Proxmox VE
128
129 =head1 SYNOPSIS
130
131 =include synopsis
132
133 =head1 DESCRIPTION
134
135 This mount hook activate storage and volumes for pve container.
136
137 =head1 SEE ALSO
138
139 lct(1)
140
141 =include pve_copyright