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 },
48 bwlimit
=> { optional
=> 1 },
57 return $scfg->{redundancy
} || $default_redundancy;
60 sub connect_drbdmanage_service
{
62 my $bus = Net
::DBus-
>system;
64 my $service = $bus->get_service("org.drbd.drbdmanaged");
66 my $hdl = $service->get_object("/interface", "org.drbd.drbdmanaged");
74 die "got undefined drbd result\n" if !$rc;
76 # Messages for return codes 1 to 99 are not considered an error.
77 foreach my $res (@$rc) {
78 my ($code, $format, $details) = @$res;
83 if (defined($format)) {
85 push @args, $details->{$1} // ""
86 while $format =~ s
,\
%\((\w
+)\
),%,;
88 $msg = sprintf($format, @args);
91 $msg = "drbd error: got error code $code";
95 die "drbd error: $msg\n";
101 sub drbd_list_volumes
{
104 $hdl = connect_drbdmanage_service
() if !$hdl;
106 my ($rc, $res) = $hdl->list_volumes([], 0, {}, []);
111 foreach my $entry (@$res) {
112 my ($volname, $properties, $vol_list) = @$entry;
114 next if $volname !~ m/^vm-(\d+)-/;
117 # fixme: we always use volid 0 ?
119 foreach my $volentry (@$vol_list) {
120 my ($vol_id, $vol_properties) = @$volentry;
121 next if $vol_id != 0;
122 my $vol_size = $vol_properties->{vol_size
} * 1024;
123 $size = $vol_size if $vol_size > $size;
126 $volumes->{$volname} = { format
=> 'raw', size
=> $size,
133 # Storage implementation
136 my ($class, $volname) = @_;
138 if ($volname =~ m/^(vm-(\d+)-[a-z][a-z0-9\-\_\.]*[a-z0-9]+)$/) {
139 return ('images', $1, $2, undef, undef, undef, 'raw');
142 die "unable to parse lvm volume name '$volname'\n";
145 sub filesystem_path
{
146 my ($class, $scfg, $volname, $snapname) = @_;
148 die "drbd snapshot is not implemented\n" if defined($snapname);
150 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
152 # fixme: always use volid 0?
153 my $path = "/dev/drbd/by-res/$volname/0";
155 return wantarray ?
($path, $vmid, $vtype) : $path;
159 my ($class, $storeid, $scfg, $volname) = @_;
161 die "can't create base images in drbd storage\n";
165 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
167 die "can't clone images in drbd storage\n";
171 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
173 die "unsupported format '$fmt'" if $fmt ne 'raw';
175 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
176 if defined($name) && $name !~ m/^vm-$vmid-/;
178 my $hdl = connect_drbdmanage_service
();
179 my $volumes = drbd_list_volumes
($hdl);
181 die "volume '$name' already exists\n" if defined($name) && $volumes->{$name};
183 if (!defined($name)) {
184 for (my $i = 1; $i < 100; $i++) {
185 my $tn = "vm-$vmid-disk-$i";
186 if (!defined ($volumes->{$tn})) {
193 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
196 my ($rc, $res) = $hdl->create_resource($name, {});
199 ($rc, $res) = $hdl->create_volume($name, $size, {});
202 ($rc, $res) = $hdl->set_drbdsetup_props(
204 target
=> "resource",
207 'allow-two-primaries' => 'yes',
211 my $redundancy = get_redundancy
($scfg);;
213 ($rc, $res) = $hdl->auto_deploy($name, $redundancy, 0, 0);
220 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
222 my $hdl = connect_drbdmanage_service
();
223 my ($rc, $res) = $hdl->remove_resource($volname, 0);
230 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
232 my $vgname = $scfg->{vgname
};
234 $cache->{drbd_volumes
} = drbd_list_volumes
() if !$cache->{drbd_volumes
};
238 my $dat = $cache->{drbd_volumes
};
240 foreach my $volname (keys %$dat) {
242 my $owner = $dat->{$volname}->{vmid
};
244 my $volid = "$storeid:$volname";
247 my $found = grep { $_ eq $volid } @$vollist;
250 next if defined ($vmid) && ($owner ne $vmid);
253 my $info = $dat->{$volname};
254 $info->{volid
} = $volid;
263 my ($class, $storeid, $scfg, $cache) = @_;
265 my ($total, $avail, $used);
268 my $hdl = connect_drbdmanage_service
();
269 my $redundancy = get_redundancy
($scfg);;
270 my ($rc, $free_space, $total_space) = $hdl->cluster_free_query($redundancy);
273 $avail = $free_space*1024;
274 $total = $total_space*1024;
275 $used = $total - $avail;
280 # assume storage if offline
285 return ($total, $avail, $used, 1);
288 sub activate_storage
{
289 my ($class, $storeid, $scfg, $cache) = @_;
294 sub deactivate_storage
{
295 my ($class, $storeid, $scfg, $cache) = @_;
300 sub activate_volume
{
301 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
303 die "Snapshot not implemented on DRBD\n" if $snapname;
305 my $path = $class->path($scfg, $volname);
307 my $hdl = connect_drbdmanage_service
();
308 my $nodename = PVE
::INotify
::nodename
();
309 my ($rc, $res) = $hdl->list_assignments([$nodename], [$volname], 0, {}, []);
312 # assignment already exists?
313 return undef if @$res;
315 # create diskless assignment
316 ($rc, $res) = $hdl->assign($nodename, $volname, { diskless
=> 'true' });
319 # wait until device is accessible
320 my $print_warning = 1;
321 my $max_wait_time = 20;
322 for (my $i = 0;; $i++) {
325 last if system("dd if=$path of=/dev/null bs=512 count=1 >/dev/null 2>&1") == 0;
327 # correct, but does not work?
328 ($rc, $res) = $hdl->list_assignments([$nodename], [$volname], 0, { "cstate:deploy" => "true" }, []);
330 my $len = scalar(@$res);
333 die "aborting wait - device '$path' still not readable\n" if $i > $max_wait_time;
334 print "waiting for device '$path' to become ready...\n" if $print_warning;
342 sub deactivate_volume
{
343 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
345 die "Snapshot not implemented on DRBD\n" if $snapname;
347 return undef; # fixme: should we unassign ?
349 # remove above return to enable this code
350 my $hdl = connect_drbdmanage_service
();
351 my $nodename = PVE
::INotify
::nodename
();
352 my ($rc, $res) = $hdl->list_assignments([$nodename], [$volname], 0,
353 { "cstate:diskless" => "true" }, []);
356 my ($rc, $res) = $hdl->unassign($nodename, $volname,0);
364 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
366 $size = ($size/1024/1024) . "M";
368 my $path = $class->path($scfg, $volname);
370 # fixme: howto implement this
371 die "drbd volume_resize is not implemented";
373 #my $cmd = ['/sbin/lvextend', '-L', $size, $path];
374 #run_command($cmd, errmsg => "error resizing volume '$path'");
379 sub volume_snapshot
{
380 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
382 die "drbd snapshot is not implemented";
385 sub volume_snapshot_rollback
{
386 my ($class, $scfg, $storeid, $volname, $snap) = @_;
388 die "drbd snapshot rollback is not implemented";
391 sub volume_snapshot_delete
{
392 my ($class, $scfg, $storeid, $volname, $snap) = @_;
394 die "drbd snapshot delete is not implemented";
397 sub volume_has_feature
{
398 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
401 copy
=> { base
=> 1, current
=> 1},
404 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
405 $class->parse_volname($volname);
411 $key = $isBase ?
'base' : 'current';
413 return 1 if $features->{$feature}->{$key};