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