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, rootdir
=> 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 content
=> { optional
=> 1 },
46 nodes
=> { optional
=> 1 },
47 disable
=> { optional
=> 1 },
56 return $scfg->{redundancy
} || $default_redundancy;
59 sub connect_drbdmanage_service
{
61 my $bus = Net
::DBus-
>system;
63 my $service = $bus->get_service("org.drbd.drbdmanaged");
65 my $hdl = $service->get_object("/interface", "org.drbd.drbdmanaged");
73 die "got undefined drbd result\n" if !$rc;
75 # Messages for return codes 1 to 99 are not considered an error.
76 foreach my $res (@$rc) {
77 my ($code, $format, $details) = @$res;
82 if (defined($format)) {
84 push @args, $details->{$1} // ""
85 while $format =~ s
,\
%\((\w
+)\
),%,;
87 $msg = sprintf($format, @args);
90 $msg = "drbd error: got error code $code";
94 die "drbd error: $msg\n";
100 sub drbd_list_volumes
{
103 $hdl = connect_drbdmanage_service
() if !$hdl;
105 my ($rc, $res) = $hdl->list_volumes([], 0, {}, []);
110 foreach my $entry (@$res) {
111 my ($volname, $properties, $vol_list) = @$entry;
113 next if $volname !~ m/^vm-(\d+)-/;
116 # fixme: we always use volid 0 ?
118 foreach my $volentry (@$vol_list) {
119 my ($vol_id, $vol_properties) = @$volentry;
120 next if $vol_id != 0;
121 my $vol_size = $vol_properties->{vol_size
} * 1024;
122 $size = $vol_size if $vol_size > $size;
125 $volumes->{$volname} = { format
=> 'raw', size
=> $size,
132 # Storage implementation
135 my ($class, $volname) = @_;
137 if ($volname =~ m/^(vm-(\d+)-[a-z][a-z0-9\-\_\.]*[a-z0-9]+)$/) {
138 return ('images', $1, $2, undef, undef, undef, 'raw');
141 die "unable to parse lvm volume name '$volname'\n";
144 sub filesystem_path
{
145 my ($class, $scfg, $volname, $snapname) = @_;
147 die "drbd snapshot is not implemented\n" if defined($snapname);
149 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
151 # fixme: always use volid 0?
152 my $path = "/dev/drbd/by-res/$volname/0";
154 return wantarray ?
($path, $vmid, $vtype) : $path;
158 my ($class, $storeid, $scfg, $volname) = @_;
160 die "can't create base images in drbd storage\n";
164 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
166 die "can't clone images in drbd storage\n";
170 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
172 die "unsupported format '$fmt'" if $fmt ne 'raw';
174 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
175 if defined($name) && $name !~ m/^vm-$vmid-/;
177 my $hdl = connect_drbdmanage_service
();
178 my $volumes = drbd_list_volumes
($hdl);
180 die "volume '$name' already exists\n" if defined($name) && $volumes->{$name};
182 if (!defined($name)) {
183 for (my $i = 1; $i < 100; $i++) {
184 my $tn = "vm-$vmid-disk-$i";
185 if (!defined ($volumes->{$tn})) {
192 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
195 my ($rc, $res) = $hdl->create_resource($name, {});
198 ($rc, $res) = $hdl->create_volume($name, $size, {});
201 ($rc, $res) = $hdl->set_drbdsetup_props(
203 target
=> "resource",
206 'allow-two-primaries' => 'yes',
210 my $redundancy = get_redundancy
($scfg);;
212 ($rc, $res) = $hdl->auto_deploy($name, $redundancy, 0, 0);
219 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
221 my $hdl = connect_drbdmanage_service
();
222 my ($rc, $res) = $hdl->remove_resource($volname, 0);
229 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
231 my $vgname = $scfg->{vgname
};
233 $cache->{drbd_volumes
} = drbd_list_volumes
() if !$cache->{drbd_volumes
};
237 my $dat = $cache->{drbd_volumes
};
239 foreach my $volname (keys %$dat) {
241 my $owner = $dat->{$volname}->{vmid
};
243 my $volid = "$storeid:$volname";
246 my $found = grep { $_ eq $volid } @$vollist;
249 next if defined ($vmid) && ($owner ne $vmid);
252 my $info = $dat->{$volname};
253 $info->{volid
} = $volid;
262 my ($class, $storeid, $scfg, $cache) = @_;
264 my ($total, $avail, $used);
267 my $hdl = connect_drbdmanage_service
();
268 my $redundancy = get_redundancy
($scfg);;
269 my ($rc, $free_space, $total_space) = $hdl->cluster_free_query($redundancy);
272 $avail = $free_space*1024;
273 $total = $total_space*1024;
274 $used = $total - $avail;
279 # assume storage if offline
284 return ($total, $avail, $used, 1);
287 sub activate_storage
{
288 my ($class, $storeid, $scfg, $cache) = @_;
293 sub deactivate_storage
{
294 my ($class, $storeid, $scfg, $cache) = @_;
299 sub activate_volume
{
300 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
302 die "Snapshot not implemented on DRBD\n" if $snapname;
304 my $path = $class->path($scfg, $volname);
306 my $hdl = connect_drbdmanage_service
();
307 my $nodename = PVE
::INotify
::nodename
();
308 my ($rc, $res) = $hdl->list_assignments([$nodename], [$volname], 0, {}, []);
311 # assignment already exists?
312 return undef if @$res;
314 # create diskless assignment
315 ($rc, $res) = $hdl->assign($nodename, $volname, { diskless
=> 'true' });
318 # wait until device is accessible
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, $snapname, $cache) = @_;
344 die "Snapshot not implemented on DRBD\n" if $snapname;
346 return undef; # fixme: should we unassign ?
348 # remove above return to enable this code
349 my $hdl = connect_drbdmanage_service
();
350 my $nodename = PVE
::INotify
::nodename
();
351 my ($rc, $res) = $hdl->list_assignments([$nodename], [$volname], 0,
352 { "cstate:diskless" => "true" }, []);
355 my ($rc, $res) = $hdl->unassign($nodename, $volname,0);
363 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
365 $size = ($size/1024/1024) . "M";
367 my $path = $class->path($scfg, $volname);
369 # fixme: howto implement this
370 die "drbd volume_resize is not implemented";
372 #my $cmd = ['/sbin/lvextend', '-L', $size, $path];
373 #run_command($cmd, errmsg => "error resizing volume '$path'");
378 sub volume_snapshot
{
379 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
381 die "drbd snapshot is not implemented";
384 sub volume_snapshot_rollback
{
385 my ($class, $scfg, $storeid, $volname, $snap) = @_;
387 die "drbd snapshot rollback is not implemented";
390 sub volume_snapshot_delete
{
391 my ($class, $scfg, $storeid, $volname, $snap) = @_;
393 die "drbd snapshot delete is not implemented";
396 sub volume_has_feature
{
397 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
400 copy
=> { base
=> 1, current
=> 1},
403 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
404 $class->parse_volname($volname);
410 $key = $isBase ?
'base' : 'current';
412 return 1 if $features->{$feature}->{$key};