]> git.proxmox.com Git - pve-manager.git/blame - PVE/API2/Ceph.pm
enable using a device for bluestore block db/wal
[pve-manager.git] / PVE / API2 / Ceph.pm
CommitLineData
7d4fc5ef 1package PVE::API2::CephOSD;
38db610a
DM
2
3use strict;
4use warnings;
13f4d762 5use Cwd qw(abs_path);
98901f1d 6use Net::IP;
38db610a
DM
7
8use PVE::SafeSyslog;
13f4d762 9use PVE::Tools qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach);
38db610a
DM
10use PVE::Exception qw(raise raise_param_exc);
11use PVE::INotify;
12use PVE::Cluster qw(cfs_lock_file cfs_read_file cfs_write_file);
13use PVE::AccessControl;
14use PVE::Storage;
15use PVE::RESTHandler;
16use PVE::RPCEnvironment;
17use PVE::JSONSchema qw(get_standard_option);
970236b3 18use PVE::RADOS;
a34866f0 19use PVE::CephTools;
7f4924bd 20use PVE::Diskmanage;
38db610a
DM
21
22use base qw(PVE::RESTHandler);
23
24use Data::Dumper; # fixme: remove
25
7d4fc5ef
DM
26my $get_osd_status = sub {
27 my ($rados, $osdid) = @_;
0e5816e4 28
7d4fc5ef 29 my $stat = $rados->mon_command({ prefix => 'osd dump' });
2f804640 30
7d4fc5ef 31 my $osdlist = $stat->{osds} || [];
38db610a 32
85c17d96
DC
33 my $flags = $stat->{flags} || undef;
34
7d4fc5ef
DM
35 my $osdstat;
36 foreach my $d (@$osdlist) {
be753927 37 $osdstat->{$d->{osd}} = $d if defined($d->{osd});
7d4fc5ef
DM
38 }
39 if (defined($osdid)) {
40 die "no such OSD '$osdid'\n" if !$osdstat->{$osdid};
41 return $osdstat->{$osdid};
42 }
38db610a 43
85c17d96 44 return wantarray? ($osdstat, $flags):$osdstat;
7d4fc5ef 45};
13f4d762 46
941c0195
DM
47my $get_osd_usage = sub {
48 my ($rados) = @_;
49
be753927 50 my $osdlist = $rados->mon_command({ prefix => 'pg dump',
941c0195
DM
51 dumpcontents => [ 'osds' ]}) || [];
52
53 my $osdstat;
54 foreach my $d (@$osdlist) {
be753927 55 $osdstat->{$d->{osd}} = $d if defined($d->{osd});
941c0195
DM
56 }
57
58 return $osdstat;
59};
60
7d4fc5ef
DM
61__PACKAGE__->register_method ({
62 name => 'index',
63 path => '',
64 method => 'GET',
65 description => "Get Ceph osd list/tree.",
66 proxyto => 'node',
67 protected => 1,
90c75580
TL
68 permissions => {
69 check => ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any => 1],
70 },
7d4fc5ef 71 parameters => {
be753927 72 additionalProperties => 0,
7d4fc5ef
DM
73 properties => {
74 node => get_standard_option('pve-node'),
75 },
76 },
77 # fixme: return a list instead of extjs tree format ?
78 returns => {
79 type => "object",
80 },
81 code => sub {
82 my ($param) = @_;
13f4d762 83
7d4fc5ef 84 PVE::CephTools::check_ceph_inited();
13f4d762 85
7d4fc5ef
DM
86 my $rados = PVE::RADOS->new();
87 my $res = $rados->mon_command({ prefix => 'osd tree' });
13f4d762 88
7d4fc5ef 89 die "no tree nodes found\n" if !($res && $res->{nodes});
13f4d762 90
85c17d96 91 my ($osdhash, $flags) = &$get_osd_status($rados);
13f4d762 92
941c0195
DM
93 my $usagehash = &$get_osd_usage($rados);
94
7d4fc5ef
DM
95 my $nodes = {};
96 my $newnodes = {};
97 foreach my $e (@{$res->{nodes}}) {
98 $nodes->{$e->{id}} = $e;
be753927
DC
99
100 my $new = {
101 id => $e->{id},
102 name => $e->{name},
7d4fc5ef
DM
103 type => $e->{type}
104 };
13f4d762 105
7d4fc5ef
DM
106 foreach my $opt (qw(status crush_weight reweight)) {
107 $new->{$opt} = $e->{$opt} if defined($e->{$opt});
108 }
13f4d762 109
7d4fc5ef
DM
110 if (my $stat = $osdhash->{$e->{id}}) {
111 $new->{in} = $stat->{in} if defined($stat->{in});
112 }
13f4d762 113
941c0195
DM
114 if (my $stat = $usagehash->{$e->{id}}) {
115 $new->{total_space} = ($stat->{kb} || 1) * 1024;
116 $new->{bytes_used} = ($stat->{kb_used} || 0) * 1024;
117 $new->{percent_used} = ($new->{bytes_used}*100)/$new->{total_space};
cc81005a
DM
118 if (my $d = $stat->{fs_perf_stat}) {
119 $new->{commit_latency_ms} = $d->{commit_latency_ms};
120 $new->{apply_latency_ms} = $d->{apply_latency_ms};
121 }
941c0195
DM
122 }
123
7d4fc5ef 124 $newnodes->{$e->{id}} = $new;
13f4d762
DM
125 }
126
7d4fc5ef
DM
127 foreach my $e (@{$res->{nodes}}) {
128 my $new = $newnodes->{$e->{id}};
129 if ($e->{children} && scalar(@{$e->{children}})) {
130 $new->{children} = [];
131 $new->{leaf} = 0;
132 foreach my $cid (@{$e->{children}}) {
133 $nodes->{$cid}->{parent} = $e->{id};
134 if ($nodes->{$cid}->{type} eq 'osd' &&
135 $e->{type} eq 'host') {
136 $newnodes->{$cid}->{host} = $e->{name};
137 }
138 push @{$new->{children}}, $newnodes->{$cid};
139 }
140 } else {
141 $new->{leaf} = ($e->{id} >= 0) ? 1 : 0;
142 }
0e5816e4
DM
143 }
144
4aed5e3e 145 my $roots = [];
7d4fc5ef
DM
146 foreach my $e (@{$res->{nodes}}) {
147 if (!$nodes->{$e->{id}}->{parent}) {
4aed5e3e 148 push @$roots, $newnodes->{$e->{id}};
7d4fc5ef 149 }
0e5816e4
DM
150 }
151
4aed5e3e 152 die "no root node\n" if !@$roots;
13f4d762 153
4aed5e3e 154 my $data = { root => { leaf => 0, children => $roots } };
0e5816e4 155
85c17d96
DC
156 # we want this for the noout flag
157 $data->{flags} = $flags if $flags;
158
7d4fc5ef
DM
159 return $data;
160 }});
13f4d762 161
7d4fc5ef
DM
162__PACKAGE__->register_method ({
163 name => 'createosd',
164 path => '',
165 method => 'POST',
166 description => "Create OSD",
167 proxyto => 'node',
168 protected => 1,
169 parameters => {
be753927 170 additionalProperties => 0,
7d4fc5ef
DM
171 properties => {
172 node => get_standard_option('pve-node'),
173 dev => {
174 description => "Block device name.",
175 type => 'string',
176 },
177 journal_dev => {
8d64bd8c
DC
178 description => "Block device name for journal (filestore) or block.db (bluestore).",
179 optional => 1,
180 type => 'string',
181 },
182 wal_dev => {
183 description => "Block device name for block.wal (bluestore only).",
7d4fc5ef
DM
184 optional => 1,
185 type => 'string',
186 },
187 fstype => {
50239dba 188 description => "File system type (filestore only).",
7d4fc5ef
DM
189 type => 'string',
190 enum => ['xfs', 'ext4', 'btrfs'],
191 default => 'xfs',
192 optional => 1,
193 },
50239dba
FG
194 bluestore => {
195 description => "Use bluestore instead of filestore.",
196 type => 'boolean',
197 default => 0,
198 optional => 1,
199 },
7d4fc5ef
DM
200 },
201 },
202 returns => { type => 'string' },
203 code => sub {
204 my ($param) = @_;
13f4d762 205
7d4fc5ef 206 my $rpcenv = PVE::RPCEnvironment::get();
13f4d762 207
7d4fc5ef 208 my $authuser = $rpcenv->get_user();
13f4d762 209
50239dba
FG
210 raise_param_exc({ 'bluestore' => "conflicts with parameter 'fstype'" })
211 if (defined($param->{fstype}) && defined($param->{bluestore}) && $param->{bluestore});
212
7d4fc5ef 213 PVE::CephTools::check_ceph_inited();
0e5816e4 214
7d4fc5ef 215 PVE::CephTools::setup_pve_symlinks();
a7a7fb00 216
8d64bd8c
DC
217 my $bluestore = $param->{bluestore} // 0;
218
7d4fc5ef 219 my $journal_dev;
8d64bd8c 220 my $wal_dev;
a7a7fb00 221
7d4fc5ef 222 if ($param->{journal_dev} && ($param->{journal_dev} ne $param->{dev})) {
7f4924bd 223 $journal_dev = PVE::Diskmanage::verify_blockdev_path($param->{journal_dev});
8d64bd8c
DC
224 # if only journal is given, also put the wal there
225 $wal_dev = $journal_dev;
226 }
227
228 if ($param->{wal_dev} &&
229 ($param->{wal_dev} ne $param->{dev}) &&
230 (!$param->{journal_dev} || $param->{wal_dev} ne $param->{journal_dev})) {
231 raise_param_exc({ 'wal_dev' => "can only be set with paramater 'bluestore'"})
232 if !$bluestore;
233 $wal_dev = PVE::Diskmanage::verify_blockdev_path($param->{wal_dev});
7d4fc5ef 234 }
13f4d762 235
7f4924bd 236 $param->{dev} = PVE::Diskmanage::verify_blockdev_path($param->{dev});
a7a7fb00 237
7d4fc5ef
DM
238 my $devname = $param->{dev};
239 $devname =~ s|/dev/||;
929376d7
DC
240
241 my $disklist = PVE::Diskmanage::get_disks($devname, 1);
242
7d4fc5ef
DM
243 my $diskinfo = $disklist->{$devname};
244 die "unable to get device info for '$devname'\n"
245 if !$diskinfo;
13f4d762 246
be753927 247 die "device '$param->{dev}' is in use\n"
7d4fc5ef 248 if $diskinfo->{used};
0e5816e4 249
929376d7 250 my $devpath = $diskinfo->{devpath};
7d4fc5ef
DM
251 my $rados = PVE::RADOS->new();
252 my $monstat = $rados->mon_command({ prefix => 'mon_status' });
253 die "unable to get fsid\n" if !$monstat->{monmap} || !$monstat->{monmap}->{fsid};
0e5816e4 254
7d4fc5ef
DM
255 my $fsid = $monstat->{monmap}->{fsid};
256 $fsid = $1 if $fsid =~ m/^([0-9a-f\-]+)$/;
0e5816e4 257
7d4fc5ef 258 my $ceph_bootstrap_osd_keyring = PVE::CephTools::get_config('ceph_bootstrap_osd_keyring');
0e5816e4 259
7d4fc5ef 260 if (! -f $ceph_bootstrap_osd_keyring) {
1f3e956a 261 my $bindata = $rados->mon_command({ prefix => 'auth get', entity => 'client.bootstrap-osd', format => 'plain' });
7d4fc5ef
DM
262 PVE::Tools::file_set_contents($ceph_bootstrap_osd_keyring, $bindata);
263 };
be753927 264
7d4fc5ef
DM
265 my $worker = sub {
266 my $upid = shift;
0e5816e4 267
7d4fc5ef 268 my $fstype = $param->{fstype} || 'xfs';
0e5816e4 269
0e5816e4 270
7d4fc5ef 271 my $ccname = PVE::CephTools::get_config('ccname');
0e5816e4 272
50239dba 273 my $cmd = ['ceph-disk', 'prepare', '--zap-disk',
7d4fc5ef 274 '--cluster', $ccname, '--cluster-uuid', $fsid ];
38db610a 275
8d64bd8c 276 if ($bluestore) {
50239dba
FG
277 print "create OSD on $devpath (bluestore)\n";
278 push @$cmd, '--bluestore';
8d64bd8c
DC
279
280 if ($journal_dev) {
281 print "using device '$journal_dev' for block.db\n";
282 push @$cmd, '--block.db', $journal_dev;
283 }
284
285 if ($wal_dev) {
286 print "using device '$wal_dev' for block.wal\n";
287 push @$cmd, '--block.wal', $wal_dev;
288 }
289
290 push @$cmd, $devpath;
50239dba
FG
291 } else {
292 print "create OSD on $devpath ($fstype)\n";
b6c42726 293 push @$cmd, '--filestore', '--fs-type', $fstype;
8d64bd8c
DC
294 if ($journal_dev) {
295 print "using device '$journal_dev' for journal\n";
296 push @$cmd, '--journal-dev', $devpath, $journal_dev;
297 } else {
298 push @$cmd, $devpath;
299 }
50239dba
FG
300 }
301
be753927 302
7d4fc5ef
DM
303 run_command($cmd);
304 };
38db610a 305
7d4fc5ef 306 return $rpcenv->fork_worker('cephcreateosd', $devname, $authuser, $worker);
38db610a
DM
307 }});
308
13f4d762 309__PACKAGE__->register_method ({
7d4fc5ef
DM
310 name => 'destroyosd',
311 path => '{osdid}',
312 method => 'DELETE',
313 description => "Destroy OSD",
13f4d762
DM
314 proxyto => 'node',
315 protected => 1,
316 parameters => {
be753927 317 additionalProperties => 0,
13f4d762
DM
318 properties => {
319 node => get_standard_option('pve-node'),
7d4fc5ef
DM
320 osdid => {
321 description => 'OSD ID',
322 type => 'integer',
0e5816e4 323 },
7d4fc5ef
DM
324 cleanup => {
325 description => "If set, we remove partition table entries.",
326 type => 'boolean',
327 optional => 1,
328 default => 0,
13f4d762
DM
329 },
330 },
13f4d762 331 },
7d4fc5ef 332 returns => { type => 'string' },
13f4d762
DM
333 code => sub {
334 my ($param) = @_;
335
7d4fc5ef 336 my $rpcenv = PVE::RPCEnvironment::get();
13f4d762 337
7d4fc5ef 338 my $authuser = $rpcenv->get_user();
13f4d762 339
7d4fc5ef 340 PVE::CephTools::check_ceph_inited();
0e5816e4 341
7d4fc5ef 342 my $osdid = $param->{osdid};
0e5816e4 343
7d4fc5ef
DM
344 my $rados = PVE::RADOS->new();
345 my $osdstat = &$get_osd_status($rados, $osdid);
13f4d762 346
7d4fc5ef
DM
347 die "osd is in use (in == 1)\n" if $osdstat->{in};
348 #&$run_ceph_cmd(['osd', 'out', $osdid]);
68e0c4bd 349
7d4fc5ef 350 die "osd is still runnung (up == 1)\n" if $osdstat->{up};
68e0c4bd 351
7d4fc5ef 352 my $osdsection = "osd.$osdid";
68e0c4bd 353
7d4fc5ef
DM
354 my $worker = sub {
355 my $upid = shift;
68e0c4bd 356
7d4fc5ef 357 # reopen with longer timeout
be753927 358 $rados = PVE::RADOS->new(timeout => PVE::CephTools::get_config('long_rados_timeout'));
68e0c4bd 359
7d4fc5ef 360 print "destroy OSD $osdsection\n";
68e0c4bd 361
7d4fc5ef
DM
362 eval { PVE::CephTools::ceph_service_cmd('stop', $osdsection); };
363 warn $@ if $@;
68e0c4bd 364
7d4fc5ef
DM
365 print "Remove $osdsection from the CRUSH map\n";
366 $rados->mon_command({ prefix => "osd crush remove", name => $osdsection, format => 'plain' });
68e0c4bd 367
7d4fc5ef
DM
368 print "Remove the $osdsection authentication key.\n";
369 $rados->mon_command({ prefix => "auth del", entity => $osdsection, format => 'plain' });
370
371 print "Remove OSD $osdsection\n";
372 $rados->mon_command({ prefix => "osd rm", ids => [ $osdsection ], format => 'plain' });
373
374 # try to unmount from standard mount point
375 my $mountpoint = "/var/lib/ceph/osd/ceph-$osdid";
376
377 my $remove_partition = sub {
84aed461 378 my ($part) = @_;
7d4fc5ef
DM
379
380 return if !$part || (! -b $part );
84aed461
WL
381 my $partnum = PVE::Diskmanage::get_partnum($part);
382 my $devpath = PVE::Diskmanage::get_blockdev($part);
383
384 print "remove partition $part (disk '${devpath}', partnum $partnum)\n";
385 eval { run_command(['/sbin/sgdisk', '-d', $partnum, "${devpath}"]); };
386 warn $@ if $@;
7d4fc5ef 387 };
68e0c4bd 388
7d4fc5ef
DM
389 my $journal_part;
390 my $data_part;
be753927 391
7d4fc5ef
DM
392 if ($param->{cleanup}) {
393 my $jpath = "$mountpoint/journal";
394 $journal_part = abs_path($jpath);
395
396 if (my $fd = IO::File->new("/proc/mounts", "r")) {
397 while (defined(my $line = <$fd>)) {
398 my ($dev, $path, $fstype) = split(/\s+/, $line);
399 next if !($dev && $path && $fstype);
400 next if $dev !~ m|^/dev/|;
401 if ($path eq $mountpoint) {
402 $data_part = abs_path($dev);
403 last;
404 }
405 }
406 close($fd);
68e0c4bd
DM
407 }
408 }
7d4fc5ef
DM
409
410 print "Unmount OSD $osdsection from $mountpoint\n";
84aed461 411 eval { run_command(['/bin/umount', $mountpoint]); };
7d4fc5ef
DM
412 if (my $err = $@) {
413 warn $err;
414 } elsif ($param->{cleanup}) {
ef3d095b 415 #be aware of the ceph udev rules which can remount.
84aed461 416 &$remove_partition($data_part);
ef3d095b 417 &$remove_partition($journal_part);
7d4fc5ef 418 }
68e0c4bd 419 };
68e0c4bd 420
7d4fc5ef 421 return $rpcenv->fork_worker('cephdestroyosd', $osdsection, $authuser, $worker);
68e0c4bd
DM
422 }});
423
38db610a 424__PACKAGE__->register_method ({
7d4fc5ef
DM
425 name => 'in',
426 path => '{osdid}/in',
38db610a 427 method => 'POST',
7d4fc5ef 428 description => "ceph osd in",
38db610a
DM
429 proxyto => 'node',
430 protected => 1,
90c75580
TL
431 permissions => {
432 check => ['perm', '/', [ 'Sys.Modify' ]],
433 },
38db610a 434 parameters => {
be753927 435 additionalProperties => 0,
38db610a
DM
436 properties => {
437 node => get_standard_option('pve-node'),
7d4fc5ef
DM
438 osdid => {
439 description => 'OSD ID',
38db610a 440 type => 'integer',
38db610a
DM
441 },
442 },
443 },
7d4fc5ef 444 returns => { type => "null" },
38db610a
DM
445 code => sub {
446 my ($param) = @_;
447
7d4fc5ef 448 PVE::CephTools::check_ceph_inited();
38db610a 449
7d4fc5ef 450 my $osdid = $param->{osdid};
0e5816e4 451
7d4fc5ef 452 my $rados = PVE::RADOS->new();
f7e342ea 453
7d4fc5ef 454 my $osdstat = &$get_osd_status($rados, $osdid); # osd exists?
f7e342ea 455
7d4fc5ef 456 my $osdsection = "osd.$osdid";
38db610a 457
7d4fc5ef 458 $rados->mon_command({ prefix => "osd in", ids => [ $osdsection ], format => 'plain' });
38db610a
DM
459
460 return undef;
461 }});
462
463__PACKAGE__->register_method ({
7d4fc5ef
DM
464 name => 'out',
465 path => '{osdid}/out',
38db610a 466 method => 'POST',
7d4fc5ef 467 description => "ceph osd out",
38db610a
DM
468 proxyto => 'node',
469 protected => 1,
90c75580
TL
470 permissions => {
471 check => ['perm', '/', [ 'Sys.Modify' ]],
472 },
38db610a 473 parameters => {
be753927 474 additionalProperties => 0,
38db610a
DM
475 properties => {
476 node => get_standard_option('pve-node'),
7d4fc5ef
DM
477 osdid => {
478 description => 'OSD ID',
479 type => 'integer',
480 },
38db610a
DM
481 },
482 },
7d4fc5ef 483 returns => { type => "null" },
38db610a
DM
484 code => sub {
485 my ($param) = @_;
486
a34866f0 487 PVE::CephTools::check_ceph_inited();
38db610a 488
7d4fc5ef 489 my $osdid = $param->{osdid};
38db610a 490
7d4fc5ef 491 my $rados = PVE::RADOS->new();
38db610a 492
7d4fc5ef 493 my $osdstat = &$get_osd_status($rados, $osdid); # osd exists?
38db610a 494
7d4fc5ef 495 my $osdsection = "osd.$osdid";
38db610a 496
7d4fc5ef 497 $rados->mon_command({ prefix => "osd out", ids => [ $osdsection ], format => 'plain' });
38db610a 498
7d4fc5ef
DM
499 return undef;
500 }});
38db610a 501
7d4fc5ef
DM
502package PVE::API2::Ceph;
503
504use strict;
505use warnings;
506use File::Basename;
507use File::Path;
508use POSIX qw (LONG_MAX);
509use Cwd qw(abs_path);
510use IO::Dir;
511use UUID;
512use Net::IP;
513
514use PVE::SafeSyslog;
515use PVE::Tools qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach);
516use PVE::Exception qw(raise raise_param_exc);
517use PVE::INotify;
518use PVE::Cluster qw(cfs_lock_file cfs_read_file cfs_write_file);
519use PVE::AccessControl;
520use PVE::Storage;
521use PVE::RESTHandler;
522use PVE::RPCEnvironment;
523use PVE::JSONSchema qw(get_standard_option);
524use JSON;
525use PVE::RADOS;
526use PVE::CephTools;
527
528use base qw(PVE::RESTHandler);
529
530use Data::Dumper; # fixme: remove
531
532my $pve_osd_default_journal_size = 1024*5;
533
534__PACKAGE__->register_method ({
be753927 535 subclass => "PVE::API2::CephOSD",
7d4fc5ef
DM
536 path => 'osd',
537});
538
539__PACKAGE__->register_method ({
540 name => 'index',
541 path => '',
542 method => 'GET',
543 description => "Directory index.",
544 permissions => { user => 'all' },
90c75580
TL
545 permissions => {
546 check => ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any => 1],
547 },
7d4fc5ef 548 parameters => {
be753927 549 additionalProperties => 0,
7d4fc5ef
DM
550 properties => {
551 node => get_standard_option('pve-node'),
552 },
553 },
554 returns => {
555 type => 'array',
556 items => {
557 type => "object",
558 properties => {},
559 },
560 links => [ { rel => 'child', href => "{name}" } ],
561 },
562 code => sub {
563 my ($param) = @_;
564
565 my $result = [
566 { name => 'init' },
567 { name => 'mon' },
568 { name => 'osd' },
569 { name => 'pools' },
570 { name => 'stop' },
571 { name => 'start' },
572 { name => 'status' },
573 { name => 'crush' },
574 { name => 'config' },
575 { name => 'log' },
576 { name => 'disks' },
a46ad02a 577 { name => 'flags' },
7d4fc5ef
DM
578 ];
579
580 return $result;
581 }});
582
583__PACKAGE__->register_method ({
584 name => 'disks',
585 path => 'disks',
586 method => 'GET',
587 description => "List local disks.",
588 proxyto => 'node',
589 protected => 1,
90c75580
TL
590 permissions => {
591 check => ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any => 1],
592 },
7d4fc5ef 593 parameters => {
be753927 594 additionalProperties => 0,
7d4fc5ef
DM
595 properties => {
596 node => get_standard_option('pve-node'),
597 type => {
598 description => "Only list specific types of disks.",
599 type => 'string',
600 enum => ['unused', 'journal_disks'],
601 optional => 1,
602 },
603 },
604 },
605 returns => {
606 type => 'array',
607 items => {
608 type => "object",
609 properties => {
610 dev => { type => 'string' },
611 used => { type => 'string', optional => 1 },
612 gpt => { type => 'boolean' },
613 size => { type => 'integer' },
614 osdid => { type => 'integer' },
615 vendor => { type => 'string', optional => 1 },
616 model => { type => 'string', optional => 1 },
617 serial => { type => 'string', optional => 1 },
618 },
619 },
620 # links => [ { rel => 'child', href => "{}" } ],
621 },
622 code => sub {
623 my ($param) = @_;
624
625 PVE::CephTools::check_ceph_inited();
626
5fd5c30d 627 my $disks = PVE::Diskmanage::get_disks(undef, 1);
7d4fc5ef
DM
628
629 my $res = [];
630 foreach my $dev (keys %$disks) {
631 my $d = $disks->{$dev};
632 if ($param->{type}) {
633 if ($param->{type} eq 'journal_disks') {
634 next if $d->{osdid} >= 0;
635 next if !$d->{gpt};
636 } elsif ($param->{type} eq 'unused') {
637 next if $d->{used};
638 } else {
639 die "internal error"; # should not happen
640 }
641 }
642
643 $d->{dev} = "/dev/$dev";
be753927 644 push @$res, $d;
7d4fc5ef
DM
645 }
646
647 return $res;
648 }});
649
650__PACKAGE__->register_method ({
651 name => 'config',
652 path => 'config',
653 method => 'GET',
90c75580
TL
654 permissions => {
655 check => ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any => 1],
656 },
7d4fc5ef
DM
657 description => "Get Ceph configuration.",
658 parameters => {
be753927 659 additionalProperties => 0,
7d4fc5ef
DM
660 properties => {
661 node => get_standard_option('pve-node'),
662 },
663 },
664 returns => { type => 'string' },
665 code => sub {
666 my ($param) = @_;
667
668 PVE::CephTools::check_ceph_inited();
669
670 my $path = PVE::CephTools::get_config('pve_ceph_cfgpath');
671 return PVE::Tools::file_get_contents($path);
672
673 }});
674
675__PACKAGE__->register_method ({
676 name => 'listmon',
677 path => 'mon',
678 method => 'GET',
679 description => "Get Ceph monitor list.",
680 proxyto => 'node',
681 protected => 1,
90c75580
TL
682 permissions => {
683 check => ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any => 1],
684 },
7d4fc5ef 685 parameters => {
be753927 686 additionalProperties => 0,
7d4fc5ef
DM
687 properties => {
688 node => get_standard_option('pve-node'),
689 },
690 },
691 returns => {
692 type => 'array',
693 items => {
694 type => "object",
695 properties => {
696 name => { type => 'string' },
697 addr => { type => 'string' },
698 },
699 },
700 links => [ { rel => 'child', href => "{name}" } ],
701 },
702 code => sub {
703 my ($param) = @_;
704
705 PVE::CephTools::check_ceph_inited();
706
707 my $res = [];
708
709 my $cfg = PVE::CephTools::parse_ceph_config();
710
711 my $monhash = {};
712 foreach my $section (keys %$cfg) {
713 my $d = $cfg->{$section};
714 if ($section =~ m/^mon\.(\S+)$/) {
715 my $monid = $1;
716 if ($d->{'mon addr'} && $d->{'host'}) {
717 $monhash->{$monid} = {
718 addr => $d->{'mon addr'},
719 host => $d->{'host'},
720 name => $monid,
721 }
722 }
723 }
724 }
725
726 eval {
727 my $rados = PVE::RADOS->new();
728 my $monstat = $rados->mon_command({ prefix => 'mon_status' });
729 my $mons = $monstat->{monmap}->{mons};
730 foreach my $d (@$mons) {
731 next if !defined($d->{name});
be753927 732 $monhash->{$d->{name}}->{rank} = $d->{rank};
7d4fc5ef
DM
733 $monhash->{$d->{name}}->{addr} = $d->{addr};
734 if (grep { $_ eq $d->{rank} } @{$monstat->{quorum}}) {
735 $monhash->{$d->{name}}->{quorum} = 1;
736 }
737 }
738 };
739 warn $@ if $@;
740
741 return PVE::RESTHandler::hash_to_array($monhash, 'name');
742 }});
743
744__PACKAGE__->register_method ({
745 name => 'init',
746 path => 'init',
747 method => 'POST',
748 description => "Create initial ceph default configuration and setup symlinks.",
749 proxyto => 'node',
750 protected => 1,
90c75580
TL
751 permissions => {
752 check => ['perm', '/', [ 'Sys.Modify' ]],
753 },
7d4fc5ef 754 parameters => {
be753927 755 additionalProperties => 0,
7d4fc5ef
DM
756 properties => {
757 node => get_standard_option('pve-node'),
758 network => {
be753927 759 description => "Use specific network for all ceph related traffic",
7d4fc5ef
DM
760 type => 'string', format => 'CIDR',
761 optional => 1,
762 maxLength => 128,
763 },
764 size => {
207f4932
FG
765 description => 'Targeted number of replicas per object',
766 type => 'integer',
767 default => 3,
768 optional => 1,
769 minimum => 1,
770 maximum => 7,
771 },
772 min_size => {
773 description => 'Minimum number of available replicas per object to allow I/O',
7d4fc5ef
DM
774 type => 'integer',
775 default => 2,
776 optional => 1,
777 minimum => 1,
83663637 778 maximum => 7,
7d4fc5ef
DM
779 },
780 pg_bits => {
3cba09d5
FG
781 description => "Placement group bits, used to specify the " .
782 "default number of placement groups.\n\nNOTE: 'osd pool " .
783 "default pg num' does not work for default pools.",
7d4fc5ef
DM
784 type => 'integer',
785 default => 6,
786 optional => 1,
787 minimum => 6,
788 maximum => 14,
789 },
4280f25c 790 disable_cephx => {
97f050bb
FG
791 description => "Disable cephx authentification.\n\n" .
792 "WARNING: cephx is a security feature protecting against " .
793 "man-in-the-middle attacks. Only consider disabling cephx ".
794 "if your network is private!",
77bb90b0
AD
795 type => 'boolean',
796 optional => 1,
797 default => 0,
4280f25c 798 },
7d4fc5ef
DM
799 },
800 },
801 returns => { type => 'null' },
802 code => sub {
803 my ($param) = @_;
804
805 PVE::CephTools::check_ceph_installed();
806
807 # simply load old config if it already exists
808 my $cfg = PVE::CephTools::parse_ceph_config();
809
810 if (!$cfg->{global}) {
811
812 my $fsid;
813 my $uuid;
814
815 UUID::generate($uuid);
816 UUID::unparse($uuid, $fsid);
817
4280f25c 818 my $auth = $param->{disable_cephx} ? 'none' : 'cephx';
77bb90b0 819
7d4fc5ef
DM
820 $cfg->{global} = {
821 'fsid' => $fsid,
77bb90b0
AD
822 'auth cluster required' => $auth,
823 'auth service required' => $auth,
824 'auth client required' => $auth,
7d4fc5ef 825 'osd journal size' => $pve_osd_default_journal_size,
207f4932
FG
826 'osd pool default size' => $param->{size} // 3,
827 'osd pool default min size' => $param->{min_size} // 2,
2e9d791e 828 'mon allow pool delete' => 'true',
7d4fc5ef
DM
829 };
830
be753927 831 # this does not work for default pools
7d4fc5ef 832 #'osd pool default pg num' => $pg_num,
be753927 833 #'osd pool default pgp num' => $pg_num,
7d4fc5ef 834 }
be753927 835
7d4fc5ef
DM
836 $cfg->{global}->{keyring} = '/etc/pve/priv/$cluster.$name.keyring';
837 $cfg->{osd}->{keyring} = '/var/lib/ceph/osd/ceph-$id/keyring';
838
7d4fc5ef
DM
839 if ($param->{pg_bits}) {
840 $cfg->{global}->{'osd pg bits'} = $param->{pg_bits};
841 $cfg->{global}->{'osd pgp bits'} = $param->{pg_bits};
842 }
843
844 if ($param->{network}) {
845 $cfg->{global}->{'public network'} = $param->{network};
846 $cfg->{global}->{'cluster network'} = $param->{network};
847 }
848
849 PVE::CephTools::write_ceph_config($cfg);
850
851 PVE::CephTools::setup_pve_symlinks();
852
853 return undef;
854 }});
855
856my $find_node_ip = sub {
857 my ($cidr) = @_;
858
7d4fc5ef 859 my $net = Net::IP->new($cidr) || die Net::IP::Error() . "\n";
905c2f51
WB
860 my $id = $net->version == 6 ? 'address6' : 'address';
861
862 my $config = PVE::INotify::read_file('interfaces');
863 my $ifaces = $config->{ifaces};
7d4fc5ef 864
905c2f51 865 foreach my $iface (keys %$ifaces) {
957a59b3 866 my $d = $ifaces->{$iface};
905c2f51
WB
867 next if !$d->{$id};
868 my $a = Net::IP->new($d->{$id});
7d4fc5ef 869 next if !$a;
905c2f51 870 return $d->{$id} if $net->overlaps($a);
7d4fc5ef
DM
871 }
872
873 die "unable to find local address within network '$cidr'\n";
874};
875
c05ff7b4
DC
876my $create_mgr = sub {
877 my ($rados, $id) = @_;
878
879 my $clustername = PVE::CephTools::get_config('ccname');
880 my $mgrdir = "/var/lib/ceph/mgr/$clustername-$id";
881 my $mgrkeyring = "$mgrdir/keyring";
882 my $mgrname = "mgr.$id";
883
884 die "ceph manager directory '$mgrdir' already exists\n"
885 if -d $mgrdir;
886
887 print "creating manager directory '$mgrdir'\n";
888 mkdir $mgrdir;
889 print "creating keys for '$mgrname'\n";
890 my $output = $rados->mon_command({ prefix => 'auth get-or-create',
891 entity => $mgrname,
892 caps => [
893 mon => 'allow profile mgr',
894 osd => 'allow *',
895 mds => 'allow *',
896 ],
897 format => 'plain'});
898 PVE::Tools::file_set_contents($mgrkeyring, $output);
899
900 print "setting owner for directory\n";
901 run_command(["chown", 'ceph:ceph', '-R', $mgrdir]);
902
903 print "enabling service 'ceph-mgr\@$id.service'\n";
904 PVE::CephTools::ceph_service_cmd('enable', $mgrname);
905 print "starting service 'ceph-mgr\@$id.service'\n";
906 PVE::CephTools::ceph_service_cmd('start', $mgrname);
907};
908
909my $destroy_mgr = sub {
910 my ($mgrid) = @_;
911
912 my $clustername = PVE::CephTools::get_config('ccname');
913 my $mgrname = "mgr.$mgrid";
914 my $mgrdir = "/var/lib/ceph/mgr/$clustername-$mgrid";
915
916 die "ceph manager directory '$mgrdir' not found\n"
917 if ! -d $mgrdir;
918
919 print "disabling service 'ceph-mgr\@$mgrid.service'\n";
920 PVE::CephTools::ceph_service_cmd('disable', $mgrname);
921 print "stopping service 'ceph-mgr\@$mgrid.service'\n";
922 PVE::CephTools::ceph_service_cmd('stop', $mgrname);
923
924 print "removing manager directory '$mgrdir'\n";
925 File::Path::remove_tree($mgrdir);
926};
927
7d4fc5ef
DM
928__PACKAGE__->register_method ({
929 name => 'createmon',
930 path => 'mon',
931 method => 'POST',
c05ff7b4 932 description => "Create Ceph Monitor and Manager",
7d4fc5ef
DM
933 proxyto => 'node',
934 protected => 1,
90c75580
TL
935 permissions => {
936 check => ['perm', '/', [ 'Sys.Modify' ]],
937 },
7d4fc5ef 938 parameters => {
be753927 939 additionalProperties => 0,
7d4fc5ef
DM
940 properties => {
941 node => get_standard_option('pve-node'),
c05ff7b4
DC
942 id => {
943 type => 'string',
944 optional => 1,
945 pattern => '[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?',
946 description => "The ID for the monitor, when omitted the same as the nodename",
947 },
948 'exclude-manager' => {
949 type => 'boolean',
950 optional => 1,
951 default => 0,
952 description => "When set, only a monitor will be created.",
953 },
7d4fc5ef
DM
954 },
955 },
956 returns => { type => 'string' },
957 code => sub {
958 my ($param) = @_;
959
960 PVE::CephTools::check_ceph_inited();
961
962 PVE::CephTools::setup_pve_symlinks();
963
964 my $rpcenv = PVE::RPCEnvironment::get();
965
966 my $authuser = $rpcenv->get_user();
967
968 my $cfg = PVE::CephTools::parse_ceph_config();
969
970 my $moncount = 0;
971
be753927 972 my $monaddrhash = {};
7d4fc5ef 973
3279b1d2
WL
974 my $systemd_managed = PVE::CephTools::systemd_managed();
975
7d4fc5ef
DM
976 foreach my $section (keys %$cfg) {
977 next if $section eq 'global';
978 my $d = $cfg->{$section};
979 if ($section =~ m/^mon\./) {
980 $moncount++;
981 if ($d->{'mon addr'}) {
982 $monaddrhash->{$d->{'mon addr'}} = $section;
983 }
984 }
985 }
38db610a 986
c05ff7b4 987 my $monid = $param->{id} // $param->{node};
38db610a 988
be753927 989 my $monsection = "mon.$monid";
f7e342ea
DM
990 my $ip;
991 if (my $pubnet = $cfg->{global}->{'public network'}) {
992 $ip = &$find_node_ip($pubnet);
993 } else {
994 $ip = PVE::Cluster::remote_node_ip($param->{node});
995 }
996
98901f1d 997 my $monaddr = Net::IP::ip_is_ipv6($ip) ? "[$ip]:6789" : "$ip:6789";
38db610a
DM
998 my $monname = $param->{node};
999
1000 die "monitor '$monsection' already exists\n" if $cfg->{$monsection};
be753927 1001 die "monitor address '$monaddr' already in use by '$monaddrhash->{$monaddr}'\n"
38db610a
DM
1002 if $monaddrhash->{$monaddr};
1003
52d7be41
DM
1004 my $worker = sub {
1005 my $upid = shift;
38db610a 1006
a34866f0
DM
1007 my $pve_ckeyring_path = PVE::CephTools::get_config('pve_ckeyring_path');
1008
52d7be41
DM
1009 if (! -f $pve_ckeyring_path) {
1010 run_command("ceph-authtool $pve_ckeyring_path --create-keyring " .
1011 "--gen-key -n client.admin");
1012 }
38db610a 1013
a34866f0 1014 my $pve_mon_key_path = PVE::CephTools::get_config('pve_mon_key_path');
52d7be41
DM
1015 if (! -f $pve_mon_key_path) {
1016 run_command("cp $pve_ckeyring_path $pve_mon_key_path.tmp");
1017 run_command("ceph-authtool $pve_mon_key_path.tmp -n client.admin --set-uid=0 " .
1018 "--cap mds 'allow' " .
1019 "--cap osd 'allow *' " .
d197634b 1020 "--cap mgr 'allow *' " .
52d7be41 1021 "--cap mon 'allow *'");
3279b1d2
WL
1022 run_command("cp $pve_mon_key_path.tmp /etc/ceph/ceph.client.admin.keyring") if $systemd_managed;
1023 run_command("chown ceph:ceph /etc/ceph/ceph.client.admin.keyring") if $systemd_managed;
52d7be41
DM
1024 run_command("ceph-authtool $pve_mon_key_path.tmp --gen-key -n mon. --cap mon 'allow *'");
1025 run_command("mv $pve_mon_key_path.tmp $pve_mon_key_path");
38db610a
DM
1026 }
1027
a34866f0
DM
1028 my $ccname = PVE::CephTools::get_config('ccname');
1029
52d7be41
DM
1030 my $mondir = "/var/lib/ceph/mon/$ccname-$monid";
1031 -d $mondir && die "monitor filesystem '$mondir' already exist\n";
be753927 1032
52d7be41 1033 my $monmap = "/tmp/monmap";
c05ff7b4 1034
52d7be41
DM
1035 eval {
1036 mkdir $mondir;
1037
3279b1d2
WL
1038 run_command("chown ceph:ceph $mondir") if $systemd_managed;
1039
52d7be41 1040 if ($moncount > 0) {
87eb0fc2 1041 my $rados = PVE::RADOS->new(timeout => PVE::CephTools::get_config('long_rados_timeout'));
970236b3
DM
1042 my $mapdata = $rados->mon_command({ prefix => 'mon getmap', format => 'plain' });
1043 PVE::Tools::file_set_contents($monmap, $mapdata);
52d7be41
DM
1044 } else {
1045 run_command("monmaptool --create --clobber --add $monid $monaddr --print $monmap");
1046 }
38db610a 1047
52d7be41 1048 run_command("ceph-mon --mkfs -i $monid --monmap $monmap --keyring $pve_mon_key_path");
3279b1d2 1049 run_command("chown ceph:ceph -R $mondir") if $systemd_managed;
52d7be41
DM
1050 };
1051 my $err = $@;
1052 unlink $monmap;
1053 if ($err) {
1054 File::Path::remove_tree($mondir);
1055 die $err;
1056 }
38db610a 1057
52d7be41
DM
1058 $cfg->{$monsection} = {
1059 'host' => $monname,
1060 'mon addr' => $monaddr,
1061 };
38db610a 1062
a34866f0 1063 PVE::CephTools::write_ceph_config($cfg);
38db610a 1064
cc5bb515
FG
1065 my $create_keys_pid = fork();
1066 if (!defined($create_keys_pid)) {
1067 die "Could not spawn ceph-create-keys to create bootstrap keys\n";
1068 } elsif ($create_keys_pid == 0) {
1069 exit PVE::Tools::run_command(['ceph-create-keys', '-i', $monid]);
1070 } else {
1071 PVE::CephTools::ceph_service_cmd('start', $monsection);
19bada0c 1072
cc5bb515
FG
1073 if ($systemd_managed) {
1074 #to ensure we have the correct startup order.
1075 eval { PVE::Tools::run_command(['/bin/systemctl', 'enable', "ceph-mon\@${monid}.service"]); };
1076 warn "Enable ceph-mon\@${monid}.service manually"if $@;
1077 }
1078 waitpid($create_keys_pid, 0);
19bada0c 1079 }
c05ff7b4
DC
1080
1081 # create manager
1082 if (!$param->{'exclude-manager'}) {
87eb0fc2 1083 my $rados = PVE::RADOS->new(timeout => PVE::CephTools::get_config('long_rados_timeout'));
c05ff7b4
DC
1084 $create_mgr->($rados, $monid);
1085 }
52d7be41 1086 };
38db610a 1087
52d7be41 1088 return $rpcenv->fork_worker('cephcreatemon', $monsection, $authuser, $worker);
38db610a
DM
1089 }});
1090
1091__PACKAGE__->register_method ({
1092 name => 'destroymon',
39e1ad70
DM
1093 path => 'mon/{monid}',
1094 method => 'DELETE',
c05ff7b4 1095 description => "Destroy Ceph Monitor and Manager.",
38db610a
DM
1096 proxyto => 'node',
1097 protected => 1,
90c75580
TL
1098 permissions => {
1099 check => ['perm', '/', [ 'Sys.Modify' ]],
1100 },
38db610a 1101 parameters => {
be753927 1102 additionalProperties => 0,
38db610a
DM
1103 properties => {
1104 node => get_standard_option('pve-node'),
1105 monid => {
1106 description => 'Monitor ID',
c05ff7b4
DC
1107 type => 'string',
1108 pattern => '[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?',
38db610a 1109 },
c05ff7b4
DC
1110 'exclude-manager' => {
1111 type => 'boolean',
1112 default => 0,
1113 optional => 1,
1114 description => "When set, removes only the monitor, not the manager"
1115 }
38db610a
DM
1116 },
1117 },
52d7be41 1118 returns => { type => 'string' },
38db610a
DM
1119 code => sub {
1120 my ($param) = @_;
1121
52d7be41
DM
1122 my $rpcenv = PVE::RPCEnvironment::get();
1123
1124 my $authuser = $rpcenv->get_user();
1125
a34866f0 1126 PVE::CephTools::check_ceph_inited();
38db610a 1127
a34866f0 1128 my $cfg = PVE::CephTools::parse_ceph_config();
be753927 1129
38db610a 1130 my $monid = $param->{monid};
be753927 1131 my $monsection = "mon.$monid";
38db610a 1132
36fd0190 1133 my $rados = PVE::RADOS->new();
970236b3 1134 my $monstat = $rados->mon_command({ prefix => 'mon_status' });
38db610a
DM
1135 my $monlist = $monstat->{monmap}->{mons};
1136
be753927 1137 die "no such monitor id '$monid'\n"
38db610a
DM
1138 if !defined($cfg->{$monsection});
1139
a34866f0
DM
1140 my $ccname = PVE::CephTools::get_config('ccname');
1141
38db610a
DM
1142 my $mondir = "/var/lib/ceph/mon/$ccname-$monid";
1143 -d $mondir || die "monitor filesystem '$mondir' does not exist on this node\n";
1144
1145 die "can't remove last monitor\n" if scalar(@$monlist) <= 1;
1146
52d7be41
DM
1147 my $worker = sub {
1148 my $upid = shift;
38db610a 1149
2f804640 1150 # reopen with longer timeout
be753927 1151 $rados = PVE::RADOS->new(timeout => PVE::CephTools::get_config('long_rados_timeout'));
f26b46db 1152
6e3c2f47 1153 $rados->mon_command({ prefix => "mon remove", name => $monid, format => 'plain' });
38db610a 1154
a34866f0 1155 eval { PVE::CephTools::ceph_service_cmd('stop', $monsection); };
52d7be41 1156 warn $@ if $@;
38db610a 1157
52d7be41 1158 delete $cfg->{$monsection};
a34866f0 1159 PVE::CephTools::write_ceph_config($cfg);
52d7be41 1160 File::Path::remove_tree($mondir);
c05ff7b4
DC
1161
1162 # remove manager
1163 if (!$param->{'exclude-manager'}) {
1164 eval { $destroy_mgr->($monid); };
1165 warn $@ if $@;
1166 }
52d7be41
DM
1167 };
1168
1169 return $rpcenv->fork_worker('cephdestroymon', $monsection, $authuser, $worker);
38db610a
DM
1170 }});
1171
ca68ac3e
DC
1172__PACKAGE__->register_method ({
1173 name => 'createmgr',
1174 path => 'mgr',
1175 method => 'POST',
1176 description => "Create Ceph Manager",
1177 proxyto => 'node',
1178 protected => 1,
1179 permissions => {
1180 check => ['perm', '/', [ 'Sys.Modify' ]],
1181 },
1182 parameters => {
1183 additionalProperties => 0,
1184 properties => {
1185 node => get_standard_option('pve-node'),
1186 id => {
1187 type => 'string',
1188 optional => 1,
1189 pattern => '[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?',
1190 description => "The ID for the manager, when omitted the same as the nodename",
1191 },
1192 },
1193 },
1194 returns => { type => 'string' },
1195 code => sub {
1196 my ($param) = @_;
1197
1198 PVE::CephTools::check_ceph_inited();
1199
1200 my $rpcenv = PVE::RPCEnvironment::get();
1201
1202 my $authuser = $rpcenv->get_user();
1203
1204 my $mgrid = $param->{id} // $param->{node};
1205
1206 my $worker = sub {
1207 my $upid = shift;
1208
1209 my $rados = PVE::RADOS->new(timeout => PVE::CephTools::get_config('long_rados_timeout'));
1210
1211 $create_mgr->($rados, $mgrid);
1212 };
1213
1214 return $rpcenv->fork_worker('cephcreatemgr', "mgr.$mgrid", $authuser, $worker);
1215 }});
1216
1217__PACKAGE__->register_method ({
1218 name => 'destroymgr',
1219 path => 'mgr/{id}',
1220 method => 'DELETE',
1221 description => "Destroy Ceph Manager.",
1222 proxyto => 'node',
1223 protected => 1,
1224 permissions => {
1225 check => ['perm', '/', [ 'Sys.Modify' ]],
1226 },
1227 parameters => {
1228 additionalProperties => 0,
1229 properties => {
1230 node => get_standard_option('pve-node'),
1231 id => {
1232 description => 'The ID of the manager',
1233 type => 'string',
1234 pattern => '[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?',
1235 },
1236 },
1237 },
1238 returns => { type => 'string' },
1239 code => sub {
1240 my ($param) = @_;
1241
1242 my $rpcenv = PVE::RPCEnvironment::get();
1243
1244 my $authuser = $rpcenv->get_user();
1245
1246 PVE::CephTools::check_ceph_inited();
1247
1248 my $mgrid = $param->{id};
1249
1250 my $worker = sub {
1251 my $upid = shift;
1252
1253 $destroy_mgr->($mgrid);
1254 };
1255
1256 return $rpcenv->fork_worker('cephdestroymgr', "mgr.$mgrid", $authuser, $worker);
1257 }});
1258
38db610a
DM
1259__PACKAGE__->register_method ({
1260 name => 'stop',
1261 path => 'stop',
1262 method => 'POST',
1263 description => "Stop ceph services.",
1264 proxyto => 'node',
1265 protected => 1,
90c75580
TL
1266 permissions => {
1267 check => ['perm', '/', [ 'Sys.Modify' ]],
1268 },
38db610a 1269 parameters => {
be753927 1270 additionalProperties => 0,
38db610a
DM
1271 properties => {
1272 node => get_standard_option('pve-node'),
68e0c4bd
DM
1273 service => {
1274 description => 'Ceph service name.',
1275 type => 'string',
1276 optional => 1,
b0e5ae21 1277 pattern => '(mon|mds|osd|mgr)\.[A-Za-z0-9\-]{1,32}',
68e0c4bd 1278 },
38db610a
DM
1279 },
1280 },
68e0c4bd 1281 returns => { type => 'string' },
38db610a
DM
1282 code => sub {
1283 my ($param) = @_;
1284
68e0c4bd
DM
1285 my $rpcenv = PVE::RPCEnvironment::get();
1286
1287 my $authuser = $rpcenv->get_user();
1288
a34866f0 1289 PVE::CephTools::check_ceph_inited();
38db610a 1290
a34866f0 1291 my $cfg = PVE::CephTools::parse_ceph_config();
38db610a
DM
1292 scalar(keys %$cfg) || die "no configuration\n";
1293
68e0c4bd
DM
1294 my $worker = sub {
1295 my $upid = shift;
38db610a 1296
68e0c4bd
DM
1297 my $cmd = ['stop'];
1298 if ($param->{service}) {
1299 push @$cmd, $param->{service};
1300 }
1301
a34866f0 1302 PVE::CephTools::ceph_service_cmd(@$cmd);
68e0c4bd
DM
1303 };
1304
1305 return $rpcenv->fork_worker('srvstop', $param->{service} || 'ceph',
1306 $authuser, $worker);
38db610a
DM
1307 }});
1308
1309__PACKAGE__->register_method ({
1310 name => 'start',
1311 path => 'start',
1312 method => 'POST',
1313 description => "Start ceph services.",
1314 proxyto => 'node',
1315 protected => 1,
90c75580
TL
1316 permissions => {
1317 check => ['perm', '/', [ 'Sys.Modify' ]],
1318 },
38db610a 1319 parameters => {
be753927 1320 additionalProperties => 0,
38db610a
DM
1321 properties => {
1322 node => get_standard_option('pve-node'),
68e0c4bd
DM
1323 service => {
1324 description => 'Ceph service name.',
1325 type => 'string',
1326 optional => 1,
b0e5ae21 1327 pattern => '(mon|mds|osd|mgr)\.[A-Za-z0-9\-]{1,32}',
68e0c4bd 1328 },
38db610a
DM
1329 },
1330 },
68e0c4bd 1331 returns => { type => 'string' },
38db610a
DM
1332 code => sub {
1333 my ($param) = @_;
1334
68e0c4bd
DM
1335 my $rpcenv = PVE::RPCEnvironment::get();
1336
1337 my $authuser = $rpcenv->get_user();
1338
a34866f0 1339 PVE::CephTools::check_ceph_inited();
38db610a 1340
a34866f0 1341 my $cfg = PVE::CephTools::parse_ceph_config();
38db610a
DM
1342 scalar(keys %$cfg) || die "no configuration\n";
1343
68e0c4bd
DM
1344 my $worker = sub {
1345 my $upid = shift;
38db610a 1346
68e0c4bd
DM
1347 my $cmd = ['start'];
1348 if ($param->{service}) {
1349 push @$cmd, $param->{service};
1350 }
1351
a34866f0 1352 PVE::CephTools::ceph_service_cmd(@$cmd);
68e0c4bd
DM
1353 };
1354
1355 return $rpcenv->fork_worker('srvstart', $param->{service} || 'ceph',
1356 $authuser, $worker);
38db610a
DM
1357 }});
1358
1359__PACKAGE__->register_method ({
1360 name => 'status',
1361 path => 'status',
1362 method => 'GET',
1363 description => "Get ceph status.",
1364 proxyto => 'node',
1365 protected => 1,
90c75580
TL
1366 permissions => {
1367 check => ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any => 1],
1368 },
38db610a 1369 parameters => {
be753927 1370 additionalProperties => 0,
38db610a
DM
1371 properties => {
1372 node => get_standard_option('pve-node'),
1373 },
1374 },
1375 returns => { type => 'object' },
1376 code => sub {
1377 my ($param) = @_;
1378
a34866f0 1379 PVE::CephTools::check_ceph_enabled();
38db610a 1380
36fd0190 1381 my $rados = PVE::RADOS->new();
84caf265
DC
1382 my $status = $rados->mon_command({ prefix => 'status' });
1383 $status->{health} = $rados->mon_command({ prefix => 'health', detail => 'detail' });
1384 return $status;
38db610a
DM
1385 }});
1386
b0537f7b
DM
1387__PACKAGE__->register_method ({
1388 name => 'lspools',
1389 path => 'pools',
1390 method => 'GET',
1391 description => "List all pools.",
1392 proxyto => 'node',
1393 protected => 1,
90c75580
TL
1394 permissions => {
1395 check => ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any => 1],
1396 },
b0537f7b 1397 parameters => {
be753927 1398 additionalProperties => 0,
b0537f7b
DM
1399 properties => {
1400 node => get_standard_option('pve-node'),
1401 },
1402 },
1403 returns => {
1404 type => 'array',
1405 items => {
1406 type => "object",
1407 properties => {
1408 pool => { type => 'integer' },
1409 pool_name => { type => 'string' },
1410 size => { type => 'integer' },
1411 },
1412 },
1413 links => [ { rel => 'child', href => "{pool_name}" } ],
1414 },
1415 code => sub {
1416 my ($param) = @_;
1417
a34866f0 1418 PVE::CephTools::check_ceph_inited();
b0537f7b 1419
36fd0190 1420 my $rados = PVE::RADOS->new();
d54f1126
DM
1421
1422 my $stats = {};
1423 my $res = $rados->mon_command({ prefix => 'df' });
6f9ea1c1
WL
1424 my $total = $res->{stats}->{total_avail_bytes} || 0;
1425
d54f1126
DM
1426 foreach my $d (@{$res->{pools}}) {
1427 next if !$d->{stats};
1428 next if !defined($d->{id});
1429 $stats->{$d->{id}} = $d->{stats};
1430 }
1431
1432 $res = $rados->mon_command({ prefix => 'osd dump' });
b0537f7b
DM
1433
1434 my $data = [];
1435 foreach my $e (@{$res->{pools}}) {
1436 my $d = {};
1437 foreach my $attr (qw(pool pool_name size min_size pg_num crush_ruleset)) {
1438 $d->{$attr} = $e->{$attr} if defined($e->{$attr});
1439 }
d54f1126
DM
1440 if (my $s = $stats->{$d->{pool}}) {
1441 $d->{bytes_used} = $s->{bytes_used};
6f9ea1c1
WL
1442 $d->{percent_used} = ($s->{bytes_used} / $total)*100
1443 if $s->{max_avail} && $total;
d54f1126 1444 }
b0537f7b
DM
1445 push @$data, $d;
1446 }
1447
d54f1126 1448
b0537f7b
DM
1449 return $data;
1450 }});
1451
1452__PACKAGE__->register_method ({
1453 name => 'createpool',
7d4fc5ef 1454 path => 'pools',
38db610a 1455 method => 'POST',
7d4fc5ef 1456 description => "Create POOL",
38db610a
DM
1457 proxyto => 'node',
1458 protected => 1,
90c75580
TL
1459 permissions => {
1460 check => ['perm', '/', [ 'Sys.Modify' ]],
1461 },
38db610a 1462 parameters => {
be753927 1463 additionalProperties => 0,
38db610a
DM
1464 properties => {
1465 node => get_standard_option('pve-node'),
7d4fc5ef
DM
1466 name => {
1467 description => "The name of the pool. It must be unique.",
38db610a 1468 type => 'string',
43d85563 1469 },
7d4fc5ef
DM
1470 size => {
1471 description => 'Number of replicas per object',
1472 type => 'integer',
1473 default => 2,
0e5816e4 1474 optional => 1,
7d4fc5ef 1475 minimum => 1,
83663637 1476 maximum => 7,
0e5816e4 1477 },
7d4fc5ef
DM
1478 min_size => {
1479 description => 'Minimum number of replicas per object',
1480 type => 'integer',
1481 default => 1,
1482 optional => 1,
1483 minimum => 1,
83663637 1484 maximum => 7,
7d4fc5ef
DM
1485 },
1486 pg_num => {
1487 description => "Number of placement groups.",
1488 type => 'integer',
1489 default => 64,
1490 optional => 1,
1491 minimum => 8,
1492 maximum => 32768,
1493 },
1494 crush_ruleset => {
1495 description => "The ruleset to use for mapping object placement in the cluster.",
1496 type => 'integer',
1497 minimum => 0,
1498 maximum => 32768,
1499 default => 0,
43d85563
DM
1500 optional => 1,
1501 },
38db610a
DM
1502 },
1503 },
7d4fc5ef 1504 returns => { type => 'null' },
38db610a
DM
1505 code => sub {
1506 my ($param) = @_;
1507
a34866f0 1508 PVE::CephTools::check_ceph_inited();
38db610a 1509
7d4fc5ef 1510 my $pve_ckeyring_path = PVE::CephTools::get_config('pve_ckeyring_path');
13f4d762 1511
be753927 1512 die "not fully configured - missing '$pve_ckeyring_path'\n"
7d4fc5ef 1513 if ! -f $pve_ckeyring_path;
13f4d762 1514
7d4fc5ef
DM
1515 my $pg_num = $param->{pg_num} || 64;
1516 my $size = $param->{size} || 2;
1517 my $min_size = $param->{min_size} || 1;
1518 my $ruleset = $param->{crush_ruleset} || 0;
36fd0190 1519 my $rados = PVE::RADOS->new();
38db610a 1520
be753927 1521 $rados->mon_command({
7d4fc5ef
DM
1522 prefix => "osd pool create",
1523 pool => $param->{name},
1524 pg_num => int($pg_num),
1525# this does not work for unknown reason
1526# properties => ["size=$size", "min_size=$min_size", "crush_ruleset=$ruleset"],
1527 format => 'plain',
1528 });
52d7be41 1529
be753927 1530 $rados->mon_command({
7d4fc5ef
DM
1531 prefix => "osd pool set",
1532 pool => $param->{name},
1533 var => 'min_size',
1534 val => $min_size,
1535 format => 'plain',
1536 });
a34866f0 1537
be753927 1538 $rados->mon_command({
7d4fc5ef
DM
1539 prefix => "osd pool set",
1540 pool => $param->{name},
1541 var => 'size',
1542 val => $size,
1543 format => 'plain',
1544 });
0e5816e4 1545
7d4fc5ef 1546 if (defined($param->{crush_ruleset})) {
be753927 1547 $rados->mon_command({
7d4fc5ef
DM
1548 prefix => "osd pool set",
1549 pool => $param->{name},
1550 var => 'crush_ruleset',
1551 val => $param->{crush_ruleset},
1552 format => 'plain',
1553 });
1554 }
52d7be41 1555
7d4fc5ef 1556 return undef;
38db610a
DM
1557 }});
1558
a46ad02a
DC
1559__PACKAGE__->register_method ({
1560 name => 'get_flags',
1561 path => 'flags',
1562 method => 'GET',
1563 description => "get all set ceph flags",
1564 proxyto => 'node',
1565 protected => 1,
1566 permissions => {
1567 check => ['perm', '/', [ 'Sys.Audit' ]],
1568 },
1569 parameters => {
1570 additionalProperties => 0,
1571 properties => {
1572 node => get_standard_option('pve-node'),
1573 },
1574 },
1575 returns => { type => 'string' },
1576 code => sub {
1577 my ($param) = @_;
1578
1579 PVE::CephTools::check_ceph_inited();
1580
1581 my $pve_ckeyring_path = PVE::CephTools::get_config('pve_ckeyring_path');
1582
1583 die "not fully configured - missing '$pve_ckeyring_path'\n"
1584 if ! -f $pve_ckeyring_path;
1585
1586 my $rados = PVE::RADOS->new();
1587
1588 my $stat = $rados->mon_command({ prefix => 'osd dump' });
1589
1590 return $stat->{flags} // '';
1591 }});
1592
1593__PACKAGE__->register_method ({
1594 name => 'set_flag',
1595 path => 'flags/{flag}',
1596 method => 'POST',
1597 description => "Set a ceph flag",
1598 proxyto => 'node',
1599 protected => 1,
1600 permissions => {
1601 check => ['perm', '/', [ 'Sys.Modify' ]],
1602 },
1603 parameters => {
1604 additionalProperties => 0,
1605 properties => {
1606 node => get_standard_option('pve-node'),
1607 flag => {
1608 description => 'The ceph flag to set/unset',
1609 type => 'string',
1610 enum => [ 'full', 'pause', 'noup', 'nodown', 'noout', 'noin', 'nobackfill', 'norebalance', 'norecover', 'noscrub', 'nodeep-scrub', 'notieragent'],
1611 },
1612 },
1613 },
1614 returns => { type => 'null' },
1615 code => sub {
1616 my ($param) = @_;
1617
1618 PVE::CephTools::check_ceph_inited();
1619
1620 my $pve_ckeyring_path = PVE::CephTools::get_config('pve_ckeyring_path');
1621
1622 die "not fully configured - missing '$pve_ckeyring_path'\n"
1623 if ! -f $pve_ckeyring_path;
1624
1625 my $set = $param->{set} // !$param->{unset};
1626 my $rados = PVE::RADOS->new();
1627
1628 $rados->mon_command({
1629 prefix => "osd set",
1630 key => $param->{flag},
1631 });
1632
1633 return undef;
1634 }});
1635
1636__PACKAGE__->register_method ({
1637 name => 'unset_flag',
1638 path => 'flags/{flag}',
1639 method => 'DELETE',
1640 description => "Unset a ceph flag",
1641 proxyto => 'node',
1642 protected => 1,
1643 permissions => {
1644 check => ['perm', '/', [ 'Sys.Modify' ]],
1645 },
1646 parameters => {
1647 additionalProperties => 0,
1648 properties => {
1649 node => get_standard_option('pve-node'),
1650 flag => {
1651 description => 'The ceph flag to set/unset',
1652 type => 'string',
1653 enum => [ 'full', 'pause', 'noup', 'nodown', 'noout', 'noin', 'nobackfill', 'norebalance', 'norecover', 'noscrub', 'nodeep-scrub', 'notieragent'],
1654 },
1655 },
1656 },
1657 returns => { type => 'null' },
1658 code => sub {
1659 my ($param) = @_;
1660
1661 PVE::CephTools::check_ceph_inited();
1662
1663 my $pve_ckeyring_path = PVE::CephTools::get_config('pve_ckeyring_path');
1664
1665 die "not fully configured - missing '$pve_ckeyring_path'\n"
1666 if ! -f $pve_ckeyring_path;
1667
1668 my $set = $param->{set} // !$param->{unset};
1669 my $rados = PVE::RADOS->new();
1670
1671 $rados->mon_command({
1672 prefix => "osd unset",
1673 key => $param->{flag},
1674 });
1675
1676 return undef;
1677 }});
1678
38db610a 1679__PACKAGE__->register_method ({
7d4fc5ef
DM
1680 name => 'destroypool',
1681 path => 'pools/{name}',
39e1ad70 1682 method => 'DELETE',
7d4fc5ef 1683 description => "Destroy pool",
38db610a
DM
1684 proxyto => 'node',
1685 protected => 1,
90c75580
TL
1686 permissions => {
1687 check => ['perm', '/', [ 'Sys.Modify' ]],
1688 },
38db610a 1689 parameters => {
be753927 1690 additionalProperties => 0,
38db610a
DM
1691 properties => {
1692 node => get_standard_option('pve-node'),
7d4fc5ef
DM
1693 name => {
1694 description => "The name of the pool. It must be unique.",
1695 type => 'string',
0e5816e4 1696 },
76dc2ad0
DC
1697 force => {
1698 description => "If true, destroys pool even if in use",
1699 type => 'boolean',
1700 optional => 1,
1701 default => 0,
1702 }
38db610a
DM
1703 },
1704 },
7d4fc5ef 1705 returns => { type => 'null' },
38db610a
DM
1706 code => sub {
1707 my ($param) = @_;
1708
a34866f0 1709 PVE::CephTools::check_ceph_inited();
38db610a 1710
76dc2ad0
DC
1711 # if not forced, destroy ceph pool only when no
1712 # vm disks are on it anymore
1713 if (!$param->{force}) {
1714 my $storagecfg = PVE::Storage::config();
1715 foreach my $storageid (keys %{$storagecfg->{ids}}) {
1716 my $storage = $storagecfg->{ids}->{$storageid};
1717 next if $storage->{type} ne 'rbd';
1718 next if $storage->{pool} ne $param->{name};
1719
1720 # check if any vm disks are on the pool
1721 my $res = PVE::Storage::vdisk_list($storagecfg, $storageid);
1722 die "ceph pool '$param->{name}' still in use by storage '$storageid'\n"
1723 if @{$res->{$storageid}} != 0;
1724 }
1725 }
1726
36fd0190 1727 my $rados = PVE::RADOS->new();
7d4fc5ef 1728 # fixme: '--yes-i-really-really-mean-it'
be753927 1729 $rados->mon_command({
7d4fc5ef
DM
1730 prefix => "osd pool delete",
1731 pool => $param->{name},
1732 pool2 => $param->{name},
1733 sure => '--yes-i-really-really-mean-it',
1734 format => 'plain',
1735 });
52d7be41 1736
7d4fc5ef 1737 return undef;
38db610a 1738 }});
2f692121 1739
a34866f0 1740
2f692121
DM
1741__PACKAGE__->register_method ({
1742 name => 'crush',
1743 path => 'crush',
1744 method => 'GET',
1745 description => "Get OSD crush map",
1746 proxyto => 'node',
1747 protected => 1,
90c75580
TL
1748 permissions => {
1749 check => ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any => 1],
1750 },
2f692121 1751 parameters => {
be753927 1752 additionalProperties => 0,
2f692121
DM
1753 properties => {
1754 node => get_standard_option('pve-node'),
1755 },
1756 },
1757 returns => { type => 'string' },
1758 code => sub {
1759 my ($param) = @_;
1760
a34866f0 1761 PVE::CephTools::check_ceph_inited();
2f692121 1762
8b336060
DM
1763 # this produces JSON (difficult to read for the user)
1764 # my $txt = &$run_ceph_cmd_text(['osd', 'crush', 'dump'], quiet => 1);
2f692121 1765
8b336060
DM
1766 my $txt = '';
1767
1768 my $mapfile = "/var/tmp/ceph-crush.map.$$";
1769 my $mapdata = "/var/tmp/ceph-crush.txt.$$";
1770
36fd0190 1771 my $rados = PVE::RADOS->new();
be753927 1772
8b336060 1773 eval {
970236b3
DM
1774 my $bindata = $rados->mon_command({ prefix => 'osd getcrushmap', format => 'plain' });
1775 PVE::Tools::file_set_contents($mapfile, $bindata);
8b336060
DM
1776 run_command(['crushtool', '-d', $mapfile, '-o', $mapdata]);
1777 $txt = PVE::Tools::file_get_contents($mapdata);
1778 };
1779 my $err = $@;
1780
1781 unlink $mapfile;
1782 unlink $mapdata;
1783
1784 die $err if $err;
be753927 1785
2f692121
DM
1786 return $txt;
1787 }});
1788
570278fa 1789__PACKAGE__->register_method({
be753927
DC
1790 name => 'log',
1791 path => 'log',
570278fa
DM
1792 method => 'GET',
1793 description => "Read ceph log",
1794 proxyto => 'node',
1795 permissions => {
1796 check => ['perm', '/nodes/{node}', [ 'Sys.Syslog' ]],
1797 },
1798 protected => 1,
1799 parameters => {
be753927 1800 additionalProperties => 0,
570278fa
DM
1801 properties => {
1802 node => get_standard_option('pve-node'),
1803 start => {
1804 type => 'integer',
1805 minimum => 0,
1806 optional => 1,
1807 },
1808 limit => {
1809 type => 'integer',
1810 minimum => 0,
1811 optional => 1,
1812 },
1813 },
1814 },
1815 returns => {
1816 type => 'array',
be753927 1817 items => {
570278fa
DM
1818 type => "object",
1819 properties => {
1820 n => {
1821 description=> "Line number",
1822 type=> 'integer',
1823 },
1824 t => {
1825 description=> "Line text",
1826 type => 'string',
1827 }
1828 }
1829 }
1830 },
1831 code => sub {
1832 my ($param) = @_;
1833
1834 my $rpcenv = PVE::RPCEnvironment::get();
1835 my $user = $rpcenv->get_user();
1836 my $node = $param->{node};
1837
1838 my $logfile = "/var/log/ceph/ceph.log";
1839 my ($count, $lines) = PVE::Tools::dump_logfile($logfile, $param->{start}, $param->{limit});
1840
1841 $rpcenv->set_result_attrib('total', $count);
be753927
DC
1842
1843 return $lines;
570278fa
DM
1844 }});
1845
2f692121 1846