]>
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 = {
15 "G" => 1024*1024*1024,
16 "T" => 1024*1024*1024*1024,
19 my $add_pool_to_disk = sub {
20 my ($scfg, $disk) = @_;
22 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
28 my ($list_text, $separator) = @_;
30 my @monhostlist = PVE
::Tools
::split_list
($list_text);
31 return join($separator, map {
32 my ($host, $port) = PVE
::Tools
::parse_host_and_port
($_);
33 $port = defined($port) ?
":$port" : '';
34 $host = "[$host]" if Net
::IP
::ip_is_ipv6
($host);
40 my ($scfg, $storeid, $op, @options) = @_;
42 my $monhost = &$hostlist($scfg->{monhost
}, ',');
44 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
45 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
46 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
48 my $cmd = ['/usr/bin/rbd', '-p', $pool, '-m', $monhost];
51 push @$cmd, '-n', "client.$username";
52 push @$cmd, '--keyring', $keyring;
53 push @$cmd, '--auth_supported', 'cephx';
55 push @$cmd, '--auth_supported', 'none';
58 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
61 push @$cmd, '-c', $cephconfig;
66 push @$cmd, @options if scalar(@options);
72 my ($scfg, $storeid, $op, @options) = @_;
74 my $monhost = &$hostlist($scfg->{monhost
}, ',');
76 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
77 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
78 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
80 my $cmd = ['/usr/bin/rados', '-p', $pool, '-m', $monhost];
83 push @$cmd, '-n', "client.$username";
84 push @$cmd, '--keyring', $keyring;
85 push @$cmd, '--auth_supported', 'cephx';
87 push @$cmd, '--auth_supported', 'none';
90 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
93 push @$cmd, '-c', $cephconfig;
98 push @$cmd, @options if scalar(@options);
103 # needed for volumes created using ceph jewel (or higher)
104 my $krdb_feature_disable = sub {
105 my ($scfg, $storeid, $name) = @_;
107 return 1 if !$scfg->{krbd
};
109 my ($major, undef, undef, undef) = ceph_version
();
110 return 1 if $major < 10;
112 my $feature_cmd = &$rbd_cmd($scfg, $storeid, 'feature', 'disable', $name, 'deep-flatten,fast-diff,object-map,exclusive-lock');
113 run_rbd_command
($feature_cmd, errmsg
=> "could not disable krbd-incompatible image features of rbd volume $name");
116 my $ceph_version_parser = sub {
118 if ($line =~ m/^ceph version ((\d+)\.(\d+)\.(\d+))(?: \([a-fA-F0-9]+\))?$/) {
119 return ($2, $3, $4, $1);
121 warn "Could not parse Ceph version: '$line'\n";
128 my $version_string = $cache;
134 if (defined($version_string)) {
135 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($version_string);
137 run_command
('ceph --version', outfunc
=> sub {
139 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($line);
142 return undef if !defined($version_string);
143 return wantarray ?
($major, $minor, $bugfix, $version_string) : $version_string;
146 sub run_rbd_command
{
147 my ($cmd, %args) = @_;
150 my $errmsg = $args{errmsg
} . ": " || "";
151 if (!exists($args{errfunc
})) {
152 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
153 # at least 1 child(ren) in pool cephstor1
154 $args{errfunc
} = sub {
156 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
161 print STDERR
$lasterr;
166 eval { run_command
($cmd, %args); };
168 die $errmsg . $lasterr if length($lasterr);
176 my ($scfg, $storeid) = @_;
178 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l');
179 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
186 if ($line =~ m/^((vm|base)-(\d+)-disk-\d+)\s+(\d+)(M|G|T)\s((\S+)\/((vm
|base
)-\d
+-\S
+@\S+))?
/) {
187 my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8);
189 $list->{$pool}->{$image} = {
191 size
=> $size*$rbd_unittobytes->{$unit},
199 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
203 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
208 sub rbd_volume_info
{
209 my ($scfg, $storeid, $volname, $snap) = @_;
214 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
216 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
222 my $protected = undef;
227 if ($line =~ m/size (\d+) (M|G|T)B in (\d+) objects/) {
228 $size = $1 * $rbd_unittobytes->{$2} if ($1);
229 } elsif ($line =~ m/parent:\s(\S+)\/(\S
+)/) {
231 } elsif ($line =~ m/format:\s(\d+)/) {
233 } elsif ($line =~ m/protected:\s(\S+)/) {
234 $protected = 1 if $1 eq "True";
239 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
241 return ($size, $parent, $format, $protected);
252 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
259 description
=> "Monitors daemon ips.",
260 type
=> 'string', format
=> 'pve-storage-portal-dns-list',
263 description
=> "Pool.",
267 description
=> "RBD Id.",
271 description
=> "Authsupported.",
275 description
=> "Access rbd through krbd kernel module.",
283 nodes
=> { optional
=> 1 },
284 disable
=> { optional
=> 1 },
285 monhost
=> { fixed
=> 1 },
286 pool
=> { optional
=> 1 },
287 username
=> { optional
=> 1 },
288 content
=> { optional
=> 1 },
289 krbd
=> { optional
=> 1 },
293 # Storage implementation
296 my ($class, $volname) = @_;
298 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
299 return ('images', $4, $7, $2, $3, $5, 'raw');
302 die "unable to parse rbd volume name '$volname'\n";
306 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
308 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
309 $name .= '@'.$snapname if $snapname;
311 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
312 return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd
};
314 my $monhost = &$hostlist($scfg->{monhost
}, ';');
315 $monhost =~ s/:/\\:/g;
317 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
319 my $path = "rbd:$pool/$name: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 $path .= ":conf=$cephconfig";
334 return ($path, $vmid, $vtype);
337 my $find_free_diskname = sub {
338 my ($storeid, $scfg, $vmid) = @_;
340 my $rbd = rbd_ls
($scfg, $storeid);
341 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
343 my $dat = $rbd->{$pool};
345 foreach my $image (keys %$dat) {
346 my $volname = $dat->{$image}->{name
};
347 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
351 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
352 for (my $i = 1; $i < 100; $i++) {
353 if (!$disk_ids->{$i}) {
354 return "vm-$vmid-disk-$i";
358 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
362 my ($class, $storeid, $scfg, $volname) = @_;
364 my $snap = '__base__';
366 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
367 $class->parse_volname($volname);
369 die "create_base not possible with base image\n" if $isBase;
371 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
372 die "rbd volume info on '$name' failed\n" if !($size);
374 die "rbd image must be at format V2" if $format ne "2";
376 die "volname '$volname' contains wrong information about parent $parent $basename\n"
377 if $basename && (!$parent || $parent ne $basename."@".$snap);
380 $newname =~ s/^vm-/base-/;
382 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
384 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
385 run_rbd_command
($cmd, errmsg
=> "rbd rename '$name' error");
387 my $running = undef; #fixme : is create_base always offline ?
389 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
391 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
394 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
395 run_rbd_command
($cmd, errmsg
=> "rbd protect $newname snap '$snap' error");
403 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
405 my $snap = '__base__';
406 $snap = $snapname if length $snapname;
408 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
409 $class->parse_volname($volname);
411 die "$volname is not a base image and snapname is not provided\n"
412 if !$isBase && !length($snapname);
414 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
416 warn "clone $volname: $basename snapname $snap to $name\n";
418 if (length($snapname)) {
419 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $volname, $snapname);
422 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
423 run_rbd_command
($cmd, errmsg
=> "rbd protect $volname snap $snapname error");
427 my $newvol = "$basename/$name";
428 $newvol = $name if length($snapname);
430 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
431 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
433 run_rbd_command
($cmd, errmsg
=> "rbd clone '$basename' error");
435 &$krdb_feature_disable($scfg, $storeid, $name);
441 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
444 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
445 if $name && $name !~ m/^vm-$vmid-/;
447 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
449 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
450 run_rbd_command
($cmd, errmsg
=> "rbd create $name' error");
452 &$krdb_feature_disable($scfg, $storeid, $name);
458 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
460 my ($vtype, $name, $vmid, undef, undef, undef) =
461 $class->parse_volname($volname);
464 my $snap = '__base__';
465 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
467 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
468 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
472 $class->deactivate_volume($storeid, $scfg, $volname);
474 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
475 run_rbd_command
($cmd, errmsg
=> "rbd snap purge '$volname' error");
477 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
478 run_rbd_command
($cmd, errmsg
=> "rbd rm '$volname' error");
484 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
486 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
487 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
491 if (my $dat = $cache->{rbd
}->{$pool}) {
492 foreach my $image (keys %$dat) {
494 my $volname = $dat->{$image}->{name
};
495 my $parent = $dat->{$image}->{parent
};
497 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
498 $volname = "$1/$volname";
501 my $volid = "$storeid:$volname";
503 my $owner = $dat->{$image}->{vmid
};
505 my $found = grep { $_ eq $volid } @$vollist;
508 next if defined ($vmid) && ($owner ne $vmid);
511 my $info = $dat->{$image};
512 $info->{volid
} = $volid;
513 $info->{format
} = 'raw';
523 my ($class, $storeid, $scfg, $cache) = @_;
525 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
531 if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) {
537 run_rbd_command
($cmd, errmsg
=> "rados error", errfunc
=> sub {}, outfunc
=> $parser);
540 my $total = $stats->{space
} ?
$stats->{space
}*1024 : 0;
541 my $free = $stats->{avail
} ?
$stats->{avail
}*1024 : 0;
542 my $used = $stats->{used
} ?
$stats->{used
}*1024: 0;
545 return ($total, $free, $used, $active);
548 sub activate_storage
{
549 my ($class, $storeid, $scfg, $cache) = @_;
553 sub deactivate_storage
{
554 my ($class, $storeid, $scfg, $cache) = @_;
558 sub activate_volume
{
559 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
561 return 1 if !$scfg->{krbd
};
563 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
564 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
566 my $path = "/dev/rbd/$pool/$name";
567 $path .= '@'.$snapname if $snapname;
570 $name .= '@'.$snapname if $snapname;
571 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
572 run_rbd_command
($cmd, errmsg
=> "can't mount rbd volume $name");
577 sub deactivate_volume
{
578 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
580 return 1 if !$scfg->{krbd
};
582 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
583 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
585 my $path = "/dev/rbd/$pool/$name";
586 $path .= '@'.$snapname if $snapname;
587 return if ! -b
$path;
589 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
590 run_rbd_command
($cmd, errmsg
=> "can't unmap rbd volume $name");
595 sub volume_size_info
{
596 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
598 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
599 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
604 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
606 return 1 if $running;
608 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
610 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $name);
611 run_rbd_command
($cmd, errmsg
=> "rbd resize '$volname' error");
615 sub volume_snapshot
{
616 my ($class, $scfg, $storeid, $volname, $snap) = @_;
618 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
620 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
621 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
625 sub volume_snapshot_rollback
{
626 my ($class, $scfg, $storeid, $volname, $snap) = @_;
628 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
630 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
631 run_rbd_command
($cmd, errmsg
=> "rbd snapshot $volname to '$snap' error");
634 sub volume_snapshot_delete
{
635 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
637 return 1 if $running;
639 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
641 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
643 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
645 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
646 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
649 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
651 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
656 sub volume_has_feature
{
657 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
660 snapshot
=> { current
=> 1, snap
=> 1},
661 clone
=> { base
=> 1, snap
=> 1},
662 template
=> { current
=> 1},
663 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
664 sparseinit
=> { base
=> 1, current
=> 1},
667 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
668 $class->parse_volname($volname);
674 $key = $isBase ?
'base' : 'current';
676 return 1 if $features->{$feature}->{$key};