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