]>
git.proxmox.com Git - pve-container.git/blob - src/lxc-pve-prestart-hook
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 if (!$skiplock && !PVE
::LXC
::Config-
>has_lock($conf, 'mounted')) {
34 PVE
::LXC
::Config-
>check_lock($conf);
37 my $storage_cfg = PVE
::Storage
::config
();
39 my $vollist = PVE
::LXC
::Config-
>get_vm_volumes($conf);
40 my $loopdevlist = PVE
::LXC
::Config-
>get_vm_volumes($conf, 'rootfs');
42 PVE
::Storage
::activate_volumes
($storage_cfg, $vollist);
44 my $rootdir = $vars->{ROOTFS_PATH
};
46 # Delete any leftover reboot-trigger file
47 unlink("/var/lib/lxc/$vmid/reboot");
49 my $devlist_file = "/var/lib/lxc/$vmid/devices";
53 my (undef, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
55 # Unmount first when the user mounted the container with "pct mount".
57 PVE
::Tools
::run_command
(['umount', '--recursive', $rootdir], outfunc
=> sub {}, errfunc
=> sub {});
61 if (!PVE
::LXC
::Tools
::can_use_new_mount_api
()) {
62 # Legacy mode for old kernels:
63 $setup_mountpoint = sub {
64 my ($opt, $mountpoint) = @_;
66 my (undef, undef, $dev) = PVE
::LXC
::mountpoint_mount
(
74 push @$devices, $dev if $dev && $mountpoint->{quota
};
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) = @_;
82 my $dir = PVE
::LXC
::get_staging_mount_path
($opt);
83 my (undef, undef, $dev, $mount_fd) = PVE
::LXC
::mountpoint_stage
(
94 # Mount relative to the rootdir fd.
95 $ddir = './' . $mountpoint->{mp
};
97 # Assert that 'rootfs' is the first one:
98 die "foreach_mount() error\n" if $opt ne 'rootfs';
100 # Mount the rootfs absolutely (rootdir_fd=undef uses AT_FDCWD).
101 # $rootdir is not controlled by the container, so this is fine.
105 PVE
::LXC
::mountpoint_insert_staged
(
114 # From now on we mount inside our rootfs:
116 $rootdir_fd = $mount_fd;
119 push @$devices, $dev if $dev && $mountpoint->{quota
};
123 PVE
::LXC
::Config-
>foreach_mountpoint($conf, $setup_mountpoint);
125 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir);
126 $lxc_setup->pre_start_hook();
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";
137 PVE
::Tools
::file_set_contents
($devlist_file, $devlist);