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