]> git.proxmox.com Git - pve-container.git/blob - src/lxc-pve-prestart-hook
use CGroup::get_io_stats
[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 use Fcntl qw(O_DIRECTORY :mode);
9 use File::Path;
10 use POSIX;
11
12 use PVE::Cluster;
13 use PVE::LXC::Config;
14 use PVE::LXC::Setup;
15 use PVE::LXC::Tools;
16 use PVE::LXC;
17 use PVE::Storage;
18 use PVE::Syscall qw(:fsmount);
19 use PVE::Tools qw(AT_FDCWD O_PATH);
20
21 PVE::LXC::Tools::lxc_hook('pre-start', 'lxc', sub {
22 my ($vmid, $vars, undef, undef) = @_;
23
24 my $skiplock_flag_fn = "/run/lxc/skiplock-$vmid";
25 my $skiplock = 1 if -e $skiplock_flag_fn;
26 unlink $skiplock_flag_fn if $skiplock;
27
28 PVE::Cluster::check_cfs_quorum(); # only start if we have quorum
29
30 return undef if ! -f PVE::LXC::Config->config_file($vmid);
31
32 my $conf = PVE::LXC::Config->load_config($vmid);
33 if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
34 PVE::LXC::Config->check_lock($conf);
35 }
36
37 my $storage_cfg = PVE::Storage::config();
38
39 my $vollist = PVE::LXC::Config->get_vm_volumes($conf);
40 my $loopdevlist = PVE::LXC::Config->get_vm_volumes($conf, 'rootfs');
41
42 PVE::Storage::activate_volumes($storage_cfg, $vollist);
43
44 my $rootdir = $vars->{ROOTFS_PATH};
45
46 # Delete any leftover reboot-trigger file
47 unlink("/var/lib/lxc/$vmid/reboot");
48
49 my $devlist_file = "/var/lib/lxc/$vmid/devices";
50 unlink $devlist_file;
51 my $devices = [];
52
53 my (undef, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
54
55 # Unmount first when the user mounted the container with "pct mount".
56 eval {
57 PVE::Tools::run_command(['umount', '--recursive', $rootdir], outfunc => sub {}, errfunc => sub {});
58 };
59
60 my $setup_mountpoint;
61 if (!PVE::LXC::Tools::can_use_new_mount_api()) {
62 # Legacy mode for old kernels:
63 $setup_mountpoint = sub {
64 my ($opt, $mountpoint) = @_;
65
66 my (undef, undef, $dev) = PVE::LXC::mountpoint_mount(
67 $mountpoint,
68 $rootdir,
69 $storage_cfg,
70 undef,
71 $rootuid,
72 $rootgid,
73 );
74 push @$devices, $dev if $dev && $mountpoint->{quota};
75 };
76 } else {
77 # With newer kernels we stage mount points and then use move_mount().
78 my $rootdir_fd = undef;
79 $setup_mountpoint = sub {
80 my ($opt, $mountpoint) = @_;
81
82 my $dir = PVE::LXC::get_staging_mount_path($opt);
83 my (undef, undef, $dev, $mount_fd) = PVE::LXC::mountpoint_stage(
84 $mountpoint,
85 $dir,
86 $storage_cfg,
87 undef,
88 $rootuid,
89 $rootgid,
90 );
91
92 my ($dest_dir, $dest_base_fd);
93 if ($rootdir_fd) {
94 # Mount relative to the rootdir fd.
95 $dest_base_fd = $rootdir_fd;
96 $dest_dir = './' . $mountpoint->{mp};
97 } else {
98 # Assert that 'rootfs' is the first one:
99 die "foreach_mount() error\n" if $opt ne 'rootfs';
100
101 # Mount the rootfs absolutely.
102 # $rootdir is not controlled by the container, so this is fine.
103 sysopen($dest_base_fd, '/', O_PATH | O_DIRECTORY)
104 or die "failed to open '.': $!\n";
105 $dest_dir = $rootdir;
106 }
107
108 PVE::LXC::mountpoint_insert_staged(
109 $mount_fd,
110 $dest_base_fd,
111 $dest_dir,
112 $opt,
113 $rootuid,
114 $rootgid,
115 );
116
117 # From now on we mount inside our rootfs:
118 if (!$rootdir_fd) {
119 $rootdir_fd = $mount_fd;
120 }
121
122 push @$devices, $dev if $dev && $mountpoint->{quota};
123 };
124 }
125
126 PVE::LXC::Config->foreach_mountpoint($conf, $setup_mountpoint);
127
128 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir);
129 $lxc_setup->pre_start_hook();
130
131 if (@$devices) {
132 my $devlist = '';
133 foreach my $dev (@$devices) {
134 my ($mode, $rdev) = (stat($dev))[2,6];
135 next if !$mode || !S_ISBLK($mode) || !$rdev;
136 my $major = PVE::Tools::dev_t_major($rdev);
137 my $minor = PVE::Tools::dev_t_minor($rdev);
138 $devlist .= "b:$major:$minor:$dev\n";
139 }
140 PVE::Tools::file_set_contents($devlist_file, $devlist);
141 }
142 });