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