[pve-devel] [PATCH manager v3 1/5] ceph: move create/destroy pool to CephTools

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Nov 23 12:01:37 CET 2018


We will reuse this in the future, e.g., when creating a data and
metadata pool for CephFS.

Allow to pass a $rados object (to reuse it, as initializing is not
that cheap) but also create it if it's undefined, fro convenience.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---

no changes

 PVE/API2/Ceph.pm | 50 ++--------------------------------
 PVE/CephTools.pm | 71 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 48 deletions(-)

diff --git a/PVE/API2/Ceph.pm b/PVE/API2/Ceph.pm
index 0a70db05..a6eec24a 100644
--- a/PVE/API2/Ceph.pm
+++ b/PVE/API2/Ceph.pm
@@ -1624,45 +1624,7 @@ __PACKAGE__->register_method ({
 
 	my $worker = sub {
 
-	    my $rados = PVE::RADOS->new();
-	    $rados->mon_command({
-		prefix => "osd pool create",
-		pool => $pool,
-		pg_num => int($pg_num),
-		format => 'plain',
-	    });
-
-	    $rados->mon_command({
-		prefix => "osd pool set",
-		pool => $pool,
-		var => 'min_size',
-		val => $min_size,
-		format => 'plain',
-	    });
-
-	    $rados->mon_command({
-		prefix => "osd pool set",
-		pool => $pool,
-		var => 'size',
-		val => $size,
-		format => 'plain',
-	    });
-
-	    if (defined($param->{crush_rule})) {
-		$rados->mon_command({
-		    prefix => "osd pool set",
-		    pool => $pool,
-		    var => 'crush_rule',
-		    val => $param->{crush_rule},
-		    format => 'plain',
-		});
-	    }
-
-	    $rados->mon_command({
-		    prefix => "osd pool application enable",
-		    pool => $pool,
-		    app => $application,
-	    });
+	    PVE::CephTools::create_pool($pool, $param);
 
 	    if ($param->{add_storages}) {
 		my $err;
@@ -1862,15 +1824,7 @@ __PACKAGE__->register_method ({
 		}
 	    }
 
-	    my $rados = PVE::RADOS->new();
-	    # fixme: '--yes-i-really-really-mean-it'
-	    $rados->mon_command({
-		prefix => "osd pool delete",
-		pool => $pool,
-		pool2 => $pool,
-		sure => '--yes-i-really-really-mean-it',
-		format => 'plain',
-	    });
+	    PVE::CephTools::destroy_pool($pool);
 
 	    if ($param->{remove_storages}) {
 		my $err;
diff --git a/PVE/CephTools.pm b/PVE/CephTools.pm
index 600243ca..8a9afa84 100644
--- a/PVE/CephTools.pm
+++ b/PVE/CephTools.pm
@@ -7,6 +7,7 @@ use File::Path;
 use IO::File;
 
 use PVE::Tools qw(run_command dir_glob_foreach);
+use PVE::RADOS;
 
 my $ccname = 'ceph'; # ceph cluster name
 my $ceph_cfgdir = "/etc/ceph";
@@ -183,6 +184,76 @@ sub write_ceph_config {
     PVE::Tools::file_set_contents($pve_ceph_cfgpath, $out);
 }
 
+sub create_pool {
+    my ($pool, $param, $rados) = @_;
+
+    if (!defined($rados)) {
+	$rados = PVE::RADOS->new();
+    }
+
+    my $pg_num = $param->{pg_num} || 64;
+    my $size = $param->{size} || 3;
+    my $min_size = $param->{min_size} || 2;
+    my $application = $param->{application} // 'rbd';
+
+    $rados->mon_command({
+	prefix => "osd pool create",
+	pool => $pool,
+	pg_num => int($pg_num),
+	format => 'plain',
+    });
+
+    $rados->mon_command({
+	prefix => "osd pool set",
+	pool => $pool,
+	var => 'min_size',
+	val => $min_size,
+	format => 'plain',
+    });
+
+    $rados->mon_command({
+	prefix => "osd pool set",
+	pool => $pool,
+	var => 'size',
+	val => $size,
+	format => 'plain',
+    });
+
+    if (defined($param->{crush_rule})) {
+	$rados->mon_command({
+	    prefix => "osd pool set",
+	    pool => $pool,
+	    var => 'crush_rule',
+	    val => $param->{crush_rule},
+	    format => 'plain',
+	});
+    }
+
+    $rados->mon_command({
+	prefix => "osd pool application enable",
+	pool => $pool,
+	app => $application,
+    });
+
+}
+
+sub destroy_pool {
+    my ($pool, $rados) = @_;
+
+    if (!defined($rados)) {
+	$rados = PVE::RADOS->new();
+    }
+
+    # fixme: '--yes-i-really-really-mean-it'
+    $rados->mon_command({
+	prefix => "osd pool delete",
+	pool => $pool,
+	pool2 => $pool,
+	sure => '--yes-i-really-really-mean-it',
+	format => 'plain',
+    });
+}
+
 sub setup_pve_symlinks {
     # fail if we find a real file instead of a link
     if (-f $ceph_cfgpath) {
-- 
2.19.1





More information about the pve-devel mailing list