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