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