]> git.proxmox.com Git - pve-container.git/blame - src/lxc-pve-prestart-hook
Implement pct mount/unmount
[pve-container.git] / src / lxc-pve-prestart-hook
CommitLineData
deaf7667
AD
1#!/usr/bin/perl
2
b056d074
DM
3package lxc_pve_prestart_hook;
4
deaf7667
AD
5use strict;
6use warnings;
4ed2b825 7
d6811d3f 8exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/;
4ed2b825 9
deaf7667
AD
10use POSIX;
11use File::Path;
c9a5774b 12use Fcntl ':mode';
deaf7667
AD
13
14use PVE::SafeSyslog;
15use PVE::Tools;
16use PVE::Cluster;
17use PVE::INotify;
18use PVE::RPCEnvironment;
19use PVE::JSONSchema qw(get_standard_option);
20use PVE::CLIHandler;
21use PVE::Storage;
22use PVE::LXC;
c9a5774b 23use PVE::LXC::Setup;
deaf7667
AD
24use Data::Dumper;
25
26use base qw(PVE::CLIHandler);
27
deaf7667
AD
28__PACKAGE__->register_method ({
29 name => 'lxc-pve-prestart-hook',
30 path => 'lxc-pve-prestart-hook',
31 method => 'GET',
32 description => "Create a new container root directory.",
33 parameters => {
34 additionalProperties => 0,
35 properties => {
36 name => {
c0c77476 37 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
38 type => 'string',
39 pattern => '\S+',
40 maxLength => 64,
41 },
42 path => {
43 description => "The path to the container configuration directory (LXC internal argument - do not pass manually!).",
44 type => 'string',
45 },
46 rootfs => {
47 description => "The path to the container's rootfs (LXC internal argument - do not pass manually!)",
48 type => 'string',
49 },
50 },
51 },
52 returns => { type => 'null' },
53
54 code => sub {
55 my ($param) = @_;
56
57 return undef if $param->{name} !~ m/^\d+$/;
58
9459f07d 59 my $vmid = $param->{name};
d886a5f9
DM
60
61 PVE::Cluster::check_cfs_quorum(); # only start if we have quorum
62
9459f07d 63 return undef if ! -f PVE::LXC::config_file($vmid);
d886a5f9 64
9459f07d 65 my $conf = PVE::LXC::load_config($vmid);
2de0b3d4 66 PVE::LXC::check_lock($conf) if !$ENV{PVE_SKIPLOCK};
deaf7667 67
9459f07d 68 my $storage_cfg = PVE::Storage::config();
deaf7667 69
09aa32fd
AD
70 my $vollist = PVE::LXC::get_vm_volumes($conf);
71 my $loopdevlist = PVE::LXC::get_vm_volumes($conf, 'rootfs');
deaf7667 72
571b33da 73 PVE::Storage::activate_volumes($storage_cfg, $vollist);
c9a5774b
WB
74
75 my $rootdir = $param->{rootfs};
76
50df544c
WB
77 my $devlist_file = "/var/lib/lxc/$vmid/devices";
78 unlink $devlist_file;
79 my $devices = [];
80
c9a5774b
WB
81 my $setup_mountpoint = sub {
82 my ($ms, $mountpoint) = @_;
83
84 #return if $ms eq 'rootfs';
50df544c
WB
85 my (undef, undef, $dev) = PVE::LXC::mountpoint_mount($mountpoint, $rootdir, $storage_cfg);
86 push @$devices, $dev if $dev && $mountpoint->{quota};
c9a5774b
WB
87 };
88
c9a5774b
WB
89 PVE::LXC::foreach_mountpoint($conf, $setup_mountpoint);
90
c9a5774b
WB
91 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir);
92 $lxc_setup->pre_start_hook();
50df544c
WB
93
94 if (@$devices) {
1e678370 95 my $devlist = '';
50df544c
WB
96 foreach my $dev (@$devices) {
97 my ($mode, $rdev) = (stat($dev))[2,6];
98 next if !$mode || !S_ISBLK($mode) || !$rdev;
99 my $major = int($rdev / 0x100);
100 my $minor = $rdev % 0x100;
1e678370 101 $devlist .= "b:$major:$minor:$dev\n";
50df544c 102 }
1e678370 103 PVE::Tools::file_set_contents($devlist_file, $devlist);
50df544c 104 }
deaf7667
AD
105 return undef;
106 }});
107
108
109push @ARGV, 'help' if !scalar(@ARGV);
110
111my $param = {};
112
b056d074 113if ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'pre-start')) {
deaf7667
AD
114 $param->{name} = $ENV{'LXC_NAME'};
115 die "got wrong name" if $param->{name} ne $ARGV[0];
116
117 $param->{path} = $ENV{'LXC_CONFIG_FILE'};
118 $param->{rootfs} = $ENV{'LXC_ROOTFS_PATH'};
119 @ARGV = ();
120} else {
121 @ARGV = ('help');
122}
123
b056d074 124our $cmddef = [ __PACKAGE__, 'lxc-pve-prestart-hook', [], $param];
deaf7667 125
b056d074 126__PACKAGE__->run_cli_handler();