]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/RBDPlugin.pm
1 package PVE
::Storage
::RBDPlugin
;
6 use PVE
::Tools
qw(run_command trim);
7 use PVE
::Storage
::Plugin
;
8 use PVE
::JSONSchema
qw(get_standard_option);
10 use base
qw(PVE::Storage::Plugin);
15 "G" => 1024*1024*1024,
16 "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 ($scfg, $storeid, $op, @options) = @_;
31 my $monhost = $scfg->{monhost
};
34 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
35 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
36 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
38 my $cmd = ['/usr/bin/rbd', '-p', $pool, '-m', $monhost];
41 push @$cmd, '-n', "client.$username";
42 push @$cmd, '--keyring', $keyring;
43 push @$cmd, '--auth_supported', 'cephx';
45 push @$cmd, '--auth_supported', 'none';
50 push @$cmd, @options if scalar(@options);
56 my ($scfg, $storeid, $op, @options) = @_;
58 my $monhost = $scfg->{monhost
};
61 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
62 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
63 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
65 my $cmd = ['/usr/bin/rados', '-p', $pool, '-m', $monhost];
68 push @$cmd, '-n', "client.$username";
69 push @$cmd, '--keyring', $keyring;
70 push @$cmd, '--auth_supported', 'cephx';
72 push @$cmd, '--auth_supported', 'none';
77 push @$cmd, @options if scalar(@options);
83 my ($cmd, %args) = @_;
86 my $errmsg = $args{errmsg
} . ": " || "";
87 if (!exists($args{errfunc
})) {
88 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
89 # at least 1 child(ren) in pool cephstor1
90 $args{errfunc
} = sub {
92 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
97 print STDERR
$lasterr;
102 eval { run_command
($cmd, %args); };
104 die $errmsg . $lasterr if length($lasterr);
112 my ($scfg, $storeid) = @_;
114 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l');
115 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
122 if ($line =~ m/^((vm|base)-(\d+)-disk-\d+)\s+(\d+)(M|G|T)\s((\S+)\/((vm
|base
)-\d
+-\S
+@\S+))?
/) {
123 my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8);
125 $list->{$pool}->{$image} = {
127 size
=> $size*rbd_unittobytes()->{$unit},
135 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
139 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
144 sub rbd_volume_info
{
145 my ($scfg, $storeid, $volname, $snap) = @_;
150 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
152 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
158 my $protected = undef;
163 if ($line =~ m/size (\d+) (M|G|T)B in (\d+) objects/) {
164 $size = $1 * rbd_unittobytes
()->{$2} if ($1);
165 } elsif ($line =~ m/parent:\s(\S+)\/(\S
+)/) {
167 } elsif ($line =~ m/format:\s(\d+)/) {
169 } elsif ($line =~ m/protected:\s(\S+)/) {
170 $protected = 1 if $1 eq "True";
175 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
177 return ($size, $parent, $format, $protected);
189 PVE
::JSONSchema
::register_format
('pve-storage-monhost', \
&parse_monhost
);
191 my ($name, $noerr) = @_;
193 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
194 return undef if $noerr;
195 die "lvm name '$name' contains illegal characters\n";
207 content
=> [ {images
=> 1}, { images
=> 1 }],
214 description
=> "Monitors daemon ips.",
218 description
=> "Pool.",
222 description
=> "RBD Id.",
226 description
=> "Authsupported.",
234 nodes
=> { optional
=> 1 },
235 disable
=> { optional
=> 1 },
236 monhost
=> { fixed
=> 1 },
237 pool
=> { optional
=> 1 },
238 username
=> { optional
=> 1 },
239 content
=> { optional
=> 1 },
243 # Storage implementation
246 my ($class, $volname) = @_;
248 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
249 return ('images', $4, $7, $2, $3, $5);
252 die "unable to parse rbd volume name '$volname'\n";
256 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
258 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
259 $name .= '@'.$snapname if $snapname;
261 my $monhost = addslashes
($scfg->{monhost
});
262 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
263 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
265 my $path = "rbd:$pool/$name:mon_host=$monhost";
266 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
269 $path .= ":id=$username:auth_supported=cephx:keyring=$keyring";
271 $path .= ":auth_supported=none";
274 return ($path, $vmid, $vtype);
277 my $find_free_diskname = sub {
278 my ($storeid, $scfg, $vmid) = @_;
280 my $rbd = rbd_ls
($scfg, $storeid);
281 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
283 my $dat = $rbd->{$pool};
285 foreach my $image (keys %$dat) {
286 my $volname = $dat->{$image}->{name
};
287 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
291 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
292 for (my $i = 1; $i < 100; $i++) {
293 if (!$disk_ids->{$i}) {
294 return "vm-$vmid-disk-$i";
298 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
302 my ($class, $storeid, $scfg, $volname) = @_;
304 my $snap = '__base__';
306 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
307 $class->parse_volname($volname);
309 die "create_base not possible with base image\n" if $isBase;
311 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
312 die "rbd volume info on '$name' failed\n" if !($size);
314 die "rbd image must be at format V2" if $format ne "2";
316 die "volname '$volname' contains wrong information about parent $parent $basename\n"
317 if $basename && (!$parent || $parent ne $basename."@".$snap);
320 $newname =~ s/^vm-/base-/;
322 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
324 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
325 run_rbd_command
($cmd, errmsg
=> "rbd rename '$name' error");
327 my $running = undef; #fixme : is create_base always offline ?
329 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
331 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
334 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
335 run_rbd_command
($cmd, errmsg
=> "rbd protect $newname snap '$snap' error");
343 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
345 my $snap = '__base__';
346 $snap = $snapname if length $snapname;
348 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
349 $class->parse_volname($volname);
351 die "$volname is not a base image and snapname is not provided\n"
352 if !$isBase && !length($snapname);
354 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
356 warn "clone $volname: $basename snapname $snap to $name\n";
358 if (length($snapname)) {
359 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $volname, $snapname);
362 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
363 run_rbd_command
($cmd, errmsg
=> "rbd protect $volname snap $snapname error");
367 my $newvol = "$basename/$name";
368 $newvol = $name if length($snapname);
370 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
371 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
373 run_rbd_command
($cmd, errmsg
=> "rbd clone '$basename' error");
379 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
382 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
383 if $name && $name !~ m/^vm-$vmid-/;
385 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
387 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
388 run_rbd_command
($cmd, errmsg
=> "rbd create $name' error");
394 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
396 my ($vtype, $name, $vmid, undef, undef, undef) =
397 $class->parse_volname($volname);
400 my $snap = '__base__';
401 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
403 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
404 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
408 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
409 run_rbd_command
($cmd, errmsg
=> "rbd snap purge '$volname' error");
411 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
412 run_rbd_command
($cmd, errmsg
=> "rbd rm '$volname' error");
418 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
420 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
421 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
425 if (my $dat = $cache->{rbd
}->{$pool}) {
426 foreach my $image (keys %$dat) {
428 my $volname = $dat->{$image}->{name
};
430 my $volid = "$storeid:$volname";
432 my $owner = $dat->{$volname}->{vmid
};
434 my $found = grep { $_ eq $volid } @$vollist;
437 next if defined ($vmid) && ($owner ne $vmid);
440 my $info = $dat->{$volname};
441 $info->{volid
} = $volid;
442 $info->{format
} = 'raw';
452 my ($class, $storeid, $scfg, $cache) = @_;
454 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
460 if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) {
466 run_rbd_command
($cmd, errmsg
=> "rados error", errfunc
=> sub {}, outfunc
=> $parser);
469 my $total = $stats->{space
} ?
$stats->{space
}*1024 : 0;
470 my $free = $stats->{avail
} ?
$stats->{avail
}*1024 : 0;
471 my $used = $stats->{used
} ?
$stats->{used
}*1024: 0;
474 return ($total, $free, $used, $active);
477 sub activate_storage
{
478 my ($class, $storeid, $scfg, $cache) = @_;
482 sub deactivate_storage
{
483 my ($class, $storeid, $scfg, $cache) = @_;
487 sub activate_volume
{
488 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
492 sub deactivate_volume
{
493 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
497 sub volume_size_info
{
498 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
500 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
501 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
506 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
508 return 1 if $running;
510 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
512 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $name);
513 run_rbd_command
($cmd, errmsg
=> "rbd resize '$volname' error");
517 sub volume_snapshot
{
518 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
520 return 1 if $running;
522 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
524 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
525 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
529 sub volume_snapshot_rollback
{
530 my ($class, $scfg, $storeid, $volname, $snap) = @_;
532 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
534 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
535 run_rbd_command
($cmd, errmsg
=> "rbd snapshot $volname to '$snap' error");
538 sub volume_snapshot_delete
{
539 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
541 return 1 if $running;
543 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
545 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
547 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
548 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
551 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
553 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
558 sub volume_has_feature
{
559 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
562 snapshot
=> { current
=> 1, snap
=> 1},
563 clone
=> { base
=> 1, snap
=> 1},
564 template
=> { current
=> 1},
565 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
568 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
569 $class->parse_volname($volname);
575 $key = $isBase ?
'base' : 'current';
577 return 1 if $features->{$feature}->{$key};