]>
git.proxmox.com Git - pve-container.git/blob - src/lxc-pve-prestart-hook
ee4f7e2e5a717ec1e1a1df4c2d539045ac7c89b1
3 package lxc_pve_prestart_hook
;
8 use Fcntl
qw(O_DIRECTORY :mode);
18 use PVE
::Syscall
qw(:fsmount);
19 use PVE
::Tools
qw(AT_FDCWD O_PATH);
21 PVE
::LXC
::Tools
::lxc_hook
('pre-start', 'lxc', sub {
22 my ($vmid, $vars, undef, undef) = @_;
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;
28 PVE
::Cluster
::check_cfs_quorum
(); # only start if we have quorum
30 return undef if ! -f PVE
::LXC
::Config-
>config_file($vmid);
32 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
33 my $storage_cfg = PVE
::Storage
::config
();
35 # apply pending changes if present
36 if ($conf->{pending
}) {
37 PVE
::LXC
::Config-
>vmconfig_apply_pending($vmid, $conf, $storage_cfg);
38 $conf = PVE
::LXC
::Config-
>load_config($vmid); # just to be sure
41 if (!$skiplock && !PVE
::LXC
::Config-
>has_lock($conf, 'mounted')) {
42 PVE
::LXC
::Config-
>check_lock($conf);
46 my $vollist = PVE
::LXC
::Config-
>get_vm_volumes($conf);
47 my $loopdevlist = PVE
::LXC
::Config-
>get_vm_volumes($conf, 'rootfs');
49 PVE
::Storage
::activate_volumes
($storage_cfg, $vollist);
51 my $rootdir = $vars->{ROOTFS_PATH
};
53 # Delete any leftover reboot-trigger file
54 unlink("/var/lib/lxc/$vmid/reboot");
56 my $devlist_file = "/var/lib/lxc/$vmid/devices";
60 my (undef, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
62 # Unmount first when the user mounted the container with "pct mount".
64 PVE
::Tools
::run_command
(['umount', '--recursive', $rootdir], outfunc
=> sub {}, errfunc
=> sub {});
68 if (!PVE
::LXC
::Tools
::can_use_new_mount_api
()) {
69 # Legacy mode for old kernels:
70 $setup_mountpoint = sub {
71 my ($opt, $mountpoint) = @_;
73 my (undef, undef, $dev) = PVE
::LXC
::mountpoint_mount
(
81 push @$devices, $dev if $dev && $mountpoint->{quota
};
84 # With newer kernels we stage mount points and then use move_mount().
85 my $rootdir_fd = undef;
86 $setup_mountpoint = sub {
87 my ($opt, $mountpoint) = @_;
89 my $dir = PVE
::LXC
::get_staging_mount_path
($opt);
90 my (undef, undef, $dev, $mount_fd) = PVE
::LXC
::mountpoint_stage
(
101 # Mount relative to the rootdir fd.
102 $ddir = './' . $mountpoint->{mp
};
104 # Assert that 'rootfs' is the first one:
105 die "foreach_mount() error\n" if $opt ne 'rootfs';
107 # Mount the rootfs absolutely (rootdir_fd=undef uses AT_FDCWD).
108 # $rootdir is not controlled by the container, so this is fine.
112 PVE
::LXC
::mountpoint_insert_staged
(
121 # From now on we mount inside our rootfs:
123 $rootdir_fd = $mount_fd;
126 push @$devices, $dev if $dev && $mountpoint->{quota
};
130 PVE
::LXC
::Config-
>foreach_mountpoint($conf, $setup_mountpoint);
132 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir);
133 $lxc_setup->pre_start_hook();
137 foreach my $dev (@$devices) {
138 my ($mode, $rdev) = (stat($dev))[2,6];
139 next if !$mode || !S_ISBLK
($mode) || !$rdev;
140 my $major = PVE
::Tools
::dev_t_major
($rdev);
141 my $minor = PVE
::Tools
::dev_t_minor
($rdev);
142 $devlist .= "b:$major:$minor:$dev\n";
144 PVE
::Tools
::file_set_contents
($devlist_file, $devlist);