[pve-devel] [PATCH storage v5 1/3] Merge RBD and CephFS code into a helper module

Alwin Antreich a.antreich at proxmox.com
Mon Jun 25 17:50:55 CEST 2018


Some methods for connecting to a ceph cluster are the same for RBD and
CephFS, these are merged into the helper modules.

Signed-off-by: Alwin Antreich <a.antreich at proxmox.com>
---
 PVE/Storage/CephTools.pm | 55 ++++++++++++++++++++++++++++++++++++++++++++++++
 PVE/Storage/Makefile     |  2 +-
 PVE/Storage/RBDPlugin.pm | 55 +++++-------------------------------------------
 3 files changed, 61 insertions(+), 51 deletions(-)
 create mode 100644 PVE/Storage/CephTools.pm

diff --git a/PVE/Storage/CephTools.pm b/PVE/Storage/CephTools.pm
new file mode 100644
index 0000000..7aa6069
--- /dev/null
+++ b/PVE/Storage/CephTools.pm
@@ -0,0 +1,55 @@
+package PVE::Storage::CephTools;
+
+use strict;
+use warnings;
+use Net::IP;
+use PVE::Tools qw(run_command);
+
+sub hostlist {
+    my ($list_text, $separator) = @_;
+
+    my @monhostlist = PVE::Tools::split_list($list_text);
+    return join($separator, map {
+	my ($host, $port) = PVE::Tools::parse_host_and_port($_);
+	$port = defined($port) ? ":$port" : '';
+	$host = "[$host]" if Net::IP::ip_is_ipv6($host);
+	"${host}${port}"
+    } @monhostlist);
+}
+
+sub ceph_connect_option {
+    my ($scfg, $storeid, %options) = @_;
+
+    my $cmd_option = {};
+    my $ceph_storeid_conf = "/etc/pve/priv/ceph/${storeid}.conf";
+    my $pveceph_config = '/etc/pve/ceph.conf';
+    my $keyfile = "/etc/pve/priv/ceph/${storeid}.keyring";
+    $keyfile = "/etc/pve/priv/ceph/${storeid}.secret" if ($scfg->{type} eq 'cephfs');
+    my $pveceph_managed = !defined($scfg->{monhost});
+
+    $cmd_option->{ceph_conf} = $pveceph_config if $pveceph_managed;
+
+    if (-e $ceph_storeid_conf) {
+	if ($pveceph_managed) {
+	    warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
+	} else {
+	    $cmd_option->{ceph_conf} = $ceph_storeid_conf;
+	}
+    }
+
+    $cmd_option->{keyring} = $keyfile if (-e $keyfile);
+    $cmd_option->{auth_supported} = (defined $cmd_option->{keyring}) ? 'cephx' : 'none';
+    $cmd_option->{userid} =  $scfg->{username} ? $scfg->{username} : 'admin';
+    $cmd_option->{mon_host} = hostlist($scfg->{monhost}, ',') if (defined($scfg->{monhost}));
+
+    if (%options) {
+	foreach my $k (keys %options) {
+	    $cmd_option->{$k} = $options{$k};
+	}
+    }
+
+    return $cmd_option;
+
+}
+
+1;
diff --git a/PVE/Storage/Makefile b/PVE/Storage/Makefile
index 7b168fa..82dadd6 100644
--- a/PVE/Storage/Makefile
+++ b/PVE/Storage/Makefile
@@ -1,4 +1,4 @@
-SOURCES=Plugin.pm DirPlugin.pm LVMPlugin.pm NFSPlugin.pm CIFSPlugin.pm ISCSIPlugin.pm RBDPlugin.pm SheepdogPlugin.pm ISCSIDirectPlugin.pm GlusterfsPlugin.pm ZFSPoolPlugin.pm ZFSPlugin.pm DRBDPlugin.pm LvmThinPlugin.pm
+SOURCES=Plugin.pm DirPlugin.pm LVMPlugin.pm NFSPlugin.pm CIFSPlugin.pm ISCSIPlugin.pm RBDPlugin.pm CephTools.pm SheepdogPlugin.pm ISCSIDirectPlugin.pm GlusterfsPlugin.pm ZFSPoolPlugin.pm ZFSPlugin.pm DRBDPlugin.pm LvmThinPlugin.pm
 
 .PHONY: install
 install:
diff --git a/PVE/Storage/RBDPlugin.pm b/PVE/Storage/RBDPlugin.pm
index 15cbe67..ee61f07 100644
--- a/PVE/Storage/RBDPlugin.pm
+++ b/PVE/Storage/RBDPlugin.pm
@@ -8,6 +8,7 @@ use PVE::Tools qw(run_command trim);
 use PVE::Storage::Plugin;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::RADOS;
+use PVE::Storage::CephTools;
 
 use base qw(PVE::Storage::Plugin);
 
@@ -26,56 +27,10 @@ my $add_pool_to_disk = sub {
     return "$pool/$disk";
 };
 
-my $hostlist = sub {
-    my ($list_text, $separator) = @_;
-
-    my @monhostlist = PVE::Tools::split_list($list_text);
-    return join($separator, map {
-	my ($host, $port) = PVE::Tools::parse_host_and_port($_);
-	$port = defined($port) ? ":$port" : '';
-	$host = "[$host]" if Net::IP::ip_is_ipv6($host);
-	"${host}${port}"
-    } @monhostlist);
-};
-
-my $ceph_connect_option = sub {
-    my ($scfg, $storeid, %options) = @_;
-
-    my $cmd_option = {};
-    my $ceph_storeid_conf = "/etc/pve/priv/ceph/${storeid}.conf";
-    my $pveceph_config = '/etc/pve/ceph.conf';
-    my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
-    my $pveceph_managed = !defined($scfg->{monhost});
-
-    $cmd_option->{ceph_conf} = $pveceph_config if $pveceph_managed;
-
-    if (-e $ceph_storeid_conf) {
-	if ($pveceph_managed) {
-	    warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
-	} else {
-	    $cmd_option->{ceph_conf} = $ceph_storeid_conf;
-	}
-    }
-
-    $cmd_option->{keyring} = $keyring if (-e $keyring);
-    $cmd_option->{auth_supported} = (defined $cmd_option->{keyring}) ? 'cephx' : 'none';
-    $cmd_option->{userid} =  $scfg->{username} ? $scfg->{username} : 'admin';
-    $cmd_option->{mon_host} = $hostlist->($scfg->{monhost}, ',') if (defined($scfg->{monhost}));
-
-    if (%options) {
-	foreach my $k (keys %options) {
-	    $cmd_option->{$k} = $options{$k};
-	}
-    }
-
-    return $cmd_option;
-
-};
-
 my $build_cmd = sub {
     my ($binary, $scfg, $storeid, $op, @options) = @_;
 
-    my $cmd_option = $ceph_connect_option->($scfg, $storeid);
+    my $cmd_option = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid);
     my $pool =  $scfg->{pool} ? $scfg->{pool} : 'rbd';
 
     my $cmd = [$binary, '-p', $pool];
@@ -108,7 +63,7 @@ my $rados_cmd = sub {
 my $librados_connect = sub {
     my ($scfg, $storeid, $options) = @_;
 
-    my $librados_config = $ceph_connect_option->($scfg, $storeid);
+    my $librados_config = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid);
 
     my $rados = PVE::RADOS->new(%$librados_config);
 
@@ -333,7 +288,7 @@ sub parse_volname {
 sub path {
     my ($class, $scfg, $volname, $storeid, $snapname) = @_;
 
-    my $cmd_option = $ceph_connect_option->($scfg, $storeid);
+    my $cmd_option = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid);
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
     $name .= '@'.$snapname if $snapname;
 
@@ -344,7 +299,7 @@ sub path {
 
     $path .= ":conf=$cmd_option->{ceph_conf}" if $cmd_option->{ceph_conf};
     if (defined($scfg->{monhost})) {
-	my $monhost = $hostlist->($scfg->{monhost}, ';');
+	my $monhost = PVE::Storage::CephTools::hostlist($scfg->{monhost}, ';');
 	$monhost =~ s/:/\\:/g;
 	$path .= ":mon_host=$monhost";
 	$path .= ":auth_supported=$cmd_option->{auth_supported}";
-- 
2.11.0




More information about the pve-devel mailing list