[pve-devel] [RFC 1/2 pve-storage] implement map_volume and unmap_volume

Dietmar Maurer dietmar at proxmox.com
Fri Sep 21 07:58:29 CEST 2018


This allows to request a mapped device/path explicitly,
regardles of the storage option, eg. krbd option in the RBDplugin.

Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 PVE/Storage.pm           | 24 +++++++++++++++++++
 PVE/Storage/Plugin.pm    | 12 ++++++++++
 PVE/Storage/RBDPlugin.pm | 62 ++++++++++++++++++++++++++++--------------------
 3 files changed, 72 insertions(+), 26 deletions(-)

diff --git a/PVE/Storage.pm b/PVE/Storage.pm
index f9732fe..9f3bb8e 100755
--- a/PVE/Storage.pm
+++ b/PVE/Storage.pm
@@ -671,6 +671,30 @@ sub vdisk_create_base {
     });
 }
 
+sub map_volume {
+    my ($cfg, $volid, $snapname, $nomap) = @_;
+
+    my ($storeid, $volname) = parse_volume_id($volid);
+
+    my $scfg = storage_config($cfg, $storeid);
+
+    my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+
+    return $plugin->map_volume($storeid, $scfg, $volname, $snapname, $nomap);
+}
+
+sub unmap_volume {
+    my ($cfg, $volid, $snapname) = @_;
+
+    my ($storeid, $volname) = parse_volume_id($volid);
+
+    my $scfg = storage_config($cfg, $storeid);
+
+    my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+
+    return $plugin->unmap_volume($storeid, $scfg, $volname, $snapname);
+}
+
 sub vdisk_alloc {
     my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
 
diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm
index 8ae78e9..7dd6e0a 100644
--- a/PVE/Storage/Plugin.pm
+++ b/PVE/Storage/Plugin.pm
@@ -949,6 +949,18 @@ sub deactivate_storage {
     # do nothing by default
 }
 
+sub map_volume {
+    my ($class, $storeid, $scfg, $volname, $snapname, $nomap) = @_;
+
+    return undef;
+}
+
+sub unmap_volume {
+    my ($class, $storeid, $scfg, $volname, $snapname) = @_;
+
+    return 1;
+}
+
 sub activate_volume {
     my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
 
diff --git a/PVE/Storage/RBDPlugin.pm b/PVE/Storage/RBDPlugin.pm
index 41d89b5..e8b15f6 100644
--- a/PVE/Storage/RBDPlugin.pm
+++ b/PVE/Storage/RBDPlugin.pm
@@ -74,8 +74,6 @@ my $librados_connect = sub {
 my $krbd_feature_disable = sub {
     my ($scfg, $storeid, $name) = @_;
 
-    return 1 if !$scfg->{krbd};
-
     my ($major, undef, undef, undef) = ceph_version();
     return 1 if $major < 10;
 
@@ -259,7 +257,7 @@ sub properties {
 	    type => 'string',
 	},
 	krbd => {
-	    description => "Access rbd through krbd kernel module.",
+	    description => "Always access rbd through krbd kernel module.",
 	    type => 'boolean',
 	},
     };
@@ -428,8 +426,6 @@ sub clone_image {
 
     run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
 
-    &$krbd_feature_disable($scfg, $storeid, $name);
-
     return $newvol;
 }
 
@@ -445,8 +441,6 @@ sub alloc_image {
     my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
     run_rbd_command($cmd, errmsg => "rbd create $name' error");
 
-    &$krbd_feature_disable($scfg, $storeid, $name);
-
     return $name;
 }
 
@@ -544,39 +538,55 @@ sub deactivate_storage {
     return 1;
 }
 
-sub activate_volume {
-    my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
-
-    return 1 if !$scfg->{krbd};
+sub map_volume {
+    my ($class, $storeid, $scfg, $volname, $snapname, $nomap) = @_;
 
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
+    $name .= '@'.$snapname if $snapname;
+
     my $pool =  $scfg->{pool} ? $scfg->{pool} : 'rbd';
 
-    my $path = "/dev/rbd/$pool/$name";
-    $path .= '@'.$snapname if $snapname;
-    return if -b $path;
+    my $kerneldev = "/dev/rbd/$pool/$name";
+
+    return $kerneldev if $nomap;
+
+    return $kerneldev if -b $kerneldev; # already mapped
+
+    &$krbd_feature_disable($scfg, $storeid, $volname); # FIXME: $volname or $name
 
-    $name .= '@'.$snapname if $snapname;
     my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
-    run_rbd_command($cmd, errmsg => "can't mount rbd volume $name");
+    run_rbd_command($cmd, errmsg => "can't map rbd volume $name");
+
+    return $kerneldev;
+}
+
+sub unmap_volume {
+    my ($class, $storeid, $scfg, $volname, $snapname) = @_;
+
+    my $kerneldev = $class->map_volume($storeid, $scfg, $volname, $snapname, 1);
+
+    if (-b $kerneldev) {
+	my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $kerneldev);
+	run_rbd_command($cmd, errmsg => "can't unmap rbd device $kerneldev");
+    }
 
     return 1;
 }
 
-sub deactivate_volume {
+sub activate_volume {
     my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
 
     return 1 if !$scfg->{krbd};
 
-    my ($vtype, $name, $vmid) = $class->parse_volname($volname);
-    my $pool =  $scfg->{pool} ? $scfg->{pool} : 'rbd';
+    $class->map_volume($storeid, $scfg, $volname, $snapname);
 
-    my $path = "/dev/rbd/$pool/$name";
-    $path .= '@'.$snapname if $snapname;
-    return if ! -b $path;
+    return 1;
+}
+
+sub deactivate_volume {
+    my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
 
-    my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
-    run_rbd_command($cmd, errmsg => "can't unmap rbd volume $name");
+    $class->unmap_volume($storeid, $scfg, $volname, $snapname);
 
     return 1;
 }
@@ -592,7 +602,7 @@ sub volume_size_info {
 sub volume_resize {
     my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
 
-    return 1 if $running && !$scfg->{krbd};
+    return 1 if $running && !$scfg->{krbd}; # FIXME???
 
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
 
@@ -623,7 +633,7 @@ sub volume_snapshot_rollback {
 sub volume_snapshot_delete {
     my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
 
-    return 1 if $running && !$scfg->{krbd};
+    return 1 if $running && !$scfg->{krbd}; # FIXME: ????
 
     $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
 
-- 
2.11.0




More information about the pve-devel mailing list