#!/usr/bin/perl
+package lxc_pve_prestart_hook;
+
use strict;
use warnings;
-use POSIX;
+
+use Fcntl qw(O_DIRECTORY :mode);
use File::Path;
+use POSIX;
-use PVE::SafeSyslog;
-use PVE::Tools;
use PVE::Cluster;
-use PVE::INotify;
-use PVE::RPCEnvironment;
-use PVE::JSONSchema qw(get_standard_option);
-use PVE::CLIHandler;
-use PVE::Storage;
+use PVE::LXC::Config;
+use PVE::LXC::Setup;
+use PVE::LXC::Tools;
use PVE::LXC;
-use Data::Dumper;
-
-use base qw(PVE::CLIHandler);
-
-$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
-
-initlog ('lxc-pve-prestart-hook');
-
-die "please run as root\n" if $> != 0;
-
-PVE::INotify::inotify_init();
-
-my $rpcenv = PVE::RPCEnvironment->init('cli');
-$rpcenv->set_language($ENV{LANG});
-$rpcenv->set_user('root@pam');
-
-# we cannot use cfs_read here (permission problem)
-#$rpcenv->init_request();
-
-my $nodename = PVE::INotify::nodename();
-
-__PACKAGE__->register_method ({
- name => 'lxc-pve-prestart-hook',
- path => 'lxc-pve-prestart-hook',
- method => 'GET',
- description => "Create a new container root directory.",
- parameters => {
- additionalProperties => 0,
- properties => {
- name => {
- description => "The container name. This hook is only active for containers using numeric IDs, where configuration is stored on /etc/pve/lxc/<name>.conf (else it is just a NOP).",
- type => 'string',
- pattern => '\S+',
- maxLength => 64,
- },
- path => {
- description => "The path to the container configuration directory (LXC internal argument - do not pass manually!).",
- type => 'string',
- },
- rootfs => {
- description => "The path to the container's rootfs (LXC internal argument - do not pass manually!)",
- type => 'string',
- },
- },
- },
- returns => { type => 'null' },
-
- code => sub {
- my ($param) = @_;
-
- return undef if $param->{name} !~ m/^\d+$/;
-
- my $rootdir = $ENV{LXC_ROOTFS_MOUNT};
-
- my $vmid = $param->{name};
-
- PVE::Cluster::check_cfs_quorum(); # only start if we have quorum
-
- return undef if ! -f PVE::LXC::config_file($vmid);
-
- my $conf = PVE::LXC::load_config($vmid);
-
- my $storage_cfg = PVE::Storage::config();
-
- PVE::LXC::foreach_mountpoint($conf, sub {
- my ($ms, $mountpoint) = @_;
-
- my $volid = $mountpoint->{volume};
- return if !$volid || $volid =~ m|^/dev/.+|;
-
- PVE::Storage::activate_volumes($storage_cfg, [$volid]);
-
- my ($storage, $volname) = PVE::Storage::parse_volume_id($volid);
- my $scfg = PVE::Storage::storage_config($storage_cfg, $storage);
- my ($vtype, undef, undef, undef, undef, $isBase, $format) =
- PVE::Storage::parse_volname($storage_cfg, $volid);
-
- if($ms ne 'rootfs' && $format ne 'subvol' && ($scfg->{type} eq 'dir' || $scfg->{type} eq 'nfs')) {
- my $path = PVE::Storage::path($storage_cfg, $volid);
- PVE::Tools::run_command(['losetup', '--find', '--show', $path]);
+use PVE::Storage;
+use PVE::Syscall qw(:fsmount);
+use PVE::Tools qw(AT_FDCWD O_PATH);
+
+PVE::LXC::Tools::lxc_hook('pre-start', 'lxc', sub {
+ my ($vmid, $vars, undef, undef) = @_;
+
+ my $skiplock_flag_fn = "/run/lxc/skiplock-$vmid";
+ my $skiplock = 1 if -e $skiplock_flag_fn;
+ unlink $skiplock_flag_fn if $skiplock;
+
+ PVE::Cluster::check_cfs_quorum(); # only start if we have quorum
+
+ return undef if ! -f PVE::LXC::Config->config_file($vmid);
+
+ my $conf = PVE::LXC::Config->load_config($vmid);
+ if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
+ PVE::LXC::Config->check_lock($conf);
+ }
+
+ my $storage_cfg = PVE::Storage::config();
+
+ my $vollist = PVE::LXC::Config->get_vm_volumes($conf);
+ my $loopdevlist = PVE::LXC::Config->get_vm_volumes($conf, 'rootfs');
+
+ PVE::Storage::activate_volumes($storage_cfg, $vollist);
+
+ my $rootdir = $vars->{ROOTFS_PATH};
+
+ # Delete any leftover reboot-trigger file
+ unlink("/var/lib/lxc/$vmid/reboot");
+
+ my $devlist_file = "/var/lib/lxc/$vmid/devices";
+ unlink $devlist_file;
+ my $devices = [];
+
+ my (undef, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
+
+ # Unmount first when the user mounted the container with "pct mount".
+ eval {
+ PVE::Tools::run_command(['umount', '--recursive', $rootdir], outfunc => sub {}, errfunc => sub {});
+ };
+
+ my $setup_mountpoint;
+ if (!PVE::LXC::Tools::can_use_new_mount_api()) {
+ # Legacy mode for old kernels:
+ $setup_mountpoint = sub {
+ my ($opt, $mountpoint) = @_;
+
+ my (undef, undef, $dev) = PVE::LXC::mountpoint_mount(
+ $mountpoint,
+ $rootdir,
+ $storage_cfg,
+ undef,
+ $rootuid,
+ $rootgid,
+ );
+ push @$devices, $dev if $dev && $mountpoint->{quota};
+ };
+ } else {
+ # With newer kernels we stage mount points and then use move_mount().
+ my $rootdir_fd = undef;
+ $setup_mountpoint = sub {
+ my ($opt, $mountpoint) = @_;
+
+ my $dir = PVE::LXC::get_staging_mount_path($opt);
+ my (undef, undef, $dev, $mount_fd) = PVE::LXC::mountpoint_stage(
+ $mountpoint,
+ $dir,
+ $storage_cfg,
+ undef,
+ $rootuid,
+ $rootgid,
+ );
+
+ my $ddir;
+ if ($rootdir_fd) {
+ # Mount relative to the rootdir fd.
+ $ddir = './' . $mountpoint->{mp};
+ } else {
+ # Assert that 'rootfs' is the first one:
+ die "foreach_mount() error\n" if $opt ne 'rootfs';
+
+ # Mount the rootfs absolutely (rootdir_fd=undef uses AT_FDCWD).
+ # $rootdir is not controlled by the container, so this is fine.
+ $ddir = $rootdir;
}
- });
-
- return undef;
- }});
-
-
-push @ARGV, 'help' if !scalar(@ARGV);
-
-my $param = {};
-
-if ((scalar(@ARGV) == 1) && ($ARGV[0] eq 'printmanpod') ||
- ($ARGV[0] eq 'verifyapi')) {
- # OK
-} elsif ((scalar(@ARGV) == 3) && ($ARGV[1] eq 'lxc') && ($ARGV[2] eq 'pre-start')) {
- $param->{name} = $ENV{'LXC_NAME'};
- die "got wrong name" if $param->{name} ne $ARGV[0];
-
- $param->{path} = $ENV{'LXC_CONFIG_FILE'};
- $param->{rootfs} = $ENV{'LXC_ROOTFS_PATH'};
- @ARGV = ();
-} else {
- @ARGV = ('help');
-}
-
-my $cmddef = [ __PACKAGE__, 'lxc-pve-prestart-hook', [], $param];
-PVE::CLIHandler::handle_simple_cmd($cmddef, \@ARGV, undef, $0);
-
-exit 0;
-
-__END__
-
-=head1 NAME
-
-lxc-pve - LXC pre-start hook for Proxmox VE
-
-=head1 SYNOPSIS
-
-=include synopsis
-
-=head1 DESCRIPTION
-
-This mount hook activate storage and volumes for pve container.
-
-=head1 SEE ALSO
-
-lct(1)
+ PVE::LXC::mountpoint_insert_staged(
+ $mount_fd,
+ $rootdir_fd,
+ $ddir,
+ $opt,
+ $rootuid,
+ $rootgid,
+ );
+
+ # From now on we mount inside our rootfs:
+ if (!$rootdir_fd) {
+ $rootdir_fd = $mount_fd;
+ }
-=include pve_copyright
+ push @$devices, $dev if $dev && $mountpoint->{quota};
+ };
+ }
+
+ PVE::LXC::Config->foreach_mountpoint($conf, $setup_mountpoint);
+
+ my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir);
+ $lxc_setup->pre_start_hook();
+
+ if (@$devices) {
+ my $devlist = '';
+ foreach my $dev (@$devices) {
+ my ($mode, $rdev) = (stat($dev))[2,6];
+ next if !$mode || !S_ISBLK($mode) || !$rdev;
+ my $major = PVE::Tools::dev_t_major($rdev);
+ my $minor = PVE::Tools::dev_t_minor($rdev);
+ $devlist .= "b:$major:$minor:$dev\n";
+ }
+ PVE::Tools::file_set_contents($devlist_file, $devlist);
+ }
+});