1 package PVE
::Storage
::RBDPlugin
;
11 use PVE
::JSONSchema
qw(get_standard_option);
14 use PVE
::Storage
::Plugin
;
15 use PVE
::Tools
qw(run_command trim);
17 use base
qw(PVE::Storage::Plugin);
19 my $get_parent_image_name = sub {
21 return undef if !$parent;
22 return $parent->{image
} . "@" . $parent->{snapshot
};
26 my ($scfg, $volume) = @_;
27 my $path = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
28 $path .= "/$scfg->{namespace}" if defined($scfg->{namespace
});
29 $path .= "/$volume" if defined($volume);
34 my ($binary, $scfg, $storeid, $op, @options) = @_;
36 my $cmd_option = PVE
::CephConfig
::ceph_connect_option
($scfg, $storeid);
37 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
39 my $cmd = [$binary, '-p', $pool];
41 if (defined(my $namespace = $scfg->{namespace
})) {
42 # some subcommands will fail if the --namespace parameter is present
43 my $no_namespace_parameter = {
46 push @$cmd, '--namespace', "$namespace" if !$no_namespace_parameter->{$op};
48 push @$cmd, '-c', $cmd_option->{ceph_conf
} if ($cmd_option->{ceph_conf
});
49 push @$cmd, '-m', $cmd_option->{mon_host
} if ($cmd_option->{mon_host
});
50 push @$cmd, '--auth_supported', $cmd_option->{auth_supported
} if ($cmd_option->{auth_supported
});
51 push @$cmd, '-n', "client.$cmd_option->{userid}" if ($cmd_option->{userid
});
52 push @$cmd, '--keyring', $cmd_option->{keyring
} if ($cmd_option->{keyring
});
56 push @$cmd, @options if scalar(@options);
62 my ($scfg, $storeid, $op, @options) = @_;
64 return $build_cmd->('/usr/bin/rbd', $scfg, $storeid, $op, @options);
68 my ($scfg, $storeid, $op, @options) = @_;
70 return $build_cmd->('/usr/bin/rados', $scfg, $storeid, $op, @options);
73 my $librados_connect = sub {
74 my ($scfg, $storeid, $options) = @_;
76 my $librados_config = PVE
::CephConfig
::ceph_connect_option
($scfg, $storeid);
78 my $rados = PVE
::RADOS-
>new(%$librados_config);
83 # needed for volumes created using ceph jewel (or higher)
84 my $krbd_feature_update = sub {
85 my ($scfg, $storeid, $name) = @_;
87 my (@disable, @enable);
88 my ($kmajor, $kminor) = PVE
::ProcFSTools
::kernel_version
();
90 if ($kmajor > 5 || $kmajor == 5 && $kminor >= 3) {
91 # 'deep-flatten' can only be disabled, not enabled after image creation
92 push @enable, 'fast-diff', 'object-map';
94 push @disable, 'fast-diff', 'object-map', 'deep-flatten';
98 push @enable, 'exclusive-lock';
100 push @disable, 'exclusive-lock';
103 my $active_features_list = (rbd_volume_info
($scfg, $storeid, $name))[4];
104 my $active_features = { map { $_ => 1 } @$active_features_list };
106 my $to_disable = join(',', grep { $active_features->{$_} } @disable);
107 my $to_enable = join(',', grep { !$active_features->{$_} } @enable );
110 print "disable RBD image features this kernel RBD drivers is not compatible with: $to_disable\n";
111 my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'disable', $name, $to_disable);
114 errmsg
=> "could not disable krbd-incompatible image features '$to_disable' for rbd image: $name",
118 print "enable RBD image features this kernel RBD drivers supports: $to_enable\n";
120 my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'enable', $name, $to_enable);
123 errmsg
=> "could not enable krbd-compatible image features '$to_enable' for rbd image: $name",
130 sub run_rbd_command
{
131 my ($cmd, %args) = @_;
134 my $errmsg = $args{errmsg
} . ": " || "";
135 if (!exists($args{errfunc
})) {
136 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
137 # at least 1 child(ren) in pool cephstor1
138 $args{errfunc
} = sub {
140 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
145 print STDERR
$lasterr;
150 eval { run_command
($cmd, %args); };
152 die $errmsg . $lasterr if length($lasterr);
160 my ($scfg, $storeid) = @_;
162 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
163 $pool .= "/$scfg->{namespace}" if defined($scfg->{namespace
});
166 my $parser = sub { $raw .= shift };
168 my $cmd = $rbd_cmd->($scfg, $storeid, 'ls', '-l', '--format', 'json');
170 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
174 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
179 } elsif ($raw =~ m/^(\[.*\])$/s) { # untaint
180 $result = JSON
::decode_json
($1);
182 die "got unexpected data from rbd ls: '$raw'\n";
187 foreach my $el (@$result) {
188 next if defined($el->{snapshot
});
190 my $image = $el->{image
};
192 my ($owner) = $image =~ m/^(?:vm|base)-(\d+)-/;
193 next if !defined($owner);
195 $list->{$pool}->{$image} = {
198 parent
=> $get_parent_image_name->($el->{parent
}),
207 my ($scfg, $storeid, $name) = @_;
209 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'ls', $name, '--format', 'json');
212 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> sub { $raw .= shift; });
215 if ($raw =~ m/^(\[.*\])$/s) { # untaint
216 $list = eval { JSON
::decode_json
($1) };
217 die "invalid JSON output from 'rbd snap ls $name': $@\n" if $@;
219 die "got unexpected data from 'rbd snap ls $name': '$raw'\n";
222 $list = [] if !defined($list);
225 foreach my $el (@$list) {
226 my $snap = $el->{name
};
227 my $protected = defined($el->{protected
}) && $el->{protected
} eq "true" ?
1 : undef;
230 id
=> $el->{id
} // undef,
231 size
=> $el->{size
} // 0,
232 protected
=> $protected,
238 sub rbd_volume_info
{
239 my ($scfg, $storeid, $volname, $snap) = @_;
243 my @options = ('info', $volname, '--format', 'json');
245 push @options, '--snap', $snap;
248 $cmd = $rbd_cmd->($scfg, $storeid, @options);
251 my $parser = sub { $raw .= shift };
253 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
258 } elsif ($raw =~ m/^(\{.*\})$/s) { # untaint
259 $volume = JSON
::decode_json
($1);
261 die "got unexpected data from rbd info: '$raw'\n";
264 $volume->{parent
} = $get_parent_image_name->($volume->{parent
});
265 $volume->{protected
} = defined($volume->{protected
}) && $volume->{protected
} eq "true" ?
1 : undef;
267 return $volume->@{qw(size parent format protected features)};
278 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
285 description
=> "IP addresses of monitors (for external clusters).",
286 type
=> 'string', format
=> 'pve-storage-portal-dns-list',
289 description
=> "Pool.",
293 description
=> "Data Pool (for erasure coding only)",
297 description
=> "RBD Namespace.",
301 description
=> "RBD Id.",
305 description
=> "Authsupported.",
309 description
=> "Always access rbd through krbd kernel module.",
313 description
=> "Client keyring contents (for external clusters).",
321 nodes
=> { optional
=> 1 },
322 disable
=> { optional
=> 1 },
323 monhost
=> { optional
=> 1},
324 pool
=> { optional
=> 1 },
325 'data-pool' => { optional
=> 1 },
326 namespace
=> { optional
=> 1 },
327 username
=> { optional
=> 1 },
328 content
=> { optional
=> 1 },
329 krbd
=> { optional
=> 1 },
330 keyring
=> { optional
=> 1 },
331 bwlimit
=> { optional
=> 1 },
335 # Storage implementation
338 my ($class, $storeid, $scfg, %param) = @_;
340 my $secret = $param{keyring
} if defined $param{keyring
} // undef;
341 PVE
::CephConfig
::ceph_create_keyfile
($scfg->{type
}, $storeid, $secret);
347 my ($class, $storeid, $scfg, %param) = @_;
349 if (exists($param{keyring
})) {
350 if (defined($param{keyring
})) {
351 PVE
::CephConfig
::ceph_create_keyfile
($scfg->{type
}, $storeid, $param{keyring
});
353 PVE
::CephConfig
::ceph_remove_keyfile
($scfg->{type
}, $storeid);
361 my ($class, $storeid, $scfg) = @_;
362 PVE
::CephConfig
::ceph_remove_keyfile
($scfg->{type
}, $storeid);
367 my ($class, $volname) = @_;
369 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
370 return ('images', $4, $7, $2, $3, $5, 'raw');
373 die "unable to parse rbd volume name '$volname'\n";
377 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
379 my $cmd_option = PVE
::CephConfig
::ceph_connect_option
($scfg, $storeid);
380 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
381 $name .= '@'.$snapname if $snapname;
383 my $rbd_path = get_rbd_path
($scfg, $name);
384 return ("/dev/rbd/${rbd_path}", $vmid, $vtype) if $scfg->{krbd
};
386 my $path = "rbd:${rbd_path}";
388 $path .= ":conf=$cmd_option->{ceph_conf}" if $cmd_option->{ceph_conf
};
389 if (defined($scfg->{monhost
})) {
390 my $monhost = PVE
::CephConfig
::hostlist
($scfg->{monhost
}, ';');
391 $monhost =~ s/:/\\:/g;
392 $path .= ":mon_host=$monhost";
393 $path .= ":auth_supported=$cmd_option->{auth_supported}";
396 $path .= ":id=$cmd_option->{userid}:keyring=$cmd_option->{keyring}" if ($cmd_option->{keyring
});
398 return ($path, $vmid, $vtype);
401 sub find_free_diskname
{
402 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
404 my $cmd = $rbd_cmd->($scfg, $storeid, 'ls');
410 if ($line =~ m/^(.*)$/) { # untaint
411 push @$disk_list, $1;
416 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
420 die $err if $err && $err !~ m/doesn't contain rbd images/;
422 return PVE
::Storage
::Plugin
::get_next_vm_diskname
($disk_list, $storeid, $vmid, undef, $scfg);
426 my ($class, $storeid, $scfg, $volname) = @_;
428 my $snap = '__base__';
430 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
431 $class->parse_volname($volname);
433 die "create_base not possible with base image\n" if $isBase;
435 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
436 die "rbd volume info on '$name' failed\n" if !($size);
438 die "rbd image must be at format V2" if $format ne "2";
440 die "volname '$volname' contains wrong information about parent $parent $basename\n"
441 if $basename && (!$parent || $parent ne $basename."@".$snap);
444 $newname =~ s/^vm-/base-/;
446 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
448 my $cmd = $rbd_cmd->(
452 get_rbd_path
($scfg, $name),
453 get_rbd_path
($scfg, $newname),
455 run_rbd_command
($cmd, errmsg
=> "rbd rename '$name' error");
457 my $running = undef; #fixme : is create_base always offline ?
459 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
461 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
464 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
465 run_rbd_command
($cmd, errmsg
=> "rbd protect $newname snap '$snap' error");
473 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
475 my $snap = '__base__';
476 $snap = $snapname if length $snapname;
478 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
479 $class->parse_volname($volname);
481 die "$volname is not a base image and snapname is not provided\n"
482 if !$isBase && !length($snapname);
484 my $name = $class->find_free_diskname($storeid, $scfg, $vmid);
486 warn "clone $volname: $basename snapname $snap to $name\n";
488 if (length($snapname)) {
489 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $volname, $snapname);
492 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
493 run_rbd_command
($cmd, errmsg
=> "rbd protect $volname snap $snapname error");
497 my $newvol = "$basename/$name";
498 $newvol = $name if length($snapname);
501 get_rbd_path
($scfg, $basename),
504 push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'};
506 my $cmd = $rbd_cmd->($scfg, $storeid, 'clone', @options, get_rbd_path
($scfg, $name));
507 run_rbd_command
($cmd, errmsg
=> "rbd clone '$basename' error");
513 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
516 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
517 if $name && $name !~ m/^vm-$vmid-/;
519 $name = $class->find_free_diskname($storeid, $scfg, $vmid) if !$name;
522 '--image-format' , 2,
523 '--size', int(($size + 1023) / 1024),
525 push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'};
527 my $cmd = $rbd_cmd->($scfg, $storeid, 'create', @options, $name);
528 run_rbd_command
($cmd, errmsg
=> "rbd create '$name' error");
534 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
536 my ($vtype, $name, $vmid, undef, undef, undef) =
537 $class->parse_volname($volname);
540 my $snaps = rbd_ls_snap
($scfg, $storeid, $name);
541 foreach my $snap (keys %$snaps) {
542 if ($snaps->{$snap}->{protected
}) {
543 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
544 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
548 $class->deactivate_volume($storeid, $scfg, $volname);
550 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'purge', $name);
551 run_rbd_command
($cmd, errmsg
=> "rbd snap purge '$name' error");
553 $cmd = $rbd_cmd->($scfg, $storeid, 'rm', $name);
554 run_rbd_command
($cmd, errmsg
=> "rbd rm '$name' error");
560 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
562 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
564 my $dat = $cache->{rbd
}->{get_rbd_path
($scfg)};
565 return [] if !$dat; # nothing found
568 for my $image (sort keys %$dat) {
569 my $info = $dat->{$image};
570 my ($volname, $parent, $owner) = $info->@{'name', 'parent', 'vmid'};
572 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
573 $info->{volid
} = "$storeid:$1/$volname";
575 $info->{volid
} = "$storeid:$volname";
579 my $found = grep { $_ eq $info->{volid
} } @$vollist;
582 next if defined ($vmid) && ($owner ne $vmid);
585 $info->{format
} = 'raw';
594 my ($class, $storeid, $scfg, $cache) = @_;
596 my $rados = $librados_connect->($scfg, $storeid);
597 my $df = $rados->mon_command({ prefix
=> 'df', format
=> 'json' });
599 my ($d) = grep { $_->{name
} eq $scfg->{pool
} } @{$df->{pools
}};
601 # max_avail -> max available space for data w/o replication in the pool
602 # bytes_used -> data w/o replication in the pool
603 my $free = $d->{stats
}->{max_avail
};
604 my $used = $d->{stats
}->{stored
} // $d->{stats
}->{bytes_used
};
605 my $total = $used + $free;
608 return ($total, $free, $used, $active);
611 sub activate_storage
{
612 my ($class, $storeid, $scfg, $cache) = @_;
616 sub deactivate_storage
{
617 my ($class, $storeid, $scfg, $cache) = @_;
621 my sub get_kernel_device_path
{
622 my ($scfg, $name) = @_;
623 return "/dev/rbd/" . get_rbd_path
($scfg, $name);
627 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
629 my ($vtype, $img_name, $vmid) = $class->parse_volname($volname);
631 my $name = $img_name;
632 $name .= '@'.$snapname if $snapname;
634 my $kerneldev = get_kernel_device_path
($scfg, $name);
636 return $kerneldev if -b
$kerneldev; # already mapped
638 # features can only be enabled/disabled for image, not for snapshot!
639 $krbd_feature_update->($scfg, $storeid, $img_name);
641 my $cmd = $rbd_cmd->($scfg, $storeid, 'map', $name);
642 run_rbd_command
($cmd, errmsg
=> "can't map rbd volume $name");
648 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
650 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
651 $name .= '@'.$snapname if $snapname;
653 my $kerneldev = get_kernel_device_path
($scfg, $name);
656 my $cmd = $rbd_cmd->($scfg, $storeid, 'unmap', $kerneldev);
657 run_rbd_command
($cmd, errmsg
=> "can't unmap rbd device $kerneldev");
663 sub activate_volume
{
664 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
666 $class->map_volume($storeid, $scfg, $volname, $snapname) if $scfg->{krbd
};
671 sub deactivate_volume
{
672 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
674 $class->unmap_volume($storeid, $scfg, $volname, $snapname);
679 sub volume_size_info
{
680 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
682 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
683 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
688 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
690 return 1 if $running && !$scfg->{krbd
}; # FIXME???
692 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
694 my $cmd = $rbd_cmd->($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
695 run_rbd_command
($cmd, errmsg
=> "rbd resize '$volname' error");
699 sub volume_snapshot
{
700 my ($class, $scfg, $storeid, $volname, $snap) = @_;
702 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
704 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
705 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
709 sub volume_snapshot_rollback
{
710 my ($class, $scfg, $storeid, $volname, $snap) = @_;
712 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
714 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
715 run_rbd_command
($cmd, errmsg
=> "rbd snapshot $volname to '$snap' error");
718 sub volume_snapshot_delete
{
719 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
721 return 1 if $running && !$scfg->{krbd
}; # FIXME: ????
723 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
725 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
727 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
729 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
730 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
733 my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
735 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
740 sub volume_snapshot_needs_fsfreeze
{
744 sub volume_has_feature
{
745 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
748 snapshot
=> { current
=> 1, snap
=> 1},
749 clone
=> { base
=> 1, snap
=> 1},
750 template
=> { current
=> 1},
751 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
752 sparseinit
=> { base
=> 1, current
=> 1},
753 rename => {current
=> 1},
756 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = $class->parse_volname($volname);
762 $key = $isBase ?
'base' : 'current';
764 return 1 if $features->{$feature}->{$key};
770 my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
780 ) = $class->parse_volname($source_volname);
781 $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format)
785 my $cmd = $rbd_cmd->($scfg, $storeid, 'info', $target_volname);
786 run_rbd_command
($cmd, errmsg
=> "exist check", quiet
=> 1);
788 die "target volume '${target_volname}' already exists\n" if !$@;
790 my $cmd = $rbd_cmd->($scfg, $storeid, 'rename', $source_image, $target_volname);
794 errmsg
=> "could not rename image '${source_image}' to '${target_volname}'",
797 $base_name = $base_name ?
"${base_name}/" : '';
799 return "${storeid}:${base_name}${target_volname}";