]> git.proxmox.com Git - pve-container.git/blobdiff - src/lxc-pve-prestart-hook
docs: update mountpoint descriptions
[pve-container.git] / src / lxc-pve-prestart-hook
index 764cd2bf0d3832fd2569cfec8eb976461d345d92..9835b1c6e8f958953a751988383706e5f44ad856 100755 (executable)
@@ -1,9 +1,15 @@
 #!/usr/bin/perl
 
+package lxc_pve_prestart_hook;
+
 use strict;
 use warnings;
+
+exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/;
+
 use POSIX;
 use File::Path;
+use Fcntl ':mode';
 
 use PVE::SafeSyslog;
 use PVE::Tools;
@@ -14,27 +20,11 @@ use PVE::JSONSchema qw(get_standard_option);
 use PVE::CLIHandler;
 use PVE::Storage;
 use PVE::LXC;
+use PVE::LXC::Setup;
 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',
@@ -66,23 +56,54 @@ __PACKAGE__->register_method ({
 
        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);
+       return undef if ! -f PVE::LXC::Config->config_file($vmid);
 
-       my $conf = PVE::LXC::load_config($vmid);
+       my $conf = PVE::LXC::Config->load_config($vmid);
+       if (!$ENV{PVE_SKIPLOCK} && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
+           PVE::LXC::Config->check_lock($conf);
+       }
 
        my $storage_cfg = PVE::Storage::config();
 
-       my $vollist = PVE::LXC::get_vm_volumes($conf);
-       my $loopdevlist = PVE::LXC::get_vm_volumes($conf, 'rootfs');
+       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);
-       PVE::LXC::attach_loops($storage_cfg, $loopdevlist);
+
+       my $rootdir = $param->{rootfs};
+
+       my $devlist_file = "/var/lib/lxc/$vmid/devices";
+       unlink $devlist_file;
+       my $devices = [];
+
+       my $setup_mountpoint = sub {
+           my ($ms, $mountpoint) = @_;
+
+           #return if $ms eq 'rootfs';
+           my (undef, undef, $dev) = PVE::LXC::mountpoint_mount($mountpoint, $rootdir, $storage_cfg);
+           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 = int($rdev / 0x100);
+               my $minor = $rdev % 0x100;
+               $devlist .= "b:$major:$minor:$dev\n";
+           }
+           PVE::Tools::file_set_contents($devlist_file, $devlist);
+       }
        return undef;
     }});
 
@@ -91,10 +112,7 @@ 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')) {
+if ((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];
 
@@ -105,28 +123,6 @@ if ((scalar(@ARGV) == 1) && ($ARGV[0] eq 'printmanpod') ||
     @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)
+our $cmddef = [ __PACKAGE__, 'lxc-pve-prestart-hook', [], $param];
 
-=include pve_copyright
+__PACKAGE__->run_cli_handler();