]> git.proxmox.com Git - pve-container.git/blob - src/lxc-pve-prestart-hook
fd2942323b565ae3a94a7447964a5c5e41faf49d
[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 use Fcntl ':mode';
13
14 use PVE::SafeSyslog;
15 use PVE::Tools;
16 use PVE::Cluster;
17 use PVE::INotify;
18 use PVE::RPCEnvironment;
19 use PVE::JSONSchema qw(get_standard_option);
20 use PVE::CLIHandler;
21 use PVE::Storage;
22 use PVE::LXC;
23 use PVE::LXC::Setup;
24 use Data::Dumper;
25
26 use base qw(PVE::CLIHandler);
27
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 => {
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).",
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
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->config_file($vmid);
64
65 my $conf = PVE::LXC::Config->load_config($vmid);
66 if (!$ENV{PVE_SKIPLOCK} && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
67 PVE::LXC::Config->check_lock($conf);
68 }
69
70 my $storage_cfg = PVE::Storage::config();
71
72 my $vollist = PVE::LXC::Config->get_vm_volumes($conf);
73 my $loopdevlist = PVE::LXC::Config->get_vm_volumes($conf, 'rootfs');
74
75 PVE::Storage::activate_volumes($storage_cfg, $vollist);
76
77 my $rootdir = $param->{rootfs};
78
79 # Delete any leftover reboot-trigger file
80 unlink("/var/lib/lxc/$vmid/reboot");
81
82 my $devlist_file = "/var/lib/lxc/$vmid/devices";
83 unlink $devlist_file;
84 my $devices = [];
85
86 my $setup_mountpoint = sub {
87 my ($ms, $mountpoint) = @_;
88
89 #return if $ms eq 'rootfs';
90 my (undef, undef, $dev) = PVE::LXC::mountpoint_mount($mountpoint, $rootdir, $storage_cfg);
91 push @$devices, $dev if $dev && $mountpoint->{quota};
92 };
93
94 PVE::LXC::Config->foreach_mountpoint($conf, $setup_mountpoint);
95
96 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir);
97 $lxc_setup->pre_start_hook();
98
99 if (@$devices) {
100 my $devlist = '';
101 foreach my $dev (@$devices) {
102 my ($mode, $rdev) = (stat($dev))[2,6];
103 next if !$mode || !S_ISBLK($mode) || !$rdev;
104 my $major = int($rdev / 0x100);
105 my $minor = $rdev % 0x100;
106 $devlist .= "b:$major:$minor:$dev\n";
107 }
108 PVE::Tools::file_set_contents($devlist_file, $devlist);
109 }
110 return undef;
111 }});
112
113
114 push @ARGV, 'help' if !scalar(@ARGV);
115
116 my $param = {};
117
118 if ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'pre-start')) {
119 $param->{name} = $ENV{'LXC_NAME'};
120 die "got wrong name" if $param->{name} ne $ARGV[0];
121
122 $param->{path} = $ENV{'LXC_CONFIG_FILE'};
123 $param->{rootfs} = $ENV{'LXC_ROOTFS_PATH'};
124 @ARGV = ();
125 } else {
126 @ARGV = ('help');
127 }
128
129 our $cmddef = [ __PACKAGE__, 'lxc-pve-prestart-hook', [], $param];
130
131 __PACKAGE__->run_cli_handler();