]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/RBDPlugin.pm
pbs: die if master key is missing
[pve-storage.git] / PVE / Storage / RBDPlugin.pm
CommitLineData
0509010d
AD
1package PVE::Storage::RBDPlugin;
2
3use strict;
4use warnings;
5102900d 5
a1528ffe 6use Cwd qw(abs_path);
0509010d 7use IO::File;
5102900d 8use JSON;
e858048f 9use Net::IP;
5102900d
TL
10
11use PVE::CephConfig;
cfe46e2d 12use PVE::Cluster qw(cfs_read_file);;
0509010d 13use PVE::JSONSchema qw(get_standard_option);
0ef8fb9d 14use PVE::ProcFSTools;
41aacc6c 15use PVE::RADOS;
5102900d 16use PVE::Storage::Plugin;
a1528ffe 17use PVE::Tools qw(run_command trim file_read_firstline);
0509010d
AD
18
19use base qw(PVE::Storage::Plugin);
20
89a8800b
DC
21my $get_parent_image_name = sub {
22 my ($parent) = @_;
23 return undef if !$parent;
24 return $parent->{image} . "@" . $parent->{snapshot};
25};
26
cfe46e2d
AL
27my $librados_connect = sub {
28 my ($scfg, $storeid, $options) = @_;
29
30 my $librados_config = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
31
32 my $rados = PVE::RADOS->new(%$librados_config);
33
34 return $rados;
35};
36
4cf696f6 37my sub get_rbd_path {
aeb007cb 38 my ($scfg, $volume) = @_;
c27fe648
TL
39 my $path = $scfg->{pool} ? $scfg->{pool} : 'rbd';
40 $path .= "/$scfg->{namespace}" if defined($scfg->{namespace});
41 $path .= "/$volume" if defined($volume);
42 return $path;
8897f5dc
SP
43};
44
cfe46e2d
AL
45my sub get_rbd_dev_path {
46 my ($scfg, $storeid, $volume) = @_;
47
48 my $cluster_id = '';
78638b3d
TL
49 if ($scfg->{fsid}) {
50 # NOTE: the config doesn't support this currently (but it could!), hack for qemu-server tests
51 $cluster_id = $scfg->{fsid};
52 } elsif ($scfg->{monhost}) {
cfe46e2d
AL
53 my $rados = $librados_connect->($scfg, $storeid);
54 $cluster_id = $rados->mon_command({ prefix => 'fsid', format => 'json' })->{fsid};
55 } else {
56 $cluster_id = cfs_read_file('ceph.conf')->{global}->{fsid};
57 }
58
59 my $uuid_pattern = "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})";
60 if ($cluster_id =~ qr/^${uuid_pattern}$/is) {
61 $cluster_id = $1; # use untained value
62 } else {
63 die "cluster fsid has invalid format\n";
64 }
65
66 my $rbd_path = get_rbd_path($scfg, $volume);
67 my $pve_path = "/dev/rbd-pve/${cluster_id}/${rbd_path}";
68 my $path = "/dev/rbd/${rbd_path}";
69
a1528ffe
AL
70 if (!-e $pve_path && -e $path) {
71 # possibly mapped before rbd-pve rule existed
72 my $real_dev = abs_path($path);
73 my ($rbd_id) = ($real_dev =~ m|/dev/rbd([0-9]+)$|);
74 my $dev_cluster_id = file_read_firstline("/sys/devices/rbd/${rbd_id}/cluster_fsid");
75 return $path if $cluster_id eq $dev_cluster_id;
76 }
cfe46e2d
AL
77 return $pve_path;
78}
79
6cc88e8e
AA
80my $build_cmd = sub {
81 my ($binary, $scfg, $storeid, $op, @options) = @_;
82
4050fcc1 83 my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
6cc88e8e
AA
84 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
85
86 my $cmd = [$binary, '-p', $pool];
87
92a7826f
TL
88 if (defined(my $namespace = $scfg->{namespace})) {
89 # some subcommands will fail if the --namespace parameter is present
90 my $no_namespace_parameter = {
91 unmap => 1,
92 };
93 push @$cmd, '--namespace', "$namespace" if !$no_namespace_parameter->{$op};
94 }
6cc88e8e
AA
95 push @$cmd, '-c', $cmd_option->{ceph_conf} if ($cmd_option->{ceph_conf});
96 push @$cmd, '-m', $cmd_option->{mon_host} if ($cmd_option->{mon_host});
97 push @$cmd, '--auth_supported', $cmd_option->{auth_supported} if ($cmd_option->{auth_supported});
98 push @$cmd, '-n', "client.$cmd_option->{userid}" if ($cmd_option->{userid});
99 push @$cmd, '--keyring', $cmd_option->{keyring} if ($cmd_option->{keyring});
100
101 push @$cmd, $op;
102
103 push @$cmd, @options if scalar(@options);
104
105 return $cmd;
106};
107
108my $rbd_cmd = sub {
109 my ($scfg, $storeid, $op, @options) = @_;
110
111 return $build_cmd->('/usr/bin/rbd', $scfg, $storeid, $op, @options);
112};
113
114my $rados_cmd = sub {
115 my ($scfg, $storeid, $op, @options) = @_;
116
117 return $build_cmd->('/usr/bin/rados', $scfg, $storeid, $op, @options);
118};
119
d86fd0a4 120# needed for volumes created using ceph jewel (or higher)
0ef8fb9d 121my $krbd_feature_update = sub {
d86fd0a4
FG
122 my ($scfg, $storeid, $name) = @_;
123
0ef8fb9d
TL
124 my (@disable, @enable);
125 my ($kmajor, $kminor) = PVE::ProcFSTools::kernel_version();
4c3b3085 126
0ef8fb9d
TL
127 if ($kmajor > 5 || $kmajor == 5 && $kminor >= 3) {
128 # 'deep-flatten' can only be disabled, not enabled after image creation
129 push @enable, 'fast-diff', 'object-map';
130 } else {
131 push @disable, 'fast-diff', 'object-map', 'deep-flatten';
132 }
133
134 if ($kmajor >= 5) {
135 push @enable, 'exclusive-lock';
136 } else {
137 push @disable, 'exclusive-lock';
138 }
4c3b3085 139
0ef8fb9d
TL
140 my $active_features_list = (rbd_volume_info($scfg, $storeid, $name))[4];
141 my $active_features = { map { $_ => 1 } @$active_features_list };
142
143 my $to_disable = join(',', grep { $active_features->{$_} } @disable);
144 my $to_enable = join(',', grep { !$active_features->{$_} } @enable );
145
146 if ($to_disable) {
147 print "disable RBD image features this kernel RBD drivers is not compatible with: $to_disable\n";
148 my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'disable', $name, $to_disable);
149 run_rbd_command(
150 $cmd,
151 errmsg => "could not disable krbd-incompatible image features '$to_disable' for rbd image: $name",
152 );
153 }
154 if ($to_enable) {
155 print "enable RBD image features this kernel RBD drivers supports: $to_enable\n";
156 eval {
157 my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'enable', $name, $to_enable);
158 run_rbd_command(
159 $cmd,
160 errmsg => "could not enable krbd-compatible image features '$to_enable' for rbd image: $name",
161 );
162 };
163 warn "$@" if $@;
4c3b3085 164 }
d86fd0a4
FG
165};
166
c693f749
SP
167sub run_rbd_command {
168 my ($cmd, %args) = @_;
169
170 my $lasterr;
171 my $errmsg = $args{errmsg} . ": " || "";
c97c5b3b 172 if (!exists($args{errfunc})) {
c693f749
SP
173 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
174 # at least 1 child(ren) in pool cephstor1
175 $args{errfunc} = sub {
c97c5b3b
DM
176 my $line = shift;
177 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
178 $lasterr = "$1\n";
179 } else {
180 $lasterr = $line;
181 }
182 print STDERR $lasterr;
183 *STDERR->flush();
184 };
185 }
6d0d0a97 186
c97c5b3b
DM
187 eval { run_command($cmd, %args); };
188 if (my $err = $@) {
189 die $errmsg . $lasterr if length($lasterr);
190 die $err;
c693f749
SP
191 }
192
c97c5b3b 193 return undef;
c693f749
SP
194}
195
411476cd
DM
196sub rbd_ls {
197 my ($scfg, $storeid) = @_;
d70e7f6c 198
1440604a 199 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
e9bc9931 200 $pool .= "/$scfg->{namespace}" if defined($scfg->{namespace});
d70e7f6c 201
89a8800b 202 my $raw = '';
1be93fe2 203 my $parser = sub { $raw .= shift };
8c3abf12 204
72bbd8a6 205 my $cmd = $rbd_cmd->($scfg, $storeid, 'ls', '-l', '--format', 'json');
8c3abf12 206 eval {
c693f749 207 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
8c3abf12
DM
208 };
209 my $err = $@;
210
211 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
89a8800b 212
00571710
DM
213 my $result;
214 if ($raw eq '') {
215 $result = [];
216 } elsif ($raw =~ m/^(\[.*\])$/s) { # untaint
217 $result = JSON::decode_json($1);
218 } else {
219 die "got unexpected data from rbd ls: '$raw'\n";
220 }
89a8800b
DC
221
222 my $list = {};
223
224 foreach my $el (@$result) {
225 next if defined($el->{snapshot});
226
227 my $image = $el->{image};
228
229 my ($owner) = $image =~ m/^(?:vm|base)-(\d+)-/;
aa14def4 230 next if !defined($owner);
89a8800b
DC
231
232 $list->{$pool}->{$image} = {
233 name => $image,
234 size => $el->{size},
235 parent => $get_parent_image_name->($el->{parent}),
236 vmid => $owner
237 };
238 }
239
411476cd 240 return $list;
0509010d
AD
241}
242
a573f66a
FG
243sub rbd_ls_snap {
244 my ($scfg, $storeid, $name) = @_;
245
72bbd8a6 246 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'ls', $name, '--format', 'json');
a573f66a
FG
247
248 my $raw = '';
249 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => sub { $raw .= shift; });
250
251 my $list;
252 if ($raw =~ m/^(\[.*\])$/s) { # untaint
253 $list = eval { JSON::decode_json($1) };
254 die "invalid JSON output from 'rbd snap ls $name': $@\n" if $@;
255 } else {
256 die "got unexpected data from 'rbd snap ls $name': '$raw'\n";
257 }
258
259 $list = [] if !defined($list);
260
261 my $res = {};
262 foreach my $el (@$list) {
263 my $snap = $el->{name};
264 my $protected = defined($el->{protected}) && $el->{protected} eq "true" ? 1 : undef;
265 $res->{$snap} = {
266 name => $snap,
267 id => $el->{id} // undef,
268 size => $el->{size} // 0,
269 protected => $protected,
270 };
271 }
272 return $res;
273}
274
62b98a65 275sub rbd_volume_info {
992e6835
AD
276 my ($scfg, $storeid, $volname, $snap) = @_;
277
278 my $cmd = undef;
279
89a8800b 280 my @options = ('info', $volname, '--format', 'json');
1be93fe2 281 if ($snap) {
89a8800b 282 push @options, '--snap', $snap;
992e6835 283 }
e110213e 284
72bbd8a6 285 $cmd = $rbd_cmd->($scfg, $storeid, @options);
89a8800b 286
89a8800b 287 my $raw = '';
1be93fe2 288 my $parser = sub { $raw .= shift };
e110213e 289
c693f749 290 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
1be93fe2 291
00571710
DM
292 my $volume;
293 if ($raw eq '') {
294 $volume = {};
295 } elsif ($raw =~ m/^(\{.*\})$/s) { # untaint
296 $volume = JSON::decode_json($1);
297 } else {
298 die "got unexpected data from rbd info: '$raw'\n";
299 }
1be93fe2 300
89a8800b 301 $volume->{parent} = $get_parent_image_name->($volume->{parent});
1be93fe2 302 $volume->{protected} = defined($volume->{protected}) && $volume->{protected} eq "true" ? 1 : undef;
e110213e 303
89a8800b 304 return $volume->@{qw(size parent format protected features)};
e110213e
AD
305}
306
e5427b00 307# Configuration
0509010d 308
0509010d
AD
309sub type {
310 return 'rbd';
311}
312
313sub plugindata {
314 return {
1f79bb07 315 content => [ {images => 1, rootdir => 1}, { images => 1 }],
0509010d
AD
316 };
317}
318
319sub properties {
320 return {
e5427b00 321 monhost => {
0b9ef02e 322 description => "IP addresses of monitors (for external clusters).",
e858048f 323 type => 'string', format => 'pve-storage-portal-dns-list',
0509010d 324 },
e5427b00
AD
325 pool => {
326 description => "Pool.",
0509010d
AD
327 type => 'string',
328 },
ef2afce7
AL
329 'data-pool' => {
330 description => "Data Pool (for erasure coding only)",
331 type => 'string',
332 },
6d0d0a97 333 namespace => {
78eac5ba 334 description => "Namespace.",
e9bc9931
AL
335 type => 'string',
336 },
e5427b00
AD
337 username => {
338 description => "RBD Id.",
0509010d
AD
339 type => 'string',
340 },
e5427b00 341 authsupported => {
0509010d
AD
342 description => "Authsupported.",
343 type => 'string',
344 },
9f20a8a6 345 krbd => {
40d69893 346 description => "Always access rbd through krbd kernel module.",
9f20a8a6
AD
347 type => 'boolean',
348 },
22b68016
AL
349 keyring => {
350 description => "Client keyring contents (for external clusters).",
351 type => 'string',
352 },
0509010d
AD
353 };
354}
355
356sub options {
357 return {
35d6dfaf
AD
358 nodes => { optional => 1 },
359 disable => { optional => 1 },
0b9ef02e 360 monhost => { optional => 1},
1440604a 361 pool => { optional => 1 },
ef2afce7 362 'data-pool' => { optional => 1 },
e9bc9931 363 namespace => { optional => 1 },
1440604a 364 username => { optional => 1 },
0509010d 365 content => { optional => 1 },
9f20a8a6 366 krbd => { optional => 1 },
22b68016 367 keyring => { optional => 1 },
9edb99a5 368 bwlimit => { optional => 1 },
0509010d
AD
369 };
370}
371
372# Storage implementation
373
2e109b4b
TL
374sub on_add_hook {
375 my ($class, $storeid, $scfg, %param) = @_;
376
22b68016
AL
377 my $secret = $param{keyring} if defined $param{keyring} // undef;
378 PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $secret);
379
380 return;
381}
2e109b4b 382
22b68016
AL
383sub on_update_hook {
384 my ($class, $storeid, $scfg, %param) = @_;
385
386 if (exists($param{keyring})) {
387 if (defined($param{keyring})) {
388 PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $param{keyring});
389 } else {
390 PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
391 }
392 }
f3ccd0ef
FE
393
394 return;
2e109b4b
TL
395}
396
397sub on_delete_hook {
398 my ($class, $storeid, $scfg) = @_;
4050fcc1 399 PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
f3ccd0ef 400 return;
2e109b4b
TL
401}
402
0509010d
AD
403sub parse_volname {
404 my ($class, $volname) = @_;
405
d04c7e55 406 if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
7800e84d 407 return ('images', $4, $7, $2, $3, $5, 'raw');
0509010d
AD
408 }
409
410 die "unable to parse rbd volume name '$volname'\n";
411}
412
413sub path {
38e6ec3f 414 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
0509010d 415
4050fcc1 416 my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
0509010d 417 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
38e6ec3f 418 $name .= '@'.$snapname if $snapname;
0509010d 419
cfe46e2d
AL
420 my $rbd_dev_path = get_rbd_dev_path($scfg, $storeid, $name);
421 return ($rbd_dev_path, $vmid, $vtype) if $scfg->{krbd};
33cef4c8 422
cfe46e2d 423 my $rbd_path = get_rbd_path($scfg, $name);
aeb007cb 424 my $path = "rbd:${rbd_path}";
6eebc4a7 425
5fc02afb
FG
426 $path .= ":conf=$cmd_option->{ceph_conf}" if $cmd_option->{ceph_conf};
427 if (defined($scfg->{monhost})) {
4050fcc1 428 my $monhost = PVE::CephConfig::hostlist($scfg->{monhost}, ';');
6eebc4a7
FG
429 $monhost =~ s/:/\\:/g;
430 $path .= ":mon_host=$monhost";
6cc88e8e 431 $path .= ":auth_supported=$cmd_option->{auth_supported}";
6eebc4a7
FG
432 }
433
6cc88e8e 434 $path .= ":id=$cmd_option->{userid}:keyring=$cmd_option->{keyring}" if ($cmd_option->{keyring});
13417225 435
0509010d
AD
436 return ($path, $vmid, $vtype);
437}
438
a44c0147
FE
439sub find_free_diskname {
440 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
5b9b9b14 441
72bbd8a6 442 my $cmd = $rbd_cmd->($scfg, $storeid, 'ls');
e9bc9931 443
c4a29df4 444 my $disk_list = [];
5b9b9b14 445
53a236f2
FG
446 my $parser = sub {
447 my $line = shift;
dd9e97ed 448 if ($line =~ m/^(.*)$/) { # untaint
00571710
DM
449 push @$disk_list, $1;
450 }
53a236f2
FG
451 };
452
453 eval {
454 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
455 };
456 my $err = $@;
457
458 die $err if $err && $err !~ m/doesn't contain rbd images/;
459
c4a29df4 460 return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, undef, $scfg);
a44c0147 461}
5b9b9b14 462
5eab0272
DM
463sub create_base {
464 my ($class, $storeid, $scfg, $volname) = @_;
465
992e6835
AD
466 my $snap = '__base__';
467
468 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
469 $class->parse_volname($volname);
470
471 die "create_base not possible with base image\n" if $isBase;
472
473 my ($size, $parent, $format, undef) = rbd_volume_info($scfg, $storeid, $name);
474 die "rbd volume info on '$name' failed\n" if !($size);
475
476 die "rbd image must be at format V2" if $format ne "2";
477
478 die "volname '$volname' contains wrong information about parent $parent $basename\n"
479 if $basename && (!$parent || $parent ne $basename."@".$snap);
480
481 my $newname = $name;
482 $newname =~ s/^vm-/base-/;
483
484 my $newvolname = $basename ? "$basename/$newname" : "$newname";
485
72bbd8a6 486 my $cmd = $rbd_cmd->(
aeb007cb
AL
487 $scfg,
488 $storeid,
489 'rename',
4cf696f6
TL
490 get_rbd_path($scfg, $name),
491 get_rbd_path($scfg, $newname),
aeb007cb 492 );
c693f749 493 run_rbd_command($cmd, errmsg => "rbd rename '$name' error");
992e6835 494
95947178
FE
495 eval { $class->unmap_volume($storeid, $scfg, $volname); };
496 warn $@ if $@;
497
992e6835
AD
498 my $running = undef; #fixme : is create_base always offline ?
499
500 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
501
502 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $newname, $snap);
503
504 if (!$protected){
72bbd8a6 505 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
2362bc87 506 run_rbd_command($cmd, errmsg => "rbd protect $newname snap '$snap' error");
992e6835
AD
507 }
508
509 return $newvolname;
510
5eab0272
DM
511}
512
513sub clone_image {
f236eaf8 514 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
5eab0272 515
f2708285 516 my $snap = '__base__';
f236eaf8 517 $snap = $snapname if length $snapname;
f2708285
AD
518
519 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
520 $class->parse_volname($volname);
521
63da6d79
DM
522 die "$volname is not a base image and snapname is not provided\n"
523 if !$isBase && !length($snapname);
f2708285 524
a44c0147 525 my $name = $class->find_free_diskname($storeid, $scfg, $vmid);
f2708285 526
f236eaf8
SP
527 warn "clone $volname: $basename snapname $snap to $name\n";
528
63da6d79 529 if (length($snapname)) {
f236eaf8
SP
530 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $volname, $snapname);
531
63da6d79 532 if (!$protected) {
72bbd8a6 533 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
f236eaf8
SP
534 run_rbd_command($cmd, errmsg => "rbd protect $volname snap $snapname error");
535 }
536 }
f2708285
AD
537
538 my $newvol = "$basename/$name";
63da6d79
DM
539 $newvol = $name if length($snapname);
540
c915afca
TL
541 my @options = (
542 get_rbd_path($scfg, $basename),
543 '--snap', $snap,
544 );
ef2afce7 545 push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'};
f2708285 546
c915afca 547 my $cmd = $rbd_cmd->($scfg, $storeid, 'clone', @options, get_rbd_path($scfg, $name));
c693f749 548 run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
f2708285
AD
549
550 return $newvol;
5eab0272
DM
551}
552
0509010d
AD
553sub alloc_image {
554 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
555
556
292a33fd 557 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
0509010d 558 if $name && $name !~ m/^vm-$vmid-/;
0509010d 559
a44c0147 560 $name = $class->find_free_diskname($storeid, $scfg, $vmid) if !$name;
0509010d 561
c915afca
TL
562 my @options = (
563 '--image-format' , 2,
564 '--size', int(($size + 1023) / 1024),
565 );
ef2afce7 566 push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'};
c915afca
TL
567
568 my $cmd = $rbd_cmd->($scfg, $storeid, 'create', @options, $name);
3c931155 569 run_rbd_command($cmd, errmsg => "rbd create '$name' error");
0509010d
AD
570
571 return $name;
572}
573
574sub free_image {
32437ed2 575 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
0509010d 576
42d07b9a
AD
577 my ($vtype, $name, $vmid, undef, undef, undef) =
578 $class->parse_volname($volname);
579
e9bc9931 580
a573f66a
FG
581 my $snaps = rbd_ls_snap($scfg, $storeid, $name);
582 foreach my $snap (keys %$snaps) {
583 if ($snaps->{$snap}->{protected}) {
72bbd8a6 584 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
2362bc87 585 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
42d07b9a
AD
586 }
587 }
588
515ef80b
WL
589 $class->deactivate_volume($storeid, $scfg, $volname);
590
72bbd8a6 591 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'purge', $name);
85043c01 592 run_rbd_command($cmd, errmsg => "rbd snap purge '$name' error");
c30470a3 593
72bbd8a6 594 $cmd = $rbd_cmd->($scfg, $storeid, 'rm', $name);
85043c01 595 run_rbd_command($cmd, errmsg => "rbd rm '$name' error");
0509010d
AD
596
597 return undef;
598}
599
600sub list_images {
601 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
602
e5427b00 603 $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd};
ed7ea5a3 604
c27fe648 605 my $dat = $cache->{rbd}->{get_rbd_path($scfg)};
ed7ea5a3 606 return [] if !$dat; # nothing found
cfd58f1f 607
ed7ea5a3
TL
608 my $res = [];
609 for my $image (sort keys %$dat) {
610 my $info = $dat->{$image};
611 my ($volname, $parent, $owner) = $info->@{'name', 'parent', 'vmid'};
612
613 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
614 $info->{volid} = "$storeid:$1/$volname";
615 } else {
616 $info->{volid} = "$storeid:$volname";
617 }
0509010d 618
ed7ea5a3
TL
619 if ($vollist) {
620 my $found = grep { $_ eq $info->{volid} } @$vollist;
621 next if !$found;
622 } else {
623 next if defined ($vmid) && ($owner ne $vmid);
624 }
0509010d 625
ed7ea5a3 626 $info->{format} = 'raw';
0509010d 627
ed7ea5a3 628 push @$res, $info;
0509010d 629 }
ed7ea5a3 630
411476cd 631 return $res;
0509010d
AD
632}
633
0509010d
AD
634sub status {
635 my ($class, $storeid, $scfg, $cache) = @_;
636
72bbd8a6 637 my $rados = $librados_connect->($scfg, $storeid);
41aacc6c 638 my $df = $rados->mon_command({ prefix => 'df', format => 'json' });
69589444 639
e4671f73 640 my $pool = $scfg->{'data-pool'} // $scfg->{pool} // 'rbd';
0c317c6c
SI
641
642 my ($d) = grep { $_->{name} eq $pool } @{$df->{pools}};
69589444 643
ae931633
SI
644 if (!defined($d)) {
645 warn "could not get usage stats for pool '$pool'\n";
646 return;
647 }
648
41aacc6c
AA
649 # max_avail -> max available space for data w/o replication in the pool
650 # bytes_used -> data w/o replication in the pool
651 my $free = $d->{stats}->{max_avail};
e79ab52c 652 my $used = $d->{stats}->{stored} // $d->{stats}->{bytes_used};
41aacc6c 653 my $total = $used + $free;
0509010d 654 my $active = 1;
0509010d 655
411476cd 656 return ($total, $free, $used, $active);
0509010d
AD
657}
658
659sub activate_storage {
660 my ($class, $storeid, $scfg, $cache) = @_;
661 return 1;
662}
663
664sub deactivate_storage {
665 my ($class, $storeid, $scfg, $cache) = @_;
666 return 1;
667}
668
40d69893
DM
669sub map_volume {
670 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
9f20a8a6 671
518f3908
FG
672 my ($vtype, $img_name, $vmid) = $class->parse_volname($volname);
673
674 my $name = $img_name;
40d69893
DM
675 $name .= '@'.$snapname if $snapname;
676
cc682faa 677 my $kerneldev = get_rbd_dev_path($scfg, $storeid, $name);
40d69893
DM
678
679 return $kerneldev if -b $kerneldev; # already mapped
680
518f3908
FG
681 # features can only be enabled/disabled for image, not for snapshot!
682 $krbd_feature_update->($scfg, $storeid, $img_name);
9f20a8a6 683
72bbd8a6 684 my $cmd = $rbd_cmd->($scfg, $storeid, 'map', $name);
40d69893
DM
685 run_rbd_command($cmd, errmsg => "can't map rbd volume $name");
686
687 return $kerneldev;
688}
689
690sub unmap_volume {
691 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
692
693 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
694 $name .= '@'.$snapname if $snapname;
695
cc682faa 696 my $kerneldev = get_rbd_dev_path($scfg, $storeid, $name);
40d69893
DM
697
698 if (-b $kerneldev) {
72bbd8a6 699 my $cmd = $rbd_cmd->($scfg, $storeid, 'unmap', $kerneldev);
40d69893
DM
700 run_rbd_command($cmd, errmsg => "can't unmap rbd device $kerneldev");
701 }
9f20a8a6 702
0509010d
AD
703 return 1;
704}
705
40d69893 706sub activate_volume {
02e797b8 707 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
9f20a8a6 708
40d69893 709 $class->map_volume($storeid, $scfg, $volname, $snapname) if $scfg->{krbd};
9f20a8a6 710
40d69893
DM
711 return 1;
712}
9f20a8a6 713
40d69893
DM
714sub deactivate_volume {
715 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
b50812f9 716
40d69893 717 $class->unmap_volume($storeid, $scfg, $volname, $snapname);
9f20a8a6 718
0509010d
AD
719 return 1;
720}
721
0002d9cc
AD
722sub volume_size_info {
723 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
724
81d1d017
AD
725 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
726 my ($size, undef) = rbd_volume_info($scfg, $storeid, $name);
62b98a65 727 return $size;
0002d9cc
AD
728}
729
e7a42a76
AD
730sub volume_resize {
731 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
732
40d69893 733 return 1 if $running && !$scfg->{krbd}; # FIXME???
e7a42a76 734
478fc06c
AD
735 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
736
72bbd8a6 737 my $cmd = $rbd_cmd->($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
c693f749 738 run_rbd_command($cmd, errmsg => "rbd resize '$volname' error");
e7a42a76
AD
739 return undef;
740}
741
788dd8e1 742sub volume_snapshot {
f5640e7d 743 my ($class, $scfg, $storeid, $volname, $snap) = @_;
788dd8e1 744
9af33ed0
AD
745 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
746
72bbd8a6 747 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
c693f749 748 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
788dd8e1
AD
749 return undef;
750}
751
5a2b2e2f
AD
752sub volume_snapshot_rollback {
753 my ($class, $scfg, $storeid, $volname, $snap) = @_;
754
c6ce2cc8
AD
755 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
756
72bbd8a6 757 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
2362bc87 758 run_rbd_command($cmd, errmsg => "rbd snapshot $volname to '$snap' error");
5a2b2e2f
AD
759}
760
cce29bcd
AD
761sub volume_snapshot_delete {
762 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
763
40d69893 764 return 1 if $running && !$scfg->{krbd}; # FIXME: ????
cce29bcd 765
399581a2
WB
766 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
767
c78cb110
AD
768 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
769
f90a0a20
SP
770 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
771 if ($protected){
72bbd8a6 772 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
f90a0a20
SP
773 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
774 }
775
72bbd8a6 776 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
c693f749
SP
777
778 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
779
cce29bcd
AD
780 return undef;
781}
782
2c036838 783sub volume_snapshot_needs_fsfreeze {
2c036838
SI
784 return 1;
785}
786
774f21b9
AD
787sub volume_has_feature {
788 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
789
790 my $features = {
5649ccfe 791 snapshot => { current => 1, snap => 1},
44c3689a 792 clone => { base => 1, snap => 1},
5649ccfe
AD
793 template => { current => 1},
794 copy => { base => 1, current => 1, snap => 1},
baafddbd 795 sparseinit => { base => 1, current => 1},
95dfa44c 796 rename => {current => 1},
774f21b9
AD
797 };
798
6d0d0a97 799 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = $class->parse_volname($volname);
1e7ae581
AD
800
801 my $key = undef;
6d0d0a97 802 if ($snapname){
2c5a7097 803 $key = 'snap';
6d0d0a97
TL
804 } else {
805 $key = $isBase ? 'base' : 'current';
1e7ae581
AD
806 }
807 return 1 if $features->{$feature}->{$key};
774f21b9
AD
808
809 return undef;
810}
811
95dfa44c
AL
812sub rename_volume {
813 my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
814
815 my (
816 undef,
817 $source_image,
818 $source_vmid,
819 $base_name,
820 $base_vmid,
821 undef,
822 $format
823 ) = $class->parse_volname($source_volname);
824 $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format)
825 if !$target_volname;
826
827 eval {
828 my $cmd = $rbd_cmd->($scfg, $storeid, 'info', $target_volname);
829 run_rbd_command($cmd, errmsg => "exist check", quiet => 1);
830 };
831 die "target volume '${target_volname}' already exists\n" if !$@;
832
833 my $cmd = $rbd_cmd->($scfg, $storeid, 'rename', $source_image, $target_volname);
834
835 run_rbd_command(
836 $cmd,
837 errmsg => "could not rename image '${source_image}' to '${target_volname}'",
838 );
839
95947178
FE
840 eval { $class->unmap_volume($storeid, $scfg, $source_volname); };
841 warn $@ if $@;
842
95dfa44c
AL
843 $base_name = $base_name ? "${base_name}/" : '';
844
845 return "${storeid}:${base_name}${target_volname}";
846}
847
0509010d 8481;