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