]> git.proxmox.com Git - pve-storage.git/blame - src/PVE/Storage/RBDPlugin.pm
rbd: volume resize: avoid passing floating point value to rbd
[pve-storage.git] / src / PVE / Storage / RBDPlugin.pm
CommitLineData
0509010d
AD
1package PVE::Storage::RBDPlugin;
2
3use strict;
4use warnings;
5102900d 5
a1528ffe 6use Cwd qw(abs_path);
0509010d 7use IO::File;
5102900d 8use JSON;
e858048f 9use Net::IP;
93e34f72 10use POSIX qw(ceil);
5102900d
TL
11
12use PVE::CephConfig;
cfe46e2d 13use PVE::Cluster qw(cfs_read_file);;
0509010d 14use PVE::JSONSchema qw(get_standard_option);
0ef8fb9d 15use PVE::ProcFSTools;
41aacc6c 16use PVE::RADOS;
6a44cc41 17use PVE::RPCEnvironment;
5102900d 18use PVE::Storage::Plugin;
a1528ffe 19use PVE::Tools qw(run_command trim file_read_firstline);
0509010d
AD
20
21use base qw(PVE::Storage::Plugin);
22
89a8800b
DC
23my $get_parent_image_name = sub {
24 my ($parent) = @_;
25 return undef if !$parent;
26 return $parent->{image} . "@" . $parent->{snapshot};
27};
28
cfe46e2d
AL
29my $librados_connect = sub {
30 my ($scfg, $storeid, $options) = @_;
31
6a44cc41
FE
32 $options->{timeout} = 60
33 if !defined($options->{timeout}) && PVE::RPCEnvironment->is_worker();
34
2be327ab 35 my $librados_config = PVE::CephConfig::ceph_connect_option($scfg, $storeid, $options->%*);
cfe46e2d
AL
36
37 my $rados = PVE::RADOS->new(%$librados_config);
38
39 return $rados;
40};
41
4cf696f6 42my sub get_rbd_path {
aeb007cb 43 my ($scfg, $volume) = @_;
c27fe648
TL
44 my $path = $scfg->{pool} ? $scfg->{pool} : 'rbd';
45 $path .= "/$scfg->{namespace}" if defined($scfg->{namespace});
46 $path .= "/$volume" if defined($volume);
47 return $path;
8897f5dc
SP
48};
49
cfe46e2d
AL
50my sub get_rbd_dev_path {
51 my ($scfg, $storeid, $volume) = @_;
52
53 my $cluster_id = '';
78638b3d
TL
54 if ($scfg->{fsid}) {
55 # NOTE: the config doesn't support this currently (but it could!), hack for qemu-server tests
56 $cluster_id = $scfg->{fsid};
57 } elsif ($scfg->{monhost}) {
cfe46e2d
AL
58 my $rados = $librados_connect->($scfg, $storeid);
59 $cluster_id = $rados->mon_command({ prefix => 'fsid', format => 'json' })->{fsid};
60 } else {
61 $cluster_id = cfs_read_file('ceph.conf')->{global}->{fsid};
62 }
63
64 my $uuid_pattern = "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})";
65 if ($cluster_id =~ qr/^${uuid_pattern}$/is) {
66 $cluster_id = $1; # use untained value
67 } else {
68 die "cluster fsid has invalid format\n";
69 }
70
71 my $rbd_path = get_rbd_path($scfg, $volume);
72 my $pve_path = "/dev/rbd-pve/${cluster_id}/${rbd_path}";
73 my $path = "/dev/rbd/${rbd_path}";
74
a1528ffe
AL
75 if (!-e $pve_path && -e $path) {
76 # possibly mapped before rbd-pve rule existed
77 my $real_dev = abs_path($path);
78 my ($rbd_id) = ($real_dev =~ m|/dev/rbd([0-9]+)$|);
79 my $dev_cluster_id = file_read_firstline("/sys/devices/rbd/${rbd_id}/cluster_fsid");
80 return $path if $cluster_id eq $dev_cluster_id;
81 }
cfe46e2d
AL
82 return $pve_path;
83}
84
6cc88e8e
AA
85my $build_cmd = sub {
86 my ($binary, $scfg, $storeid, $op, @options) = @_;
87
4050fcc1 88 my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
6cc88e8e
AA
89 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
90
91 my $cmd = [$binary, '-p', $pool];
92
92a7826f
TL
93 if (defined(my $namespace = $scfg->{namespace})) {
94 # some subcommands will fail if the --namespace parameter is present
95 my $no_namespace_parameter = {
96 unmap => 1,
97 };
98 push @$cmd, '--namespace', "$namespace" if !$no_namespace_parameter->{$op};
99 }
6cc88e8e
AA
100 push @$cmd, '-c', $cmd_option->{ceph_conf} if ($cmd_option->{ceph_conf});
101 push @$cmd, '-m', $cmd_option->{mon_host} if ($cmd_option->{mon_host});
102 push @$cmd, '--auth_supported', $cmd_option->{auth_supported} if ($cmd_option->{auth_supported});
103 push @$cmd, '-n', "client.$cmd_option->{userid}" if ($cmd_option->{userid});
104 push @$cmd, '--keyring', $cmd_option->{keyring} if ($cmd_option->{keyring});
105
106 push @$cmd, $op;
107
108 push @$cmd, @options if scalar(@options);
109
110 return $cmd;
111};
112
113my $rbd_cmd = sub {
114 my ($scfg, $storeid, $op, @options) = @_;
115
116 return $build_cmd->('/usr/bin/rbd', $scfg, $storeid, $op, @options);
117};
118
119my $rados_cmd = sub {
120 my ($scfg, $storeid, $op, @options) = @_;
121
122 return $build_cmd->('/usr/bin/rados', $scfg, $storeid, $op, @options);
123};
124
d86fd0a4 125# needed for volumes created using ceph jewel (or higher)
0ef8fb9d 126my $krbd_feature_update = sub {
d86fd0a4
FG
127 my ($scfg, $storeid, $name) = @_;
128
0ef8fb9d
TL
129 my (@disable, @enable);
130 my ($kmajor, $kminor) = PVE::ProcFSTools::kernel_version();
4c3b3085 131
0ef8fb9d
TL
132 if ($kmajor > 5 || $kmajor == 5 && $kminor >= 3) {
133 # 'deep-flatten' can only be disabled, not enabled after image creation
134 push @enable, 'fast-diff', 'object-map';
135 } else {
136 push @disable, 'fast-diff', 'object-map', 'deep-flatten';
137 }
138
139 if ($kmajor >= 5) {
140 push @enable, 'exclusive-lock';
141 } else {
142 push @disable, 'exclusive-lock';
143 }
4c3b3085 144
0ef8fb9d
TL
145 my $active_features_list = (rbd_volume_info($scfg, $storeid, $name))[4];
146 my $active_features = { map { $_ => 1 } @$active_features_list };
147
148 my $to_disable = join(',', grep { $active_features->{$_} } @disable);
149 my $to_enable = join(',', grep { !$active_features->{$_} } @enable );
150
151 if ($to_disable) {
152 print "disable RBD image features this kernel RBD drivers is not compatible with: $to_disable\n";
153 my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'disable', $name, $to_disable);
154 run_rbd_command(
155 $cmd,
156 errmsg => "could not disable krbd-incompatible image features '$to_disable' for rbd image: $name",
157 );
158 }
159 if ($to_enable) {
160 print "enable RBD image features this kernel RBD drivers supports: $to_enable\n";
161 eval {
162 my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'enable', $name, $to_enable);
163 run_rbd_command(
164 $cmd,
165 errmsg => "could not enable krbd-compatible image features '$to_enable' for rbd image: $name",
166 );
167 };
168 warn "$@" if $@;
4c3b3085 169 }
d86fd0a4
FG
170};
171
c693f749
SP
172sub run_rbd_command {
173 my ($cmd, %args) = @_;
174
175 my $lasterr;
176 my $errmsg = $args{errmsg} . ": " || "";
c97c5b3b 177 if (!exists($args{errfunc})) {
c693f749
SP
178 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
179 # at least 1 child(ren) in pool cephstor1
180 $args{errfunc} = sub {
c97c5b3b
DM
181 my $line = shift;
182 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
183 $lasterr = "$1\n";
184 } else {
185 $lasterr = $line;
186 }
187 print STDERR $lasterr;
188 *STDERR->flush();
189 };
190 }
6d0d0a97 191
c97c5b3b
DM
192 eval { run_command($cmd, %args); };
193 if (my $err = $@) {
194 die $errmsg . $lasterr if length($lasterr);
195 die $err;
c693f749
SP
196 }
197
c97c5b3b 198 return undef;
c693f749
SP
199}
200
411476cd
DM
201sub rbd_ls {
202 my ($scfg, $storeid) = @_;
d70e7f6c 203
1440604a 204 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
e9bc9931 205 $pool .= "/$scfg->{namespace}" if defined($scfg->{namespace});
d70e7f6c 206
89a8800b 207 my $raw = '';
1be93fe2 208 my $parser = sub { $raw .= shift };
8c3abf12 209
72bbd8a6 210 my $cmd = $rbd_cmd->($scfg, $storeid, 'ls', '-l', '--format', 'json');
8c3abf12 211 eval {
c693f749 212 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
8c3abf12
DM
213 };
214 my $err = $@;
215
216 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
89a8800b 217
00571710
DM
218 my $result;
219 if ($raw eq '') {
220 $result = [];
221 } elsif ($raw =~ m/^(\[.*\])$/s) { # untaint
222 $result = JSON::decode_json($1);
223 } else {
224 die "got unexpected data from rbd ls: '$raw'\n";
225 }
89a8800b
DC
226
227 my $list = {};
228
229 foreach my $el (@$result) {
230 next if defined($el->{snapshot});
231
232 my $image = $el->{image};
233
234 my ($owner) = $image =~ m/^(?:vm|base)-(\d+)-/;
aa14def4 235 next if !defined($owner);
89a8800b
DC
236
237 $list->{$pool}->{$image} = {
238 name => $image,
239 size => $el->{size},
240 parent => $get_parent_image_name->($el->{parent}),
241 vmid => $owner
242 };
243 }
244
411476cd 245 return $list;
0509010d
AD
246}
247
a573f66a
FG
248sub rbd_ls_snap {
249 my ($scfg, $storeid, $name) = @_;
250
72bbd8a6 251 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'ls', $name, '--format', 'json');
a573f66a
FG
252
253 my $raw = '';
254 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => sub { $raw .= shift; });
255
256 my $list;
257 if ($raw =~ m/^(\[.*\])$/s) { # untaint
258 $list = eval { JSON::decode_json($1) };
259 die "invalid JSON output from 'rbd snap ls $name': $@\n" if $@;
260 } else {
261 die "got unexpected data from 'rbd snap ls $name': '$raw'\n";
262 }
263
264 $list = [] if !defined($list);
265
266 my $res = {};
267 foreach my $el (@$list) {
268 my $snap = $el->{name};
269 my $protected = defined($el->{protected}) && $el->{protected} eq "true" ? 1 : undef;
270 $res->{$snap} = {
271 name => $snap,
272 id => $el->{id} // undef,
273 size => $el->{size} // 0,
274 protected => $protected,
275 };
276 }
277 return $res;
278}
279
62b98a65 280sub rbd_volume_info {
992e6835
AD
281 my ($scfg, $storeid, $volname, $snap) = @_;
282
283 my $cmd = undef;
284
89a8800b 285 my @options = ('info', $volname, '--format', 'json');
1be93fe2 286 if ($snap) {
89a8800b 287 push @options, '--snap', $snap;
992e6835 288 }
e110213e 289
72bbd8a6 290 $cmd = $rbd_cmd->($scfg, $storeid, @options);
89a8800b 291
89a8800b 292 my $raw = '';
1be93fe2 293 my $parser = sub { $raw .= shift };
e110213e 294
c693f749 295 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
1be93fe2 296
00571710
DM
297 my $volume;
298 if ($raw eq '') {
299 $volume = {};
300 } elsif ($raw =~ m/^(\{.*\})$/s) { # untaint
301 $volume = JSON::decode_json($1);
302 } else {
303 die "got unexpected data from rbd info: '$raw'\n";
304 }
1be93fe2 305
89a8800b 306 $volume->{parent} = $get_parent_image_name->($volume->{parent});
1be93fe2 307 $volume->{protected} = defined($volume->{protected}) && $volume->{protected} eq "true" ? 1 : undef;
e110213e 308
89a8800b 309 return $volume->@{qw(size parent format protected features)};
e110213e
AD
310}
311
d70d814c
CE
312sub rbd_volume_du {
313 my ($scfg, $storeid, $volname) = @_;
314
315 my @options = ('du', $volname, '--format', 'json');
316 my $cmd = $rbd_cmd->($scfg, $storeid, @options);
317
318 my $raw = '';
319 my $parser = sub { $raw .= shift };
320
321 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
322
323 my $volume;
324 if ($raw eq '') {
325 $volume = {};
326 } elsif ($raw =~ m/^(\{.*\})$/s) { # untaint
327 $volume = JSON::decode_json($1);
328 } else {
329 die "got unexpected data from rbd du: '$raw'\n";
330 }
331
332 if (!defined($volume->{images})) {
333 die "got no images from rbd du\n";
334 }
335
336 # `rbd du` returns array of images for name matching `volname`,
337 # including snapshots.
338 my $images = $volume->{images};
339 foreach my $image (@$images) {
340 next if defined($image->{snapshot});
341 next if !defined($image->{used_size}) || !defined($image->{name});
342
343 # Return `used_size` of first volume with matching name which
344 # is not a snapshot.
345 return $image->{used_size} if $image->{name} eq $volname;
346 }
347
348 die "got no matching image from rbd du\n";
349}
350
e5427b00 351# Configuration
0509010d 352
0509010d
AD
353sub type {
354 return 'rbd';
355}
356
357sub plugindata {
358 return {
1f79bb07 359 content => [ {images => 1, rootdir => 1}, { images => 1 }],
0509010d
AD
360 };
361}
362
363sub properties {
364 return {
e5427b00 365 monhost => {
0b9ef02e 366 description => "IP addresses of monitors (for external clusters).",
e858048f 367 type => 'string', format => 'pve-storage-portal-dns-list',
0509010d 368 },
e5427b00
AD
369 pool => {
370 description => "Pool.",
0509010d
AD
371 type => 'string',
372 },
ef2afce7
AL
373 'data-pool' => {
374 description => "Data Pool (for erasure coding only)",
375 type => 'string',
376 },
6d0d0a97 377 namespace => {
78eac5ba 378 description => "Namespace.",
e9bc9931
AL
379 type => 'string',
380 },
e5427b00
AD
381 username => {
382 description => "RBD Id.",
0509010d
AD
383 type => 'string',
384 },
e5427b00 385 authsupported => {
0509010d
AD
386 description => "Authsupported.",
387 type => 'string',
388 },
9f20a8a6 389 krbd => {
40d69893 390 description => "Always access rbd through krbd kernel module.",
9f20a8a6
AD
391 type => 'boolean',
392 },
22b68016
AL
393 keyring => {
394 description => "Client keyring contents (for external clusters).",
395 type => 'string',
396 },
0509010d
AD
397 };
398}
399
400sub options {
401 return {
35d6dfaf
AD
402 nodes => { optional => 1 },
403 disable => { optional => 1 },
0b9ef02e 404 monhost => { optional => 1},
1440604a 405 pool => { optional => 1 },
ef2afce7 406 'data-pool' => { optional => 1 },
e9bc9931 407 namespace => { optional => 1 },
1440604a 408 username => { optional => 1 },
0509010d 409 content => { optional => 1 },
9f20a8a6 410 krbd => { optional => 1 },
22b68016 411 keyring => { optional => 1 },
9edb99a5 412 bwlimit => { optional => 1 },
0509010d
AD
413 };
414}
415
416# Storage implementation
417
2e109b4b
TL
418sub on_add_hook {
419 my ($class, $storeid, $scfg, %param) = @_;
420
c698646f 421 PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $param{keyring});
22b68016
AL
422
423 return;
424}
2e109b4b 425
22b68016
AL
426sub 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 }
f3ccd0ef
FE
436
437 return;
2e109b4b
TL
438}
439
440sub on_delete_hook {
441 my ($class, $storeid, $scfg) = @_;
4050fcc1 442 PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
f3ccd0ef 443 return;
2e109b4b
TL
444}
445
0509010d
AD
446sub parse_volname {
447 my ($class, $volname) = @_;
448
d04c7e55 449 if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
7800e84d 450 return ('images', $4, $7, $2, $3, $5, 'raw');
0509010d
AD
451 }
452
453 die "unable to parse rbd volume name '$volname'\n";
454}
455
456sub path {
38e6ec3f 457 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
0509010d 458
4050fcc1 459 my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
0509010d 460 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
38e6ec3f 461 $name .= '@'.$snapname if $snapname;
0509010d 462
e8e47711
FE
463 if ($scfg->{krbd}) {
464 my $rbd_dev_path = get_rbd_dev_path($scfg, $storeid, $name);
465 return ($rbd_dev_path, $vmid, $vtype);
466 }
33cef4c8 467
cfe46e2d 468 my $rbd_path = get_rbd_path($scfg, $name);
aeb007cb 469 my $path = "rbd:${rbd_path}";
6eebc4a7 470
5fc02afb
FG
471 $path .= ":conf=$cmd_option->{ceph_conf}" if $cmd_option->{ceph_conf};
472 if (defined($scfg->{monhost})) {
4050fcc1 473 my $monhost = PVE::CephConfig::hostlist($scfg->{monhost}, ';');
6eebc4a7
FG
474 $monhost =~ s/:/\\:/g;
475 $path .= ":mon_host=$monhost";
6cc88e8e 476 $path .= ":auth_supported=$cmd_option->{auth_supported}";
6eebc4a7
FG
477 }
478
6cc88e8e 479 $path .= ":id=$cmd_option->{userid}:keyring=$cmd_option->{keyring}" if ($cmd_option->{keyring});
13417225 480
0509010d
AD
481 return ($path, $vmid, $vtype);
482}
483
a44c0147
FE
484sub find_free_diskname {
485 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
5b9b9b14 486
72bbd8a6 487 my $cmd = $rbd_cmd->($scfg, $storeid, 'ls');
e9bc9931 488
c4a29df4 489 my $disk_list = [];
5b9b9b14 490
53a236f2
FG
491 my $parser = sub {
492 my $line = shift;
dd9e97ed 493 if ($line =~ m/^(.*)$/) { # untaint
00571710
DM
494 push @$disk_list, $1;
495 }
53a236f2
FG
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
c4a29df4 505 return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, undef, $scfg);
a44c0147 506}
5b9b9b14 507
5eab0272
DM
508sub create_base {
509 my ($class, $storeid, $scfg, $volname) = @_;
510
992e6835
AD
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
72bbd8a6 531 my $cmd = $rbd_cmd->(
aeb007cb
AL
532 $scfg,
533 $storeid,
534 'rename',
4cf696f6
TL
535 get_rbd_path($scfg, $name),
536 get_rbd_path($scfg, $newname),
aeb007cb 537 );
c693f749 538 run_rbd_command($cmd, errmsg => "rbd rename '$name' error");
992e6835 539
95947178
FE
540 eval { $class->unmap_volume($storeid, $scfg, $volname); };
541 warn $@ if $@;
542
992e6835
AD
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){
72bbd8a6 550 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
2362bc87 551 run_rbd_command($cmd, errmsg => "rbd protect $newname snap '$snap' error");
992e6835
AD
552 }
553
554 return $newvolname;
555
5eab0272
DM
556}
557
558sub clone_image {
f236eaf8 559 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
5eab0272 560
f2708285 561 my $snap = '__base__';
f236eaf8 562 $snap = $snapname if length $snapname;
f2708285
AD
563
564 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
565 $class->parse_volname($volname);
566
63da6d79
DM
567 die "$volname is not a base image and snapname is not provided\n"
568 if !$isBase && !length($snapname);
f2708285 569
a44c0147 570 my $name = $class->find_free_diskname($storeid, $scfg, $vmid);
f2708285 571
f236eaf8
SP
572 warn "clone $volname: $basename snapname $snap to $name\n";
573
63da6d79 574 if (length($snapname)) {
f236eaf8
SP
575 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $volname, $snapname);
576
63da6d79 577 if (!$protected) {
72bbd8a6 578 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
f236eaf8
SP
579 run_rbd_command($cmd, errmsg => "rbd protect $volname snap $snapname error");
580 }
581 }
f2708285
AD
582
583 my $newvol = "$basename/$name";
63da6d79
DM
584 $newvol = $name if length($snapname);
585
c915afca
TL
586 my @options = (
587 get_rbd_path($scfg, $basename),
588 '--snap', $snap,
589 );
ef2afce7 590 push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'};
f2708285 591
c915afca 592 my $cmd = $rbd_cmd->($scfg, $storeid, 'clone', @options, get_rbd_path($scfg, $name));
c693f749 593 run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
f2708285
AD
594
595 return $newvol;
5eab0272
DM
596}
597
0509010d
AD
598sub alloc_image {
599 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
600
601
292a33fd 602 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
0509010d 603 if $name && $name !~ m/^vm-$vmid-/;
0509010d 604
a44c0147 605 $name = $class->find_free_diskname($storeid, $scfg, $vmid) if !$name;
0509010d 606
c915afca
TL
607 my @options = (
608 '--image-format' , 2,
609 '--size', int(($size + 1023) / 1024),
610 );
ef2afce7 611 push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'};
c915afca
TL
612
613 my $cmd = $rbd_cmd->($scfg, $storeid, 'create', @options, $name);
3c931155 614 run_rbd_command($cmd, errmsg => "rbd create '$name' error");
0509010d
AD
615
616 return $name;
617}
618
619sub free_image {
32437ed2 620 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
0509010d 621
42d07b9a
AD
622 my ($vtype, $name, $vmid, undef, undef, undef) =
623 $class->parse_volname($volname);
624
e9bc9931 625
a573f66a
FG
626 my $snaps = rbd_ls_snap($scfg, $storeid, $name);
627 foreach my $snap (keys %$snaps) {
628 if ($snaps->{$snap}->{protected}) {
72bbd8a6 629 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
2362bc87 630 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
42d07b9a
AD
631 }
632 }
633
515ef80b
WL
634 $class->deactivate_volume($storeid, $scfg, $volname);
635
72bbd8a6 636 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'purge', $name);
85043c01 637 run_rbd_command($cmd, errmsg => "rbd snap purge '$name' error");
c30470a3 638
72bbd8a6 639 $cmd = $rbd_cmd->($scfg, $storeid, 'rm', $name);
85043c01 640 run_rbd_command($cmd, errmsg => "rbd rm '$name' error");
0509010d
AD
641
642 return undef;
643}
644
645sub list_images {
646 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
647
e5427b00 648 $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd};
ed7ea5a3 649
c27fe648 650 my $dat = $cache->{rbd}->{get_rbd_path($scfg)};
ed7ea5a3 651 return [] if !$dat; # nothing found
cfd58f1f 652
ed7ea5a3
TL
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 }
0509010d 663
ed7ea5a3
TL
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 }
0509010d 670
ed7ea5a3 671 $info->{format} = 'raw';
0509010d 672
ed7ea5a3 673 push @$res, $info;
0509010d 674 }
ed7ea5a3 675
411476cd 676 return $res;
0509010d
AD
677}
678
0509010d
AD
679sub status {
680 my ($class, $storeid, $scfg, $cache) = @_;
681
72bbd8a6 682 my $rados = $librados_connect->($scfg, $storeid);
41aacc6c 683 my $df = $rados->mon_command({ prefix => 'df', format => 'json' });
69589444 684
e4671f73 685 my $pool = $scfg->{'data-pool'} // $scfg->{pool} // 'rbd';
0c317c6c
SI
686
687 my ($d) = grep { $_->{name} eq $pool } @{$df->{pools}};
69589444 688
ae931633
SI
689 if (!defined($d)) {
690 warn "could not get usage stats for pool '$pool'\n";
691 return;
692 }
693
41aacc6c
AA
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};
e79ab52c 697 my $used = $d->{stats}->{stored} // $d->{stats}->{bytes_used};
41aacc6c 698 my $total = $used + $free;
0509010d 699 my $active = 1;
0509010d 700
411476cd 701 return ($total, $free, $used, $active);
0509010d
AD
702}
703
704sub activate_storage {
705 my ($class, $storeid, $scfg, $cache) = @_;
706 return 1;
707}
708
709sub deactivate_storage {
710 my ($class, $storeid, $scfg, $cache) = @_;
711 return 1;
712}
713
40d69893
DM
714sub map_volume {
715 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
9f20a8a6 716
518f3908
FG
717 my ($vtype, $img_name, $vmid) = $class->parse_volname($volname);
718
719 my $name = $img_name;
40d69893
DM
720 $name .= '@'.$snapname if $snapname;
721
cc682faa 722 my $kerneldev = get_rbd_dev_path($scfg, $storeid, $name);
40d69893
DM
723
724 return $kerneldev if -b $kerneldev; # already mapped
725
518f3908
FG
726 # features can only be enabled/disabled for image, not for snapshot!
727 $krbd_feature_update->($scfg, $storeid, $img_name);
9f20a8a6 728
72bbd8a6 729 my $cmd = $rbd_cmd->($scfg, $storeid, 'map', $name);
40d69893
DM
730 run_rbd_command($cmd, errmsg => "can't map rbd volume $name");
731
732 return $kerneldev;
733}
734
735sub 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
cc682faa 741 my $kerneldev = get_rbd_dev_path($scfg, $storeid, $name);
40d69893
DM
742
743 if (-b $kerneldev) {
72bbd8a6 744 my $cmd = $rbd_cmd->($scfg, $storeid, 'unmap', $kerneldev);
40d69893
DM
745 run_rbd_command($cmd, errmsg => "can't unmap rbd device $kerneldev");
746 }
9f20a8a6 747
0509010d
AD
748 return 1;
749}
750
40d69893 751sub activate_volume {
02e797b8 752 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
9f20a8a6 753
40d69893 754 $class->map_volume($storeid, $scfg, $volname, $snapname) if $scfg->{krbd};
9f20a8a6 755
40d69893
DM
756 return 1;
757}
9f20a8a6 758
40d69893
DM
759sub deactivate_volume {
760 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
b50812f9 761
40d69893 762 $class->unmap_volume($storeid, $scfg, $volname, $snapname);
9f20a8a6 763
0509010d
AD
764 return 1;
765}
766
0002d9cc
AD
767sub volume_size_info {
768 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
769
81d1d017 770 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
d70d814c
CE
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;
0002d9cc
AD
774}
775
e7a42a76
AD
776sub volume_resize {
777 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
778
40d69893 779 return 1 if $running && !$scfg->{krbd}; # FIXME???
e7a42a76 780
478fc06c
AD
781 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
782
93e34f72 783 my $cmd = $rbd_cmd->($scfg, $storeid, 'resize', '--size', ceil($size/1024/1024), $name);
c693f749 784 run_rbd_command($cmd, errmsg => "rbd resize '$volname' error");
e7a42a76
AD
785 return undef;
786}
787
788dd8e1 788sub volume_snapshot {
f5640e7d 789 my ($class, $scfg, $storeid, $volname, $snap) = @_;
788dd8e1 790
9af33ed0
AD
791 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
792
72bbd8a6 793 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
c693f749 794 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
788dd8e1
AD
795 return undef;
796}
797
5a2b2e2f
AD
798sub volume_snapshot_rollback {
799 my ($class, $scfg, $storeid, $volname, $snap) = @_;
800
c6ce2cc8
AD
801 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
802
72bbd8a6 803 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
2362bc87 804 run_rbd_command($cmd, errmsg => "rbd snapshot $volname to '$snap' error");
5a2b2e2f
AD
805}
806
cce29bcd
AD
807sub volume_snapshot_delete {
808 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
809
399581a2
WB
810 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
811
c78cb110
AD
812 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
813
f90a0a20
SP
814 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
815 if ($protected){
72bbd8a6 816 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
f90a0a20
SP
817 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
818 }
819
72bbd8a6 820 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
c693f749
SP
821
822 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
823
cce29bcd
AD
824 return undef;
825}
826
2c036838 827sub volume_snapshot_needs_fsfreeze {
2c036838
SI
828 return 1;
829}
830
774f21b9
AD
831sub volume_has_feature {
832 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
833
834 my $features = {
5649ccfe 835 snapshot => { current => 1, snap => 1},
44c3689a 836 clone => { base => 1, snap => 1},
5649ccfe
AD
837 template => { current => 1},
838 copy => { base => 1, current => 1, snap => 1},
baafddbd 839 sparseinit => { base => 1, current => 1},
95dfa44c 840 rename => {current => 1},
774f21b9
AD
841 };
842
6d0d0a97 843 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = $class->parse_volname($volname);
1e7ae581
AD
844
845 my $key = undef;
6d0d0a97 846 if ($snapname){
2c5a7097 847 $key = 'snap';
6d0d0a97
TL
848 } else {
849 $key = $isBase ? 'base' : 'current';
1e7ae581
AD
850 }
851 return 1 if $features->{$feature}->{$key};
774f21b9
AD
852
853 return undef;
854}
855
95dfa44c
AL
856sub 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
95947178
FE
884 eval { $class->unmap_volume($storeid, $scfg, $source_volname); };
885 warn $@ if $@;
886
95dfa44c
AL
887 $base_name = $base_name ? "${base_name}/" : '';
888
889 return "${storeid}:${base_name}${target_volname}";
890}
891
0509010d 8921;