]> git.proxmox.com Git - pve-container.git/blob - src/lxc-pve-prestart-hook
tests: fix lxc-usernsexec invocation for uid != gid
[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
25 use base qw(PVE::CLIHandler);
26
27 __PACKAGE__->register_method ({
28 name => 'lxc-pve-prestart-hook',
29 path => 'lxc-pve-prestart-hook',
30 method => 'GET',
31 description => "Create a new container root directory.",
32 parameters => {
33 additionalProperties => 0,
34 properties => {
35 name => {
36 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).",
37 type => 'string',
38 pattern => '\S+',
39 maxLength => 64,
40 },
41 path => {
42 description => "The path to the container configuration directory (LXC internal argument - do not pass manually!).",
43 type => 'string',
44 },
45 rootfs => {
46 description => "The path to the container's rootfs (LXC internal argument - do not pass manually!)",
47 type => 'string',
48 },
49 },
50 },
51 returns => { type => 'null' },
52
53 code => sub {
54 my ($param) = @_;
55
56 return undef if $param->{name} !~ m/^\d+$/;
57
58 my $vmid = $param->{name};
59 my $skiplock_flag_fn = "/run/lxc/skiplock-$vmid";
60 my $skiplock = 1 if -e $skiplock_flag_fn;
61 unlink $skiplock_flag_fn if $skiplock;
62
63 PVE::Cluster::check_cfs_quorum(); # only start if we have quorum
64
65 return undef if ! -f PVE::LXC::Config->config_file($vmid);
66
67 my $conf = PVE::LXC::Config->load_config($vmid);
68 if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
69 PVE::LXC::Config->check_lock($conf);
70 }
71
72 my $storage_cfg = PVE::Storage::config();
73
74 my $vollist = PVE::LXC::Config->get_vm_volumes($conf);
75 my $loopdevlist = PVE::LXC::Config->get_vm_volumes($conf, 'rootfs');
76
77 PVE::Storage::activate_volumes($storage_cfg, $vollist);
78
79 my $rootdir = $param->{rootfs};
80
81 # Delete any leftover reboot-trigger file
82 unlink("/var/lib/lxc/$vmid/reboot");
83
84 my $devlist_file = "/var/lib/lxc/$vmid/devices";
85 unlink $devlist_file;
86 my $devices = [];
87
88 my $setup_mountpoint = sub {
89 my ($ms, $mountpoint) = @_;
90
91 #return if $ms eq 'rootfs';
92 my (undef, undef, $dev) = PVE::LXC::mountpoint_mount($mountpoint, $rootdir, $storage_cfg);
93 push @$devices, $dev if $dev && $mountpoint->{quota};
94 };
95
96 # Unmount first when the user mounted the container with "pct mount".
97 eval {
98 PVE::Tools::run_command(['umount', '--recursive', $rootdir], outfunc => sub {}, errfunc => sub {});
99 };
100
101 PVE::LXC::Config->foreach_mountpoint($conf, $setup_mountpoint);
102
103 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir);
104 $lxc_setup->pre_start_hook();
105
106 if (@$devices) {
107 my $devlist = '';
108 foreach my $dev (@$devices) {
109 my ($mode, $rdev) = (stat($dev))[2,6];
110 next if !$mode || !S_ISBLK($mode) || !$rdev;
111 my $major = PVE::Tools::dev_t_major($rdev);
112 my $minor = PVE::Tools::dev_t_minor($rdev);
113 $devlist .= "b:$major:$minor:$dev\n";
114 }
115 PVE::Tools::file_set_contents($devlist_file, $devlist);
116 }
117 return undef;
118 }});
119
120
121 push @ARGV, 'help' if !scalar(@ARGV);
122
123 my $param = {};
124
125 if ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'pre-start')) {
126 $param->{name} = $ENV{'LXC_NAME'};
127 die "got wrong name" if $param->{name} ne $ARGV[0];
128
129 $param->{path} = $ENV{'LXC_CONFIG_FILE'};
130 $param->{rootfs} = $ENV{'LXC_ROOTFS_PATH'};
131 @ARGV = ();
132 } else {
133 @ARGV = ('help');
134 }
135
136 our $cmddef = [ __PACKAGE__, 'lxc-pve-prestart-hook', [], $param];
137
138 __PACKAGE__->run_cli_handler();