]>
Commit | Line | Data |
---|---|---|
deaf7667 AD |
1 | #!/usr/bin/perl |
2 | ||
b056d074 DM |
3 | package lxc_pve_prestart_hook; |
4 | ||
deaf7667 AD |
5 | use strict; |
6 | use warnings; | |
4ed2b825 | 7 | |
d6811d3f | 8 | exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/; |
4ed2b825 | 9 | |
deaf7667 AD |
10 | use POSIX; |
11 | use File::Path; | |
c9a5774b | 12 | use Fcntl ':mode'; |
deaf7667 AD |
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; | |
c9a5774b | 23 | use PVE::LXC::Setup; |
deaf7667 AD |
24 | use Data::Dumper; |
25 | ||
26 | use 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}; |
2e64f057 AA |
60 | my $skiplock_flag_fn = "/run/lxc/skiplock-$vmid"; |
61 | my $skiplock = 1 if -e $skiplock_flag_fn; | |
62 | unlink $skiplock_flag_fn if $skiplock; | |
d886a5f9 DM |
63 | |
64 | PVE::Cluster::check_cfs_quorum(); # only start if we have quorum | |
65 | ||
67afe46e | 66 | return undef if ! -f PVE::LXC::Config->config_file($vmid); |
d886a5f9 | 67 | |
67afe46e | 68 | my $conf = PVE::LXC::Config->load_config($vmid); |
2e64f057 | 69 | if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) { |
67afe46e | 70 | PVE::LXC::Config->check_lock($conf); |
6da1f91b | 71 | } |
deaf7667 | 72 | |
9459f07d | 73 | my $storage_cfg = PVE::Storage::config(); |
deaf7667 | 74 | |
d250604f FG |
75 | my $vollist = PVE::LXC::Config->get_vm_volumes($conf); |
76 | my $loopdevlist = PVE::LXC::Config->get_vm_volumes($conf, 'rootfs'); | |
deaf7667 | 77 | |
571b33da | 78 | PVE::Storage::activate_volumes($storage_cfg, $vollist); |
c9a5774b WB |
79 | |
80 | my $rootdir = $param->{rootfs}; | |
81 | ||
1e1fad99 WB |
82 | # Delete any leftover reboot-trigger file |
83 | unlink("/var/lib/lxc/$vmid/reboot"); | |
84 | ||
50df544c WB |
85 | my $devlist_file = "/var/lib/lxc/$vmid/devices"; |
86 | unlink $devlist_file; | |
87 | my $devices = []; | |
88 | ||
c9a5774b WB |
89 | my $setup_mountpoint = sub { |
90 | my ($ms, $mountpoint) = @_; | |
91 | ||
92 | #return if $ms eq 'rootfs'; | |
50df544c WB |
93 | my (undef, undef, $dev) = PVE::LXC::mountpoint_mount($mountpoint, $rootdir, $storage_cfg); |
94 | push @$devices, $dev if $dev && $mountpoint->{quota}; | |
c9a5774b WB |
95 | }; |
96 | ||
3c99150a | 97 | # Unmount first when the user mounted the container with "pct mount". |
bc9c4542 WB |
98 | eval { |
99 | PVE::Tools::run_command(['umount', '--recursive', $rootdir], outfunc => sub {}, errfunc => sub {}); | |
100 | }; | |
3c99150a | 101 | |
d250604f | 102 | PVE::LXC::Config->foreach_mountpoint($conf, $setup_mountpoint); |
c9a5774b | 103 | |
c9a5774b WB |
104 | my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); |
105 | $lxc_setup->pre_start_hook(); | |
50df544c WB |
106 | |
107 | if (@$devices) { | |
1e678370 | 108 | my $devlist = ''; |
50df544c WB |
109 | foreach my $dev (@$devices) { |
110 | my ($mode, $rdev) = (stat($dev))[2,6]; | |
111 | next if !$mode || !S_ISBLK($mode) || !$rdev; | |
112 | my $major = int($rdev / 0x100); | |
113 | my $minor = $rdev % 0x100; | |
1e678370 | 114 | $devlist .= "b:$major:$minor:$dev\n"; |
50df544c | 115 | } |
1e678370 | 116 | PVE::Tools::file_set_contents($devlist_file, $devlist); |
50df544c | 117 | } |
deaf7667 AD |
118 | return undef; |
119 | }}); | |
120 | ||
121 | ||
122 | push @ARGV, 'help' if !scalar(@ARGV); | |
123 | ||
124 | my $param = {}; | |
125 | ||
b056d074 | 126 | if ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'pre-start')) { |
deaf7667 AD |
127 | $param->{name} = $ENV{'LXC_NAME'}; |
128 | die "got wrong name" if $param->{name} ne $ARGV[0]; | |
129 | ||
130 | $param->{path} = $ENV{'LXC_CONFIG_FILE'}; | |
131 | $param->{rootfs} = $ENV{'LXC_ROOTFS_PATH'}; | |
132 | @ARGV = (); | |
133 | } else { | |
134 | @ARGV = ('help'); | |
135 | } | |
136 | ||
b056d074 | 137 | our $cmddef = [ __PACKAGE__, 'lxc-pve-prestart-hook', [], $param]; |
deaf7667 | 138 | |
b056d074 | 139 | __PACKAGE__->run_cli_handler(); |