]>
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 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');
328 push @$disk_list, $line;
332 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
336 die $err if $err && $err !~ m/doesn't contain rbd images/;
338 return PVE
::Storage
::Plugin
::get_next_vm_diskname
($disk_list, $storeid, $vmid, undef, $scfg);
342 my ($class, $storeid, $scfg, $volname) = @_;
344 my $snap = '__base__';
346 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
347 $class->parse_volname($volname);
349 die "create_base not possible with base image\n" if $isBase;
351 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
352 die "rbd volume info on '$name' failed\n" if !($size);
354 die "rbd image must be at format V2" if $format ne "2";
356 die "volname '$volname' contains wrong information about parent $parent $basename\n"
357 if $basename && (!$parent || $parent ne $basename."@".$snap);
360 $newname =~ s/^vm-/base-/;
362 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
364 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
365 run_rbd_command
($cmd, errmsg
=> "rbd rename '$name' error");
367 my $running = undef; #fixme : is create_base always offline ?
369 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
371 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
374 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
375 run_rbd_command
($cmd, errmsg
=> "rbd protect $newname snap '$snap' error");
383 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
385 my $snap = '__base__';
386 $snap = $snapname if length $snapname;
388 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
389 $class->parse_volname($volname);
391 die "$volname is not a base image and snapname is not provided\n"
392 if !$isBase && !length($snapname);
394 my $name = $find_free_diskname->($storeid, $scfg, $vmid);
396 warn "clone $volname: $basename snapname $snap to $name\n";
398 if (length($snapname)) {
399 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $volname, $snapname);
402 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
403 run_rbd_command
($cmd, errmsg
=> "rbd protect $volname snap $snapname error");
407 my $newvol = "$basename/$name";
408 $newvol = $name if length($snapname);
410 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
411 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
413 run_rbd_command
($cmd, errmsg
=> "rbd clone '$basename' error");
415 &$krbd_feature_disable($scfg, $storeid, $name);
421 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
424 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
425 if $name && $name !~ m/^vm-$vmid-/;
427 $name = $find_free_diskname->($storeid, $scfg, $vmid) if !$name;
429 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
430 run_rbd_command
($cmd, errmsg
=> "rbd create $name' error");
432 &$krbd_feature_disable($scfg, $storeid, $name);
438 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
440 my ($vtype, $name, $vmid, undef, undef, undef) =
441 $class->parse_volname($volname);
444 my $snap = '__base__';
445 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
447 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
448 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
452 $class->deactivate_volume($storeid, $scfg, $volname);
454 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
455 run_rbd_command
($cmd, errmsg
=> "rbd snap purge '$volname' error");
457 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
458 run_rbd_command
($cmd, errmsg
=> "rbd rm '$volname' error");
464 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
466 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
467 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
471 if (my $dat = $cache->{rbd
}->{$pool}) {
472 foreach my $image (keys %$dat) {
474 my $info = $dat->{$image};
476 my $volname = $info->{name
};
477 my $parent = $info->{parent
};
478 my $owner = $info->{vmid
};
480 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
481 $info->{volid
} = "$storeid:$1/$volname";
483 $info->{volid
} = "$storeid:$volname";
487 my $found = grep { $_ eq $info->{volid
} } @$vollist;
490 next if defined ($vmid) && ($owner ne $vmid);
493 $info->{format
} = 'raw';
503 my ($class, $storeid, $scfg, $cache) = @_;
506 my $rados = &$librados_connect($scfg, $storeid);
507 my $df = $rados->mon_command({ prefix
=> 'df', format
=> 'json' });
509 my ($d) = grep { $_->{name
} eq $scfg->{pool
} } @{$df->{pools
}};
511 # max_avail -> max available space for data w/o replication in the pool
512 # bytes_used -> data w/o replication in the pool
513 my $free = $d->{stats
}->{max_avail
};
514 my $used = $d->{stats
}->{bytes_used
};
515 my $total = $used + $free;
518 return ($total, $free, $used, $active);
521 sub activate_storage
{
522 my ($class, $storeid, $scfg, $cache) = @_;
526 sub deactivate_storage
{
527 my ($class, $storeid, $scfg, $cache) = @_;
531 sub activate_volume
{
532 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
534 return 1 if !$scfg->{krbd
};
536 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
537 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
539 my $path = "/dev/rbd/$pool/$name";
540 $path .= '@'.$snapname if $snapname;
543 $name .= '@'.$snapname if $snapname;
544 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
545 run_rbd_command
($cmd, errmsg
=> "can't mount rbd volume $name");
550 sub deactivate_volume
{
551 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
553 return 1 if !$scfg->{krbd
};
555 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
556 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
558 my $path = "/dev/rbd/$pool/$name";
559 $path .= '@'.$snapname if $snapname;
560 return if ! -b
$path;
562 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
563 run_rbd_command
($cmd, errmsg
=> "can't unmap rbd volume $name");
568 sub volume_size_info
{
569 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
571 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
572 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
577 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
579 return 1 if $running && !$scfg->{krbd
};
581 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
583 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
584 run_rbd_command
($cmd, errmsg
=> "rbd resize '$volname' error");
588 sub volume_snapshot
{
589 my ($class, $scfg, $storeid, $volname, $snap) = @_;
591 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
593 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
594 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
598 sub volume_snapshot_rollback
{
599 my ($class, $scfg, $storeid, $volname, $snap) = @_;
601 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
603 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
604 run_rbd_command
($cmd, errmsg
=> "rbd snapshot $volname to '$snap' error");
607 sub volume_snapshot_delete
{
608 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
610 return 1 if $running && !$scfg->{krbd
};
612 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
614 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
616 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
618 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
619 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
622 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
624 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
629 sub volume_has_feature
{
630 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
633 snapshot
=> { current
=> 1, snap
=> 1},
634 clone
=> { base
=> 1, snap
=> 1},
635 template
=> { current
=> 1},
636 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
637 sparseinit
=> { base
=> 1, current
=> 1},
640 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
641 $class->parse_volname($volname);
647 $key = $isBase ?
'base' : 'current';
649 return 1 if $features->{$feature}->{$key};