]>
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);
12 use base
qw(PVE::Storage::Plugin);
14 my $pveceph_config = '/etc/pve/ceph.conf';
16 my $rbd_unittobytes = {
19 "G" => 1024*1024*1024,
20 "T" => 1024*1024*1024*1024,
23 my $add_pool_to_disk = sub {
24 my ($scfg, $disk) = @_;
26 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
32 my ($list_text, $separator) = @_;
34 my @monhostlist = PVE
::Tools
::split_list
($list_text);
35 return join($separator, map {
36 my ($host, $port) = PVE
::Tools
::parse_host_and_port
($_);
37 $port = defined($port) ?
":$port" : '';
38 $host = "[$host]" if Net
::IP
::ip_is_ipv6
($host);
44 my ($binary, $scfg, $storeid, $op, @options) = @_;
46 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
47 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
48 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
50 my $cmd = [$binary, '-p', $pool];
51 my $pveceph_managed = !defined($scfg->{monhost
});
53 if ($pveceph_managed) {
54 push @$cmd, '-c', $pveceph_config;
56 push @$cmd, '-m', $hostlist->($scfg->{monhost
}, ',');
57 push @$cmd, '--auth_supported', -e
$keyring ?
'cephx' : 'none';
61 push @$cmd, '-n', "client.$username";
62 push @$cmd, '--keyring', $keyring;
65 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
68 if ($pveceph_managed) {
69 warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
71 push @$cmd, '-c', $cephconfig;
77 push @$cmd, @options if scalar(@options);
83 my ($scfg, $storeid, $op, @options) = @_;
85 return $build_cmd->('/usr/bin/rbd', $scfg, $storeid, $op, @options);
89 my ($scfg, $storeid, $op, @options) = @_;
91 return $build_cmd->('/usr/bin/rados', $scfg, $storeid, $op, @options);
94 my $ceph_connect_option = sub {
95 my ($scfg, $storeid, %options) = @_;
98 my $ceph_storeid_conf = "/etc/pve/priv/ceph/${storeid}.conf";
99 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
100 my $pveceph_managed = !defined($scfg->{monhost
});
102 $cmd_option->{ceph_conf
} = $pveceph_config if (-e
$pveceph_config);
104 if (-e
$ceph_storeid_conf) {
105 if ($pveceph_managed) {
106 warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
108 $cmd_option->{ceph_conf
} = $ceph_storeid_conf;
112 $cmd_option->{keyring
} = $keyring if (-e
$keyring);
113 $cmd_option->{auth_supported
} = (defined $cmd_option->{keyring
}) ?
'cephx' : 'none';
114 $cmd_option->{userid
} = $scfg->{username
} ?
$scfg->{username
} : 'admin';
115 $cmd_option->{mon_host
} = $hostlist->($scfg->{monhost
}, ',') if (defined($scfg->{monhost
}));
118 foreach my $k (keys %options) {
119 $cmd_option->{$k} = $options{$k};
128 my $librados_connect = sub {
129 my ($scfg, $storeid, $options) = @_;
131 my $librados_config = $ceph_connect_option->($scfg, $storeid);
133 my $rados = PVE
::RADOS-
>new(%$librados_config);
138 # needed for volumes created using ceph jewel (or higher)
139 my $krbd_feature_disable = sub {
140 my ($scfg, $storeid, $name) = @_;
142 return 1 if !$scfg->{krbd
};
144 my ($major, undef, undef, undef) = ceph_version
();
145 return 1 if $major < 10;
147 my $krbd_feature_blacklist = ['deep-flatten', 'fast-diff', 'object-map', 'exclusive-lock'];
148 my (undef, undef, undef, undef, $features) = rbd_volume_info
($scfg, $storeid, $name);
150 my $active_features = { map { $_ => 1 } PVE
::Tools
::split_list
($features)};
151 my $incompatible_features = join(',', grep { %$active_features{$_} } @$krbd_feature_blacklist);
153 if ($incompatible_features) {
154 my $feature_cmd = &$rbd_cmd($scfg, $storeid, 'feature', 'disable', $name, $incompatible_features);
155 run_rbd_command
($feature_cmd, errmsg
=> "could not disable krbd-incompatible image features of rbd volume $name");
159 my $ceph_version_parser = sub {
161 if ($line =~ m/^ceph version ((\d+)\.(\d+)\.(\d+))(?: \([a-fA-F0-9]+\))/) {
162 return ($2, $3, $4, $1);
164 warn "Could not parse Ceph version: '$line'\n";
171 my $version_string = $cache;
177 if (defined($version_string)) {
178 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($version_string);
180 run_command
('ceph --version', outfunc
=> sub {
182 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($line);
185 return undef if !defined($version_string);
186 return wantarray ?
($major, $minor, $bugfix, $version_string) : $version_string;
189 sub run_rbd_command
{
190 my ($cmd, %args) = @_;
193 my $errmsg = $args{errmsg
} . ": " || "";
194 if (!exists($args{errfunc
})) {
195 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
196 # at least 1 child(ren) in pool cephstor1
197 $args{errfunc
} = sub {
199 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
204 print STDERR
$lasterr;
209 eval { run_command
($cmd, %args); };
211 die $errmsg . $lasterr if length($lasterr);
219 my ($scfg, $storeid) = @_;
221 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l');
222 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
229 if ($line =~ m/^((vm|base)-(\d+)-\S+)\s+(\d+)(k|M|G|T)\s((\S+)\/((vm
|base
)-\d
+-\S
+@\S+))?
/) {
230 my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8);
231 return if $image =~ /@/; #skip snapshots
233 $list->{$pool}->{$image} = {
235 size
=> $size*$rbd_unittobytes->{$unit},
243 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
247 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
252 sub rbd_volume_info
{
253 my ($scfg, $storeid, $volname, $snap) = @_;
258 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
260 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
266 my $protected = undef;
267 my $features = undef;
272 if ($line =~ m/size (\d+) (k|M|G|T)B in (\d+) objects/) {
273 $size = $1 * $rbd_unittobytes->{$2} if ($1);
274 } elsif ($line =~ m/parent:\s(\S+)\/(\S
+)/) {
276 } elsif ($line =~ m/format:\s(\d+)/) {
278 } elsif ($line =~ m/protected:\s(\S+)/) {
279 $protected = 1 if $1 eq "True";
280 } elsif ($line =~ m/features:\s(.+)/) {
286 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
288 return ($size, $parent, $format, $protected, $features);
299 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
306 description
=> "IP addresses of monitors (for external clusters).",
307 type
=> 'string', format
=> 'pve-storage-portal-dns-list',
310 description
=> "Pool.",
314 description
=> "RBD Id.",
318 description
=> "Authsupported.",
322 description
=> "Access rbd through krbd kernel module.",
330 nodes
=> { optional
=> 1 },
331 disable
=> { optional
=> 1 },
332 monhost
=> { optional
=> 1},
333 pool
=> { optional
=> 1 },
334 username
=> { optional
=> 1 },
335 content
=> { optional
=> 1 },
336 krbd
=> { optional
=> 1 },
337 bwlimit
=> { optional
=> 1 },
341 # Storage implementation
344 my ($class, $volname) = @_;
346 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
347 return ('images', $4, $7, $2, $3, $5, 'raw');
350 die "unable to parse rbd volume name '$volname'\n";
354 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
356 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
357 $name .= '@'.$snapname if $snapname;
359 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
360 return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd
};
362 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
364 my $path = "rbd:$pool/$name";
365 my $pveceph_managed = !defined($scfg->{monhost
});
366 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
368 if ($pveceph_managed) {
369 $path .= ":conf=$pveceph_config";
371 my $monhost = $hostlist->($scfg->{monhost
}, ';');
372 $monhost =~ s/:/\\:/g;
373 $path .= ":mon_host=$monhost";
374 $path .= -e
$keyring ?
":auth_supported=cephx" : ":auth_supported=none";
377 $path .= ":id=$username:keyring=$keyring" if -e
$keyring;
379 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
381 if (-e
$cephconfig) {
382 if ($pveceph_managed) {
383 warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
385 $path .= ":conf=$cephconfig";
389 return ($path, $vmid, $vtype);
392 my $find_free_diskname = sub {
393 my ($storeid, $scfg, $vmid) = @_;
395 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls');
401 if ($line =~ m/^(vm|base)-\Q$vmid\E+-disk-(\d+)$/) {
407 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
411 die $err if $err && $err !~ m/doesn't contain rbd images/;
413 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
414 for (my $i = 1; $i < 100; $i++) {
415 if (!$disk_ids->{$i}) {
416 return "vm-$vmid-disk-$i";
420 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
424 my ($class, $storeid, $scfg, $volname) = @_;
426 my $snap = '__base__';
428 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
429 $class->parse_volname($volname);
431 die "create_base not possible with base image\n" if $isBase;
433 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
434 die "rbd volume info on '$name' failed\n" if !($size);
436 die "rbd image must be at format V2" if $format ne "2";
438 die "volname '$volname' contains wrong information about parent $parent $basename\n"
439 if $basename && (!$parent || $parent ne $basename."@".$snap);
442 $newname =~ s/^vm-/base-/;
444 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
446 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
447 run_rbd_command
($cmd, errmsg
=> "rbd rename '$name' error");
449 my $running = undef; #fixme : is create_base always offline ?
451 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
453 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
456 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
457 run_rbd_command
($cmd, errmsg
=> "rbd protect $newname snap '$snap' error");
465 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
467 my $snap = '__base__';
468 $snap = $snapname if length $snapname;
470 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
471 $class->parse_volname($volname);
473 die "$volname is not a base image and snapname is not provided\n"
474 if !$isBase && !length($snapname);
476 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
478 warn "clone $volname: $basename snapname $snap to $name\n";
480 if (length($snapname)) {
481 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $volname, $snapname);
484 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
485 run_rbd_command
($cmd, errmsg
=> "rbd protect $volname snap $snapname error");
489 my $newvol = "$basename/$name";
490 $newvol = $name if length($snapname);
492 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
493 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
495 run_rbd_command
($cmd, errmsg
=> "rbd clone '$basename' error");
497 &$krbd_feature_disable($scfg, $storeid, $name);
503 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
506 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
507 if $name && $name !~ m/^vm-$vmid-/;
509 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
511 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
512 run_rbd_command
($cmd, errmsg
=> "rbd create $name' error");
514 &$krbd_feature_disable($scfg, $storeid, $name);
520 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
522 my ($vtype, $name, $vmid, undef, undef, undef) =
523 $class->parse_volname($volname);
526 my $snap = '__base__';
527 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
529 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
530 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
534 $class->deactivate_volume($storeid, $scfg, $volname);
536 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
537 run_rbd_command
($cmd, errmsg
=> "rbd snap purge '$volname' error");
539 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
540 run_rbd_command
($cmd, errmsg
=> "rbd rm '$volname' error");
546 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
548 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
549 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
553 if (my $dat = $cache->{rbd
}->{$pool}) {
554 foreach my $image (keys %$dat) {
556 my $info = $dat->{$image};
558 my $volname = $info->{name
};
559 my $parent = $info->{parent
};
560 my $owner = $info->{vmid
};
562 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
563 $info->{volid
} = "$storeid:$1/$volname";
565 $info->{volid
} = "$storeid:$volname";
569 my $found = grep { $_ eq $info->{volid
} } @$vollist;
572 next if defined ($vmid) && ($owner ne $vmid);
575 $info->{format
} = 'raw';
585 my ($class, $storeid, $scfg, $cache) = @_;
588 my $rados = &$librados_connect($scfg, $storeid);
589 my $df = $rados->mon_command({ prefix
=> 'df', format
=> 'json' });
591 my ($d) = grep { $_->{name
} eq $scfg->{pool
} } @{$df->{pools
}};
593 # max_avail -> max available space for data w/o replication in the pool
594 # bytes_used -> data w/o replication in the pool
595 my $free = $d->{stats
}->{max_avail
};
596 my $used = $d->{stats
}->{bytes_used
};
597 my $total = $used + $free;
600 return ($total, $free, $used, $active);
603 sub activate_storage
{
604 my ($class, $storeid, $scfg, $cache) = @_;
608 sub deactivate_storage
{
609 my ($class, $storeid, $scfg, $cache) = @_;
613 sub activate_volume
{
614 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
616 return 1 if !$scfg->{krbd
};
618 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
619 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
621 my $path = "/dev/rbd/$pool/$name";
622 $path .= '@'.$snapname if $snapname;
625 $name .= '@'.$snapname if $snapname;
626 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
627 run_rbd_command
($cmd, errmsg
=> "can't mount rbd volume $name");
632 sub deactivate_volume
{
633 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
635 return 1 if !$scfg->{krbd
};
637 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
638 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
640 my $path = "/dev/rbd/$pool/$name";
641 $path .= '@'.$snapname if $snapname;
642 return if ! -b
$path;
644 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
645 run_rbd_command
($cmd, errmsg
=> "can't unmap rbd volume $name");
650 sub volume_size_info
{
651 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
653 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
654 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
659 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
661 return 1 if $running && !$scfg->{krbd
};
663 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
665 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
666 run_rbd_command
($cmd, errmsg
=> "rbd resize '$volname' error");
670 sub volume_snapshot
{
671 my ($class, $scfg, $storeid, $volname, $snap) = @_;
673 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
675 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
676 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
680 sub volume_snapshot_rollback
{
681 my ($class, $scfg, $storeid, $volname, $snap) = @_;
683 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
685 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
686 run_rbd_command
($cmd, errmsg
=> "rbd snapshot $volname to '$snap' error");
689 sub volume_snapshot_delete
{
690 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
692 return 1 if $running && !$scfg->{krbd
};
694 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
696 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
698 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
700 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
701 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
704 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
706 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
711 sub volume_has_feature
{
712 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
715 snapshot
=> { current
=> 1, snap
=> 1},
716 clone
=> { base
=> 1, snap
=> 1},
717 template
=> { current
=> 1},
718 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
719 sparseinit
=> { base
=> 1, current
=> 1},
722 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
723 $class->parse_volname($volname);
729 $key = $isBase ?
'base' : 'current';
731 return 1 if $features->{$feature}->{$key};