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