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