]>
Commit | Line | Data |
---|---|---|
deaf7667 AD |
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; | |
deaf7667 AD |
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 => { | |
c0c77476 | 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).", |
deaf7667 AD |
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 | ||
9459f07d | 71 | my $vmid = $param->{name}; |
d886a5f9 DM |
72 | |
73 | PVE::Cluster::check_cfs_quorum(); # only start if we have quorum | |
74 | ||
9459f07d | 75 | return undef if ! -f PVE::LXC::config_file($vmid); |
d886a5f9 | 76 | |
9459f07d | 77 | my $conf = PVE::LXC::load_config($vmid); |
deaf7667 | 78 | |
9459f07d | 79 | my $storage_cfg = PVE::Storage::config(); |
deaf7667 AD |
80 | |
81 | PVE::LXC::foreach_mountpoint($conf, sub { | |
82 | my ($ms, $mountpoint) = @_; | |
83 | ||
84 | my $volid = $mountpoint->{volume}; | |
27163d2f AD |
85 | return if !$volid || $volid =~ m|^/dev/.+|; |
86 | ||
deaf7667 AD |
87 | PVE::Storage::activate_volumes($storage_cfg, [$volid]); |
88 | ||
89 | my ($storage, $volname) = PVE::Storage::parse_volume_id($volid); | |
90 | my $scfg = PVE::Storage::storage_config($storage_cfg, $storage); | |
91 | my ($vtype, undef, undef, undef, undef, $isBase, $format) = | |
92 | PVE::Storage::parse_volname($storage_cfg, $volid); | |
93 | ||
94 | if($ms ne 'rootfs' && $format ne 'subvol' && ($scfg->{type} eq 'dir' || $scfg->{type} eq 'nfs')) { | |
95 | my $path = PVE::Storage::path($storage_cfg, $volid); | |
96 | PVE::Tools::run_command(['losetup', '--find', '--show', $path]); | |
97 | } | |
98 | }); | |
99 | ||
100 | return undef; | |
101 | }}); | |
102 | ||
103 | ||
104 | push @ARGV, 'help' if !scalar(@ARGV); | |
105 | ||
106 | my $param = {}; | |
107 | ||
108 | if ((scalar(@ARGV) == 1) && ($ARGV[0] eq 'printmanpod') || | |
109 | ($ARGV[0] eq 'verifyapi')) { | |
110 | # OK | |
111 | } elsif ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'pre-start')) { | |
112 | $param->{name} = $ENV{'LXC_NAME'}; | |
113 | die "got wrong name" if $param->{name} ne $ARGV[0]; | |
114 | ||
115 | $param->{path} = $ENV{'LXC_CONFIG_FILE'}; | |
116 | $param->{rootfs} = $ENV{'LXC_ROOTFS_PATH'}; | |
117 | @ARGV = (); | |
118 | } else { | |
119 | @ARGV = ('help'); | |
120 | } | |
121 | ||
122 | my $cmddef = [ __PACKAGE__, 'lxc-pve-prestart-hook', [], $param]; | |
123 | ||
124 | PVE::CLIHandler::handle_simple_cmd($cmddef, \@ARGV, undef, $0); | |
125 | ||
126 | exit 0; | |
127 | ||
128 | __END__ | |
129 | ||
130 | =head1 NAME | |
131 | ||
132 | lxc-pve - LXC pre-start hook for Proxmox VE | |
133 | ||
134 | =head1 SYNOPSIS | |
135 | ||
136 | =include synopsis | |
137 | ||
138 | =head1 DESCRIPTION | |
139 | ||
140 | This mount hook activate storage and volumes for pve container. | |
141 | ||
142 | =head1 SEE ALSO | |
143 | ||
144 | lct(1) | |
145 | ||
146 | =include pve_copyright |