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