1 package PVE
::Storage
::DRBDPlugin
;
9 use PVE
::Tools
qw(run_command trim);
11 use PVE
::Storage
::Plugin
;
12 use PVE
::JSONSchema
qw(get_standard_option);
14 use base
qw(PVE::Storage::Plugin);
18 my $default_redundancy = 2;
26 content
=> [ {images
=> 1}, { images
=> 1 }],
33 description
=> "The redundancy count specifies the number of nodes to which the resource should be deployed. It must be at least 1 and at most the number of nodes in the cluster.",
37 default => $default_redundancy,
44 redundancy
=> { optional
=> 1 },
45 nodes
=> { optional
=> 1 },
46 disable
=> { optional
=> 1 },
55 return $scfg->{redundancy
} || $default_redundancy;
58 sub connect_drbdmanage_service
{
60 my $bus = Net
::DBus-
>system;
62 my $service = $bus->get_service("org.drbd.drbdmanaged");
64 my $hdl = $service->get_object("/interface", "org.drbd.drbdmanaged");
72 die "got undefined drbd result\n" if !$rc;
74 foreach my $res (@$rc) {
75 my ($code, $format, $details) = @$res;
80 if (defined($format)) {
82 push @args, $details->{$1} // ""
83 while $format =~ s
,\
%\((\w
+)\
),%,;
85 $msg = sprintf($format, @args);
88 $msg = "drbd error: got error code $code";
92 die "drbd error: $msg\n";
98 sub drbd_list_volumes
{
101 $hdl = connect_drbdmanage_service
() if !$hdl;
103 my ($rc, $res) = $hdl->list_volumes([], 0, {}, []);
108 foreach my $entry (@$res) {
109 my ($volname, $properties, $vol_list) = @$entry;
111 next if $volname !~ m/^vm-(\d+)-/;
114 # fixme: we always use volid 0 ?
116 foreach my $volentry (@$vol_list) {
117 my ($vol_id, $vol_properties) = @$volentry;
118 next if $vol_id != 0;
119 my $vol_size = $vol_properties->{vol_size
} * 1024;
120 $size = $vol_size if $vol_size > $size;
123 $volumes->{$volname} = { format
=> 'raw', size
=> $size,
130 # Storage implementation
133 my ($class, $volname) = @_;
135 if ($volname =~ m/^(vm-(\d+)-[a-z][a-z0-9\-\_\.]*[a-z0-9]+)$/) {
136 return ('images', $1, $2, undef, undef, undef, 'raw');
139 die "unable to parse lvm volume name '$volname'\n";
142 sub filesystem_path
{
143 my ($class, $scfg, $volname, $snapname) = @_;
145 die "drbd snapshot is not implemented\n" if defined($snapname);
147 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
149 # fixme: always use volid 0?
150 my $path = "/dev/drbd/by-res/$volname/0";
152 return wantarray ?
($path, $vmid, $vtype) : $path;
156 my ($class, $storeid, $scfg, $volname) = @_;
158 die "can't create base images in drbd storage\n";
162 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
164 die "can't clone images in drbd storage\n";
168 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
170 die "unsupported format '$fmt'" if $fmt ne 'raw';
172 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
173 if defined($name) && $name !~ m/^vm-$vmid-/;
175 my $hdl = connect_drbdmanage_service
();
176 my $volumes = drbd_list_volumes
($hdl);
178 die "volume '$name' already exists\n" if defined($name) && $volumes->{$name};
180 if (!defined($name)) {
181 for (my $i = 1; $i < 100; $i++) {
182 my $tn = "vm-$vmid-disk-$i";
183 if (!defined ($volumes->{$tn})) {
190 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
193 my ($rc, $res) = $hdl->create_resource($name, {});
196 ($rc, $res) = $hdl->create_volume($name, $size, {});
199 ($rc, $res) = $hdl->set_drbdsetup_props(
201 target
=> "resource",
204 'allow-two-primaries' => 'yes',
208 my $redundancy = get_redundancy
($scfg);;
210 ($rc, $res) = $hdl->auto_deploy($name, $redundancy, 0, 0);
217 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
219 my $hdl = connect_drbdmanage_service
();
220 my ($rc, $res) = $hdl->remove_resource($volname, 0);
227 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
229 my $vgname = $scfg->{vgname
};
231 $cache->{drbd_volumes
} = drbd_list_volumes
() if !$cache->{drbd_volumes
};
235 my $dat = $cache->{drbd_volumes
};
237 foreach my $volname (keys %$dat) {
239 my $owner = $dat->{$volname}->{vmid
};
241 my $volid = "$storeid:$volname";
244 my $found = grep { $_ eq $volid } @$vollist;
247 next if defined ($vmid) && ($owner ne $vmid);
250 my $info = $dat->{$volname};
251 $info->{volid
} = $volid;
260 my ($class, $storeid, $scfg, $cache) = @_;
262 my ($total, $avail, $used);
265 my $hdl = connect_drbdmanage_service
();
266 my $redundancy = get_redundancy
($scfg);;
267 my ($rc, $res) = $hdl->cluster_free_query($redundancy);
272 $total = $used + $avail;
277 # assume storage if offline
282 return ($total, $avail, $used, 1);
285 sub activate_storage
{
286 my ($class, $storeid, $scfg, $cache) = @_;
291 sub deactivate_storage
{
292 my ($class, $storeid, $scfg, $cache) = @_;
297 sub activate_volume
{
298 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
300 my $path = $class->path($scfg, $volname);
302 my $hdl = connect_drbdmanage_service
();
303 my $nodename = PVE
::INotify
::nodename
();
304 my ($rc, $res) = $hdl->list_assignments([$nodename], [], 0, {}, []);
307 foreach my $entry (@$res) {
308 my ($node, $res_name, $props, $voldata) = @$entry;
309 if (($node eq $nodename) && ($res_name eq $volname)) {
310 return undef; # assignment already exists
314 # create diskless assignment
315 ($rc, $res) = $hdl->assign($nodename, $volname, { diskless
=> 'true' });
318 # wait until device is acessitble
319 my $print_warning = 1;
320 my $max_wait_time = 20;
321 for (my $i = 0;; $i++) {
324 last if system("dd if=$path of=/dev/null bs=512 count=1 >/dev/null 2>&1") == 0;
326 # correct, but does not work?
327 ($rc, $res) = $hdl->list_assignments([$nodename], [$volname], 0, { "cstate:deploy" => "true" }, []);
329 my $len = scalar(@$res);
332 die "aborting wait - device '$path' still not readable\n" if $i > $max_wait_time;
333 print "waiting for device '$path' to become ready...\n" if $print_warning;
341 sub deactivate_volume
{
342 my ($class, $storeid, $scfg, $volname, $cache) = @_;
344 return undef; # fixme: should we unassign ?
346 # remove above return to enable this code
347 my $hdl = connect_drbdmanage_service
();
348 my $nodename = PVE
::INotify
::nodename
();
349 my ($rc, $res) = $hdl->list_assignments([$nodename], [$volname], 0,
350 { "cstate:diskless" => "true" }, []);
353 my ($rc, $res) = $hdl->unassign($nodename, $volname,0);
361 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
363 $size = ($size/1024/1024) . "M";
365 my $path = $class->path($scfg, $volname);
367 # fixme: howto implement this
368 die "drbd volume_resize is not implemented";
370 #my $cmd = ['/sbin/lvextend', '-L', $size, $path];
371 #run_command($cmd, errmsg => "error resizing volume '$path'");
376 sub volume_snapshot
{
377 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
379 die "drbd snapshot is not implemented";
382 sub volume_snapshot_rollback
{
383 my ($class, $scfg, $storeid, $volname, $snap) = @_;
385 die "drbd snapshot rollback is not implemented";
388 sub volume_snapshot_delete
{
389 my ($class, $scfg, $storeid, $volname, $snap) = @_;
391 die "drbd snapshot delete is not implemented";
394 sub volume_has_feature
{
395 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
398 copy
=> { base
=> 1, current
=> 1},
401 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
402 $class->parse_volname($volname);
408 $key = $isBase ?
'base' : 'current';
410 return 1 if $features->{$feature}->{$key};