]>
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);
182 PVE
::JSONSchema
::register_format
('pve-storage-monhost', \
&parse_monhost
);
184 my ($name, $noerr) = @_;
186 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
187 return undef if $noerr;
188 die "lvm name '$name' contains illegal characters\n";
200 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
207 description
=> "Monitors daemon ips.",
211 description
=> "Pool.",
215 description
=> "RBD Id.",
219 description
=> "Authsupported.",
223 description
=> "Access rbd through krbd kernel module.",
231 nodes
=> { optional
=> 1 },
232 disable
=> { optional
=> 1 },
233 monhost
=> { fixed
=> 1 },
234 pool
=> { optional
=> 1 },
235 username
=> { optional
=> 1 },
236 content
=> { optional
=> 1 },
237 krbd
=> { optional
=> 1 },
241 # Storage implementation
244 my ($class, $volname) = @_;
246 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
247 return ('images', $4, $7, $2, $3, $5, 'raw');
250 die "unable to parse rbd volume name '$volname'\n";
254 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
256 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
257 $name .= '@'.$snapname if $snapname;
259 my $monhost = $scfg->{monhost
};
260 $monhost =~ s/:/\\:/g;
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 $path = "/dev/rbd/$pool/$name" if $scfg->{krbd
};
276 return ($path, $vmid, $vtype);
279 my $find_free_diskname = sub {
280 my ($storeid, $scfg, $vmid) = @_;
282 my $rbd = rbd_ls
($scfg, $storeid);
283 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
285 my $dat = $rbd->{$pool};
287 foreach my $image (keys %$dat) {
288 my $volname = $dat->{$image}->{name
};
289 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
293 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
294 for (my $i = 1; $i < 100; $i++) {
295 if (!$disk_ids->{$i}) {
296 return "vm-$vmid-disk-$i";
300 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
304 my ($class, $storeid, $scfg, $volname) = @_;
306 my $snap = '__base__';
308 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
309 $class->parse_volname($volname);
311 die "create_base not possible with base image\n" if $isBase;
313 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
314 die "rbd volume info on '$name' failed\n" if !($size);
316 die "rbd image must be at format V2" if $format ne "2";
318 die "volname '$volname' contains wrong information about parent $parent $basename\n"
319 if $basename && (!$parent || $parent ne $basename."@".$snap);
322 $newname =~ s/^vm-/base-/;
324 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
326 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
327 run_rbd_command
($cmd, errmsg
=> "rbd rename '$name' error");
329 my $running = undef; #fixme : is create_base always offline ?
331 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
333 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
336 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
337 run_rbd_command
($cmd, errmsg
=> "rbd protect $newname snap '$snap' error");
345 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
347 my $snap = '__base__';
348 $snap = $snapname if length $snapname;
350 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
351 $class->parse_volname($volname);
353 die "$volname is not a base image and snapname is not provided\n"
354 if !$isBase && !length($snapname);
356 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
358 warn "clone $volname: $basename snapname $snap to $name\n";
360 if (length($snapname)) {
361 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $volname, $snapname);
364 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
365 run_rbd_command
($cmd, errmsg
=> "rbd protect $volname snap $snapname error");
369 my $newvol = "$basename/$name";
370 $newvol = $name if length($snapname);
372 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
373 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
375 run_rbd_command
($cmd, errmsg
=> "rbd clone '$basename' error");
381 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
384 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
385 if $name && $name !~ m/^vm-$vmid-/;
387 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
389 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
390 run_rbd_command
($cmd, errmsg
=> "rbd create $name' error");
396 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
398 my ($vtype, $name, $vmid, undef, undef, undef) =
399 $class->parse_volname($volname);
402 my $snap = '__base__';
403 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
405 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
406 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
410 $class->deactivate_volume($storeid, $scfg, $volname);
412 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
413 run_rbd_command
($cmd, errmsg
=> "rbd snap purge '$volname' error");
415 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
416 run_rbd_command
($cmd, errmsg
=> "rbd rm '$volname' error");
422 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
424 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
425 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
429 if (my $dat = $cache->{rbd
}->{$pool}) {
430 foreach my $image (keys %$dat) {
432 my $volname = $dat->{$image}->{name
};
434 my $volid = "$storeid:$volname";
436 my $owner = $dat->{$volname}->{vmid
};
438 my $found = grep { $_ eq $volid } @$vollist;
441 next if defined ($vmid) && ($owner ne $vmid);
444 my $info = $dat->{$volname};
445 $info->{volid
} = $volid;
446 $info->{format
} = 'raw';
456 my ($class, $storeid, $scfg, $cache) = @_;
458 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
464 if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) {
470 run_rbd_command
($cmd, errmsg
=> "rados error", errfunc
=> sub {}, outfunc
=> $parser);
473 my $total = $stats->{space
} ?
$stats->{space
}*1024 : 0;
474 my $free = $stats->{avail
} ?
$stats->{avail
}*1024 : 0;
475 my $used = $stats->{used
} ?
$stats->{used
}*1024: 0;
478 return ($total, $free, $used, $active);
481 sub activate_storage
{
482 my ($class, $storeid, $scfg, $cache) = @_;
486 sub deactivate_storage
{
487 my ($class, $storeid, $scfg, $cache) = @_;
491 sub activate_volume
{
492 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
494 return 1 if !$scfg->{krbd
};
496 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
497 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
499 my $path = "/dev/rbd/$pool/$name";
500 $path .= '@'.$snapname if $snapname;
503 $name .= '@'.$snapname if $snapname;
504 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
505 run_rbd_command
($cmd, errmsg
=> "can't mount rbd volume $name");
510 sub deactivate_volume
{
511 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
513 return 1 if !$scfg->{krbd
};
515 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
516 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
518 my $path = "/dev/rbd/$pool/$name";
519 $path .= '@'.$snapname if $snapname;
520 return if ! -b
$path;
522 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
523 run_rbd_command
($cmd, errmsg
=> "can't unmount rbd volume $name");
528 sub volume_size_info
{
529 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
531 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
532 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
537 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
539 return 1 if $running;
541 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
543 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $name);
544 run_rbd_command
($cmd, errmsg
=> "rbd resize '$volname' error");
548 sub volume_snapshot
{
549 my ($class, $scfg, $storeid, $volname, $snap) = @_;
551 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
553 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
554 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
558 sub volume_snapshot_rollback
{
559 my ($class, $scfg, $storeid, $volname, $snap) = @_;
561 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
563 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
564 run_rbd_command
($cmd, errmsg
=> "rbd snapshot $volname to '$snap' error");
567 sub volume_snapshot_delete
{
568 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
570 return 1 if $running;
572 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
574 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
576 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
578 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
579 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
582 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
584 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
589 sub volume_has_feature
{
590 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
593 snapshot
=> { current
=> 1, snap
=> 1},
594 clone
=> { base
=> 1, snap
=> 1},
595 template
=> { current
=> 1},
596 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
597 sparseinit
=> { base
=> 1, current
=> 1},
600 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
601 $class->parse_volname($volname);
607 $key = $isBase ?
'base' : 'current';
609 return 1 if $features->{$feature}->{$key};