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