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);
180 my $disk_list = [ keys %$volumes ];
182 die "volume '$name' already exists\n" if defined($name) && $volumes->{$name};
183 $name //= PVE
::Storage
::Plugin
::get_next_vm_diskname
($disk_list, $storeid, $vmid, undef, $scfg);
185 my ($rc, $res) = $hdl->create_resource($name, {});
188 ($rc, $res) = $hdl->create_volume($name, $size, {});
191 ($rc, $res) = $hdl->set_drbdsetup_props(
193 target
=> "resource",
196 'allow-two-primaries' => 'yes',
200 my $redundancy = get_redundancy
($scfg);;
202 ($rc, $res) = $hdl->auto_deploy($name, $redundancy, 0, 0);
209 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
211 my $hdl = connect_drbdmanage_service
();
212 my ($rc, $res) = $hdl->remove_resource($volname, 0);
219 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
221 my $vgname = $scfg->{vgname
};
223 $cache->{drbd_volumes
} = drbd_list_volumes
() if !$cache->{drbd_volumes
};
227 my $dat = $cache->{drbd_volumes
};
229 foreach my $volname (keys %$dat) {
231 my $owner = $dat->{$volname}->{vmid
};
233 my $volid = "$storeid:$volname";
236 my $found = grep { $_ eq $volid } @$vollist;
239 next if defined ($vmid) && ($owner ne $vmid);
242 my $info = $dat->{$volname};
243 $info->{volid
} = $volid;
252 my ($class, $storeid, $scfg, $cache) = @_;
254 my ($total, $avail, $used);
257 my $hdl = connect_drbdmanage_service
();
258 my $redundancy = get_redundancy
($scfg);;
259 my ($rc, $free_space, $total_space) = $hdl->cluster_free_query($redundancy);
262 $avail = $free_space*1024;
263 $total = $total_space*1024;
264 $used = $total - $avail;
269 # assume storage if offline
274 return ($total, $avail, $used, 1);
277 sub activate_storage
{
278 my ($class, $storeid, $scfg, $cache) = @_;
283 sub deactivate_storage
{
284 my ($class, $storeid, $scfg, $cache) = @_;
289 sub activate_volume
{
290 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
292 die "Snapshot not implemented on DRBD\n" if $snapname;
294 my $path = $class->path($scfg, $volname);
296 my $hdl = connect_drbdmanage_service
();
297 my $nodename = PVE
::INotify
::nodename
();
298 my ($rc, $res) = $hdl->list_assignments([$nodename], [$volname], 0, {}, []);
301 # assignment already exists?
302 return undef if @$res;
304 # create diskless assignment
305 ($rc, $res) = $hdl->assign($nodename, $volname, { diskless
=> 'true' });
308 # wait until device is accessible
309 my $print_warning = 1;
310 my $max_wait_time = 20;
311 for (my $i = 0;; $i++) {
314 last if system("dd if=$path of=/dev/null bs=512 count=1 >/dev/null 2>&1") == 0;
316 # correct, but does not work?
317 ($rc, $res) = $hdl->list_assignments([$nodename], [$volname], 0, { "cstate:deploy" => "true" }, []);
319 my $len = scalar(@$res);
322 die "aborting wait - device '$path' still not readable\n" if $i > $max_wait_time;
323 print "waiting for device '$path' to become ready...\n" if $print_warning;
331 sub deactivate_volume
{
332 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
334 die "Snapshot not implemented on DRBD\n" if $snapname;
336 return undef; # fixme: should we unassign ?
338 # remove above return to enable this code
339 my $hdl = connect_drbdmanage_service
();
340 my $nodename = PVE
::INotify
::nodename
();
341 my ($rc, $res) = $hdl->list_assignments([$nodename], [$volname], 0,
342 { "cstate:diskless" => "true" }, []);
345 my ($rc, $res) = $hdl->unassign($nodename, $volname,0);
353 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
355 $size = ($size/1024/1024) . "M";
357 my $path = $class->path($scfg, $volname);
359 # fixme: howto implement this
360 die "drbd volume_resize is not implemented";
362 #my $cmd = ['/sbin/lvextend', '-L', $size, $path];
363 #run_command($cmd, errmsg => "error resizing volume '$path'");
368 sub volume_snapshot
{
369 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
371 die "drbd snapshot is not implemented";
374 sub volume_snapshot_rollback
{
375 my ($class, $scfg, $storeid, $volname, $snap) = @_;
377 die "drbd snapshot rollback is not implemented";
380 sub volume_snapshot_delete
{
381 my ($class, $scfg, $storeid, $volname, $snap) = @_;
383 die "drbd snapshot delete is not implemented";
386 sub volume_has_feature
{
387 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
390 copy
=> { base
=> 1, current
=> 1},
393 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
394 $class->parse_volname($volname);
400 $key = $isBase ?
'base' : 'current';
402 return 1 if $features->{$feature}->{$key};