]> git.proxmox.com Git - pve-container.git/blob - src/lxc-pve-prestart-hook
Revert "apply pending changes in lxc prestart hook"
[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 $ddir;
93 if ($rootdir_fd) {
94 # Mount relative to the rootdir fd.
95 $ddir = './' . $mountpoint->{mp};
96 } else {
97 # Assert that 'rootfs' is the first one:
98 die "foreach_mount() error\n" if $opt ne 'rootfs';
99
100 # Mount the rootfs absolutely (rootdir_fd=undef uses AT_FDCWD).
101 # $rootdir is not controlled by the container, so this is fine.
102 $ddir = $rootdir;
103 }
104
105 PVE::LXC::mountpoint_insert_staged(
106 $mount_fd,
107 $rootdir_fd,
108 $ddir,
109 $opt,
110 $rootuid,
111 $rootgid,
112 );
113
114 # From now on we mount inside our rootfs:
115 if (!$rootdir_fd) {
116 $rootdir_fd = $mount_fd;
117 }
118
119 push @$devices, $dev if $dev && $mountpoint->{quota};
120 };
121 }
122
123 PVE::LXC::Config->foreach_mountpoint($conf, $setup_mountpoint);
124
125 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir);
126 $lxc_setup->pre_start_hook();
127
128 if (@$devices) {
129 my $devlist = '';
130 foreach my $dev (@$devices) {
131 my ($mode, $rdev) = (stat($dev))[2,6];
132 next if !$mode || !S_ISBLK($mode) || !$rdev;
133 my $major = PVE::Tools::dev_t_major($rdev);
134 my $minor = PVE::Tools::dev_t_minor($rdev);
135 $devlist .= "b:$major:$minor:$dev\n";
136 }
137 PVE::Tools::file_set_contents($devlist_file, $devlist);
138 }
139 });