]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/RBDPlugin.pm
Linux LIO/targetcli support
[pve-storage.git] / PVE / Storage / RBDPlugin.pm
CommitLineData
0509010d
AD
1package PVE::Storage::RBDPlugin;
2
3use strict;
4use warnings;
5use IO::File;
e858048f 6use Net::IP;
0509010d
AD
7use PVE::Tools qw(run_command trim);
8use PVE::Storage::Plugin;
9use PVE::JSONSchema qw(get_standard_option);
41aacc6c 10use PVE::RADOS;
9b7ba1db 11use PVE::Storage::CephTools;
0509010d
AD
12
13use base qw(PVE::Storage::Plugin);
14
e7ac2d5c 15my $rbd_unittobytes = {
a0028cf9 16 "k" => 1024,
e7ac2d5c
DM
17 "M" => 1024*1024,
18 "G" => 1024*1024*1024,
19 "T" => 1024*1024*1024*1024,
20};
249cb647 21
8897f5dc
SP
22my $add_pool_to_disk = sub {
23 my ($scfg, $disk) = @_;
24
25 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
26
27 return "$pool/$disk";
28};
29
6cc88e8e
AA
30my $build_cmd = sub {
31 my ($binary, $scfg, $storeid, $op, @options) = @_;
32
9b7ba1db 33 my $cmd_option = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid);
6cc88e8e
AA
34 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
35
36 my $cmd = [$binary, '-p', $pool];
37
38 push @$cmd, '-c', $cmd_option->{ceph_conf} if ($cmd_option->{ceph_conf});
39 push @$cmd, '-m', $cmd_option->{mon_host} if ($cmd_option->{mon_host});
40 push @$cmd, '--auth_supported', $cmd_option->{auth_supported} if ($cmd_option->{auth_supported});
41 push @$cmd, '-n', "client.$cmd_option->{userid}" if ($cmd_option->{userid});
42 push @$cmd, '--keyring', $cmd_option->{keyring} if ($cmd_option->{keyring});
43
44 push @$cmd, $op;
45
46 push @$cmd, @options if scalar(@options);
47
48 return $cmd;
49};
50
51my $rbd_cmd = sub {
52 my ($scfg, $storeid, $op, @options) = @_;
53
54 return $build_cmd->('/usr/bin/rbd', $scfg, $storeid, $op, @options);
55};
56
57my $rados_cmd = sub {
58 my ($scfg, $storeid, $op, @options) = @_;
59
60 return $build_cmd->('/usr/bin/rados', $scfg, $storeid, $op, @options);
61};
62
41aacc6c
AA
63my $librados_connect = sub {
64 my ($scfg, $storeid, $options) = @_;
65
9b7ba1db 66 my $librados_config = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid);
41aacc6c
AA
67
68 my $rados = PVE::RADOS->new(%$librados_config);
69
70 return $rados;
71};
72
d86fd0a4 73# needed for volumes created using ceph jewel (or higher)
6ea1a3f3 74my $krbd_feature_disable = sub {
d86fd0a4
FG
75 my ($scfg, $storeid, $name) = @_;
76
77 return 1 if !$scfg->{krbd};
78
79 my ($major, undef, undef, undef) = ceph_version();
80 return 1 if $major < 10;
81
4c3b3085
AA
82 my $krbd_feature_blacklist = ['deep-flatten', 'fast-diff', 'object-map', 'exclusive-lock'];
83 my (undef, undef, undef, undef, $features) = rbd_volume_info($scfg, $storeid, $name);
84
85 my $active_features = { map { $_ => 1 } PVE::Tools::split_list($features)};
86 my $incompatible_features = join(',', grep { %$active_features{$_} } @$krbd_feature_blacklist);
87
88 if ($incompatible_features) {
89 my $feature_cmd = &$rbd_cmd($scfg, $storeid, 'feature', 'disable', $name, $incompatible_features);
90 run_rbd_command($feature_cmd, errmsg => "could not disable krbd-incompatible image features of rbd volume $name");
91 }
d86fd0a4
FG
92};
93
7aeda033 94my $ceph_version_parser = sub {
6d2b278c 95 my $line = shift;
21aaefd5 96 if ($line =~ m/^ceph version ((\d+)\.(\d+)\.(\d+))(?: \([a-fA-F0-9]+\))/) {
6d2b278c
DM
97 return ($2, $3, $4, $1);
98 } else {
99 warn "Could not parse Ceph version: '$line'\n";
100 }
7aeda033
FG
101};
102
103sub ceph_version {
104 my ($cache) = @_;
105
106 my $version_string = $cache;
107
108 my $major;
109 my $minor;
110 my $bugfix;
111
112 if (defined($version_string)) {
0be02e0f 113 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($version_string);
7aeda033 114 } else {
e76dbd92 115 run_command('ceph --version', outfunc => sub {
7aeda033 116 my $line = shift;
0be02e0f 117 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($line);
7aeda033
FG
118 });
119 }
120 return undef if !defined($version_string);
121 return wantarray ? ($major, $minor, $bugfix, $version_string) : $version_string;
122}
123
c693f749
SP
124sub run_rbd_command {
125 my ($cmd, %args) = @_;
126
127 my $lasterr;
128 my $errmsg = $args{errmsg} . ": " || "";
c97c5b3b 129 if (!exists($args{errfunc})) {
c693f749
SP
130 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
131 # at least 1 child(ren) in pool cephstor1
132 $args{errfunc} = sub {
c97c5b3b
DM
133 my $line = shift;
134 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
135 $lasterr = "$1\n";
136 } else {
137 $lasterr = $line;
138 }
139 print STDERR $lasterr;
140 *STDERR->flush();
141 };
142 }
143
144 eval { run_command($cmd, %args); };
145 if (my $err = $@) {
146 die $errmsg . $lasterr if length($lasterr);
147 die $err;
c693f749
SP
148 }
149
c97c5b3b 150 return undef;
c693f749
SP
151}
152
411476cd
DM
153sub rbd_ls {
154 my ($scfg, $storeid) = @_;
d70e7f6c 155
7cb2889a 156 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l');
1440604a 157 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
d70e7f6c 158
411476cd 159 my $list = {};
0509010d 160
8c3abf12 161 my $parser = sub {
411476cd 162 my $line = shift;
0509010d 163
4db25fa2 164 if ($line =~ m/^((vm|base)-(\d+)-\S+)\s+(\d+)(k|M|G|T)\s((\S+)\/((vm|base)-\d+-\S+@\S+))?/) {
ca1e168a 165 my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8);
d746da86 166 return if $image =~ /@/; #skip snapshots
0509010d 167
1440604a 168 $list->{$pool}->{$image} = {
411476cd 169 name => $image,
e7ac2d5c 170 size => $size*$rbd_unittobytes->{$unit},
62b98a65 171 parent => $parent,
411476cd
DM
172 vmid => $owner
173 };
174 }
8c3abf12
DM
175 };
176
177 eval {
c693f749 178 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
8c3abf12
DM
179 };
180 my $err = $@;
181
182 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
411476cd
DM
183
184 return $list;
0509010d
AD
185}
186
62b98a65 187sub rbd_volume_info {
992e6835
AD
188 my ($scfg, $storeid, $volname, $snap) = @_;
189
190 my $cmd = undef;
191
192 if($snap){
193 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
194 }else{
195 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
196 }
e110213e 197
e110213e 198 my $size = undef;
62b98a65 199 my $parent = undef;
992e6835
AD
200 my $format = undef;
201 my $protected = undef;
4c3b3085 202 my $features = undef;
62b98a65 203
e110213e
AD
204 my $parser = sub {
205 my $line = shift;
206
a0028cf9 207 if ($line =~ m/size (\d+) (k|M|G|T)B in (\d+) objects/) {
e7ac2d5c 208 $size = $1 * $rbd_unittobytes->{$2} if ($1);
62b98a65
AD
209 } elsif ($line =~ m/parent:\s(\S+)\/(\S+)/) {
210 $parent = $2;
992e6835
AD
211 } elsif ($line =~ m/format:\s(\d+)/) {
212 $format = $1;
213 } elsif ($line =~ m/protected:\s(\S+)/) {
214 $protected = 1 if $1 eq "True";
4c3b3085
AA
215 } elsif ($line =~ m/features:\s(.+)/) {
216 $features = $1;
e110213e 217 }
992e6835 218
e110213e
AD
219 };
220
c693f749 221 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
e110213e 222
4c3b3085 223 return ($size, $parent, $format, $protected, $features);
e110213e
AD
224}
225
e5427b00 226# Configuration
0509010d 227
0509010d
AD
228sub type {
229 return 'rbd';
230}
231
232sub plugindata {
233 return {
1f79bb07 234 content => [ {images => 1, rootdir => 1}, { images => 1 }],
0509010d
AD
235 };
236}
237
238sub properties {
239 return {
e5427b00 240 monhost => {
0b9ef02e 241 description => "IP addresses of monitors (for external clusters).",
e858048f 242 type => 'string', format => 'pve-storage-portal-dns-list',
0509010d 243 },
e5427b00
AD
244 pool => {
245 description => "Pool.",
0509010d
AD
246 type => 'string',
247 },
e5427b00
AD
248 username => {
249 description => "RBD Id.",
0509010d
AD
250 type => 'string',
251 },
e5427b00 252 authsupported => {
0509010d
AD
253 description => "Authsupported.",
254 type => 'string',
255 },
9f20a8a6
AD
256 krbd => {
257 description => "Access rbd through krbd kernel module.",
258 type => 'boolean',
259 },
0509010d
AD
260 };
261}
262
263sub options {
264 return {
35d6dfaf
AD
265 nodes => { optional => 1 },
266 disable => { optional => 1 },
0b9ef02e 267 monhost => { optional => 1},
1440604a
AD
268 pool => { optional => 1 },
269 username => { optional => 1 },
0509010d 270 content => { optional => 1 },
9f20a8a6 271 krbd => { optional => 1 },
9edb99a5 272 bwlimit => { optional => 1 },
0509010d
AD
273 };
274}
275
276# Storage implementation
277
2e109b4b
TL
278sub on_add_hook {
279 my ($class, $storeid, $scfg, %param) = @_;
280
281 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
282
fe0d606f 283 PVE::Storage::CephTools::ceph_create_keyfile($scfg->{type}, $storeid);
2e109b4b
TL
284}
285
286sub on_delete_hook {
287 my ($class, $storeid, $scfg) = @_;
288
289 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
290
fe0d606f 291 PVE::Storage::CephTools::ceph_remove_keyfile($scfg->{type}, $storeid);
2e109b4b
TL
292}
293
0509010d
AD
294sub parse_volname {
295 my ($class, $volname) = @_;
296
d04c7e55 297 if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
7800e84d 298 return ('images', $4, $7, $2, $3, $5, 'raw');
0509010d
AD
299 }
300
301 die "unable to parse rbd volume name '$volname'\n";
302}
303
304sub path {
38e6ec3f 305 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
0509010d 306
9b7ba1db 307 my $cmd_option = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid);
0509010d 308 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
38e6ec3f 309 $name .= '@'.$snapname if $snapname;
0509010d 310
33cef4c8
WB
311 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
312 return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd};
313
6eebc4a7 314 my $path = "rbd:$pool/$name";
6eebc4a7 315
5fc02afb
FG
316 $path .= ":conf=$cmd_option->{ceph_conf}" if $cmd_option->{ceph_conf};
317 if (defined($scfg->{monhost})) {
9b7ba1db 318 my $monhost = PVE::Storage::CephTools::hostlist($scfg->{monhost}, ';');
6eebc4a7
FG
319 $monhost =~ s/:/\\:/g;
320 $path .= ":mon_host=$monhost";
6cc88e8e 321 $path .= ":auth_supported=$cmd_option->{auth_supported}";
6eebc4a7
FG
322 }
323
6cc88e8e 324 $path .= ":id=$cmd_option->{userid}:keyring=$cmd_option->{keyring}" if ($cmd_option->{keyring});
13417225 325
0509010d
AD
326 return ($path, $vmid, $vtype);
327}
328
5b9b9b14
AD
329my $find_free_diskname = sub {
330 my ($storeid, $scfg, $vmid) = @_;
331
53a236f2 332 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls');
5b9b9b14 333 my $disk_ids = {};
5b9b9b14 334
53a236f2
FG
335 my $parser = sub {
336 my $line = shift;
337
338 if ($line =~ m/^(vm|base)-\Q$vmid\E+-disk-(\d+)$/) {
5b9b9b14
AD
339 $disk_ids->{$2} = 1;
340 }
53a236f2
FG
341 };
342
343 eval {
344 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
345 };
346 my $err = $@;
347
348 die $err if $err && $err !~ m/doesn't contain rbd images/;
349
5b9b9b14
AD
350 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
351 for (my $i = 1; $i < 100; $i++) {
352 if (!$disk_ids->{$i}) {
353 return "vm-$vmid-disk-$i";
354 }
355 }
356
357 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
358};
359
5eab0272
DM
360sub create_base {
361 my ($class, $storeid, $scfg, $volname) = @_;
362
992e6835
AD
363 my $snap = '__base__';
364
365 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
366 $class->parse_volname($volname);
367
368 die "create_base not possible with base image\n" if $isBase;
369
370 my ($size, $parent, $format, undef) = rbd_volume_info($scfg, $storeid, $name);
371 die "rbd volume info on '$name' failed\n" if !($size);
372
373 die "rbd image must be at format V2" if $format ne "2";
374
375 die "volname '$volname' contains wrong information about parent $parent $basename\n"
376 if $basename && (!$parent || $parent ne $basename."@".$snap);
377
378 my $newname = $name;
379 $newname =~ s/^vm-/base-/;
380
381 my $newvolname = $basename ? "$basename/$newname" : "$newname";
382
8897f5dc 383 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
c693f749 384 run_rbd_command($cmd, errmsg => "rbd rename '$name' error");
992e6835
AD
385
386 my $running = undef; #fixme : is create_base always offline ?
387
388 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
389
390 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $newname, $snap);
391
392 if (!$protected){
393 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
2362bc87 394 run_rbd_command($cmd, errmsg => "rbd protect $newname snap '$snap' error");
992e6835
AD
395 }
396
397 return $newvolname;
398
5eab0272
DM
399}
400
401sub clone_image {
f236eaf8 402 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
5eab0272 403
f2708285 404 my $snap = '__base__';
f236eaf8 405 $snap = $snapname if length $snapname;
f2708285
AD
406
407 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
408 $class->parse_volname($volname);
409
63da6d79
DM
410 die "$volname is not a base image and snapname is not provided\n"
411 if !$isBase && !length($snapname);
f2708285
AD
412
413 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
414
f236eaf8
SP
415 warn "clone $volname: $basename snapname $snap to $name\n";
416
63da6d79 417 if (length($snapname)) {
f236eaf8
SP
418 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $volname, $snapname);
419
63da6d79 420 if (!$protected) {
f236eaf8
SP
421 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
422 run_rbd_command($cmd, errmsg => "rbd protect $volname snap $snapname error");
423 }
424 }
f2708285
AD
425
426 my $newvol = "$basename/$name";
63da6d79
DM
427 $newvol = $name if length($snapname);
428
429 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
430 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
f2708285 431
c693f749 432 run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
f2708285 433
6ea1a3f3 434 &$krbd_feature_disable($scfg, $storeid, $name);
d86fd0a4 435
f2708285 436 return $newvol;
5eab0272
DM
437}
438
0509010d
AD
439sub alloc_image {
440 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
441
442
292a33fd 443 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
0509010d 444 if $name && $name !~ m/^vm-$vmid-/;
0509010d 445
3fad2603 446 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
0509010d 447
a8c3f8f6 448 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
c693f749 449 run_rbd_command($cmd, errmsg => "rbd create $name' error");
0509010d 450
6ea1a3f3 451 &$krbd_feature_disable($scfg, $storeid, $name);
d86fd0a4 452
0509010d
AD
453 return $name;
454}
455
456sub free_image {
32437ed2 457 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
0509010d 458
42d07b9a
AD
459 my ($vtype, $name, $vmid, undef, undef, undef) =
460 $class->parse_volname($volname);
461
462 if ($isBase) {
463 my $snap = '__base__';
464 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
465 if ($protected){
466 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
2362bc87 467 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
42d07b9a
AD
468 }
469 }
470
515ef80b
WL
471 $class->deactivate_volume($storeid, $scfg, $volname);
472
42d07b9a 473 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
c693f749 474 run_rbd_command($cmd, errmsg => "rbd snap purge '$volname' error");
c30470a3 475
42d07b9a 476 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
c693f749 477 run_rbd_command($cmd, errmsg => "rbd rm '$volname' error");
0509010d
AD
478
479 return undef;
480}
481
482sub list_images {
483 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
484
e5427b00 485 $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd};
1440604a 486 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
411476cd 487
0509010d
AD
488 my $res = [];
489
1440604a 490 if (my $dat = $cache->{rbd}->{$pool}) {
883d9b81 491 foreach my $image (keys %$dat) {
0509010d 492
cfd58f1f
DM
493 my $info = $dat->{$image};
494
495 my $volname = $info->{name};
496 my $parent = $info->{parent};
497 my $owner = $info->{vmid};
9690e55e
FG
498
499 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
1b83c3d9
FG
500 $info->{volid} = "$storeid:$1/$volname";
501 } else {
502 $info->{volid} = "$storeid:$volname";
9690e55e 503 }
0509010d 504
883d9b81 505 if ($vollist) {
1b83c3d9 506 my $found = grep { $_ eq $info->{volid} } @$vollist;
883d9b81
FG
507 next if !$found;
508 } else {
509 next if defined ($vmid) && ($owner ne $vmid);
510 }
0509010d 511
411476cd 512 $info->{format} = 'raw';
0509010d 513
883d9b81
FG
514 push @$res, $info;
515 }
0509010d
AD
516 }
517
411476cd 518 return $res;
0509010d
AD
519}
520
0509010d
AD
521sub status {
522 my ($class, $storeid, $scfg, $cache) = @_;
523
69589444 524
41aacc6c
AA
525 my $rados = &$librados_connect($scfg, $storeid);
526 my $df = $rados->mon_command({ prefix => 'df', format => 'json' });
69589444 527
41aacc6c 528 my ($d) = grep { $_->{name} eq $scfg->{pool} } @{$df->{pools}};
69589444 529
41aacc6c
AA
530 # max_avail -> max available space for data w/o replication in the pool
531 # bytes_used -> data w/o replication in the pool
532 my $free = $d->{stats}->{max_avail};
533 my $used = $d->{stats}->{bytes_used};
534 my $total = $used + $free;
0509010d 535 my $active = 1;
0509010d 536
411476cd 537 return ($total, $free, $used, $active);
0509010d
AD
538}
539
540sub activate_storage {
541 my ($class, $storeid, $scfg, $cache) = @_;
542 return 1;
543}
544
545sub deactivate_storage {
546 my ($class, $storeid, $scfg, $cache) = @_;
547 return 1;
548}
549
550sub activate_volume {
02e797b8 551 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
9f20a8a6
AD
552
553 return 1 if !$scfg->{krbd};
554
555 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
4f6a99d8
DM
556 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
557
558 my $path = "/dev/rbd/$pool/$name";
02e797b8 559 $path .= '@'.$snapname if $snapname;
4f6a99d8 560 return if -b $path;
9f20a8a6 561
02e797b8 562 $name .= '@'.$snapname if $snapname;
9f20a8a6
AD
563 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
564 run_rbd_command($cmd, errmsg => "can't mount rbd volume $name");
565
0509010d
AD
566 return 1;
567}
568
569sub deactivate_volume {
02e797b8 570 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
9f20a8a6
AD
571
572 return 1 if !$scfg->{krbd};
573
574 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
575 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
576
577 my $path = "/dev/rbd/$pool/$name";
02e797b8 578 $path .= '@'.$snapname if $snapname;
b50812f9
WL
579 return if ! -b $path;
580
9f20a8a6 581 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
72e743bd 582 run_rbd_command($cmd, errmsg => "can't unmap rbd volume $name");
9f20a8a6 583
0509010d
AD
584 return 1;
585}
586
0002d9cc
AD
587sub volume_size_info {
588 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
589
81d1d017
AD
590 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
591 my ($size, undef) = rbd_volume_info($scfg, $storeid, $name);
62b98a65 592 return $size;
0002d9cc
AD
593}
594
e7a42a76
AD
595sub volume_resize {
596 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
597
a4aee433 598 return 1 if $running && !$scfg->{krbd};
e7a42a76 599
478fc06c
AD
600 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
601
4b7dd9d7 602 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
c693f749 603 run_rbd_command($cmd, errmsg => "rbd resize '$volname' error");
e7a42a76
AD
604 return undef;
605}
606
788dd8e1 607sub volume_snapshot {
f5640e7d 608 my ($class, $scfg, $storeid, $volname, $snap) = @_;
788dd8e1 609
9af33ed0
AD
610 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
611
612 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
c693f749 613 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
788dd8e1
AD
614 return undef;
615}
616
5a2b2e2f
AD
617sub volume_snapshot_rollback {
618 my ($class, $scfg, $storeid, $volname, $snap) = @_;
619
c6ce2cc8
AD
620 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
621
622 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
2362bc87 623 run_rbd_command($cmd, errmsg => "rbd snapshot $volname to '$snap' error");
5a2b2e2f
AD
624}
625
cce29bcd
AD
626sub volume_snapshot_delete {
627 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
628
e438d094 629 return 1 if $running && !$scfg->{krbd};
cce29bcd 630
399581a2
WB
631 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
632
c78cb110
AD
633 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
634
f90a0a20
SP
635 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
636 if ($protected){
637 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
638 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
639 }
640
c78cb110 641 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
c693f749
SP
642
643 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
644
cce29bcd
AD
645 return undef;
646}
647
774f21b9
AD
648sub volume_has_feature {
649 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
650
651 my $features = {
5649ccfe 652 snapshot => { current => 1, snap => 1},
44c3689a 653 clone => { base => 1, snap => 1},
5649ccfe
AD
654 template => { current => 1},
655 copy => { base => 1, current => 1, snap => 1},
baafddbd 656 sparseinit => { base => 1, current => 1},
774f21b9
AD
657 };
658
1e7ae581
AD
659 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
660 $class->parse_volname($volname);
661
662 my $key = undef;
663 if($snapname){
2c5a7097 664 $key = 'snap';
1e7ae581
AD
665 }else{
666 $key = $isBase ? 'base' : 'current';
667 }
668 return 1 if $features->{$feature}->{$key};
774f21b9
AD
669
670 return undef;
671}
672
0509010d 6731;