]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/RBDPlugin.pm
81a7b984ded8c2c7a010444266873e2edfa67975
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 ($scfg, $storeid) = @_;
85 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l');
86 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
93 if ($line =~ m/^((vm|base)-(\d+)-disk-\d+)\s+(\d+)(M|G|T)\s((\S+)\/((vm
|base
)-\d
+-\S
+@\S+))?
/) {
94 my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8);
96 $list->{$pool}->{$image} = {
98 size
=> $size*rbd_unittobytes()->{$unit},
106 run_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
110 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
115 sub rbd_volume_info
{
116 my ($scfg, $storeid, $volname, $snap) = @_;
121 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
123 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
129 my $protected = undef;
134 if ($line =~ m/size (\d+) (M|G|T)B in (\d+) objects/) {
135 $size = $1 * rbd_unittobytes
()->{$2} if ($1);
136 } elsif ($line =~ m/parent:\s(\S+)\/(\S
+)/) {
138 } elsif ($line =~ m/format:\s(\d+)/) {
140 } elsif ($line =~ m/protected:\s(\S+)/) {
141 $protected = 1 if $1 eq "True";
146 run_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
148 return ($size, $parent, $format, $protected);
160 PVE
::JSONSchema
::register_format
('pve-storage-monhost', \
&parse_monhost
);
162 my ($name, $noerr) = @_;
164 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
165 return undef if $noerr;
166 die "lvm name '$name' contains illegal characters\n";
178 content
=> [ {images
=> 1}, { images
=> 1 }],
185 description
=> "Monitors daemon ips.",
189 description
=> "Pool.",
193 description
=> "RBD Id.",
197 description
=> "Authsupported.",
205 nodes
=> { optional
=> 1 },
206 disable
=> { optional
=> 1 },
207 monhost
=> { fixed
=> 1 },
208 pool
=> { optional
=> 1 },
209 username
=> { optional
=> 1 },
210 content
=> { optional
=> 1 },
214 # Storage implementation
217 my ($class, $volname) = @_;
219 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
220 return ('images', $4, $7, $2, $3, $5);
223 die "unable to parse rbd volume name '$volname'\n";
227 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
229 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
230 $name .= '@'.$snapname if $snapname;
232 my $monhost = addslashes
($scfg->{monhost
});
233 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
234 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
236 my $path = "rbd:$pool/$name:mon_host=$monhost";
237 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
240 $path .= ":id=$username:auth_supported=cephx:keyring=$keyring";
242 $path .= ":auth_supported=none";
245 return ($path, $vmid, $vtype);
248 my $find_free_diskname = sub {
249 my ($storeid, $scfg, $vmid) = @_;
251 my $rbd = rbd_ls
($scfg, $storeid);
252 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
254 my $dat = $rbd->{$pool};
256 foreach my $image (keys %$dat) {
257 my $volname = $dat->{$image}->{name
};
258 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
262 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
263 for (my $i = 1; $i < 100; $i++) {
264 if (!$disk_ids->{$i}) {
265 return "vm-$vmid-disk-$i";
269 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
273 my ($class, $storeid, $scfg, $volname) = @_;
275 my $snap = '__base__';
277 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
278 $class->parse_volname($volname);
280 die "create_base not possible with base image\n" if $isBase;
282 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
283 die "rbd volume info on '$name' failed\n" if !($size);
285 die "rbd image must be at format V2" if $format ne "2";
287 die "volname '$volname' contains wrong information about parent $parent $basename\n"
288 if $basename && (!$parent || $parent ne $basename."@".$snap);
291 $newname =~ s/^vm-/base-/;
293 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
295 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
296 run_command
($cmd, errmsg
=> "rbd rename $name' error", errfunc
=> sub {});
298 my $running = undef; #fixme : is create_base always offline ?
300 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
302 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
305 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
306 run_command
($cmd, errmsg
=> "rbd protect $newname snap $snap' error", errfunc
=> sub {});
314 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
316 my $snap = '__base__';
318 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
319 $class->parse_volname($volname);
321 die "clone_image onyl works on base images\n" if !$isBase;
323 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
325 warn "clone $volname: $basename to $name\n";
327 my $newvol = "$basename/$name";
329 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', $basename, '--snap', $snap, $name);
330 run_command
($cmd, errmsg
=> "rbd clone $basename' error", errfunc
=> sub {});
336 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
339 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
340 if $name && $name !~ m/^vm-$vmid-/;
342 $name = &$find_free_diskname($storeid, $scfg, $vmid);
344 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--format' , 2, '--size', int(($size+1023)/1024), $name);
345 run_command
($cmd, errmsg
=> "rbd create $name' error", errfunc
=> sub {});
351 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
353 my ($vtype, $name, $vmid, undef, undef, undef) =
354 $class->parse_volname($volname);
357 my $snap = '__base__';
358 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
360 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
361 run_command
($cmd, errmsg
=> "rbd unprotect $name snap $snap' error", errfunc
=> sub {});
365 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
366 run_command
($cmd, errmsg
=> "rbd snap purge $volname' error", outfunc
=> sub {}, errfunc
=> sub {});
368 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
369 run_command
($cmd, errmsg
=> "rbd rm $volname' error", outfunc
=> sub {}, errfunc
=> sub {});
375 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
377 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
378 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
382 if (my $dat = $cache->{rbd
}->{$pool}) {
383 foreach my $image (keys %$dat) {
385 my $volname = $dat->{$image}->{name
};
387 my $volid = "$storeid:$volname";
389 my $owner = $dat->{$volname}->{vmid
};
391 my $found = grep { $_ eq $volid } @$vollist;
394 next if defined ($vmid) && ($owner ne $vmid);
397 my $info = $dat->{$volname};
398 $info->{volid
} = $volid;
399 $info->{format
} = 'raw';
409 my ($class, $storeid, $scfg, $cache) = @_;
411 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
417 if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) {
423 run_command
($cmd, errmsg
=> "rados error", errfunc
=> sub {}, outfunc
=> $parser);
426 my $total = $stats->{space
} ?
$stats->{space
}*1024 : 0;
427 my $free = $stats->{avail
} ?
$stats->{avail
}*1024 : 0;
428 my $used = $stats->{used
} ?
$stats->{used
}*1024: 0;
431 return ($total, $free, $used, $active);
434 sub activate_storage
{
435 my ($class, $storeid, $scfg, $cache) = @_;
439 sub deactivate_storage
{
440 my ($class, $storeid, $scfg, $cache) = @_;
444 sub activate_volume
{
445 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
449 sub deactivate_volume
{
450 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
454 sub volume_size_info
{
455 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
457 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
458 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
463 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
465 return 1 if $running;
467 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
469 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $name);
470 run_command
($cmd, errmsg
=> "rbd resize $volname' error", errfunc
=> sub {});
474 sub volume_snapshot
{
475 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
477 return 1 if $running;
479 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
481 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
482 run_command
($cmd, errmsg
=> "rbd snapshot $volname' error", errfunc
=> sub {});
486 sub volume_snapshot_rollback
{
487 my ($class, $scfg, $storeid, $volname, $snap) = @_;
489 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
491 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
492 run_command
($cmd, errmsg
=> "rbd snapshot $volname to $snap' error", errfunc
=> sub {});
495 sub volume_snapshot_delete
{
496 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
498 return 1 if $running;
500 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
502 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
503 run_command
($cmd, errmsg
=> "rbd snapshot $volname' error", errfunc
=> sub {});
507 sub volume_has_feature
{
508 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
511 snapshot
=> { current
=> 1, snap
=> 1},
512 clone
=> { base
=> 1},
513 template
=> { current
=> 1},
514 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
517 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
518 $class->parse_volname($volname);
524 $key = $isBase ?
'base' : 'current';
526 return 1 if $features->{$feature}->{$key};