[pve-devel] [PATCH 7/8] add setup_blockdev to pve-mount hook.

Alexandre Derumier aderumier at odiso.com
Fri Aug 14 14:56:48 CEST 2015


Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 src/PVE/API2/LXC.pm      |  1 -
 src/PVE/LXCSetup.pm      |  8 ++++----
 src/PVE/LXCSetup/Base.pm | 29 ++++++++++++++++++++++++++---
 src/lxc-pve-mount-hook   |  5 +++--
 4 files changed, 33 insertions(+), 10 deletions(-)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index 10b981d..d311dce 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -1000,7 +1000,6 @@ __PACKAGE__->register_method({
 		    if PVE::LXC::is_template($conf);
 
 		my $storage_cfg = cfs_read_file("storage.cfg");
-
 		PVE::LXC::foreach_mountpoint($conf, sub {
 		    my ($ms, $mountpoint) = @_;
 		    PVE::Storage::activate_volumes($storage_cfg, [$mountpoint->{volume}]);
diff --git a/src/PVE/LXCSetup.pm b/src/PVE/LXCSetup.pm
index e57ed1a..f3dc0db 100644
--- a/src/PVE/LXCSetup.pm
+++ b/src/PVE/LXCSetup.pm
@@ -32,11 +32,11 @@ my $autodetect_type = sub {
 };
 
 sub new {
-    my ($class, $conf, $rootdir, $type) = @_;
+    my ($class, $conf, $rootdir, $type, $vmid) = @_;
 
     die "no root directory\n" if !$rootdir || $rootdir eq '/';
 
-    my $self = bless { conf => $conf, $rootdir => $rootdir};
+    my $self = bless { conf => $conf, $rootdir => $rootdir, vmid => $vmid};
 
     if (!defined($type)) {
 	# try to autodetect type
@@ -46,7 +46,7 @@ sub new {
     my $plugin_class = $plugins->{$type} ||
 	"no such OS type '$type'\n";
 
-    $self->{plugin} = $plugin_class->new($conf, $rootdir);
+    $self->{plugin} = $plugin_class->new($conf, $rootdir, $vmid);
     
     return $self;
 }
@@ -96,7 +96,7 @@ sub rewrite_ssh_host_keys {
 sub pre_start_hook {
     my ($self) = @_;
 
-    $self->{plugin}->pre_start_hook($self->{conf});
+    $self->{plugin}->pre_start_hook($self->{conf}, $self->{vmid});
 }
 
 sub post_create_hook {
diff --git a/src/PVE/LXCSetup/Base.pm b/src/PVE/LXCSetup/Base.pm
index 49e60e0..8db5451 100644
--- a/src/PVE/LXCSetup/Base.pm
+++ b/src/PVE/LXCSetup/Base.pm
@@ -12,9 +12,9 @@ use PVE::INotify;
 use PVE::Tools;
 
 sub new {
-    my ($class, $conf, $rootdir) = @_;
+    my ($class, $conf, $rootdir, $vmid) = @_;
 
-    return bless { conf => $conf, rootdir => $rootdir }, $class;
+    return bless { conf => $conf, rootdir => $rootdir, vmid => $vmid }, $class;
 }
 
 sub lookup_dns_conf {
@@ -195,6 +195,28 @@ sub setup_init {
     die "please implement this inside subclass"
 }
 
+sub setup_blockdev {
+    my ($self, $vmid) = @_;
+
+    my $rootdir = $self->{rootdir};
+
+    my $config_filename = "/var/lib/lxc/$vmid/config";
+    return undef if ! -f $config_filename;
+    my $raw = PVE::Tools::file_get_contents($config_filename);
+    my @lines = split(/\n/, $raw);
+    foreach my $line (@lines) {
+	if ($line =~ m/lxc\.cgroup\.devices\.allow = b (\d+):(\d+) rwm/) {
+	    my $major = $1;
+	    my $minor = $2;
+	    my $bdev = readlink("/sys/dev/block/$major:$minor");
+	    $bdev =~ s/\.\.\/\.\.\/devices\/virtual\/block\//\/dev\//;
+	    my $path = $rootdir . $bdev;
+	    PVE::Tools::run_command(['mknod', '-m', '666', $path, 'b', $major, $minor]);
+
+	}
+    }
+}
+
 sub setup_systemd_console {
     my ($self, $conf) = @_;
 
@@ -373,9 +395,10 @@ sub rewrite_ssh_host_keys {
 }
 
 sub pre_start_hook {
-    my ($self, $conf) = @_;
+    my ($self, $conf, $vmid) = @_;
 
     $self->setup_init($conf);
+    $self->setup_blockdev($vmid);
     $self->setup_network($conf);
     $self->set_hostname($conf);
     $self->set_dns($conf);
diff --git a/src/lxc-pve-mount-hook b/src/lxc-pve-mount-hook
index f5f0867..ea4fdbd 100755
--- a/src/lxc-pve-mount-hook
+++ b/src/lxc-pve-mount-hook
@@ -79,10 +79,11 @@ __PACKAGE__->register_method ({
 	
 	my $raw = PVE::Tools::file_get_contents($config_filename);
 	my $conf = PVE::LXC::parse_pct_config($config_filename, $raw);
-	
+
+
 	my $mountpoint = $ENV{LXC_ROOTFS_MOUNT};
 
-	my $lxc_setup = PVE::LXCSetup->new($conf, $mountpoint);
+	my $lxc_setup = PVE::LXCSetup->new($conf, $mountpoint, undef, $param->{name});
 	$lxc_setup->pre_start_hook();
 	
 	return undef;
-- 
2.1.4




More information about the pve-devel mailing list