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 foreach my $res (@$rc) {
76 my ($code, $format, $details) = @$res;
81 if (defined($format)) {
83 push @args, $details->{$1} // ""
84 while $format =~ s
,\
%\((\w
+)\
),%,;
86 $msg = sprintf($format, @args);
89 $msg = "drbd error: got error code $code";
93 die "drbd error: $msg\n";
99 sub drbd_list_volumes
{
102 $hdl = connect_drbdmanage_service
() if !$hdl;
104 my ($rc, $res) = $hdl->list_volumes([], 0, {}, []);
109 foreach my $entry (@$res) {
110 my ($volname, $properties, $vol_list) = @$entry;
112 next if $volname !~ m/^vm-(\d+)-/;
115 # fixme: we always use volid 0 ?
117 foreach my $volentry (@$vol_list) {
118 my ($vol_id, $vol_properties) = @$volentry;
119 next if $vol_id != 0;
120 my $vol_size = $vol_properties->{vol_size
} * 1024;
121 $size = $vol_size if $vol_size > $size;
124 $volumes->{$volname} = { format
=> 'raw', size
=> $size,
131 # Storage implementation
134 my ($class, $volname) = @_;
136 if ($volname =~ m/^(vm-(\d+)-[a-z][a-z0-9\-\_\.]*[a-z0-9]+)$/) {
137 return ('images', $1, $2, undef, undef, undef, 'raw');
140 die "unable to parse lvm volume name '$volname'\n";
143 sub filesystem_path
{
144 my ($class, $scfg, $volname, $snapname) = @_;
146 die "drbd snapshot is not implemented\n" if defined($snapname);
148 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
150 # fixme: always use volid 0?
151 my $path = "/dev/drbd/by-res/$volname/0";
153 return wantarray ?
($path, $vmid, $vtype) : $path;
157 my ($class, $storeid, $scfg, $volname) = @_;
159 die "can't create base images in drbd storage\n";
163 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
165 die "can't clone images in drbd storage\n";
169 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
171 die "unsupported format '$fmt'" if $fmt ne 'raw';
173 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
174 if defined($name) && $name !~ m/^vm-$vmid-/;
176 my $hdl = connect_drbdmanage_service
();
177 my $volumes = drbd_list_volumes
($hdl);
179 die "volume '$name' already exists\n" if defined($name) && $volumes->{$name};
181 if (!defined($name)) {
182 for (my $i = 1; $i < 100; $i++) {
183 my $tn = "vm-$vmid-disk-$i";
184 if (!defined ($volumes->{$tn})) {
191 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
194 my ($rc, $res) = $hdl->create_resource($name, {});
197 ($rc, $res) = $hdl->create_volume($name, $size, {});
200 ($rc, $res) = $hdl->set_drbdsetup_props(
202 target
=> "resource",
205 'allow-two-primaries' => 'yes',
209 my $redundancy = get_redundancy
($scfg);;
211 ($rc, $res) = $hdl->auto_deploy($name, $redundancy, 0, 0);
218 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
220 my $hdl = connect_drbdmanage_service
();
221 my ($rc, $res) = $hdl->remove_resource($volname, 0);
228 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
230 my $vgname = $scfg->{vgname
};
232 $cache->{drbd_volumes
} = drbd_list_volumes
() if !$cache->{drbd_volumes
};
236 my $dat = $cache->{drbd_volumes
};
238 foreach my $volname (keys %$dat) {
240 my $owner = $dat->{$volname}->{vmid
};
242 my $volid = "$storeid:$volname";
245 my $found = grep { $_ eq $volid } @$vollist;
248 next if defined ($vmid) && ($owner ne $vmid);
251 my $info = $dat->{$volname};
252 $info->{volid
} = $volid;
261 my ($class, $storeid, $scfg, $cache) = @_;
263 my ($total, $avail, $used);
266 my $hdl = connect_drbdmanage_service
();
267 my $redundancy = get_redundancy
($scfg);;
268 my ($rc, $free_space, $total_space) = $hdl->cluster_free_query($redundancy);
271 $avail = $free_space;
272 $total = $total_space;
273 $used = $total_space - $free_space;
278 # assume storage if offline
283 return ($total, $avail, $used, 1);
286 sub activate_storage
{
287 my ($class, $storeid, $scfg, $cache) = @_;
292 sub deactivate_storage
{
293 my ($class, $storeid, $scfg, $cache) = @_;
298 sub activate_volume
{
299 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
301 die "Snapshot not implemented on DRBD\n" if $snapname;
303 my $path = $class->path($scfg, $volname);
305 my $hdl = connect_drbdmanage_service
();
306 my $nodename = PVE
::INotify
::nodename
();
307 my ($rc, $res) = $hdl->list_assignments([$nodename], [], 0, {}, []);
310 foreach my $entry (@$res) {
311 my ($node, $res_name, $props, $voldata) = @$entry;
312 if (($node eq $nodename) && ($res_name eq $volname)) {
313 return undef; # assignment already exists
317 # create diskless assignment
318 ($rc, $res) = $hdl->assign($nodename, $volname, { diskless
=> 'true' });
321 # wait until device is acessitble
322 my $print_warning = 1;
323 my $max_wait_time = 20;
324 for (my $i = 0;; $i++) {
327 last if system("dd if=$path of=/dev/null bs=512 count=1 >/dev/null 2>&1") == 0;
329 # correct, but does not work?
330 ($rc, $res) = $hdl->list_assignments([$nodename], [$volname], 0, { "cstate:deploy" => "true" }, []);
332 my $len = scalar(@$res);
335 die "aborting wait - device '$path' still not readable\n" if $i > $max_wait_time;
336 print "waiting for device '$path' to become ready...\n" if $print_warning;
344 sub deactivate_volume
{
345 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
347 die "Snapshot not implemented on DRBD\n" if $snapname;
349 return undef; # fixme: should we unassign ?
351 # remove above return to enable this code
352 my $hdl = connect_drbdmanage_service
();
353 my $nodename = PVE
::INotify
::nodename
();
354 my ($rc, $res) = $hdl->list_assignments([$nodename], [$volname], 0,
355 { "cstate:diskless" => "true" }, []);
358 my ($rc, $res) = $hdl->unassign($nodename, $volname,0);
366 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
368 $size = ($size/1024/1024) . "M";
370 my $path = $class->path($scfg, $volname);
372 # fixme: howto implement this
373 die "drbd volume_resize is not implemented";
375 #my $cmd = ['/sbin/lvextend', '-L', $size, $path];
376 #run_command($cmd, errmsg => "error resizing volume '$path'");
381 sub volume_snapshot
{
382 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
384 die "drbd snapshot is not implemented";
387 sub volume_snapshot_rollback
{
388 my ($class, $scfg, $storeid, $volname, $snap) = @_;
390 die "drbd snapshot rollback is not implemented";
393 sub volume_snapshot_delete
{
394 my ($class, $scfg, $storeid, $volname, $snap) = @_;
396 die "drbd snapshot delete is not implemented";
399 sub volume_has_feature
{
400 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
403 copy
=> { base
=> 1, current
=> 1},
406 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
407 $class->parse_volname($volname);
413 $key = $isBase ?
'base' : 'current';
415 return 1 if $features->{$feature}->{$key};