]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/RBDPlugin.pm
bd8c28d86e3f25815c42e0960d038015974b2428
1 package PVE
::Storage
::RBDPlugin
;
7 use PVE
::Tools
qw(run_command trim);
8 use PVE
::Storage
::Plugin
;
9 use PVE
::JSONSchema
qw(get_standard_option);
11 use PVE
::Storage
::CephTools
;
14 use base
qw(PVE::Storage::Plugin);
16 my $get_parent_image_name = sub {
18 return undef if !$parent;
19 return $parent->{image
} . "@" . $parent->{snapshot
};
22 my $add_pool_to_disk = sub {
23 my ($scfg, $disk) = @_;
25 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
31 my ($binary, $scfg, $storeid, $op, @options) = @_;
33 my $cmd_option = PVE
::Storage
::CephTools
::ceph_connect_option
($scfg, $storeid);
34 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
36 my $cmd = [$binary, '-p', $pool];
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
});
46 push @$cmd, @options if scalar(@options);
52 my ($scfg, $storeid, $op, @options) = @_;
54 return $build_cmd->('/usr/bin/rbd', $scfg, $storeid, $op, @options);
58 my ($scfg, $storeid, $op, @options) = @_;
60 return $build_cmd->('/usr/bin/rados', $scfg, $storeid, $op, @options);
63 my $librados_connect = sub {
64 my ($scfg, $storeid, $options) = @_;
66 my $librados_config = PVE
::Storage
::CephTools
::ceph_connect_option
($scfg, $storeid);
68 my $rados = PVE
::RADOS-
>new(%$librados_config);
73 # needed for volumes created using ceph jewel (or higher)
74 my $krbd_feature_disable = sub {
75 my ($scfg, $storeid, $name) = @_;
77 return 1 if !$scfg->{krbd
};
79 my ($major, undef, undef, undef) = ceph_version
();
80 return 1 if $major < 10;
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);
85 my $active_features = { map { $_ => 1 } $features };
86 my $incompatible_features = join(',', grep { %$active_features{$_} } @$krbd_feature_blacklist);
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");
94 my $ceph_version_parser = sub {
96 if ($line =~ m/^ceph version ((\d+)\.(\d+)\.(\d+))(?: \([a-fA-F0-9]+\))/) {
97 return ($2, $3, $4, $1);
99 warn "Could not parse Ceph version: '$line'\n";
106 my $version_string = $cache;
112 if (defined($version_string)) {
113 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($version_string);
115 run_command
('ceph --version', outfunc
=> sub {
117 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($line);
120 return undef if !defined($version_string);
121 return wantarray ?
($major, $minor, $bugfix, $version_string) : $version_string;
124 sub run_rbd_command
{
125 my ($cmd, %args) = @_;
128 my $errmsg = $args{errmsg
} . ": " || "";
129 if (!exists($args{errfunc
})) {
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 {
134 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
139 print STDERR
$lasterr;
144 eval { run_command
($cmd, %args); };
146 die $errmsg . $lasterr if length($lasterr);
154 my ($scfg, $storeid) = @_;
156 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l', '--format', 'json');
157 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
160 my $parser = sub { $raw .= shift };
163 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
167 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
169 my $result = $raw ne '' ? JSON
::decode_json
($raw) : [];
173 foreach my $el (@$result) {
174 next if defined($el->{snapshot
});
176 my $image = $el->{image
};
178 my ($owner) = $image =~ m/^(?:vm|base)-(\d+)-/;
179 next if !defined($owner);
181 $list->{$pool}->{$image} = {
184 parent
=> $get_parent_image_name->($el->{parent
}),
192 sub rbd_volume_info
{
193 my ($scfg, $storeid, $volname, $snap) = @_;
197 my @options = ('info', $volname, '--format', 'json');
199 push @options, '--snap', $snap;
202 $cmd = &$rbd_cmd($scfg, $storeid, @options);
205 my $parser = sub { $raw .= shift };
207 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
209 my $volume = $raw ne '' ? JSON
::decode_json
($raw) : {};
211 $volume->{parent
} = $get_parent_image_name->($volume->{parent
});
212 $volume->{protected
} = defined($volume->{protected
}) && $volume->{protected
} eq "true" ?
1 : undef;
214 return $volume->@{qw(size parent format protected features)};
225 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
232 description
=> "IP addresses of monitors (for external clusters).",
233 type
=> 'string', format
=> 'pve-storage-portal-dns-list',
236 description
=> "Pool.",
240 description
=> "RBD Id.",
244 description
=> "Authsupported.",
248 description
=> "Access rbd through krbd kernel module.",
256 nodes
=> { optional
=> 1 },
257 disable
=> { optional
=> 1 },
258 monhost
=> { optional
=> 1},
259 pool
=> { optional
=> 1 },
260 username
=> { optional
=> 1 },
261 content
=> { optional
=> 1 },
262 krbd
=> { optional
=> 1 },
263 bwlimit
=> { optional
=> 1 },
267 # Storage implementation
270 my ($class, $storeid, $scfg, %param) = @_;
272 return if defined($scfg->{monhost
}); # nothing to do if not pve managed ceph
274 PVE
::Storage
::CephTools
::ceph_create_keyfile
($scfg->{type
}, $storeid);
278 my ($class, $storeid, $scfg) = @_;
280 return if defined($scfg->{monhost
}); # nothing to do if not pve managed ceph
282 PVE
::Storage
::CephTools
::ceph_remove_keyfile
($scfg->{type
}, $storeid);
286 my ($class, $volname) = @_;
288 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
289 return ('images', $4, $7, $2, $3, $5, 'raw');
292 die "unable to parse rbd volume name '$volname'\n";
296 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
298 my $cmd_option = PVE
::Storage
::CephTools
::ceph_connect_option
($scfg, $storeid);
299 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
300 $name .= '@'.$snapname if $snapname;
302 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
303 return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd
};
305 my $path = "rbd:$pool/$name";
307 $path .= ":conf=$cmd_option->{ceph_conf}" if $cmd_option->{ceph_conf
};
308 if (defined($scfg->{monhost
})) {
309 my $monhost = PVE
::Storage
::CephTools
::hostlist
($scfg->{monhost
}, ';');
310 $monhost =~ s/:/\\:/g;
311 $path .= ":mon_host=$monhost";
312 $path .= ":auth_supported=$cmd_option->{auth_supported}";
315 $path .= ":id=$cmd_option->{userid}:keyring=$cmd_option->{keyring}" if ($cmd_option->{keyring
});
317 return ($path, $vmid, $vtype);
320 my $find_free_diskname = sub {
321 my ($storeid, $scfg, $vmid) = @_;
323 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls');
329 if ($line =~ m/^(vm|base)-\Q$vmid\E+-disk-(\d+)$/) {
335 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
339 die $err if $err && $err !~ m/doesn't contain rbd images/;
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";
348 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
352 my ($class, $storeid, $scfg, $volname) = @_;
354 my $snap = '__base__';
356 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
357 $class->parse_volname($volname);
359 die "create_base not possible with base image\n" if $isBase;
361 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
362 die "rbd volume info on '$name' failed\n" if !($size);
364 die "rbd image must be at format V2" if $format ne "2";
366 die "volname '$volname' contains wrong information about parent $parent $basename\n"
367 if $basename && (!$parent || $parent ne $basename."@".$snap);
370 $newname =~ s/^vm-/base-/;
372 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
374 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
375 run_rbd_command
($cmd, errmsg
=> "rbd rename '$name' error");
377 my $running = undef; #fixme : is create_base always offline ?
379 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
381 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
384 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
385 run_rbd_command
($cmd, errmsg
=> "rbd protect $newname snap '$snap' error");
393 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
395 my $snap = '__base__';
396 $snap = $snapname if length $snapname;
398 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
399 $class->parse_volname($volname);
401 die "$volname is not a base image and snapname is not provided\n"
402 if !$isBase && !length($snapname);
404 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
406 warn "clone $volname: $basename snapname $snap to $name\n";
408 if (length($snapname)) {
409 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $volname, $snapname);
412 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
413 run_rbd_command
($cmd, errmsg
=> "rbd protect $volname snap $snapname error");
417 my $newvol = "$basename/$name";
418 $newvol = $name if length($snapname);
420 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
421 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
423 run_rbd_command
($cmd, errmsg
=> "rbd clone '$basename' error");
425 &$krbd_feature_disable($scfg, $storeid, $name);
431 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
434 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
435 if $name && $name !~ m/^vm-$vmid-/;
437 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
439 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
440 run_rbd_command
($cmd, errmsg
=> "rbd create $name' error");
442 &$krbd_feature_disable($scfg, $storeid, $name);
448 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
450 my ($vtype, $name, $vmid, undef, undef, undef) =
451 $class->parse_volname($volname);
454 my $snap = '__base__';
455 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
457 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
458 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
462 $class->deactivate_volume($storeid, $scfg, $volname);
464 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
465 run_rbd_command
($cmd, errmsg
=> "rbd snap purge '$volname' error");
467 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
468 run_rbd_command
($cmd, errmsg
=> "rbd rm '$volname' error");
474 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
476 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
477 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
481 if (my $dat = $cache->{rbd
}->{$pool}) {
482 foreach my $image (keys %$dat) {
484 my $info = $dat->{$image};
486 my $volname = $info->{name
};
487 my $parent = $info->{parent
};
488 my $owner = $info->{vmid
};
490 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
491 $info->{volid
} = "$storeid:$1/$volname";
493 $info->{volid
} = "$storeid:$volname";
497 my $found = grep { $_ eq $info->{volid
} } @$vollist;
500 next if defined ($vmid) && ($owner ne $vmid);
503 $info->{format
} = 'raw';
513 my ($class, $storeid, $scfg, $cache) = @_;
516 my $rados = &$librados_connect($scfg, $storeid);
517 my $df = $rados->mon_command({ prefix
=> 'df', format
=> 'json' });
519 my ($d) = grep { $_->{name
} eq $scfg->{pool
} } @{$df->{pools
}};
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;
528 return ($total, $free, $used, $active);
531 sub activate_storage
{
532 my ($class, $storeid, $scfg, $cache) = @_;
536 sub deactivate_storage
{
537 my ($class, $storeid, $scfg, $cache) = @_;
541 sub activate_volume
{
542 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
544 return 1 if !$scfg->{krbd
};
546 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
547 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
549 my $path = "/dev/rbd/$pool/$name";
550 $path .= '@'.$snapname if $snapname;
553 $name .= '@'.$snapname if $snapname;
554 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
555 run_rbd_command
($cmd, errmsg
=> "can't mount rbd volume $name");
560 sub deactivate_volume
{
561 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
563 return 1 if !$scfg->{krbd
};
565 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
566 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
568 my $path = "/dev/rbd/$pool/$name";
569 $path .= '@'.$snapname if $snapname;
570 return if ! -b
$path;
572 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
573 run_rbd_command
($cmd, errmsg
=> "can't unmap rbd volume $name");
578 sub volume_size_info
{
579 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
581 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
582 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
587 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
589 return 1 if $running && !$scfg->{krbd
};
591 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
593 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
594 run_rbd_command
($cmd, errmsg
=> "rbd resize '$volname' error");
598 sub volume_snapshot
{
599 my ($class, $scfg, $storeid, $volname, $snap) = @_;
601 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
603 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
604 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
608 sub volume_snapshot_rollback
{
609 my ($class, $scfg, $storeid, $volname, $snap) = @_;
611 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
613 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
614 run_rbd_command
($cmd, errmsg
=> "rbd snapshot $volname to '$snap' error");
617 sub volume_snapshot_delete
{
618 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
620 return 1 if $running && !$scfg->{krbd
};
622 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
624 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
626 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
628 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
629 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
632 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
634 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
639 sub volume_has_feature
{
640 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
643 snapshot
=> { current
=> 1, snap
=> 1},
644 clone
=> { base
=> 1, snap
=> 1},
645 template
=> { current
=> 1},
646 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
647 sparseinit
=> { base
=> 1, current
=> 1},
650 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
651 $class->parse_volname($volname);
657 $key = $isBase ?
'base' : 'current';
659 return 1 if $features->{$feature}->{$key};