]> git.proxmox.com Git - pve-manager.git/blame - PVE/CLI/pveceph.pm
buildsys: split list of CLI sources over multiple lines
[pve-manager.git] / PVE / CLI / pveceph.pm
CommitLineData
3e8560ac
DM
1package PVE::CLI::pveceph;
2
3use strict;
4use warnings;
5
6use Fcntl ':flock';
7use File::Path;
8use IO::File;
9use JSON;
10use Data::Dumper;
11use LWP::UserAgent;
12
13use PVE::SafeSyslog;
14use PVE::Cluster;
15use PVE::INotify;
16use PVE::RPCEnvironment;
17use PVE::Storage;
18use PVE::Tools qw(run_command);
19use PVE::JSONSchema qw(get_standard_option);
6fb08cb9 20use PVE::Ceph::Tools;
91dfa228 21use PVE::Ceph::Services;
3e8560ac 22use PVE::API2::Ceph;
7e1a9d25 23use PVE::API2::Ceph::FS;
b82649cc 24use PVE::API2::Ceph::MDS;
4fec2764 25use PVE::API2::Ceph::MGR;
98fe93ae 26use PVE::API2::Ceph::MON;
79fa41a2 27use PVE::API2::Ceph::OSD;
3e8560ac
DM
28
29use PVE::CLIHandler;
30
31use base qw(PVE::CLIHandler);
32
33my $nodename = PVE::INotify::nodename();
34
35my $upid_exit = sub {
36 my $upid = shift;
37 my $status = PVE::Tools::upid_read_status($upid);
4bb46baa 38 exit(PVE::Tools::upid_status_is_error($status) ? -1 : 0);
3e8560ac
DM
39};
40
7e017024
DM
41sub setup_environment {
42 PVE::RPCEnvironment->setup_default_cli_env();
43}
44
3e8560ac
DM
45__PACKAGE__->register_method ({
46 name => 'purge',
47 path => 'purge',
48 method => 'POST',
49 description => "Destroy ceph related data and configuration files.",
50 parameters => {
51 additionalProperties => 0,
52 properties => {
91dfa228
AA
53 logs => {
54 description => 'Additionally purge Ceph logs, /var/log/ceph.',
55 type => 'boolean',
56 optional => 1,
57 },
58 crash => {
59 description => 'Additionally purge Ceph crash logs, /var/lib/ceph/crash.',
60 type => 'boolean',
61 optional => 1,
62 },
3e8560ac
DM
63 },
64 },
65 returns => { type => 'null' },
66 code => sub {
67 my ($param) = @_;
68
91dfa228
AA
69 my $message;
70 my $pools = [];
71 my $monstat = {};
72 my $mdsstat = {};
73 my $osdstat = [];
3e8560ac
DM
74
75 eval {
76 my $rados = PVE::RADOS->new();
91dfa228
AA
77 $pools = PVE::Ceph::Tools::ls_pools(undef, $rados);
78 $monstat = PVE::Ceph::Services::get_services_info('mon', undef, $rados);
79 $mdsstat = PVE::Ceph::Services::get_services_info('mds', undef, $rados);
80 $osdstat = $rados->mon_command({ prefix => 'osd metadata' });
3e8560ac 81 };
c3a3f3ab 82 warn "Error gathering ceph info, already purged? Message: $@" if $@;
3e8560ac 83
91dfa228
AA
84 my $osd = grep { $_->{hostname} eq $nodename } @$osdstat;
85 my $mds = grep { $mdsstat->{$_}->{host} eq $nodename } keys %$mdsstat;
86 my $mon = grep { $monstat->{$_}->{host} eq $nodename } keys %$monstat;
3e8560ac 87
91dfa228
AA
88 # no pools = no data
89 $message .= "- remove pools, this will !!DESTROY DATA!!\n" if @$pools;
90 $message .= "- remove active OSD on $nodename\n" if $osd;
91 $message .= "- remove active MDS on $nodename\n" if $mds;
92 $message .= "- remove other MONs, $nodename is not the last MON\n"
93 if scalar(keys %$monstat) > 1 && $mon;
94
95 # display all steps at once
96 die "Unable to purge Ceph!\n\nTo continue:\n$message" if $message;
97
98 my $services = PVE::Ceph::Services::get_local_services();
99 $services->{mon} = $monstat if $mon;
100 $services->{crash}->{$nodename} = { direxists => 1 } if $param->{crash};
101 $services->{logs}->{$nodename} = { direxists => 1 } if $param->{logs};
102
103 PVE::Ceph::Tools::purge_all_ceph_services($services);
104 PVE::Ceph::Tools::purge_all_ceph_files($services);
3e8560ac
DM
105
106 return undef;
107 }});
108
983921b9
TL
109my $supported_ceph_versions = ['octopus', 'pacific', 'quincy'];
110my $default_ceph_version = 'pacific';
111
3e8560ac
DM
112__PACKAGE__->register_method ({
113 name => 'install',
114 path => 'install',
115 method => 'POST',
116 description => "Install ceph related packages.",
117 parameters => {
118 additionalProperties => 0,
119 properties => {
120 version => {
121 type => 'string',
983921b9
TL
122 enum => $supported_ceph_versions,
123 default => $default_ceph_version,
c3d9c698 124 description => "Ceph version to install.",
3e8560ac 125 optional => 1,
61819403
TL
126 },
127 'allow-experimental' => {
128 type => 'boolean',
129 default => 0,
130 optional => 1,
131 description => "Allow experimental versions. Use with care!",
132 },
3f7d054e
TL
133 'test-repository' => {
134 type => 'boolean',
135 default => 0,
136 optional => 1,
137 description => "Use the test, not the main repository. Use with care!",
138 },
3e8560ac
DM
139 },
140 },
141 returns => { type => 'null' },
142 code => sub {
143 my ($param) = @_;
144
983921b9 145 my $cephver = $param->{version} || $default_ceph_version;
3e8560ac 146
3f7d054e
TL
147 my $repo = $param->{'test-repository'} ? 'test' : 'main';
148
c3d9c698 149 my $repolist;
00fa70bf 150 if ($cephver eq 'octopus') {
ef25743f 151 warn "Ceph Octopus will go EOL after 2022-07\n";
3f7d054e 152 $repolist = "deb http://download.proxmox.com/debian/ceph-octopus bullseye $repo\n";
00fa70bf 153 } elsif ($cephver eq 'pacific') {
3f7d054e 154 $repolist = "deb http://download.proxmox.com/debian/ceph-pacific bullseye $repo\n";
9e81f364
TL
155 } elsif ($cephver eq 'quincy') {
156 $repolist = "deb http://download.proxmox.com/debian/ceph-quincy bullseye $repo\n";
caf37855 157 } else {
3f7d054e 158 die "unsupported ceph version: $cephver";
caf37855 159 }
c3d9c698
TL
160 PVE::Tools::file_set_contents("/etc/apt/sources.list.d/ceph.list", $repolist);
161
7271e6f6
TL
162 my $supported_re = join('|', $supported_ceph_versions->@*);
163 warn "WARNING: installing non-default ceph release '$cephver'!\n" if $cephver !~ qr/^(?:$supported_re)$/;
3e8560ac 164
87166f36 165 local $ENV{DEBIAN_FRONTEND} = 'noninteractive';
3e8560ac 166 print "update available package list\n";
d26556c0
TL
167 eval {
168 run_command(
169 ['apt-get', '-q', 'update'],
170 outfunc => sub {},
171 errfunc => sub { print STDERR "$_[0]\n" },
172 )
173 };
3e8560ac 174
ecddd2e2 175 my @apt_install = qw(apt-get --no-install-recommends -o Dpkg::Options::=--force-confnew install --);
2ca75379
TL
176 my @ceph_packages = qw(
177 ceph
178 ceph-common
179 ceph-mds
180 ceph-fuse
181 gdisk
08e22c1e 182 nvme-cli
2ca75379
TL
183 );
184
b23305d5
TL
185 # got split out with quincy and is required by PVE tooling, conditionally exclude it for older
186 # FIXME: remove condition with PVE 8.0, i.e., once we only support quincy+ new installations
3409fbea
SS
187 if ($cephver ne 'octopus' and $cephver ne 'pacific') {
188 push @ceph_packages, 'ceph-volume';
189 }
190
55ab726e 191 print "start installation\n";
4dd27d50 192
d380d000 193 # this flag helps to determine when apt is actually done installing (vs. partial extracing)
9f6dc075 194 my $install_flag_fn = PVE::Ceph::Tools::ceph_install_flag_file();
d380d000 195 open(my $install_flag, '>', $install_flag_fn) or die "could not create install flag - $!\n";
4dd27d50
AL
196 close $install_flag;
197
2ca75379 198 if (system(@apt_install, @ceph_packages) != 0) {
d380d000 199 unlink $install_flag_fn or warn "could not remove Ceph installation flag - $!";
2ca75379
TL
200 die "apt failed during ceph installation ($?)\n";
201 }
55ab726e 202
37bf860e 203 print "\ninstalled ceph $cephver successfully!\n";
d380d000
TL
204 # done: drop flag file so that the PVE::Ceph::Tools check returns Ok now.
205 unlink $install_flag_fn or warn "could not remove Ceph installation flag - $!";
ece49b3c
TL
206
207 print "\nreloading API to load new Ceph RADOS library...\n";
208 run_command([
209 'systemctl', 'try-reload-or-restart', 'pvedaemon.service', 'pveproxy.service'
210 ]);
3e8560ac
DM
211
212 return undef;
213 }});
214
1baeb2d1
AL
215__PACKAGE__->register_method ({
216 name => 'status',
217 path => 'status',
218 method => 'GET',
219 description => "Get Ceph Status.",
220 parameters => {
221 additionalProperties => 0,
222 },
223 returns => { type => 'null' },
224 code => sub {
225 PVE::Ceph::Tools::check_ceph_inited();
226
227 run_command(
228 ['ceph', '-s'],
229 outfunc => sub { print "$_[0]\n" },
ab459f6d
TL
230 errfunc => sub { print STDERR "$_[0]\n" },
231 timeout => 15,
1baeb2d1
AL
232 );
233 return undef;
234 }});
235
02c1e98e
DC
236my $get_storages = sub {
237 my ($fs, $is_default) = @_;
238
239 my $cfg = PVE::Storage::config();
240
241 my $storages = $cfg->{ids};
242 my $res = {};
243 foreach my $storeid (keys %$storages) {
244 my $curr = $storages->{$storeid};
245 next if $curr->{type} ne 'cephfs';
246 my $cur_fs = $curr->{'fs-name'};
247 $res->{$storeid} = $storages->{$storeid}
248 if (!defined($cur_fs) && $is_default) || (defined($cur_fs) && $fs eq $cur_fs);
249 }
250
251 return $res;
252};
253
254__PACKAGE__->register_method ({
255 name => 'destroyfs',
256 path => 'destroyfs',
257 method => 'DELETE',
258 description => "Destroy a Ceph filesystem",
259 parameters => {
260 additionalProperties => 0,
261 properties => {
262 node => get_standard_option('pve-node'),
263 name => {
264 description => "The ceph filesystem name.",
265 type => 'string',
266 },
267 'remove-storages' => {
268 description => "Remove all pveceph-managed storages configured for this fs.",
269 type => 'boolean',
270 optional => 1,
271 default => 0,
272 },
273 'remove-pools' => {
274 description => "Remove data and metadata pools configured for this fs.",
275 type => 'boolean',
276 optional => 1,
277 default => 0,
278 },
279 },
280 },
281 returns => { type => 'string' },
282 code => sub {
283 my ($param) = @_;
284
285 PVE::Ceph::Tools::check_ceph_inited();
286
287 my $rpcenv = PVE::RPCEnvironment::get();
288 my $user = $rpcenv->get_user();
289
290 my $fs_name = $param->{name};
291
292 my $fs;
293 my $fs_list = PVE::Ceph::Tools::ls_fs();
294 for my $entry (@$fs_list) {
295 next if $entry->{name} ne $fs_name;
296 $fs = $entry;
297 last;
298 }
299 die "no such cephfs '$fs_name'\n" if !$fs;
300
301 my $worker = sub {
302 my $rados = PVE::RADOS->new();
303
304 if ($param->{'remove-storages'}) {
305 my $defaultfs;
306 my $fs_dump = $rados->mon_command({ prefix => "fs dump" });
307 for my $fs ($fs_dump->{filesystems}->@*) {
308 next if $fs->{id} != $fs_dump->{default_fscid};
309 $defaultfs = $fs->{mdsmap}->{fs_name};
310 }
311 warn "no default fs found, maybe not all relevant storages are removed\n"
312 if !defined($defaultfs);
313
314 my $storages = $get_storages->($fs_name, $fs_name eq ($defaultfs // ''));
315 for my $storeid (keys %$storages) {
316 my $store = $storages->{$storeid};
317 if (!$store->{disable}) {
318 die "storage '$storeid' is not disabled, make sure to disable ".
319 "and unmount the storage first\n";
320 }
321 }
322
323 my $err;
324 for my $storeid (keys %$storages) {
325 # skip external clusters, not managed by pveceph
326 next if $storages->{$storeid}->{monhost};
327 eval { PVE::API2::Storage::Config->delete({storage => $storeid}) };
328 if ($@) {
329 warn "failed to remove storage '$storeid': $@\n";
330 $err = 1;
331 }
332 }
333 die "failed to remove (some) storages - check log and remove manually!\n"
334 if $err;
335 }
336
337 PVE::Ceph::Tools::destroy_fs($fs_name, $rados);
338
339 if ($param->{'remove-pools'}) {
340 warn "removing metadata pool '$fs->{metadata_pool}'\n";
341 eval { PVE::Ceph::Tools::destroy_pool($fs->{metadata_pool}, $rados) };
342 warn "$@\n" if $@;
343
344 foreach my $pool ($fs->{data_pools}->@*) {
345 warn "removing data pool '$pool'\n";
346 eval { PVE::Ceph::Tools::destroy_pool($pool, $rados) };
347 warn "$@\n" if $@;
348 }
349 }
350
351 };
352 return $rpcenv->fork_worker('cephdestroyfs', $fs_name, $user, $worker);
353 }});
354
3e8560ac
DM
355our $cmddef = {
356 init => [ 'PVE::API2::Ceph', 'init', [], { node => $nodename } ],
8c476782 357 pool => {
a0665001 358 ls => [ 'PVE::API2::Ceph::Pool', 'lspools', [], { node => $nodename }, sub {
d4dba076
AA
359 my ($data, $schema, $options) = @_;
360 PVE::CLIFormatter::print_api_result($data, $schema,
361 [
362 'pool_name',
363 'size',
364 'min_size',
365 'pg_num',
5a3d7942
AA
366 'pg_num_min',
367 'pg_num_final',
d4dba076 368 'pg_autoscale_mode',
5a3d7942
AA
369 'target_size',
370 'target_size_ratio',
d4dba076
AA
371 'crush_rule_name',
372 'percent_used',
373 'bytes_used',
374 ],
375 $options);
376 }, $PVE::RESTHandler::standard_output_options],
a0665001
AL
377 create => [ 'PVE::API2::Ceph::Pool', 'createpool', ['name'], { node => $nodename }],
378 destroy => [ 'PVE::API2::Ceph::Pool', 'destroypool', ['name'], { node => $nodename } ],
379 set => [ 'PVE::API2::Ceph::Pool', 'setpool', ['name'], { node => $nodename } ],
380 get => [ 'PVE::API2::Ceph::Pool', 'getpool', ['name'], { node => $nodename }, sub {
54ba7dd9
AA
381 my ($data, $schema, $options) = @_;
382 PVE::CLIFormatter::print_api_result($data, $schema, undef, $options);
383 }, $PVE::RESTHandler::standard_output_options],
8c476782
TL
384 },
385 lspools => { alias => 'pool ls' },
386 createpool => { alias => 'pool create' },
387 destroypool => { alias => 'pool destroy' },
388 fs => {
389 create => [ 'PVE::API2::Ceph::FS', 'createfs', [], { node => $nodename }],
02c1e98e 390 destroy => [ __PACKAGE__, 'destroyfs', ['name'], { node => $nodename }],
8c476782
TL
391 },
392 osd => {
79fa41a2
DC
393 create => [ 'PVE::API2::Ceph::OSD', 'createosd', ['dev'], { node => $nodename }, $upid_exit],
394 destroy => [ 'PVE::API2::Ceph::OSD', 'destroyosd', ['osdid'], { node => $nodename }, $upid_exit],
8c476782
TL
395 },
396 createosd => { alias => 'osd create' },
397 destroyosd => { alias => 'osd destroy' },
398 mon => {
98fe93ae
DC
399 create => [ 'PVE::API2::Ceph::MON', 'createmon', [], { node => $nodename }, $upid_exit],
400 destroy => [ 'PVE::API2::Ceph::MON', 'destroymon', ['monid'], { node => $nodename }, $upid_exit],
8c476782
TL
401 },
402 createmon => { alias => 'mon create' },
403 destroymon => { alias => 'mon destroy' },
404 mgr => {
4fec2764
DC
405 create => [ 'PVE::API2::Ceph::MGR', 'createmgr', [], { node => $nodename }, $upid_exit],
406 destroy => [ 'PVE::API2::Ceph::MGR', 'destroymgr', ['id'], { node => $nodename }, $upid_exit],
8c476782
TL
407 },
408 createmgr => { alias => 'mgr create' },
409 destroymgr => { alias => 'mgr destroy' },
410 mds => {
411 create => [ 'PVE::API2::Ceph::MDS', 'createmds', [], { node => $nodename }, $upid_exit],
f16bb531 412 destroy => [ 'PVE::API2::Ceph::MDS', 'destroymds', ['name'], { node => $nodename }, $upid_exit],
8c476782 413 },
7da6ff26
DJ
414 start => [ 'PVE::API2::Ceph', 'start', [], { node => $nodename }, $upid_exit],
415 stop => [ 'PVE::API2::Ceph', 'stop', [], { node => $nodename }, $upid_exit],
3e8560ac
DM
416 install => [ __PACKAGE__, 'install', [] ],
417 purge => [ __PACKAGE__, 'purge', [] ],
1baeb2d1 418 status => [ __PACKAGE__, 'status', []],
3e8560ac
DM
419};
420
4211;