]>
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 $rbd_unittobytes = {
16 "G" => 1024*1024*1024,
17 "T" => 1024*1024*1024*1024,
20 my $add_pool_to_disk = sub {
21 my ($scfg, $disk) = @_;
23 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
29 my ($list_text, $separator) = @_;
31 my @monhostlist = PVE
::Tools
::split_list
($list_text);
32 return join($separator, map {
33 my ($host, $port) = PVE
::Tools
::parse_host_and_port
($_);
34 $port = defined($port) ?
":$port" : '';
35 $host = "[$host]" if Net
::IP
::ip_is_ipv6
($host);
41 my ($scfg, $storeid, $op, @options) = @_;
43 my $monhost = &$hostlist($scfg->{monhost
}, ',');
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 = ['/usr/bin/rbd', '-p', $pool, '-m', $monhost];
52 push @$cmd, '-n', "client.$username";
53 push @$cmd, '--keyring', $keyring;
54 push @$cmd, '--auth_supported', 'cephx';
56 push @$cmd, '--auth_supported', 'none';
59 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
62 push @$cmd, '-c', $cephconfig;
67 push @$cmd, @options if scalar(@options);
73 my ($scfg, $storeid, $op, @options) = @_;
75 my $monhost = &$hostlist($scfg->{monhost
}, ',');
77 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
78 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
79 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
81 my $cmd = ['/usr/bin/rados', '-p', $pool, '-m', $monhost];
84 push @$cmd, '-n', "client.$username";
85 push @$cmd, '--keyring', $keyring;
86 push @$cmd, '--auth_supported', 'cephx';
88 push @$cmd, '--auth_supported', 'none';
91 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
94 push @$cmd, '-c', $cephconfig;
99 push @$cmd, @options if scalar(@options);
104 # needed for volumes created using ceph jewel (or higher)
105 my $krdb_feature_disable = sub {
106 my ($scfg, $storeid, $name) = @_;
108 return 1 if !$scfg->{krbd
};
110 my ($major, undef, undef, undef) = ceph_version
();
111 return 1 if $major < 10;
113 my $feature_cmd = &$rbd_cmd($scfg, $storeid, 'feature', 'disable', $name, 'deep-flatten,fast-diff,object-map,exclusive-lock');
114 run_rbd_command
($feature_cmd, errmsg
=> "could not disable krbd-incompatible image features of rbd volume $name");
117 my $ceph_version_parser = sub {
119 if ($line =~ m/^ceph version ((\d+)\.(\d+)\.(\d+))(?: \([a-fA-F0-9]+\))?$/) {
120 return ($2, $3, $4, $1);
122 warn "Could not parse Ceph version: '$line'\n";
129 my $version_string = $cache;
135 if (defined($version_string)) {
136 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($version_string);
138 run_command
('ceph --version', outfunc
=> sub {
140 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($line);
143 return undef if !defined($version_string);
144 return wantarray ?
($major, $minor, $bugfix, $version_string) : $version_string;
147 sub run_rbd_command
{
148 my ($cmd, %args) = @_;
151 my $errmsg = $args{errmsg
} . ": " || "";
152 if (!exists($args{errfunc
})) {
153 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
154 # at least 1 child(ren) in pool cephstor1
155 $args{errfunc
} = sub {
157 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
162 print STDERR
$lasterr;
167 eval { run_command
($cmd, %args); };
169 die $errmsg . $lasterr if length($lasterr);
177 my ($scfg, $storeid) = @_;
179 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l');
180 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
187 if ($line =~ m/^((vm|base)-(\d+)-\S+)\s+(\d+)(k|M|G|T)\s((\S+)\/((vm
|base
)-\d
+-\S
+@\S+))?
/) {
188 my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8);
189 return if $image =~ /@/; #skip snapshots
191 $list->{$pool}->{$image} = {
193 size
=> $size*$rbd_unittobytes->{$unit},
201 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
205 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
210 sub rbd_volume_info
{
211 my ($scfg, $storeid, $volname, $snap) = @_;
216 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
218 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
224 my $protected = undef;
229 if ($line =~ m/size (\d+) (k|M|G|T)B in (\d+) objects/) {
230 $size = $1 * $rbd_unittobytes->{$2} if ($1);
231 } elsif ($line =~ m/parent:\s(\S+)\/(\S
+)/) {
233 } elsif ($line =~ m/format:\s(\d+)/) {
235 } elsif ($line =~ m/protected:\s(\S+)/) {
236 $protected = 1 if $1 eq "True";
241 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
243 return ($size, $parent, $format, $protected);
254 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
261 description
=> "Monitors daemon ips.",
262 type
=> 'string', format
=> 'pve-storage-portal-dns-list',
265 description
=> "Pool.",
269 description
=> "RBD Id.",
273 description
=> "Authsupported.",
277 description
=> "Access rbd through krbd kernel module.",
285 nodes
=> { optional
=> 1 },
286 disable
=> { optional
=> 1 },
287 monhost
=> { fixed
=> 1 },
288 pool
=> { optional
=> 1 },
289 username
=> { optional
=> 1 },
290 content
=> { optional
=> 1 },
291 krbd
=> { optional
=> 1 },
295 # Storage implementation
298 my ($class, $volname) = @_;
300 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
301 return ('images', $4, $7, $2, $3, $5, 'raw');
304 die "unable to parse rbd volume name '$volname'\n";
308 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
310 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
311 $name .= '@'.$snapname if $snapname;
313 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
314 return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd
};
316 my $monhost = &$hostlist($scfg->{monhost
}, ';');
317 $monhost =~ s/:/\\:/g;
319 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
321 my $path = "rbd:$pool/$name:mon_host=$monhost";
322 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
325 $path .= ":id=$username:auth_supported=cephx:keyring=$keyring";
327 $path .= ":auth_supported=none";
330 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
332 if (-e
$cephconfig) {
333 $path .= ":conf=$cephconfig";
336 return ($path, $vmid, $vtype);
339 my $find_free_diskname = sub {
340 my ($storeid, $scfg, $vmid) = @_;
342 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls');
348 if ($line =~ m/^(vm|base)-\Q$vmid\E+-disk-(\d+)$/) {
354 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
358 die $err if $err && $err !~ m/doesn't contain rbd images/;
360 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
361 for (my $i = 1; $i < 100; $i++) {
362 if (!$disk_ids->{$i}) {
363 return "vm-$vmid-disk-$i";
367 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
371 my ($class, $storeid, $scfg, $volname) = @_;
373 my $snap = '__base__';
375 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
376 $class->parse_volname($volname);
378 die "create_base not possible with base image\n" if $isBase;
380 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
381 die "rbd volume info on '$name' failed\n" if !($size);
383 die "rbd image must be at format V2" if $format ne "2";
385 die "volname '$volname' contains wrong information about parent $parent $basename\n"
386 if $basename && (!$parent || $parent ne $basename."@".$snap);
389 $newname =~ s/^vm-/base-/;
391 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
393 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
394 run_rbd_command
($cmd, errmsg
=> "rbd rename '$name' error");
396 my $running = undef; #fixme : is create_base always offline ?
398 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
400 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
403 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
404 run_rbd_command
($cmd, errmsg
=> "rbd protect $newname snap '$snap' error");
412 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
414 my $snap = '__base__';
415 $snap = $snapname if length $snapname;
417 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
418 $class->parse_volname($volname);
420 die "$volname is not a base image and snapname is not provided\n"
421 if !$isBase && !length($snapname);
423 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
425 warn "clone $volname: $basename snapname $snap to $name\n";
427 if (length($snapname)) {
428 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $volname, $snapname);
431 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
432 run_rbd_command
($cmd, errmsg
=> "rbd protect $volname snap $snapname error");
436 my $newvol = "$basename/$name";
437 $newvol = $name if length($snapname);
439 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
440 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
442 run_rbd_command
($cmd, errmsg
=> "rbd clone '$basename' error");
444 &$krdb_feature_disable($scfg, $storeid, $name);
450 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
453 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
454 if $name && $name !~ m/^vm-$vmid-/;
456 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
458 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
459 run_rbd_command
($cmd, errmsg
=> "rbd create $name' error");
461 &$krdb_feature_disable($scfg, $storeid, $name);
467 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
469 my ($vtype, $name, $vmid, undef, undef, undef) =
470 $class->parse_volname($volname);
473 my $snap = '__base__';
474 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
476 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
477 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
481 $class->deactivate_volume($storeid, $scfg, $volname);
483 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
484 run_rbd_command
($cmd, errmsg
=> "rbd snap purge '$volname' error");
486 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
487 run_rbd_command
($cmd, errmsg
=> "rbd rm '$volname' error");
493 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
495 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
496 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
500 if (my $dat = $cache->{rbd
}->{$pool}) {
501 foreach my $image (keys %$dat) {
503 my $info = $dat->{$image};
505 my $volname = $info->{name
};
506 my $parent = $info->{parent
};
507 my $owner = $info->{vmid
};
509 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
510 $info->{volid
} = "$storeid:$1/$volname";
512 $info->{volid
} = "$storeid:$volname";
516 my $found = grep { $_ eq $info->{volid
} } @$vollist;
519 next if defined ($vmid) && ($owner ne $vmid);
522 $info->{format
} = 'raw';
532 my ($class, $storeid, $scfg, $cache) = @_;
534 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
540 if ($line =~ m/^\s*total(?:\s|_)(\S+)\s+(\d+)(k|M|G|T)?/) {
542 # luminous has units here..
544 $stats->{$1} *= $rbd_unittobytes->{$3}/1024;
550 run_rbd_command
($cmd, errmsg
=> "rados error", errfunc
=> sub {}, outfunc
=> $parser);
553 my $total = $stats->{space
} ?
$stats->{space
}*1024 : 0;
554 my $free = $stats->{avail
} ?
$stats->{avail
}*1024 : 0;
555 my $used = $stats->{used
} ?
$stats->{used
}*1024: 0;
558 return ($total, $free, $used, $active);
561 sub activate_storage
{
562 my ($class, $storeid, $scfg, $cache) = @_;
566 sub deactivate_storage
{
567 my ($class, $storeid, $scfg, $cache) = @_;
571 sub activate_volume
{
572 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
574 return 1 if !$scfg->{krbd
};
576 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
577 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
579 my $path = "/dev/rbd/$pool/$name";
580 $path .= '@'.$snapname if $snapname;
583 $name .= '@'.$snapname if $snapname;
584 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
585 run_rbd_command
($cmd, errmsg
=> "can't mount rbd volume $name");
590 sub deactivate_volume
{
591 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
593 return 1 if !$scfg->{krbd
};
595 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
596 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
598 my $path = "/dev/rbd/$pool/$name";
599 $path .= '@'.$snapname if $snapname;
600 return if ! -b
$path;
602 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
603 run_rbd_command
($cmd, errmsg
=> "can't unmap rbd volume $name");
608 sub volume_size_info
{
609 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
611 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
612 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
617 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
619 return 1 if $running && !$scfg->{krbd
};
621 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
623 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
624 run_rbd_command
($cmd, errmsg
=> "rbd resize '$volname' error");
628 sub volume_snapshot
{
629 my ($class, $scfg, $storeid, $volname, $snap) = @_;
631 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
633 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
634 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
638 sub volume_snapshot_rollback
{
639 my ($class, $scfg, $storeid, $volname, $snap) = @_;
641 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
643 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
644 run_rbd_command
($cmd, errmsg
=> "rbd snapshot $volname to '$snap' error");
647 sub volume_snapshot_delete
{
648 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
650 return 1 if $running && !$scfg->{krbd
};
652 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
654 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
656 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
658 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
659 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
662 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
664 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
669 sub volume_has_feature
{
670 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
673 snapshot
=> { current
=> 1, snap
=> 1},
674 clone
=> { base
=> 1, snap
=> 1},
675 template
=> { current
=> 1},
676 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
677 sparseinit
=> { base
=> 1, current
=> 1},
680 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
681 $class->parse_volname($volname);
687 $key = $isBase ?
'base' : 'current';
689 return 1 if $features->{$feature}->{$key};