]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/RBDPlugin.pm
Fix #1542: show storage utilization per pool
[pve-storage.git] / PVE / Storage / RBDPlugin.pm
1 package PVE::Storage::RBDPlugin;
2
3 use strict;
4 use warnings;
5 use IO::File;
6 use Net::IP;
7 use PVE::Tools qw(run_command trim);
8 use PVE::Storage::Plugin;
9 use PVE::JSONSchema qw(get_standard_option);
10 use PVE::RADOS;
11
12 use base qw(PVE::Storage::Plugin);
13
14 my $pveceph_config = '/etc/pve/ceph.conf';
15
16 my $rbd_unittobytes = {
17 "k" => 1024,
18 "M" => 1024*1024,
19 "G" => 1024*1024*1024,
20 "T" => 1024*1024*1024*1024,
21 };
22
23 my $add_pool_to_disk = sub {
24 my ($scfg, $disk) = @_;
25
26 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
27
28 return "$pool/$disk";
29 };
30
31 my $hostlist = sub {
32 my ($list_text, $separator) = @_;
33
34 my @monhostlist = PVE::Tools::split_list($list_text);
35 return join($separator, map {
36 my ($host, $port) = PVE::Tools::parse_host_and_port($_);
37 $port = defined($port) ? ":$port" : '';
38 $host = "[$host]" if Net::IP::ip_is_ipv6($host);
39 "${host}${port}"
40 } @monhostlist);
41 };
42
43 my $build_cmd = sub {
44 my ($binary, $scfg, $storeid, $op, @options) = @_;
45
46 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
47 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
48 my $username = $scfg->{username} ? $scfg->{username} : 'admin';
49
50 my $cmd = [$binary, '-p', $pool];
51 my $pveceph_managed = !defined($scfg->{monhost});
52
53 if ($pveceph_managed) {
54 push @$cmd, '-c', $pveceph_config;
55 } else {
56 push @$cmd, '-m', $hostlist->($scfg->{monhost}, ',');
57 push @$cmd, '--auth_supported', -e $keyring ? 'cephx' : 'none';
58 }
59
60 if (-e $keyring) {
61 push @$cmd, '-n', "client.$username";
62 push @$cmd, '--keyring', $keyring;
63 }
64
65 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
66
67 if (-e $cephconfig) {
68 if ($pveceph_managed) {
69 warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
70 } else {
71 push @$cmd, '-c', $cephconfig;
72 }
73 }
74
75 push @$cmd, $op;
76
77 push @$cmd, @options if scalar(@options);
78
79 return $cmd;
80 };
81
82 my $rbd_cmd = sub {
83 my ($scfg, $storeid, $op, @options) = @_;
84
85 return $build_cmd->('/usr/bin/rbd', $scfg, $storeid, $op, @options);
86 };
87
88 my $rados_cmd = sub {
89 my ($scfg, $storeid, $op, @options) = @_;
90
91 return $build_cmd->('/usr/bin/rados', $scfg, $storeid, $op, @options);
92 };
93
94 my $ceph_connect_option = sub {
95 my ($scfg, $storeid, %options) = @_;
96
97 my $cmd_option = {};
98 my $ceph_storeid_conf = "/etc/pve/priv/ceph/${storeid}.conf";
99 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
100 my $pveceph_managed = !defined($scfg->{monhost});
101
102 $cmd_option->{ceph_conf} = $pveceph_config if (-e $pveceph_config);
103
104 if (-e $ceph_storeid_conf) {
105 if ($pveceph_managed) {
106 warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
107 } else {
108 $cmd_option->{ceph_conf} = $ceph_storeid_conf;
109 }
110 }
111
112 $cmd_option->{keyring} = $keyring if (-e $keyring);
113 $cmd_option->{auth_supported} = (defined $cmd_option->{keyring}) ? 'cephx' : 'none';
114 $cmd_option->{userid} = $scfg->{username} ? $scfg->{username} : 'admin';
115 $cmd_option->{mon_host} = $hostlist->($scfg->{monhost}, ',') if (defined($scfg->{monhost}));
116
117 if (%options) {
118 foreach my $k (keys %options) {
119 $cmd_option->{$k} = $options{$k};
120 }
121 }
122
123
124 return $cmd_option;
125
126 };
127
128 my $librados_connect = sub {
129 my ($scfg, $storeid, $options) = @_;
130
131 my $librados_config = $ceph_connect_option->($scfg, $storeid);
132
133 my $rados = PVE::RADOS->new(%$librados_config);
134
135 return $rados;
136 };
137
138 # needed for volumes created using ceph jewel (or higher)
139 my $krbd_feature_disable = sub {
140 my ($scfg, $storeid, $name) = @_;
141
142 return 1 if !$scfg->{krbd};
143
144 my ($major, undef, undef, undef) = ceph_version();
145 return 1 if $major < 10;
146
147 my $krbd_feature_blacklist = ['deep-flatten', 'fast-diff', 'object-map', 'exclusive-lock'];
148 my (undef, undef, undef, undef, $features) = rbd_volume_info($scfg, $storeid, $name);
149
150 my $active_features = { map { $_ => 1 } PVE::Tools::split_list($features)};
151 my $incompatible_features = join(',', grep { %$active_features{$_} } @$krbd_feature_blacklist);
152
153 if ($incompatible_features) {
154 my $feature_cmd = &$rbd_cmd($scfg, $storeid, 'feature', 'disable', $name, $incompatible_features);
155 run_rbd_command($feature_cmd, errmsg => "could not disable krbd-incompatible image features of rbd volume $name");
156 }
157 };
158
159 my $ceph_version_parser = sub {
160 my $line = shift;
161 if ($line =~ m/^ceph version ((\d+)\.(\d+)\.(\d+))(?: \([a-fA-F0-9]+\))/) {
162 return ($2, $3, $4, $1);
163 } else {
164 warn "Could not parse Ceph version: '$line'\n";
165 }
166 };
167
168 sub ceph_version {
169 my ($cache) = @_;
170
171 my $version_string = $cache;
172
173 my $major;
174 my $minor;
175 my $bugfix;
176
177 if (defined($version_string)) {
178 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($version_string);
179 } else {
180 run_command('ceph --version', outfunc => sub {
181 my $line = shift;
182 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($line);
183 });
184 }
185 return undef if !defined($version_string);
186 return wantarray ? ($major, $minor, $bugfix, $version_string) : $version_string;
187 }
188
189 sub run_rbd_command {
190 my ($cmd, %args) = @_;
191
192 my $lasterr;
193 my $errmsg = $args{errmsg} . ": " || "";
194 if (!exists($args{errfunc})) {
195 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
196 # at least 1 child(ren) in pool cephstor1
197 $args{errfunc} = sub {
198 my $line = shift;
199 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
200 $lasterr = "$1\n";
201 } else {
202 $lasterr = $line;
203 }
204 print STDERR $lasterr;
205 *STDERR->flush();
206 };
207 }
208
209 eval { run_command($cmd, %args); };
210 if (my $err = $@) {
211 die $errmsg . $lasterr if length($lasterr);
212 die $err;
213 }
214
215 return undef;
216 }
217
218 sub rbd_ls {
219 my ($scfg, $storeid) = @_;
220
221 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l');
222 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
223
224 my $list = {};
225
226 my $parser = sub {
227 my $line = shift;
228
229 if ($line =~ m/^((vm|base)-(\d+)-\S+)\s+(\d+)(k|M|G|T)\s((\S+)\/((vm|base)-\d+-\S+@\S+))?/) {
230 my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8);
231 return if $image =~ /@/; #skip snapshots
232
233 $list->{$pool}->{$image} = {
234 name => $image,
235 size => $size*$rbd_unittobytes->{$unit},
236 parent => $parent,
237 vmid => $owner
238 };
239 }
240 };
241
242 eval {
243 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
244 };
245 my $err = $@;
246
247 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
248
249 return $list;
250 }
251
252 sub rbd_volume_info {
253 my ($scfg, $storeid, $volname, $snap) = @_;
254
255 my $cmd = undef;
256
257 if($snap){
258 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
259 }else{
260 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
261 }
262
263 my $size = undef;
264 my $parent = undef;
265 my $format = undef;
266 my $protected = undef;
267 my $features = undef;
268
269 my $parser = sub {
270 my $line = shift;
271
272 if ($line =~ m/size (\d+) (k|M|G|T)B in (\d+) objects/) {
273 $size = $1 * $rbd_unittobytes->{$2} if ($1);
274 } elsif ($line =~ m/parent:\s(\S+)\/(\S+)/) {
275 $parent = $2;
276 } elsif ($line =~ m/format:\s(\d+)/) {
277 $format = $1;
278 } elsif ($line =~ m/protected:\s(\S+)/) {
279 $protected = 1 if $1 eq "True";
280 } elsif ($line =~ m/features:\s(.+)/) {
281 $features = $1;
282 }
283
284 };
285
286 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
287
288 return ($size, $parent, $format, $protected, $features);
289 }
290
291 # Configuration
292
293 sub type {
294 return 'rbd';
295 }
296
297 sub plugindata {
298 return {
299 content => [ {images => 1, rootdir => 1}, { images => 1 }],
300 };
301 }
302
303 sub properties {
304 return {
305 monhost => {
306 description => "IP addresses of monitors (for external clusters).",
307 type => 'string', format => 'pve-storage-portal-dns-list',
308 },
309 pool => {
310 description => "Pool.",
311 type => 'string',
312 },
313 username => {
314 description => "RBD Id.",
315 type => 'string',
316 },
317 authsupported => {
318 description => "Authsupported.",
319 type => 'string',
320 },
321 krbd => {
322 description => "Access rbd through krbd kernel module.",
323 type => 'boolean',
324 },
325 };
326 }
327
328 sub options {
329 return {
330 nodes => { optional => 1 },
331 disable => { optional => 1 },
332 monhost => { optional => 1},
333 pool => { optional => 1 },
334 username => { optional => 1 },
335 content => { optional => 1 },
336 krbd => { optional => 1 },
337 bwlimit => { optional => 1 },
338 };
339 }
340
341 # Storage implementation
342
343 sub parse_volname {
344 my ($class, $volname) = @_;
345
346 if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
347 return ('images', $4, $7, $2, $3, $5, 'raw');
348 }
349
350 die "unable to parse rbd volume name '$volname'\n";
351 }
352
353 sub path {
354 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
355
356 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
357 $name .= '@'.$snapname if $snapname;
358
359 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
360 return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd};
361
362 my $username = $scfg->{username} ? $scfg->{username} : 'admin';
363
364 my $path = "rbd:$pool/$name";
365 my $pveceph_managed = !defined($scfg->{monhost});
366 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
367
368 if ($pveceph_managed) {
369 $path .= ":conf=$pveceph_config";
370 } else {
371 my $monhost = $hostlist->($scfg->{monhost}, ';');
372 $monhost =~ s/:/\\:/g;
373 $path .= ":mon_host=$monhost";
374 $path .= -e $keyring ? ":auth_supported=cephx" : ":auth_supported=none";
375 }
376
377 $path .= ":id=$username:keyring=$keyring" if -e $keyring;
378
379 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
380
381 if (-e $cephconfig) {
382 if ($pveceph_managed) {
383 warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
384 } else {
385 $path .= ":conf=$cephconfig";
386 }
387 }
388
389 return ($path, $vmid, $vtype);
390 }
391
392 my $find_free_diskname = sub {
393 my ($storeid, $scfg, $vmid) = @_;
394
395 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls');
396 my $disk_ids = {};
397
398 my $parser = sub {
399 my $line = shift;
400
401 if ($line =~ m/^(vm|base)-\Q$vmid\E+-disk-(\d+)$/) {
402 $disk_ids->{$2} = 1;
403 }
404 };
405
406 eval {
407 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
408 };
409 my $err = $@;
410
411 die $err if $err && $err !~ m/doesn't contain rbd images/;
412
413 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
414 for (my $i = 1; $i < 100; $i++) {
415 if (!$disk_ids->{$i}) {
416 return "vm-$vmid-disk-$i";
417 }
418 }
419
420 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
421 };
422
423 sub create_base {
424 my ($class, $storeid, $scfg, $volname) = @_;
425
426 my $snap = '__base__';
427
428 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
429 $class->parse_volname($volname);
430
431 die "create_base not possible with base image\n" if $isBase;
432
433 my ($size, $parent, $format, undef) = rbd_volume_info($scfg, $storeid, $name);
434 die "rbd volume info on '$name' failed\n" if !($size);
435
436 die "rbd image must be at format V2" if $format ne "2";
437
438 die "volname '$volname' contains wrong information about parent $parent $basename\n"
439 if $basename && (!$parent || $parent ne $basename."@".$snap);
440
441 my $newname = $name;
442 $newname =~ s/^vm-/base-/;
443
444 my $newvolname = $basename ? "$basename/$newname" : "$newname";
445
446 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
447 run_rbd_command($cmd, errmsg => "rbd rename '$name' error");
448
449 my $running = undef; #fixme : is create_base always offline ?
450
451 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
452
453 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $newname, $snap);
454
455 if (!$protected){
456 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
457 run_rbd_command($cmd, errmsg => "rbd protect $newname snap '$snap' error");
458 }
459
460 return $newvolname;
461
462 }
463
464 sub clone_image {
465 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
466
467 my $snap = '__base__';
468 $snap = $snapname if length $snapname;
469
470 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
471 $class->parse_volname($volname);
472
473 die "$volname is not a base image and snapname is not provided\n"
474 if !$isBase && !length($snapname);
475
476 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
477
478 warn "clone $volname: $basename snapname $snap to $name\n";
479
480 if (length($snapname)) {
481 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $volname, $snapname);
482
483 if (!$protected) {
484 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
485 run_rbd_command($cmd, errmsg => "rbd protect $volname snap $snapname error");
486 }
487 }
488
489 my $newvol = "$basename/$name";
490 $newvol = $name if length($snapname);
491
492 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
493 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
494
495 run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
496
497 &$krbd_feature_disable($scfg, $storeid, $name);
498
499 return $newvol;
500 }
501
502 sub alloc_image {
503 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
504
505
506 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
507 if $name && $name !~ m/^vm-$vmid-/;
508
509 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
510
511 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
512 run_rbd_command($cmd, errmsg => "rbd create $name' error");
513
514 &$krbd_feature_disable($scfg, $storeid, $name);
515
516 return $name;
517 }
518
519 sub free_image {
520 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
521
522 my ($vtype, $name, $vmid, undef, undef, undef) =
523 $class->parse_volname($volname);
524
525 if ($isBase) {
526 my $snap = '__base__';
527 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
528 if ($protected){
529 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
530 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
531 }
532 }
533
534 $class->deactivate_volume($storeid, $scfg, $volname);
535
536 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
537 run_rbd_command($cmd, errmsg => "rbd snap purge '$volname' error");
538
539 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
540 run_rbd_command($cmd, errmsg => "rbd rm '$volname' error");
541
542 return undef;
543 }
544
545 sub list_images {
546 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
547
548 $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd};
549 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
550
551 my $res = [];
552
553 if (my $dat = $cache->{rbd}->{$pool}) {
554 foreach my $image (keys %$dat) {
555
556 my $info = $dat->{$image};
557
558 my $volname = $info->{name};
559 my $parent = $info->{parent};
560 my $owner = $info->{vmid};
561
562 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
563 $info->{volid} = "$storeid:$1/$volname";
564 } else {
565 $info->{volid} = "$storeid:$volname";
566 }
567
568 if ($vollist) {
569 my $found = grep { $_ eq $info->{volid} } @$vollist;
570 next if !$found;
571 } else {
572 next if defined ($vmid) && ($owner ne $vmid);
573 }
574
575 $info->{format} = 'raw';
576
577 push @$res, $info;
578 }
579 }
580
581 return $res;
582 }
583
584 sub status {
585 my ($class, $storeid, $scfg, $cache) = @_;
586
587
588 my $rados = &$librados_connect($scfg, $storeid);
589 my $df = $rados->mon_command({ prefix => 'df', format => 'json' });
590
591 my ($d) = grep { $_->{name} eq $scfg->{pool} } @{$df->{pools}};
592
593 # max_avail -> max available space for data w/o replication in the pool
594 # bytes_used -> data w/o replication in the pool
595 my $free = $d->{stats}->{max_avail};
596 my $used = $d->{stats}->{bytes_used};
597 my $total = $used + $free;
598 my $active = 1;
599
600 return ($total, $free, $used, $active);
601 }
602
603 sub activate_storage {
604 my ($class, $storeid, $scfg, $cache) = @_;
605 return 1;
606 }
607
608 sub deactivate_storage {
609 my ($class, $storeid, $scfg, $cache) = @_;
610 return 1;
611 }
612
613 sub activate_volume {
614 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
615
616 return 1 if !$scfg->{krbd};
617
618 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
619 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
620
621 my $path = "/dev/rbd/$pool/$name";
622 $path .= '@'.$snapname if $snapname;
623 return if -b $path;
624
625 $name .= '@'.$snapname if $snapname;
626 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
627 run_rbd_command($cmd, errmsg => "can't mount rbd volume $name");
628
629 return 1;
630 }
631
632 sub deactivate_volume {
633 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
634
635 return 1 if !$scfg->{krbd};
636
637 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
638 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
639
640 my $path = "/dev/rbd/$pool/$name";
641 $path .= '@'.$snapname if $snapname;
642 return if ! -b $path;
643
644 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
645 run_rbd_command($cmd, errmsg => "can't unmap rbd volume $name");
646
647 return 1;
648 }
649
650 sub volume_size_info {
651 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
652
653 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
654 my ($size, undef) = rbd_volume_info($scfg, $storeid, $name);
655 return $size;
656 }
657
658 sub volume_resize {
659 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
660
661 return 1 if $running && !$scfg->{krbd};
662
663 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
664
665 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
666 run_rbd_command($cmd, errmsg => "rbd resize '$volname' error");
667 return undef;
668 }
669
670 sub volume_snapshot {
671 my ($class, $scfg, $storeid, $volname, $snap) = @_;
672
673 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
674
675 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
676 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
677 return undef;
678 }
679
680 sub volume_snapshot_rollback {
681 my ($class, $scfg, $storeid, $volname, $snap) = @_;
682
683 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
684
685 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
686 run_rbd_command($cmd, errmsg => "rbd snapshot $volname to '$snap' error");
687 }
688
689 sub volume_snapshot_delete {
690 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
691
692 return 1 if $running && !$scfg->{krbd};
693
694 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
695
696 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
697
698 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
699 if ($protected){
700 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
701 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
702 }
703
704 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
705
706 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
707
708 return undef;
709 }
710
711 sub volume_has_feature {
712 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
713
714 my $features = {
715 snapshot => { current => 1, snap => 1},
716 clone => { base => 1, snap => 1},
717 template => { current => 1},
718 copy => { base => 1, current => 1, snap => 1},
719 sparseinit => { base => 1, current => 1},
720 };
721
722 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
723 $class->parse_volname($volname);
724
725 my $key = undef;
726 if($snapname){
727 $key = 'snap';
728 }else{
729 $key = $isBase ? 'base' : 'current';
730 }
731 return 1 if $features->{$feature}->{$key};
732
733 return undef;
734 }
735
736 1;