]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/RBDPlugin.pm
next diskname: start ids with 0 to honor MAX_VOLUMES_PER_GUEST
[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;
89a8800b 12use JSON;
0509010d
AD
13
14use base qw(PVE::Storage::Plugin);
15
89a8800b
DC
16my $get_parent_image_name = sub {
17 my ($parent) = @_;
18 return undef if !$parent;
19 return $parent->{image} . "@" . $parent->{snapshot};
20};
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
89a8800b 85 my $active_features = { map { $_ => 1 } $features };
4c3b3085
AA
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
89a8800b 156 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l', '--format', 'json');
1440604a 157 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
d70e7f6c 158
89a8800b 159 my $raw = '';
1be93fe2 160 my $parser = sub { $raw .= shift };
8c3abf12
DM
161
162 eval {
c693f749 163 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
8c3abf12
DM
164 };
165 my $err = $@;
166
167 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
89a8800b
DC
168
169 my $result = $raw ne '' ? JSON::decode_json($raw) : [];
170
171 my $list = {};
172
173 foreach my $el (@$result) {
174 next if defined($el->{snapshot});
175
176 my $image = $el->{image};
177
178 my ($owner) = $image =~ m/^(?:vm|base)-(\d+)-/;
aa14def4 179 next if !defined($owner);
89a8800b
DC
180
181 $list->{$pool}->{$image} = {
182 name => $image,
183 size => $el->{size},
184 parent => $get_parent_image_name->($el->{parent}),
185 vmid => $owner
186 };
187 }
188
411476cd 189 return $list;
0509010d
AD
190}
191
62b98a65 192sub rbd_volume_info {
992e6835
AD
193 my ($scfg, $storeid, $volname, $snap) = @_;
194
195 my $cmd = undef;
196
89a8800b 197 my @options = ('info', $volname, '--format', 'json');
1be93fe2 198 if ($snap) {
89a8800b 199 push @options, '--snap', $snap;
992e6835 200 }
e110213e 201
89a8800b
DC
202 $cmd = &$rbd_cmd($scfg, $storeid, @options);
203
89a8800b 204 my $raw = '';
1be93fe2 205 my $parser = sub { $raw .= shift };
e110213e 206
c693f749 207 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
1be93fe2 208
89a8800b 209 my $volume = $raw ne '' ? JSON::decode_json($raw) : {};
1be93fe2 210
89a8800b 211 $volume->{parent} = $get_parent_image_name->($volume->{parent});
1be93fe2 212 $volume->{protected} = defined($volume->{protected}) && $volume->{protected} eq "true" ? 1 : undef;
e110213e 213
89a8800b 214 return $volume->@{qw(size parent format protected features)};
e110213e
AD
215}
216
e5427b00 217# Configuration
0509010d 218
0509010d
AD
219sub type {
220 return 'rbd';
221}
222
223sub plugindata {
224 return {
1f79bb07 225 content => [ {images => 1, rootdir => 1}, { images => 1 }],
0509010d
AD
226 };
227}
228
229sub properties {
230 return {
e5427b00 231 monhost => {
0b9ef02e 232 description => "IP addresses of monitors (for external clusters).",
e858048f 233 type => 'string', format => 'pve-storage-portal-dns-list',
0509010d 234 },
e5427b00
AD
235 pool => {
236 description => "Pool.",
0509010d
AD
237 type => 'string',
238 },
e5427b00
AD
239 username => {
240 description => "RBD Id.",
0509010d
AD
241 type => 'string',
242 },
e5427b00 243 authsupported => {
0509010d
AD
244 description => "Authsupported.",
245 type => 'string',
246 },
9f20a8a6
AD
247 krbd => {
248 description => "Access rbd through krbd kernel module.",
249 type => 'boolean',
250 },
0509010d
AD
251 };
252}
253
254sub options {
255 return {
35d6dfaf
AD
256 nodes => { optional => 1 },
257 disable => { optional => 1 },
0b9ef02e 258 monhost => { optional => 1},
1440604a
AD
259 pool => { optional => 1 },
260 username => { optional => 1 },
0509010d 261 content => { optional => 1 },
9f20a8a6 262 krbd => { optional => 1 },
9edb99a5 263 bwlimit => { optional => 1 },
0509010d
AD
264 };
265}
266
267# Storage implementation
268
2e109b4b
TL
269sub on_add_hook {
270 my ($class, $storeid, $scfg, %param) = @_;
271
272 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
273
fe0d606f 274 PVE::Storage::CephTools::ceph_create_keyfile($scfg->{type}, $storeid);
2e109b4b
TL
275}
276
277sub on_delete_hook {
278 my ($class, $storeid, $scfg) = @_;
279
280 return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
281
fe0d606f 282 PVE::Storage::CephTools::ceph_remove_keyfile($scfg->{type}, $storeid);
2e109b4b
TL
283}
284
0509010d
AD
285sub parse_volname {
286 my ($class, $volname) = @_;
287
d04c7e55 288 if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
7800e84d 289 return ('images', $4, $7, $2, $3, $5, 'raw');
0509010d
AD
290 }
291
292 die "unable to parse rbd volume name '$volname'\n";
293}
294
295sub path {
38e6ec3f 296 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
0509010d 297
9b7ba1db 298 my $cmd_option = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid);
0509010d 299 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
38e6ec3f 300 $name .= '@'.$snapname if $snapname;
0509010d 301
33cef4c8
WB
302 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
303 return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd};
304
6eebc4a7 305 my $path = "rbd:$pool/$name";
6eebc4a7 306
5fc02afb
FG
307 $path .= ":conf=$cmd_option->{ceph_conf}" if $cmd_option->{ceph_conf};
308 if (defined($scfg->{monhost})) {
9b7ba1db 309 my $monhost = PVE::Storage::CephTools::hostlist($scfg->{monhost}, ';');
6eebc4a7
FG
310 $monhost =~ s/:/\\:/g;
311 $path .= ":mon_host=$monhost";
6cc88e8e 312 $path .= ":auth_supported=$cmd_option->{auth_supported}";
6eebc4a7
FG
313 }
314
6cc88e8e 315 $path .= ":id=$cmd_option->{userid}:keyring=$cmd_option->{keyring}" if ($cmd_option->{keyring});
13417225 316
0509010d
AD
317 return ($path, $vmid, $vtype);
318}
319
5b9b9b14
AD
320my $find_free_diskname = sub {
321 my ($storeid, $scfg, $vmid) = @_;
322
53a236f2 323 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls');
5b9b9b14 324 my $disk_ids = {};
5b9b9b14 325
53a236f2
FG
326 my $parser = sub {
327 my $line = shift;
328
329 if ($line =~ m/^(vm|base)-\Q$vmid\E+-disk-(\d+)$/) {
5b9b9b14
AD
330 $disk_ids->{$2} = 1;
331 }
53a236f2
FG
332 };
333
334 eval {
335 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
336 };
337 my $err = $@;
338
339 die $err if $err && $err !~ m/doesn't contain rbd images/;
340
5b9b9b14
AD
341 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
342 for (my $i = 1; $i < 100; $i++) {
343 if (!$disk_ids->{$i}) {
344 return "vm-$vmid-disk-$i";
345 }
346 }
347
348 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
349};
350
5eab0272
DM
351sub create_base {
352 my ($class, $storeid, $scfg, $volname) = @_;
353
992e6835
AD
354 my $snap = '__base__';
355
356 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
357 $class->parse_volname($volname);
358
359 die "create_base not possible with base image\n" if $isBase;
360
361 my ($size, $parent, $format, undef) = rbd_volume_info($scfg, $storeid, $name);
362 die "rbd volume info on '$name' failed\n" if !($size);
363
364 die "rbd image must be at format V2" if $format ne "2";
365
366 die "volname '$volname' contains wrong information about parent $parent $basename\n"
367 if $basename && (!$parent || $parent ne $basename."@".$snap);
368
369 my $newname = $name;
370 $newname =~ s/^vm-/base-/;
371
372 my $newvolname = $basename ? "$basename/$newname" : "$newname";
373
8897f5dc 374 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
c693f749 375 run_rbd_command($cmd, errmsg => "rbd rename '$name' error");
992e6835
AD
376
377 my $running = undef; #fixme : is create_base always offline ?
378
379 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
380
381 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $newname, $snap);
382
383 if (!$protected){
384 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
2362bc87 385 run_rbd_command($cmd, errmsg => "rbd protect $newname snap '$snap' error");
992e6835
AD
386 }
387
388 return $newvolname;
389
5eab0272
DM
390}
391
392sub clone_image {
f236eaf8 393 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
5eab0272 394
f2708285 395 my $snap = '__base__';
f236eaf8 396 $snap = $snapname if length $snapname;
f2708285
AD
397
398 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
399 $class->parse_volname($volname);
400
63da6d79
DM
401 die "$volname is not a base image and snapname is not provided\n"
402 if !$isBase && !length($snapname);
f2708285
AD
403
404 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
405
f236eaf8
SP
406 warn "clone $volname: $basename snapname $snap to $name\n";
407
63da6d79 408 if (length($snapname)) {
f236eaf8
SP
409 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $volname, $snapname);
410
63da6d79 411 if (!$protected) {
f236eaf8
SP
412 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
413 run_rbd_command($cmd, errmsg => "rbd protect $volname snap $snapname error");
414 }
415 }
f2708285
AD
416
417 my $newvol = "$basename/$name";
63da6d79
DM
418 $newvol = $name if length($snapname);
419
420 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
421 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
f2708285 422
c693f749 423 run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
f2708285 424
6ea1a3f3 425 &$krbd_feature_disable($scfg, $storeid, $name);
d86fd0a4 426
f2708285 427 return $newvol;
5eab0272
DM
428}
429
0509010d
AD
430sub alloc_image {
431 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
432
433
292a33fd 434 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
0509010d 435 if $name && $name !~ m/^vm-$vmid-/;
0509010d 436
3fad2603 437 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
0509010d 438
a8c3f8f6 439 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
c693f749 440 run_rbd_command($cmd, errmsg => "rbd create $name' error");
0509010d 441
6ea1a3f3 442 &$krbd_feature_disable($scfg, $storeid, $name);
d86fd0a4 443
0509010d
AD
444 return $name;
445}
446
447sub free_image {
32437ed2 448 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
0509010d 449
42d07b9a
AD
450 my ($vtype, $name, $vmid, undef, undef, undef) =
451 $class->parse_volname($volname);
452
453 if ($isBase) {
454 my $snap = '__base__';
455 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
456 if ($protected){
457 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
2362bc87 458 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
42d07b9a
AD
459 }
460 }
461
515ef80b
WL
462 $class->deactivate_volume($storeid, $scfg, $volname);
463
42d07b9a 464 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
c693f749 465 run_rbd_command($cmd, errmsg => "rbd snap purge '$volname' error");
c30470a3 466
42d07b9a 467 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
c693f749 468 run_rbd_command($cmd, errmsg => "rbd rm '$volname' error");
0509010d
AD
469
470 return undef;
471}
472
473sub list_images {
474 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
475
e5427b00 476 $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd};
1440604a 477 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
411476cd 478
0509010d
AD
479 my $res = [];
480
1440604a 481 if (my $dat = $cache->{rbd}->{$pool}) {
883d9b81 482 foreach my $image (keys %$dat) {
0509010d 483
cfd58f1f
DM
484 my $info = $dat->{$image};
485
486 my $volname = $info->{name};
487 my $parent = $info->{parent};
488 my $owner = $info->{vmid};
9690e55e
FG
489
490 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
1b83c3d9
FG
491 $info->{volid} = "$storeid:$1/$volname";
492 } else {
493 $info->{volid} = "$storeid:$volname";
9690e55e 494 }
0509010d 495
883d9b81 496 if ($vollist) {
1b83c3d9 497 my $found = grep { $_ eq $info->{volid} } @$vollist;
883d9b81
FG
498 next if !$found;
499 } else {
500 next if defined ($vmid) && ($owner ne $vmid);
501 }
0509010d 502
411476cd 503 $info->{format} = 'raw';
0509010d 504
883d9b81
FG
505 push @$res, $info;
506 }
0509010d
AD
507 }
508
411476cd 509 return $res;
0509010d
AD
510}
511
0509010d
AD
512sub status {
513 my ($class, $storeid, $scfg, $cache) = @_;
514
69589444 515
41aacc6c
AA
516 my $rados = &$librados_connect($scfg, $storeid);
517 my $df = $rados->mon_command({ prefix => 'df', format => 'json' });
69589444 518
41aacc6c 519 my ($d) = grep { $_->{name} eq $scfg->{pool} } @{$df->{pools}};
69589444 520
41aacc6c
AA
521 # max_avail -> max available space for data w/o replication in the pool
522 # bytes_used -> data w/o replication in the pool
523 my $free = $d->{stats}->{max_avail};
524 my $used = $d->{stats}->{bytes_used};
525 my $total = $used + $free;
0509010d 526 my $active = 1;
0509010d 527
411476cd 528 return ($total, $free, $used, $active);
0509010d
AD
529}
530
531sub activate_storage {
532 my ($class, $storeid, $scfg, $cache) = @_;
533 return 1;
534}
535
536sub deactivate_storage {
537 my ($class, $storeid, $scfg, $cache) = @_;
538 return 1;
539}
540
541sub activate_volume {
02e797b8 542 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
9f20a8a6
AD
543
544 return 1 if !$scfg->{krbd};
545
546 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
4f6a99d8
DM
547 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
548
549 my $path = "/dev/rbd/$pool/$name";
02e797b8 550 $path .= '@'.$snapname if $snapname;
4f6a99d8 551 return if -b $path;
9f20a8a6 552
02e797b8 553 $name .= '@'.$snapname if $snapname;
9f20a8a6
AD
554 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
555 run_rbd_command($cmd, errmsg => "can't mount rbd volume $name");
556
0509010d
AD
557 return 1;
558}
559
560sub deactivate_volume {
02e797b8 561 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
9f20a8a6
AD
562
563 return 1 if !$scfg->{krbd};
564
565 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
566 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
567
568 my $path = "/dev/rbd/$pool/$name";
02e797b8 569 $path .= '@'.$snapname if $snapname;
b50812f9
WL
570 return if ! -b $path;
571
9f20a8a6 572 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
72e743bd 573 run_rbd_command($cmd, errmsg => "can't unmap rbd volume $name");
9f20a8a6 574
0509010d
AD
575 return 1;
576}
577
0002d9cc
AD
578sub volume_size_info {
579 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
580
81d1d017
AD
581 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
582 my ($size, undef) = rbd_volume_info($scfg, $storeid, $name);
62b98a65 583 return $size;
0002d9cc
AD
584}
585
e7a42a76
AD
586sub volume_resize {
587 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
588
a4aee433 589 return 1 if $running && !$scfg->{krbd};
e7a42a76 590
478fc06c
AD
591 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
592
4b7dd9d7 593 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
c693f749 594 run_rbd_command($cmd, errmsg => "rbd resize '$volname' error");
e7a42a76
AD
595 return undef;
596}
597
788dd8e1 598sub volume_snapshot {
f5640e7d 599 my ($class, $scfg, $storeid, $volname, $snap) = @_;
788dd8e1 600
9af33ed0
AD
601 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
602
603 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
c693f749 604 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
788dd8e1
AD
605 return undef;
606}
607
5a2b2e2f
AD
608sub volume_snapshot_rollback {
609 my ($class, $scfg, $storeid, $volname, $snap) = @_;
610
c6ce2cc8
AD
611 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
612
613 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
2362bc87 614 run_rbd_command($cmd, errmsg => "rbd snapshot $volname to '$snap' error");
5a2b2e2f
AD
615}
616
cce29bcd
AD
617sub volume_snapshot_delete {
618 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
619
e438d094 620 return 1 if $running && !$scfg->{krbd};
cce29bcd 621
399581a2
WB
622 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
623
c78cb110
AD
624 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
625
f90a0a20
SP
626 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
627 if ($protected){
628 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
629 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
630 }
631
c78cb110 632 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
c693f749
SP
633
634 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
635
cce29bcd
AD
636 return undef;
637}
638
774f21b9
AD
639sub volume_has_feature {
640 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
641
642 my $features = {
5649ccfe 643 snapshot => { current => 1, snap => 1},
44c3689a 644 clone => { base => 1, snap => 1},
5649ccfe
AD
645 template => { current => 1},
646 copy => { base => 1, current => 1, snap => 1},
baafddbd 647 sparseinit => { base => 1, current => 1},
774f21b9
AD
648 };
649
1e7ae581
AD
650 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
651 $class->parse_volname($volname);
652
653 my $key = undef;
654 if($snapname){
2c5a7097 655 $key = 'snap';
1e7ae581
AD
656 }else{
657 $key = $isBase ? 'base' : 'current';
658 }
659 return 1 if $features->{$feature}->{$key};
774f21b9
AD
660
661 return undef;
662}
663
0509010d 6641;