]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/RBDPlugin.pm
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 base
qw(PVE::Storage::Plugin);
13 my $pveceph_config = '/etc/pve/ceph.conf';
15 my $rbd_unittobytes = {
18 "G" => 1024*1024*1024,
19 "T" => 1024*1024*1024*1024,
22 my $add_pool_to_disk = sub {
23 my ($scfg, $disk) = @_;
25 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
31 my ($list_text, $separator) = @_;
33 my @monhostlist = PVE
::Tools
::split_list
($list_text);
34 return join($separator, map {
35 my ($host, $port) = PVE
::Tools
::parse_host_and_port
($_);
36 $port = defined($port) ?
":$port" : '';
37 $host = "[$host]" if Net
::IP
::ip_is_ipv6
($host);
43 my ($binary, $scfg, $storeid, $op, @options) = @_;
45 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
46 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
47 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
49 my $cmd = [$binary, '-p', $pool];
50 my $pveceph_managed = !defined($scfg->{monhost
});
52 if ($pveceph_managed) {
53 push @$cmd, '-c', $pveceph_config;
55 push @$cmd, '-m', $hostlist->($scfg->{monhost
}, ',');
56 push @$cmd, '--auth_supported', -e
$keyring ?
'cephx' : 'none';
60 push @$cmd, '-n', "client.$username";
61 push @$cmd, '--keyring', $keyring;
64 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
67 if ($pveceph_managed) {
68 warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
70 push @$cmd, '-c', $cephconfig;
76 push @$cmd, @options if scalar(@options);
82 my ($scfg, $storeid, $op, @options) = @_;
84 return $build_cmd->('/usr/bin/rbd', $scfg, $storeid, $op, @options);
88 my ($scfg, $storeid, $op, @options) = @_;
90 return $build_cmd->('/usr/bin/rados', $scfg, $storeid, $op, @options);
93 # needed for volumes created using ceph jewel (or higher)
94 my $krdb_feature_disable = sub {
95 my ($scfg, $storeid, $name) = @_;
97 return 1 if !$scfg->{krbd
};
99 my ($major, undef, undef, undef) = ceph_version
();
100 return 1 if $major < 10;
102 my $feature_cmd = &$rbd_cmd($scfg, $storeid, 'feature', 'disable', $name, 'deep-flatten,fast-diff,object-map,exclusive-lock');
103 run_rbd_command
($feature_cmd, errmsg
=> "could not disable krbd-incompatible image features of rbd volume $name");
106 my $ceph_version_parser = sub {
108 if ($line =~ m/^ceph version ((\d+)\.(\d+)\.(\d+))(?: \([a-fA-F0-9]+\))/) {
109 return ($2, $3, $4, $1);
111 warn "Could not parse Ceph version: '$line'\n";
118 my $version_string = $cache;
124 if (defined($version_string)) {
125 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($version_string);
127 run_command
('ceph --version', outfunc
=> sub {
129 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($line);
132 return undef if !defined($version_string);
133 return wantarray ?
($major, $minor, $bugfix, $version_string) : $version_string;
136 sub run_rbd_command
{
137 my ($cmd, %args) = @_;
140 my $errmsg = $args{errmsg
} . ": " || "";
141 if (!exists($args{errfunc
})) {
142 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
143 # at least 1 child(ren) in pool cephstor1
144 $args{errfunc
} = sub {
146 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
151 print STDERR
$lasterr;
156 eval { run_command
($cmd, %args); };
158 die $errmsg . $lasterr if length($lasterr);
166 my ($scfg, $storeid) = @_;
168 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l');
169 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
176 if ($line =~ m/^((vm|base)-(\d+)-\S+)\s+(\d+)(k|M|G|T)\s((\S+)\/((vm
|base
)-\d
+-\S
+@\S+))?
/) {
177 my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8);
178 return if $image =~ /@/; #skip snapshots
180 $list->{$pool}->{$image} = {
182 size
=> $size*$rbd_unittobytes->{$unit},
190 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
194 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
199 sub rbd_volume_info
{
200 my ($scfg, $storeid, $volname, $snap) = @_;
205 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
207 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
213 my $protected = undef;
218 if ($line =~ m/size (\d+) (k|M|G|T)B in (\d+) objects/) {
219 $size = $1 * $rbd_unittobytes->{$2} if ($1);
220 } elsif ($line =~ m/parent:\s(\S+)\/(\S
+)/) {
222 } elsif ($line =~ m/format:\s(\d+)/) {
224 } elsif ($line =~ m/protected:\s(\S+)/) {
225 $protected = 1 if $1 eq "True";
230 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
232 return ($size, $parent, $format, $protected);
243 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
250 description
=> "IP addresses of monitors (for external clusters).",
251 type
=> 'string', format
=> 'pve-storage-portal-dns-list',
254 description
=> "Pool.",
258 description
=> "RBD Id.",
262 description
=> "Authsupported.",
266 description
=> "Access rbd through krbd kernel module.",
274 nodes
=> { optional
=> 1 },
275 disable
=> { optional
=> 1 },
276 monhost
=> { optional
=> 1},
277 pool
=> { optional
=> 1 },
278 username
=> { optional
=> 1 },
279 content
=> { optional
=> 1 },
280 krbd
=> { optional
=> 1 },
281 bwlimit
=> { optional
=> 1 },
285 # Storage implementation
288 my ($class, $volname) = @_;
290 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
291 return ('images', $4, $7, $2, $3, $5, 'raw');
294 die "unable to parse rbd volume name '$volname'\n";
298 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
300 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
301 $name .= '@'.$snapname if $snapname;
303 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
304 return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd
};
306 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
308 my $path = "rbd:$pool/$name";
309 my $pveceph_managed = !defined($scfg->{monhost
});
310 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
312 if ($pveceph_managed) {
313 $path .= ":conf=$pveceph_config";
315 my $monhost = $hostlist->($scfg->{monhost
}, ';');
316 $monhost =~ s/:/\\:/g;
317 $path .= ":mon_host=$monhost";
318 $path .= -e
$keyring ?
":auth_supported=cephx" : ":auth_supported=none";
321 $path .= ":id=$username:keyring=$keyring" if -e
$keyring;
323 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
325 if (-e
$cephconfig) {
326 if ($pveceph_managed) {
327 warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
329 $path .= ":conf=$cephconfig";
333 return ($path, $vmid, $vtype);
336 my $find_free_diskname = sub {
337 my ($storeid, $scfg, $vmid) = @_;
339 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls');
345 if ($line =~ m/^(vm|base)-\Q$vmid\E+-disk-(\d+)$/) {
351 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
355 die $err if $err && $err !~ m/doesn't contain rbd images/;
357 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
358 for (my $i = 1; $i < 100; $i++) {
359 if (!$disk_ids->{$i}) {
360 return "vm-$vmid-disk-$i";
364 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
368 my ($class, $storeid, $scfg, $volname) = @_;
370 my $snap = '__base__';
372 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
373 $class->parse_volname($volname);
375 die "create_base not possible with base image\n" if $isBase;
377 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
378 die "rbd volume info on '$name' failed\n" if !($size);
380 die "rbd image must be at format V2" if $format ne "2";
382 die "volname '$volname' contains wrong information about parent $parent $basename\n"
383 if $basename && (!$parent || $parent ne $basename."@".$snap);
386 $newname =~ s/^vm-/base-/;
388 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
390 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
391 run_rbd_command
($cmd, errmsg
=> "rbd rename '$name' error");
393 my $running = undef; #fixme : is create_base always offline ?
395 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
397 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
400 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
401 run_rbd_command
($cmd, errmsg
=> "rbd protect $newname snap '$snap' error");
409 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
411 my $snap = '__base__';
412 $snap = $snapname if length $snapname;
414 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
415 $class->parse_volname($volname);
417 die "$volname is not a base image and snapname is not provided\n"
418 if !$isBase && !length($snapname);
420 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
422 warn "clone $volname: $basename snapname $snap to $name\n";
424 if (length($snapname)) {
425 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $volname, $snapname);
428 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
429 run_rbd_command
($cmd, errmsg
=> "rbd protect $volname snap $snapname error");
433 my $newvol = "$basename/$name";
434 $newvol = $name if length($snapname);
436 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
437 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
439 run_rbd_command
($cmd, errmsg
=> "rbd clone '$basename' error");
441 &$krdb_feature_disable($scfg, $storeid, $name);
447 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
450 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
451 if $name && $name !~ m/^vm-$vmid-/;
453 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
455 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
456 run_rbd_command
($cmd, errmsg
=> "rbd create $name' error");
458 &$krdb_feature_disable($scfg, $storeid, $name);
464 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
466 my ($vtype, $name, $vmid, undef, undef, undef) =
467 $class->parse_volname($volname);
470 my $snap = '__base__';
471 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
473 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
474 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
478 $class->deactivate_volume($storeid, $scfg, $volname);
480 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
481 run_rbd_command
($cmd, errmsg
=> "rbd snap purge '$volname' error");
483 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
484 run_rbd_command
($cmd, errmsg
=> "rbd rm '$volname' error");
490 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
492 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
493 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
497 if (my $dat = $cache->{rbd
}->{$pool}) {
498 foreach my $image (keys %$dat) {
500 my $info = $dat->{$image};
502 my $volname = $info->{name
};
503 my $parent = $info->{parent
};
504 my $owner = $info->{vmid
};
506 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
507 $info->{volid
} = "$storeid:$1/$volname";
509 $info->{volid
} = "$storeid:$volname";
513 my $found = grep { $_ eq $info->{volid
} } @$vollist;
516 next if defined ($vmid) && ($owner ne $vmid);
519 $info->{format
} = 'raw';
529 my ($class, $storeid, $scfg, $cache) = @_;
531 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
537 if ($line =~ m/^\s*total(?:\s|_)(\S+)\s+(\d+)(k|M|G|T)?/) {
539 # luminous has units here..
541 $stats->{$1} *= $rbd_unittobytes->{$3}/1024;
547 run_rbd_command
($cmd, errmsg
=> "rados error", errfunc
=> sub {}, outfunc
=> $parser);
550 my $total = $stats->{space
} ?
$stats->{space
}*1024 : 0;
551 my $free = $stats->{avail
} ?
$stats->{avail
}*1024 : 0;
552 my $used = $stats->{used
} ?
$stats->{used
}*1024: 0;
555 return ($total, $free, $used, $active);
558 sub activate_storage
{
559 my ($class, $storeid, $scfg, $cache) = @_;
563 sub deactivate_storage
{
564 my ($class, $storeid, $scfg, $cache) = @_;
568 sub activate_volume
{
569 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
571 return 1 if !$scfg->{krbd
};
573 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
574 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
576 my $path = "/dev/rbd/$pool/$name";
577 $path .= '@'.$snapname if $snapname;
580 $name .= '@'.$snapname if $snapname;
581 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
582 run_rbd_command
($cmd, errmsg
=> "can't mount rbd volume $name");
587 sub deactivate_volume
{
588 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
590 return 1 if !$scfg->{krbd
};
592 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
593 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
595 my $path = "/dev/rbd/$pool/$name";
596 $path .= '@'.$snapname if $snapname;
597 return if ! -b
$path;
599 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
600 run_rbd_command
($cmd, errmsg
=> "can't unmap rbd volume $name");
605 sub volume_size_info
{
606 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
608 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
609 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
614 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
616 return 1 if $running && !$scfg->{krbd
};
618 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
620 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
621 run_rbd_command
($cmd, errmsg
=> "rbd resize '$volname' error");
625 sub volume_snapshot
{
626 my ($class, $scfg, $storeid, $volname, $snap) = @_;
628 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
630 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
631 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
635 sub volume_snapshot_rollback
{
636 my ($class, $scfg, $storeid, $volname, $snap) = @_;
638 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
640 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
641 run_rbd_command
($cmd, errmsg
=> "rbd snapshot $volname to '$snap' error");
644 sub volume_snapshot_delete
{
645 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
647 return 1 if $running && !$scfg->{krbd
};
649 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
651 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
653 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
655 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
656 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
659 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
661 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
666 sub volume_has_feature
{
667 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
670 snapshot
=> { current
=> 1, snap
=> 1},
671 clone
=> { base
=> 1, snap
=> 1},
672 template
=> { current
=> 1},
673 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
674 sparseinit
=> { base
=> 1, current
=> 1},
677 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
678 $class->parse_volname($volname);
684 $key = $isBase ?
'base' : 'current';
686 return 1 if $features->{$feature}->{$key};