]>
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);
16 "G" => 1024*1024*1024,
17 "T" => 1024*1024*1024*1024,
21 my $add_pool_to_disk = sub {
22 my ($scfg, $disk) = @_;
24 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
30 my ($scfg, $storeid, $op, @options) = @_;
32 my $monhost = $scfg->{monhost
};
35 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
36 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
37 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
39 my $cmd = ['/usr/bin/rbd', '-p', $pool, '-m', $monhost];
42 push @$cmd, '-n', "client.$username";
43 push @$cmd, '--keyring', $keyring;
44 push @$cmd, '--auth_supported', 'cephx';
46 push @$cmd, '--auth_supported', 'none';
51 push @$cmd, @options if scalar(@options);
57 my ($scfg, $storeid, $op, @options) = @_;
59 my $monhost = $scfg->{monhost
};
62 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
63 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
64 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
66 my $cmd = ['/usr/bin/rados', '-p', $pool, '-m', $monhost];
69 push @$cmd, '-n', "client.$username";
70 push @$cmd, '--keyring', $keyring;
71 push @$cmd, '--auth_supported', 'cephx';
73 push @$cmd, '--auth_supported', 'none';
78 push @$cmd, @options if scalar(@options);
84 my ($cmd, %args) = @_;
87 my $errmsg = $args{errmsg
} . ": " || "";
88 if (!exists $args{errfunc
}) {
89 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
90 # at least 1 child(ren) in pool cephstor1
91 $args{errfunc
} = sub {
93 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
98 print STDERR
$lasterr;
105 $r = run_command
($cmd, %args);
108 die $errmsg . $lasterr if $@ && length $lasterr;
115 my ($scfg, $storeid) = @_;
117 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l');
118 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
125 if ($line =~ m/^((vm|base)-(\d+)-disk-\d+)\s+(\d+)(M|G|T)\s((\S+)\/((vm
|base
)-\d
+-\S
+@\S+))?
/) {
126 my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8);
128 $list->{$pool}->{$image} = {
130 size
=> $size*rbd_unittobytes()->{$unit},
138 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
142 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
147 sub rbd_volume_info
{
148 my ($scfg, $storeid, $volname, $snap) = @_;
153 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
155 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
161 my $protected = undef;
166 if ($line =~ m/size (\d+) (M|G|T)B in (\d+) objects/) {
167 $size = $1 * rbd_unittobytes
()->{$2} if ($1);
168 } elsif ($line =~ m/parent:\s(\S+)\/(\S
+)/) {
170 } elsif ($line =~ m/format:\s(\d+)/) {
172 } elsif ($line =~ m/protected:\s(\S+)/) {
173 $protected = 1 if $1 eq "True";
178 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
180 return ($size, $parent, $format, $protected);
192 PVE
::JSONSchema
::register_format
('pve-storage-monhost', \
&parse_monhost
);
194 my ($name, $noerr) = @_;
196 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
197 return undef if $noerr;
198 die "lvm name '$name' contains illegal characters\n";
210 content
=> [ {images
=> 1}, { images
=> 1 }],
217 description
=> "Monitors daemon ips.",
221 description
=> "Pool.",
225 description
=> "RBD Id.",
229 description
=> "Authsupported.",
237 nodes
=> { optional
=> 1 },
238 disable
=> { optional
=> 1 },
239 monhost
=> { fixed
=> 1 },
240 pool
=> { optional
=> 1 },
241 username
=> { optional
=> 1 },
242 content
=> { optional
=> 1 },
246 # Storage implementation
249 my ($class, $volname) = @_;
251 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
252 return ('images', $4, $7, $2, $3, $5);
255 die "unable to parse rbd volume name '$volname'\n";
259 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
261 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
262 $name .= '@'.$snapname if $snapname;
264 my $monhost = addslashes
($scfg->{monhost
});
265 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
266 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
268 my $path = "rbd:$pool/$name:mon_host=$monhost";
269 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
272 $path .= ":id=$username:auth_supported=cephx:keyring=$keyring";
274 $path .= ":auth_supported=none";
277 return ($path, $vmid, $vtype);
280 my $find_free_diskname = sub {
281 my ($storeid, $scfg, $vmid) = @_;
283 my $rbd = rbd_ls
($scfg, $storeid);
284 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
286 my $dat = $rbd->{$pool};
288 foreach my $image (keys %$dat) {
289 my $volname = $dat->{$image}->{name
};
290 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
294 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
295 for (my $i = 1; $i < 100; $i++) {
296 if (!$disk_ids->{$i}) {
297 return "vm-$vmid-disk-$i";
301 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
305 my ($class, $storeid, $scfg, $volname) = @_;
307 my $snap = '__base__';
309 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
310 $class->parse_volname($volname);
312 die "create_base not possible with base image\n" if $isBase;
314 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
315 die "rbd volume info on '$name' failed\n" if !($size);
317 die "rbd image must be at format V2" if $format ne "2";
319 die "volname '$volname' contains wrong information about parent $parent $basename\n"
320 if $basename && (!$parent || $parent ne $basename."@".$snap);
323 $newname =~ s/^vm-/base-/;
325 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
327 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
328 run_rbd_command
($cmd, errmsg
=> "rbd rename '$name' error");
330 my $running = undef; #fixme : is create_base always offline ?
332 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
334 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
337 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
338 run_rbd_command
($cmd, errmsg
=> "rbd protect $newname snap $snap' error");
346 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
348 my $snap = '__base__';
350 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
351 $class->parse_volname($volname);
353 die "clone_image onyl works on base images\n" if !$isBase;
355 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
357 warn "clone $volname: $basename to $name\n";
359 my $newvol = "$basename/$name";
361 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename), '--snap', $snap, &$add_pool_to_disk($scfg, $name));
362 run_rbd_command
($cmd, errmsg
=> "rbd clone '$basename' error");
368 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
371 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
372 if $name && $name !~ m/^vm-$vmid-/;
374 $name = &$find_free_diskname($storeid, $scfg, $vmid);
376 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
377 run_rbd_command
($cmd, errmsg
=> "rbd create $name' error");
383 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
385 my ($vtype, $name, $vmid, undef, undef, undef) =
386 $class->parse_volname($volname);
389 my $snap = '__base__';
390 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
392 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
393 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap $snap' error");
397 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
398 run_rbd_command
($cmd, errmsg
=> "rbd snap purge '$volname' error");
400 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
401 run_rbd_command
($cmd, errmsg
=> "rbd rm '$volname' error");
407 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
409 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
410 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
414 if (my $dat = $cache->{rbd
}->{$pool}) {
415 foreach my $image (keys %$dat) {
417 my $volname = $dat->{$image}->{name
};
419 my $volid = "$storeid:$volname";
421 my $owner = $dat->{$volname}->{vmid
};
423 my $found = grep { $_ eq $volid } @$vollist;
426 next if defined ($vmid) && ($owner ne $vmid);
429 my $info = $dat->{$volname};
430 $info->{volid
} = $volid;
431 $info->{format
} = 'raw';
441 my ($class, $storeid, $scfg, $cache) = @_;
443 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
449 if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) {
455 run_rbd_command
($cmd, errmsg
=> "rados error", errfunc
=> sub {}, outfunc
=> $parser);
458 my $total = $stats->{space
} ?
$stats->{space
}*1024 : 0;
459 my $free = $stats->{avail
} ?
$stats->{avail
}*1024 : 0;
460 my $used = $stats->{used
} ?
$stats->{used
}*1024: 0;
463 return ($total, $free, $used, $active);
466 sub activate_storage
{
467 my ($class, $storeid, $scfg, $cache) = @_;
471 sub deactivate_storage
{
472 my ($class, $storeid, $scfg, $cache) = @_;
476 sub activate_volume
{
477 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
481 sub deactivate_volume
{
482 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
486 sub volume_size_info
{
487 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
489 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
490 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
495 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
497 return 1 if $running;
499 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
501 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $name);
502 run_rbd_command
($cmd, errmsg
=> "rbd resize '$volname' error");
506 sub volume_snapshot
{
507 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
509 return 1 if $running;
511 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
513 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
514 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
518 sub volume_snapshot_rollback
{
519 my ($class, $scfg, $storeid, $volname, $snap) = @_;
521 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
523 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
524 run_rbd_command
($cmd, errmsg
=> "rbd snapshot $volname to $snap' error");
527 sub volume_snapshot_delete
{
528 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
530 return 1 if $running;
532 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
534 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
536 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
541 sub volume_has_feature
{
542 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
545 snapshot
=> { current
=> 1, snap
=> 1},
546 clone
=> { base
=> 1},
547 template
=> { current
=> 1},
548 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
551 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
552 $class->parse_volname($volname);
558 $key = $isBase ?
'base' : 'current';
560 return 1 if $features->{$feature}->{$key};