]>
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+)-disk-\d+)\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);
190 $list->{$pool}->{$image} = {
192 size
=> $size*$rbd_unittobytes->{$unit},
200 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
204 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
209 sub rbd_volume_info
{
210 my ($scfg, $storeid, $volname, $snap) = @_;
215 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
217 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
223 my $protected = undef;
228 if ($line =~ m/size (\d+) (k|M|G|T)B in (\d+) objects/) {
229 $size = $1 * $rbd_unittobytes->{$2} if ($1);
230 } elsif ($line =~ m/parent:\s(\S+)\/(\S
+)/) {
232 } elsif ($line =~ m/format:\s(\d+)/) {
234 } elsif ($line =~ m/protected:\s(\S+)/) {
235 $protected = 1 if $1 eq "True";
240 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
242 return ($size, $parent, $format, $protected);
253 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
260 description
=> "Monitors daemon ips.",
261 type
=> 'string', format
=> 'pve-storage-portal-dns-list',
264 description
=> "Pool.",
268 description
=> "RBD Id.",
272 description
=> "Authsupported.",
276 description
=> "Access rbd through krbd kernel module.",
284 nodes
=> { optional
=> 1 },
285 disable
=> { optional
=> 1 },
286 monhost
=> { fixed
=> 1 },
287 pool
=> { optional
=> 1 },
288 username
=> { optional
=> 1 },
289 content
=> { optional
=> 1 },
290 krbd
=> { optional
=> 1 },
294 # Storage implementation
297 my ($class, $volname) = @_;
299 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
300 return ('images', $4, $7, $2, $3, $5, 'raw');
303 die "unable to parse rbd volume name '$volname'\n";
307 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
309 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
310 $name .= '@'.$snapname if $snapname;
312 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
313 return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd
};
315 my $monhost = &$hostlist($scfg->{monhost
}, ';');
316 $monhost =~ s/:/\\:/g;
318 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
320 my $path = "rbd:$pool/$name:mon_host=$monhost";
321 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
324 $path .= ":id=$username:auth_supported=cephx:keyring=$keyring";
326 $path .= ":auth_supported=none";
329 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
331 if (-e
$cephconfig) {
332 $path .= ":conf=$cephconfig";
335 return ($path, $vmid, $vtype);
338 my $find_free_diskname = sub {
339 my ($storeid, $scfg, $vmid) = @_;
341 my $rbd = rbd_ls
($scfg, $storeid);
342 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
344 my $dat = $rbd->{$pool};
346 foreach my $image (keys %$dat) {
347 my $volname = $dat->{$image}->{name
};
348 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
352 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
353 for (my $i = 1; $i < 100; $i++) {
354 if (!$disk_ids->{$i}) {
355 return "vm-$vmid-disk-$i";
359 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
363 my ($class, $storeid, $scfg, $volname) = @_;
365 my $snap = '__base__';
367 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
368 $class->parse_volname($volname);
370 die "create_base not possible with base image\n" if $isBase;
372 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
373 die "rbd volume info on '$name' failed\n" if !($size);
375 die "rbd image must be at format V2" if $format ne "2";
377 die "volname '$volname' contains wrong information about parent $parent $basename\n"
378 if $basename && (!$parent || $parent ne $basename."@".$snap);
381 $newname =~ s/^vm-/base-/;
383 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
385 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
386 run_rbd_command
($cmd, errmsg
=> "rbd rename '$name' error");
388 my $running = undef; #fixme : is create_base always offline ?
390 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
392 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
395 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
396 run_rbd_command
($cmd, errmsg
=> "rbd protect $newname snap '$snap' error");
404 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
406 my $snap = '__base__';
407 $snap = $snapname if length $snapname;
409 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
410 $class->parse_volname($volname);
412 die "$volname is not a base image and snapname is not provided\n"
413 if !$isBase && !length($snapname);
415 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
417 warn "clone $volname: $basename snapname $snap to $name\n";
419 if (length($snapname)) {
420 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $volname, $snapname);
423 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
424 run_rbd_command
($cmd, errmsg
=> "rbd protect $volname snap $snapname error");
428 my $newvol = "$basename/$name";
429 $newvol = $name if length($snapname);
431 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
432 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
434 run_rbd_command
($cmd, errmsg
=> "rbd clone '$basename' error");
436 &$krdb_feature_disable($scfg, $storeid, $name);
442 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
445 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
446 if $name && $name !~ m/^vm-$vmid-/;
448 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
450 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
451 run_rbd_command
($cmd, errmsg
=> "rbd create $name' error");
453 &$krdb_feature_disable($scfg, $storeid, $name);
459 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
461 my ($vtype, $name, $vmid, undef, undef, undef) =
462 $class->parse_volname($volname);
465 my $snap = '__base__';
466 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
468 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
469 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
473 $class->deactivate_volume($storeid, $scfg, $volname);
475 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
476 run_rbd_command
($cmd, errmsg
=> "rbd snap purge '$volname' error");
478 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
479 run_rbd_command
($cmd, errmsg
=> "rbd rm '$volname' error");
485 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
487 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
488 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
492 if (my $dat = $cache->{rbd
}->{$pool}) {
493 foreach my $image (keys %$dat) {
495 my $info = $dat->{$image};
497 my $volname = $info->{name
};
498 my $parent = $info->{parent
};
499 my $owner = $info->{vmid
};
501 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
502 $info->{volid
} = "$storeid:$1/$volname";
504 $info->{volid
} = "$storeid:$volname";
508 my $found = grep { $_ eq $info->{volid
} } @$vollist;
511 next if defined ($vmid) && ($owner ne $vmid);
514 $info->{format
} = 'raw';
524 my ($class, $storeid, $scfg, $cache) = @_;
526 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
532 if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) {
538 run_rbd_command
($cmd, errmsg
=> "rados error", errfunc
=> sub {}, outfunc
=> $parser);
541 my $total = $stats->{space
} ?
$stats->{space
}*1024 : 0;
542 my $free = $stats->{avail
} ?
$stats->{avail
}*1024 : 0;
543 my $used = $stats->{used
} ?
$stats->{used
}*1024: 0;
546 return ($total, $free, $used, $active);
549 sub activate_storage
{
550 my ($class, $storeid, $scfg, $cache) = @_;
554 sub deactivate_storage
{
555 my ($class, $storeid, $scfg, $cache) = @_;
559 sub activate_volume
{
560 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
562 return 1 if !$scfg->{krbd
};
564 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
565 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
567 my $path = "/dev/rbd/$pool/$name";
568 $path .= '@'.$snapname if $snapname;
571 $name .= '@'.$snapname if $snapname;
572 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
573 run_rbd_command
($cmd, errmsg
=> "can't mount rbd volume $name");
578 sub deactivate_volume
{
579 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
581 return 1 if !$scfg->{krbd
};
583 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
584 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
586 my $path = "/dev/rbd/$pool/$name";
587 $path .= '@'.$snapname if $snapname;
588 return if ! -b
$path;
590 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
591 run_rbd_command
($cmd, errmsg
=> "can't unmap rbd volume $name");
596 sub volume_size_info
{
597 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
599 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
600 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
605 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
607 return 1 if $running;
609 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
611 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
612 run_rbd_command
($cmd, errmsg
=> "rbd resize '$volname' error");
616 sub volume_snapshot
{
617 my ($class, $scfg, $storeid, $volname, $snap) = @_;
619 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
621 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
622 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
626 sub volume_snapshot_rollback
{
627 my ($class, $scfg, $storeid, $volname, $snap) = @_;
629 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
631 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
632 run_rbd_command
($cmd, errmsg
=> "rbd snapshot $volname to '$snap' error");
635 sub volume_snapshot_delete
{
636 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
638 return 1 if $running;
640 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
642 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
644 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
646 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
647 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
650 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
652 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
657 sub volume_has_feature
{
658 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
661 snapshot
=> { current
=> 1, snap
=> 1},
662 clone
=> { base
=> 1, snap
=> 1},
663 template
=> { current
=> 1},
664 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
665 sparseinit
=> { base
=> 1, current
=> 1},
668 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
669 $class->parse_volname($volname);
675 $key = $isBase ?
'base' : 'current';
677 return 1 if $features->{$feature}->{$key};