]>
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
(
92 my ($dest_dir, $dest_base_fd);
94 # Mount relative to the rootdir fd.
95 $dest_base_fd = $rootdir_fd;
96 $dest_dir = './' . $mountpoint->{mp
};
98 # Assert that 'rootfs' is the first one:
99 die "foreach_mount() error\n" if $opt ne 'rootfs';
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;
108 PVE
::LXC
::mountpoint_insert_staged
(
117 # From now on we mount inside our rootfs:
119 $rootdir_fd = $mount_fd;
122 push @$devices, $dev if $dev && $mountpoint->{quota
};
126 PVE
::LXC
::Config-
>foreach_mountpoint($conf, $setup_mountpoint);
128 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir);
129 $lxc_setup->pre_start_hook();
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";
140 PVE
::Tools
::file_set_contents
($devlist_file, $devlist);