]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/RBDPlugin.pm
35b2372b704ba6dcf53fd7d87b46140c6b30421b
[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 sub rbd_volume_du {
312 my ($scfg, $storeid, $volname) = @_;
313
314 my @options = ('du', $volname, '--format', 'json');
315 my $cmd = $rbd_cmd->($scfg, $storeid, @options);
316
317 my $raw = '';
318 my $parser = sub { $raw .= shift };
319
320 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
321
322 my $volume;
323 if ($raw eq '') {
324 $volume = {};
325 } elsif ($raw =~ m/^(\{.*\})$/s) { # untaint
326 $volume = JSON::decode_json($1);
327 } else {
328 die "got unexpected data from rbd du: '$raw'\n";
329 }
330
331 if (!defined($volume->{images})) {
332 die "got no images from rbd du\n";
333 }
334
335 # `rbd du` returns array of images for name matching `volname`,
336 # including snapshots.
337 my $images = $volume->{images};
338 foreach my $image (@$images) {
339 next if defined($image->{snapshot});
340 next if !defined($image->{used_size}) || !defined($image->{name});
341
342 # Return `used_size` of first volume with matching name which
343 # is not a snapshot.
344 return $image->{used_size} if $image->{name} eq $volname;
345 }
346
347 die "got no matching image from rbd du\n";
348 }
349
350 # Configuration
351
352 sub type {
353 return 'rbd';
354 }
355
356 sub plugindata {
357 return {
358 content => [ {images => 1, rootdir => 1}, { images => 1 }],
359 };
360 }
361
362 sub properties {
363 return {
364 monhost => {
365 description => "IP addresses of monitors (for external clusters).",
366 type => 'string', format => 'pve-storage-portal-dns-list',
367 },
368 pool => {
369 description => "Pool.",
370 type => 'string',
371 },
372 'data-pool' => {
373 description => "Data Pool (for erasure coding only)",
374 type => 'string',
375 },
376 namespace => {
377 description => "Namespace.",
378 type => 'string',
379 },
380 username => {
381 description => "RBD Id.",
382 type => 'string',
383 },
384 authsupported => {
385 description => "Authsupported.",
386 type => 'string',
387 },
388 krbd => {
389 description => "Always access rbd through krbd kernel module.",
390 type => 'boolean',
391 },
392 keyring => {
393 description => "Client keyring contents (for external clusters).",
394 type => 'string',
395 },
396 };
397 }
398
399 sub options {
400 return {
401 nodes => { optional => 1 },
402 disable => { optional => 1 },
403 monhost => { optional => 1},
404 pool => { optional => 1 },
405 'data-pool' => { optional => 1 },
406 namespace => { optional => 1 },
407 username => { optional => 1 },
408 content => { optional => 1 },
409 krbd => { optional => 1 },
410 keyring => { optional => 1 },
411 bwlimit => { optional => 1 },
412 };
413 }
414
415 # Storage implementation
416
417 sub on_add_hook {
418 my ($class, $storeid, $scfg, %param) = @_;
419
420 my $secret = $param{keyring} if defined $param{keyring} // undef;
421 PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $secret);
422
423 return;
424 }
425
426 sub on_update_hook {
427 my ($class, $storeid, $scfg, %param) = @_;
428
429 if (exists($param{keyring})) {
430 if (defined($param{keyring})) {
431 PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $param{keyring});
432 } else {
433 PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
434 }
435 }
436
437 return;
438 }
439
440 sub on_delete_hook {
441 my ($class, $storeid, $scfg) = @_;
442 PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
443 return;
444 }
445
446 sub parse_volname {
447 my ($class, $volname) = @_;
448
449 if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
450 return ('images', $4, $7, $2, $3, $5, 'raw');
451 }
452
453 die "unable to parse rbd volume name '$volname'\n";
454 }
455
456 sub path {
457 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
458
459 my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
460 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
461 $name .= '@'.$snapname if $snapname;
462
463 if ($scfg->{krbd}) {
464 my $rbd_dev_path = get_rbd_dev_path($scfg, $storeid, $name);
465 return ($rbd_dev_path, $vmid, $vtype);
466 }
467
468 my $rbd_path = get_rbd_path($scfg, $name);
469 my $path = "rbd:${rbd_path}";
470
471 $path .= ":conf=$cmd_option->{ceph_conf}" if $cmd_option->{ceph_conf};
472 if (defined($scfg->{monhost})) {
473 my $monhost = PVE::CephConfig::hostlist($scfg->{monhost}, ';');
474 $monhost =~ s/:/\\:/g;
475 $path .= ":mon_host=$monhost";
476 $path .= ":auth_supported=$cmd_option->{auth_supported}";
477 }
478
479 $path .= ":id=$cmd_option->{userid}:keyring=$cmd_option->{keyring}" if ($cmd_option->{keyring});
480
481 return ($path, $vmid, $vtype);
482 }
483
484 sub find_free_diskname {
485 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
486
487 my $cmd = $rbd_cmd->($scfg, $storeid, 'ls');
488
489 my $disk_list = [];
490
491 my $parser = sub {
492 my $line = shift;
493 if ($line =~ m/^(.*)$/) { # untaint
494 push @$disk_list, $1;
495 }
496 };
497
498 eval {
499 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
500 };
501 my $err = $@;
502
503 die $err if $err && $err !~ m/doesn't contain rbd images/;
504
505 return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, undef, $scfg);
506 }
507
508 sub create_base {
509 my ($class, $storeid, $scfg, $volname) = @_;
510
511 my $snap = '__base__';
512
513 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
514 $class->parse_volname($volname);
515
516 die "create_base not possible with base image\n" if $isBase;
517
518 my ($size, $parent, $format, undef) = rbd_volume_info($scfg, $storeid, $name);
519 die "rbd volume info on '$name' failed\n" if !($size);
520
521 die "rbd image must be at format V2" if $format ne "2";
522
523 die "volname '$volname' contains wrong information about parent $parent $basename\n"
524 if $basename && (!$parent || $parent ne $basename."@".$snap);
525
526 my $newname = $name;
527 $newname =~ s/^vm-/base-/;
528
529 my $newvolname = $basename ? "$basename/$newname" : "$newname";
530
531 my $cmd = $rbd_cmd->(
532 $scfg,
533 $storeid,
534 'rename',
535 get_rbd_path($scfg, $name),
536 get_rbd_path($scfg, $newname),
537 );
538 run_rbd_command($cmd, errmsg => "rbd rename '$name' error");
539
540 eval { $class->unmap_volume($storeid, $scfg, $volname); };
541 warn $@ if $@;
542
543 my $running = undef; #fixme : is create_base always offline ?
544
545 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
546
547 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $newname, $snap);
548
549 if (!$protected){
550 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
551 run_rbd_command($cmd, errmsg => "rbd protect $newname snap '$snap' error");
552 }
553
554 return $newvolname;
555
556 }
557
558 sub clone_image {
559 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
560
561 my $snap = '__base__';
562 $snap = $snapname if length $snapname;
563
564 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
565 $class->parse_volname($volname);
566
567 die "$volname is not a base image and snapname is not provided\n"
568 if !$isBase && !length($snapname);
569
570 my $name = $class->find_free_diskname($storeid, $scfg, $vmid);
571
572 warn "clone $volname: $basename snapname $snap to $name\n";
573
574 if (length($snapname)) {
575 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $volname, $snapname);
576
577 if (!$protected) {
578 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
579 run_rbd_command($cmd, errmsg => "rbd protect $volname snap $snapname error");
580 }
581 }
582
583 my $newvol = "$basename/$name";
584 $newvol = $name if length($snapname);
585
586 my @options = (
587 get_rbd_path($scfg, $basename),
588 '--snap', $snap,
589 );
590 push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'};
591
592 my $cmd = $rbd_cmd->($scfg, $storeid, 'clone', @options, get_rbd_path($scfg, $name));
593 run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
594
595 return $newvol;
596 }
597
598 sub alloc_image {
599 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
600
601
602 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
603 if $name && $name !~ m/^vm-$vmid-/;
604
605 $name = $class->find_free_diskname($storeid, $scfg, $vmid) if !$name;
606
607 my @options = (
608 '--image-format' , 2,
609 '--size', int(($size + 1023) / 1024),
610 );
611 push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'};
612
613 my $cmd = $rbd_cmd->($scfg, $storeid, 'create', @options, $name);
614 run_rbd_command($cmd, errmsg => "rbd create '$name' error");
615
616 return $name;
617 }
618
619 sub free_image {
620 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
621
622 my ($vtype, $name, $vmid, undef, undef, undef) =
623 $class->parse_volname($volname);
624
625
626 my $snaps = rbd_ls_snap($scfg, $storeid, $name);
627 foreach my $snap (keys %$snaps) {
628 if ($snaps->{$snap}->{protected}) {
629 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
630 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
631 }
632 }
633
634 $class->deactivate_volume($storeid, $scfg, $volname);
635
636 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'purge', $name);
637 run_rbd_command($cmd, errmsg => "rbd snap purge '$name' error");
638
639 $cmd = $rbd_cmd->($scfg, $storeid, 'rm', $name);
640 run_rbd_command($cmd, errmsg => "rbd rm '$name' error");
641
642 return undef;
643 }
644
645 sub list_images {
646 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
647
648 $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd};
649
650 my $dat = $cache->{rbd}->{get_rbd_path($scfg)};
651 return [] if !$dat; # nothing found
652
653 my $res = [];
654 for my $image (sort keys %$dat) {
655 my $info = $dat->{$image};
656 my ($volname, $parent, $owner) = $info->@{'name', 'parent', 'vmid'};
657
658 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
659 $info->{volid} = "$storeid:$1/$volname";
660 } else {
661 $info->{volid} = "$storeid:$volname";
662 }
663
664 if ($vollist) {
665 my $found = grep { $_ eq $info->{volid} } @$vollist;
666 next if !$found;
667 } else {
668 next if defined ($vmid) && ($owner ne $vmid);
669 }
670
671 $info->{format} = 'raw';
672
673 push @$res, $info;
674 }
675
676 return $res;
677 }
678
679 sub status {
680 my ($class, $storeid, $scfg, $cache) = @_;
681
682 my $rados = $librados_connect->($scfg, $storeid);
683 my $df = $rados->mon_command({ prefix => 'df', format => 'json' });
684
685 my $pool = $scfg->{'data-pool'} // $scfg->{pool} // 'rbd';
686
687 my ($d) = grep { $_->{name} eq $pool } @{$df->{pools}};
688
689 if (!defined($d)) {
690 warn "could not get usage stats for pool '$pool'\n";
691 return;
692 }
693
694 # max_avail -> max available space for data w/o replication in the pool
695 # bytes_used -> data w/o replication in the pool
696 my $free = $d->{stats}->{max_avail};
697 my $used = $d->{stats}->{stored} // $d->{stats}->{bytes_used};
698 my $total = $used + $free;
699 my $active = 1;
700
701 return ($total, $free, $used, $active);
702 }
703
704 sub activate_storage {
705 my ($class, $storeid, $scfg, $cache) = @_;
706 return 1;
707 }
708
709 sub deactivate_storage {
710 my ($class, $storeid, $scfg, $cache) = @_;
711 return 1;
712 }
713
714 sub map_volume {
715 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
716
717 my ($vtype, $img_name, $vmid) = $class->parse_volname($volname);
718
719 my $name = $img_name;
720 $name .= '@'.$snapname if $snapname;
721
722 my $kerneldev = get_rbd_dev_path($scfg, $storeid, $name);
723
724 return $kerneldev if -b $kerneldev; # already mapped
725
726 # features can only be enabled/disabled for image, not for snapshot!
727 $krbd_feature_update->($scfg, $storeid, $img_name);
728
729 my $cmd = $rbd_cmd->($scfg, $storeid, 'map', $name);
730 run_rbd_command($cmd, errmsg => "can't map rbd volume $name");
731
732 return $kerneldev;
733 }
734
735 sub unmap_volume {
736 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
737
738 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
739 $name .= '@'.$snapname if $snapname;
740
741 my $kerneldev = get_rbd_dev_path($scfg, $storeid, $name);
742
743 if (-b $kerneldev) {
744 my $cmd = $rbd_cmd->($scfg, $storeid, 'unmap', $kerneldev);
745 run_rbd_command($cmd, errmsg => "can't unmap rbd device $kerneldev");
746 }
747
748 return 1;
749 }
750
751 sub activate_volume {
752 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
753
754 $class->map_volume($storeid, $scfg, $volname, $snapname) if $scfg->{krbd};
755
756 return 1;
757 }
758
759 sub deactivate_volume {
760 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
761
762 $class->unmap_volume($storeid, $scfg, $volname, $snapname);
763
764 return 1;
765 }
766
767 sub volume_size_info {
768 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
769
770 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
771 my ($size, $parent) = rbd_volume_info($scfg, $storeid, $name);
772 my $used = wantarray ? rbd_volume_du($scfg, $storeid, $name) : 0;
773 return wantarray ? ($size, 'raw', $used, $parent) : $size;
774 }
775
776 sub volume_resize {
777 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
778
779 return 1 if $running && !$scfg->{krbd}; # FIXME???
780
781 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
782
783 my $cmd = $rbd_cmd->($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
784 run_rbd_command($cmd, errmsg => "rbd resize '$volname' error");
785 return undef;
786 }
787
788 sub volume_snapshot {
789 my ($class, $scfg, $storeid, $volname, $snap) = @_;
790
791 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
792
793 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
794 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
795 return undef;
796 }
797
798 sub volume_snapshot_rollback {
799 my ($class, $scfg, $storeid, $volname, $snap) = @_;
800
801 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
802
803 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
804 run_rbd_command($cmd, errmsg => "rbd snapshot $volname to '$snap' error");
805 }
806
807 sub volume_snapshot_delete {
808 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
809
810 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
811
812 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
813
814 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
815 if ($protected){
816 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
817 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
818 }
819
820 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
821
822 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
823
824 return undef;
825 }
826
827 sub volume_snapshot_needs_fsfreeze {
828 return 1;
829 }
830
831 sub volume_has_feature {
832 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
833
834 my $features = {
835 snapshot => { current => 1, snap => 1},
836 clone => { base => 1, snap => 1},
837 template => { current => 1},
838 copy => { base => 1, current => 1, snap => 1},
839 sparseinit => { base => 1, current => 1},
840 rename => {current => 1},
841 };
842
843 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = $class->parse_volname($volname);
844
845 my $key = undef;
846 if ($snapname){
847 $key = 'snap';
848 } else {
849 $key = $isBase ? 'base' : 'current';
850 }
851 return 1 if $features->{$feature}->{$key};
852
853 return undef;
854 }
855
856 sub rename_volume {
857 my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
858
859 my (
860 undef,
861 $source_image,
862 $source_vmid,
863 $base_name,
864 $base_vmid,
865 undef,
866 $format
867 ) = $class->parse_volname($source_volname);
868 $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format)
869 if !$target_volname;
870
871 eval {
872 my $cmd = $rbd_cmd->($scfg, $storeid, 'info', $target_volname);
873 run_rbd_command($cmd, errmsg => "exist check", quiet => 1);
874 };
875 die "target volume '${target_volname}' already exists\n" if !$@;
876
877 my $cmd = $rbd_cmd->($scfg, $storeid, 'rename', $source_image, $target_volname);
878
879 run_rbd_command(
880 $cmd,
881 errmsg => "could not rename image '${source_image}' to '${target_volname}'",
882 );
883
884 eval { $class->unmap_volume($storeid, $scfg, $source_volname); };
885 warn $@ if $@;
886
887 $base_name = $base_name ? "${base_name}/" : '';
888
889 return "${storeid}:${base_name}${target_volname}";
890 }
891
892 1;