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