]>
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
}, ',');
59 push @$cmd, '-n', "client.$username";
60 push @$cmd, '--keyring', $keyring;
61 push @$cmd, '--auth_supported', 'cephx';
63 push @$cmd, '--auth_supported', 'none';
66 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
69 if ($pveceph_managed) {
70 warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
72 push @$cmd, '-c', $cephconfig;
78 push @$cmd, @options if scalar(@options);
84 my ($scfg, $storeid, $op, @options) = @_;
86 return $build_cmd->('/usr/bin/rbd', $scfg, $storeid, $op, @options);
90 my ($scfg, $storeid, $op, @options) = @_;
92 return $build_cmd->('/usr/bin/rados', $scfg, $storeid, $op, @options);
95 # needed for volumes created using ceph jewel (or higher)
96 my $krdb_feature_disable = sub {
97 my ($scfg, $storeid, $name) = @_;
99 return 1 if !$scfg->{krbd
};
101 my ($major, undef, undef, undef) = ceph_version
();
102 return 1 if $major < 10;
104 my $feature_cmd = &$rbd_cmd($scfg, $storeid, 'feature', 'disable', $name, 'deep-flatten,fast-diff,object-map,exclusive-lock');
105 run_rbd_command
($feature_cmd, errmsg
=> "could not disable krbd-incompatible image features of rbd volume $name");
108 my $ceph_version_parser = sub {
110 if ($line =~ m/^ceph version ((\d+)\.(\d+)\.(\d+))(?: \([a-fA-F0-9]+\))/) {
111 return ($2, $3, $4, $1);
113 warn "Could not parse Ceph version: '$line'\n";
120 my $version_string = $cache;
126 if (defined($version_string)) {
127 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($version_string);
129 run_command
('ceph --version', outfunc
=> sub {
131 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($line);
134 return undef if !defined($version_string);
135 return wantarray ?
($major, $minor, $bugfix, $version_string) : $version_string;
138 sub run_rbd_command
{
139 my ($cmd, %args) = @_;
142 my $errmsg = $args{errmsg
} . ": " || "";
143 if (!exists($args{errfunc
})) {
144 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
145 # at least 1 child(ren) in pool cephstor1
146 $args{errfunc
} = sub {
148 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
153 print STDERR
$lasterr;
158 eval { run_command
($cmd, %args); };
160 die $errmsg . $lasterr if length($lasterr);
168 my ($scfg, $storeid) = @_;
170 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l');
171 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
178 if ($line =~ m/^((vm|base)-(\d+)-\S+)\s+(\d+)(k|M|G|T)\s((\S+)\/((vm
|base
)-\d
+-\S
+@\S+))?
/) {
179 my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8);
180 return if $image =~ /@/; #skip snapshots
182 $list->{$pool}->{$image} = {
184 size
=> $size*$rbd_unittobytes->{$unit},
192 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
196 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
201 sub rbd_volume_info
{
202 my ($scfg, $storeid, $volname, $snap) = @_;
207 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
209 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
215 my $protected = undef;
220 if ($line =~ m/size (\d+) (k|M|G|T)B in (\d+) objects/) {
221 $size = $1 * $rbd_unittobytes->{$2} if ($1);
222 } elsif ($line =~ m/parent:\s(\S+)\/(\S
+)/) {
224 } elsif ($line =~ m/format:\s(\d+)/) {
226 } elsif ($line =~ m/protected:\s(\S+)/) {
227 $protected = 1 if $1 eq "True";
232 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
234 return ($size, $parent, $format, $protected);
245 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
252 description
=> "IP addresses of monitors (for external clusters).",
253 type
=> 'string', format
=> 'pve-storage-portal-dns-list',
256 description
=> "Pool.",
260 description
=> "RBD Id.",
264 description
=> "Authsupported.",
268 description
=> "Access rbd through krbd kernel module.",
276 nodes
=> { optional
=> 1 },
277 disable
=> { optional
=> 1 },
278 monhost
=> { optional
=> 1},
279 pool
=> { optional
=> 1 },
280 username
=> { optional
=> 1 },
281 content
=> { optional
=> 1 },
282 krbd
=> { optional
=> 1 },
286 # Storage implementation
289 my ($class, $volname) = @_;
291 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
292 return ('images', $4, $7, $2, $3, $5, 'raw');
295 die "unable to parse rbd volume name '$volname'\n";
299 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
301 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
302 $name .= '@'.$snapname if $snapname;
304 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
305 return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd
};
307 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
309 my $path = "rbd:$pool/$name";
310 my $pveceph_managed = !defined($scfg->{monhost
});
312 if ($pveceph_managed) {
313 $path .= ":conf=$pveceph_config";
315 my $monhost = $hostlist->($scfg->{monhost
}, ';');
316 $monhost =~ s/:/\\:/g;
317 $path .= ":mon_host=$monhost";
320 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
323 $path .= ":id=$username:auth_supported=cephx:keyring=$keyring";
325 $path .= ":auth_supported=none";
328 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
330 if (-e
$cephconfig) {
331 if ($pveceph_managed) {
332 warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
334 $path .= ":conf=$cephconfig";
338 return ($path, $vmid, $vtype);
341 my $find_free_diskname = sub {
342 my ($storeid, $scfg, $vmid) = @_;
344 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls');
350 if ($line =~ m/^(vm|base)-\Q$vmid\E+-disk-(\d+)$/) {
356 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
360 die $err if $err && $err !~ m/doesn't contain rbd images/;
362 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
363 for (my $i = 1; $i < 100; $i++) {
364 if (!$disk_ids->{$i}) {
365 return "vm-$vmid-disk-$i";
369 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
373 my ($class, $storeid, $scfg, $volname) = @_;
375 my $snap = '__base__';
377 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
378 $class->parse_volname($volname);
380 die "create_base not possible with base image\n" if $isBase;
382 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
383 die "rbd volume info on '$name' failed\n" if !($size);
385 die "rbd image must be at format V2" if $format ne "2";
387 die "volname '$volname' contains wrong information about parent $parent $basename\n"
388 if $basename && (!$parent || $parent ne $basename."@".$snap);
391 $newname =~ s/^vm-/base-/;
393 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
395 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
396 run_rbd_command
($cmd, errmsg
=> "rbd rename '$name' error");
398 my $running = undef; #fixme : is create_base always offline ?
400 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
402 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
405 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
406 run_rbd_command
($cmd, errmsg
=> "rbd protect $newname snap '$snap' error");
414 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
416 my $snap = '__base__';
417 $snap = $snapname if length $snapname;
419 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
420 $class->parse_volname($volname);
422 die "$volname is not a base image and snapname is not provided\n"
423 if !$isBase && !length($snapname);
425 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
427 warn "clone $volname: $basename snapname $snap to $name\n";
429 if (length($snapname)) {
430 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $volname, $snapname);
433 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
434 run_rbd_command
($cmd, errmsg
=> "rbd protect $volname snap $snapname error");
438 my $newvol = "$basename/$name";
439 $newvol = $name if length($snapname);
441 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
442 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
444 run_rbd_command
($cmd, errmsg
=> "rbd clone '$basename' error");
446 &$krdb_feature_disable($scfg, $storeid, $name);
452 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
455 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
456 if $name && $name !~ m/^vm-$vmid-/;
458 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
460 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
461 run_rbd_command
($cmd, errmsg
=> "rbd create $name' error");
463 &$krdb_feature_disable($scfg, $storeid, $name);
469 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
471 my ($vtype, $name, $vmid, undef, undef, undef) =
472 $class->parse_volname($volname);
475 my $snap = '__base__';
476 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
478 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
479 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
483 $class->deactivate_volume($storeid, $scfg, $volname);
485 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
486 run_rbd_command
($cmd, errmsg
=> "rbd snap purge '$volname' error");
488 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
489 run_rbd_command
($cmd, errmsg
=> "rbd rm '$volname' error");
495 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
497 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
498 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
502 if (my $dat = $cache->{rbd
}->{$pool}) {
503 foreach my $image (keys %$dat) {
505 my $info = $dat->{$image};
507 my $volname = $info->{name
};
508 my $parent = $info->{parent
};
509 my $owner = $info->{vmid
};
511 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
512 $info->{volid
} = "$storeid:$1/$volname";
514 $info->{volid
} = "$storeid:$volname";
518 my $found = grep { $_ eq $info->{volid
} } @$vollist;
521 next if defined ($vmid) && ($owner ne $vmid);
524 $info->{format
} = 'raw';
534 my ($class, $storeid, $scfg, $cache) = @_;
536 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
542 if ($line =~ m/^\s*total(?:\s|_)(\S+)\s+(\d+)(k|M|G|T)?/) {
544 # luminous has units here..
546 $stats->{$1} *= $rbd_unittobytes->{$3}/1024;
552 run_rbd_command
($cmd, errmsg
=> "rados error", errfunc
=> sub {}, outfunc
=> $parser);
555 my $total = $stats->{space
} ?
$stats->{space
}*1024 : 0;
556 my $free = $stats->{avail
} ?
$stats->{avail
}*1024 : 0;
557 my $used = $stats->{used
} ?
$stats->{used
}*1024: 0;
560 return ($total, $free, $used, $active);
563 sub activate_storage
{
564 my ($class, $storeid, $scfg, $cache) = @_;
568 sub deactivate_storage
{
569 my ($class, $storeid, $scfg, $cache) = @_;
573 sub activate_volume
{
574 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
576 return 1 if !$scfg->{krbd
};
578 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
579 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
581 my $path = "/dev/rbd/$pool/$name";
582 $path .= '@'.$snapname if $snapname;
585 $name .= '@'.$snapname if $snapname;
586 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
587 run_rbd_command
($cmd, errmsg
=> "can't mount rbd volume $name");
592 sub deactivate_volume
{
593 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
595 return 1 if !$scfg->{krbd
};
597 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
598 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
600 my $path = "/dev/rbd/$pool/$name";
601 $path .= '@'.$snapname if $snapname;
602 return if ! -b
$path;
604 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
605 run_rbd_command
($cmd, errmsg
=> "can't unmap rbd volume $name");
610 sub volume_size_info
{
611 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
613 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
614 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
619 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
621 return 1 if $running && !$scfg->{krbd
};
623 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
625 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
626 run_rbd_command
($cmd, errmsg
=> "rbd resize '$volname' error");
630 sub volume_snapshot
{
631 my ($class, $scfg, $storeid, $volname, $snap) = @_;
633 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
635 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
636 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
640 sub volume_snapshot_rollback
{
641 my ($class, $scfg, $storeid, $volname, $snap) = @_;
643 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
645 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
646 run_rbd_command
($cmd, errmsg
=> "rbd snapshot $volname to '$snap' error");
649 sub volume_snapshot_delete
{
650 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
652 return 1 if $running && !$scfg->{krbd
};
654 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
656 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
658 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
660 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
661 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
664 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
666 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
671 sub volume_has_feature
{
672 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
675 snapshot
=> { current
=> 1, snap
=> 1},
676 clone
=> { base
=> 1, snap
=> 1},
677 template
=> { current
=> 1},
678 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
679 sparseinit
=> { base
=> 1, current
=> 1},
682 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
683 $class->parse_volname($volname);
689 $key = $isBase ?
'base' : 'current';
691 return 1 if $features->{$feature}->{$key};