]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/DRBDPlugin.pm
add Storage::get_bandwidth_limit helper
[pve-storage.git] / PVE / Storage / DRBDPlugin.pm
1 package PVE::Storage::DRBDPlugin;
2
3 use strict;
4 use warnings;
5 use IO::File;
6 use Net::DBus;
7 use Data::Dumper;
8
9 use PVE::Tools qw(run_command trim);
10 use PVE::INotify;
11 use PVE::Storage::Plugin;
12 use PVE::JSONSchema qw(get_standard_option);
13
14 use base qw(PVE::Storage::Plugin);
15
16 # Configuration
17
18 my $default_redundancy = 2;
19
20 sub type {
21 return 'drbd';
22 }
23
24 sub plugindata {
25 return {
26 content => [ {images => 1, rootdir => 1}, { images => 1 }],
27 };
28 }
29
30 sub properties {
31 return {
32 redundancy => {
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.",
34 type => 'integer',
35 minimum => 1,
36 maximum => 16,
37 default => $default_redundancy,
38 },
39 };
40 }
41
42 sub options {
43 return {
44 redundancy => { optional => 1 },
45 content => { optional => 1 },
46 nodes => { optional => 1 },
47 disable => { optional => 1 },
48 bwlimit => { optional => 1 },
49 };
50 }
51
52 # helper
53
54 sub get_redundancy {
55 my ($scfg) = @_;
56
57 return $scfg->{redundancy} || $default_redundancy;
58 }
59
60 sub connect_drbdmanage_service {
61
62 my $bus = Net::DBus->system;
63
64 my $service = $bus->get_service("org.drbd.drbdmanaged");
65
66 my $hdl = $service->get_object("/interface", "org.drbd.drbdmanaged");
67
68 return $hdl;
69 }
70
71 sub check_drbd_res {
72 my ($rc) = @_;
73
74 die "got undefined drbd result\n" if !$rc;
75
76 # Messages for return codes 1 to 99 are not considered an error.
77 foreach my $res (@$rc) {
78 my ($code, $format, $details) = @$res;
79
80 next if $code < 100;
81
82 my $msg;
83 if (defined($format)) {
84 my @args = ();
85 push @args, $details->{$1} // ""
86 while $format =~ s,\%\((\w+)\),%,;
87
88 $msg = sprintf($format, @args);
89
90 } else {
91 $msg = "drbd error: got error code $code";
92 }
93
94 chomp $msg;
95 die "drbd error: $msg\n";
96 }
97
98 return undef;
99 }
100
101 sub drbd_list_volumes {
102 my ($hdl) = @_;
103
104 $hdl = connect_drbdmanage_service() if !$hdl;
105
106 my ($rc, $res) = $hdl->list_volumes([], 0, {}, []);
107 check_drbd_res($rc);
108
109 my $volumes = {};
110
111 foreach my $entry (@$res) {
112 my ($volname, $properties, $vol_list) = @$entry;
113
114 next if $volname !~ m/^vm-(\d+)-/;
115 my $vmid = $1;
116
117 # fixme: we always use volid 0 ?
118 my $size = 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;
124 }
125
126 $volumes->{$volname} = { format => 'raw', size => $size,
127 vmid => $vmid };
128 }
129
130 return $volumes;
131 }
132
133 # Storage implementation
134
135 sub parse_volname {
136 my ($class, $volname) = @_;
137
138 if ($volname =~ m/^(vm-(\d+)-[a-z][a-z0-9\-\_\.]*[a-z0-9]+)$/) {
139 return ('images', $1, $2, undef, undef, undef, 'raw');
140 }
141
142 die "unable to parse lvm volume name '$volname'\n";
143 }
144
145 sub filesystem_path {
146 my ($class, $scfg, $volname, $snapname) = @_;
147
148 die "drbd snapshot is not implemented\n" if defined($snapname);
149
150 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
151
152 # fixme: always use volid 0?
153 my $path = "/dev/drbd/by-res/$volname/0";
154
155 return wantarray ? ($path, $vmid, $vtype) : $path;
156 }
157
158 sub create_base {
159 my ($class, $storeid, $scfg, $volname) = @_;
160
161 die "can't create base images in drbd storage\n";
162 }
163
164 sub clone_image {
165 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
166
167 die "can't clone images in drbd storage\n";
168 }
169
170 sub alloc_image {
171 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
172
173 die "unsupported format '$fmt'" if $fmt ne 'raw';
174
175 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
176 if defined($name) && $name !~ m/^vm-$vmid-/;
177
178 my $hdl = connect_drbdmanage_service();
179 my $volumes = drbd_list_volumes($hdl);
180
181 die "volume '$name' already exists\n" if defined($name) && $volumes->{$name};
182
183 if (!defined($name)) {
184 for (my $i = 1; $i < 100; $i++) {
185 my $tn = "vm-$vmid-disk-$i";
186 if (!defined ($volumes->{$tn})) {
187 $name = $tn;
188 last;
189 }
190 }
191 }
192
193 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
194 if !defined($name);
195
196 my ($rc, $res) = $hdl->create_resource($name, {});
197 check_drbd_res($rc);
198
199 ($rc, $res) = $hdl->create_volume($name, $size, {});
200 check_drbd_res($rc);
201
202 ($rc, $res) = $hdl->set_drbdsetup_props(
203 {
204 target => "resource",
205 resource => $name,
206 type => 'neto',
207 'allow-two-primaries' => 'yes',
208 });
209 check_drbd_res($rc);
210
211 my $redundancy = get_redundancy($scfg);;
212
213 ($rc, $res) = $hdl->auto_deploy($name, $redundancy, 0, 0);
214 check_drbd_res($rc);
215
216 return $name;
217 }
218
219 sub free_image {
220 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
221
222 my $hdl = connect_drbdmanage_service();
223 my ($rc, $res) = $hdl->remove_resource($volname, 0);
224 check_drbd_res($rc);
225
226 return undef;
227 }
228
229 sub list_images {
230 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
231
232 my $vgname = $scfg->{vgname};
233
234 $cache->{drbd_volumes} = drbd_list_volumes() if !$cache->{drbd_volumes};
235
236 my $res = [];
237
238 my $dat = $cache->{drbd_volumes};
239
240 foreach my $volname (keys %$dat) {
241
242 my $owner = $dat->{$volname}->{vmid};
243
244 my $volid = "$storeid:$volname";
245
246 if ($vollist) {
247 my $found = grep { $_ eq $volid } @$vollist;
248 next if !$found;
249 } else {
250 next if defined ($vmid) && ($owner ne $vmid);
251 }
252
253 my $info = $dat->{$volname};
254 $info->{volid} = $volid;
255
256 push @$res, $info;
257 }
258
259 return $res;
260 }
261
262 sub status {
263 my ($class, $storeid, $scfg, $cache) = @_;
264
265 my ($total, $avail, $used);
266
267 eval {
268 my $hdl = connect_drbdmanage_service();
269 my $redundancy = get_redundancy($scfg);;
270 my ($rc, $free_space, $total_space) = $hdl->cluster_free_query($redundancy);
271 check_drbd_res($rc);
272
273 $avail = $free_space*1024;
274 $total = $total_space*1024;
275 $used = $total - $avail;
276
277 };
278 if (my $err = $@) {
279 # ignore error,
280 # assume storage if offline
281
282 return undef;
283 }
284
285 return ($total, $avail, $used, 1);
286 }
287
288 sub activate_storage {
289 my ($class, $storeid, $scfg, $cache) = @_;
290
291 return undef;
292 }
293
294 sub deactivate_storage {
295 my ($class, $storeid, $scfg, $cache) = @_;
296
297 return undef;
298 }
299
300 sub activate_volume {
301 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
302
303 die "Snapshot not implemented on DRBD\n" if $snapname;
304
305 my $path = $class->path($scfg, $volname);
306
307 my $hdl = connect_drbdmanage_service();
308 my $nodename = PVE::INotify::nodename();
309 my ($rc, $res) = $hdl->list_assignments([$nodename], [$volname], 0, {}, []);
310 check_drbd_res($rc);
311
312 # assignment already exists?
313 return undef if @$res;
314
315 # create diskless assignment
316 ($rc, $res) = $hdl->assign($nodename, $volname, { diskless => 'true' });
317 check_drbd_res($rc);
318
319 # wait until device is accessible
320 my $print_warning = 1;
321 my $max_wait_time = 20;
322 for (my $i = 0;; $i++) {
323 if (1) {
324 # clumsy, but works
325 last if system("dd if=$path of=/dev/null bs=512 count=1 >/dev/null 2>&1") == 0;
326 } else {
327 # correct, but does not work?
328 ($rc, $res) = $hdl->list_assignments([$nodename], [$volname], 0, { "cstate:deploy" => "true" }, []);
329 check_drbd_res($rc);
330 my $len = scalar(@$res);
331 last if $len > 0;
332 }
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;
335 $print_warning = 0;
336 sleep(1);
337 }
338
339 return undef;
340 }
341
342 sub deactivate_volume {
343 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
344
345 die "Snapshot not implemented on DRBD\n" if $snapname;
346
347 return undef; # fixme: should we unassign ?
348
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" }, []);
354 check_drbd_res($rc);
355 if (scalar(@$res)) {
356 my ($rc, $res) = $hdl->unassign($nodename, $volname,0);
357 check_drbd_res($rc);
358 }
359
360 return undef;
361 }
362
363 sub volume_resize {
364 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
365
366 $size = ($size/1024/1024) . "M";
367
368 my $path = $class->path($scfg, $volname);
369
370 # fixme: howto implement this
371 die "drbd volume_resize is not implemented";
372
373 #my $cmd = ['/sbin/lvextend', '-L', $size, $path];
374 #run_command($cmd, errmsg => "error resizing volume '$path'");
375
376 return 1;
377 }
378
379 sub volume_snapshot {
380 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
381
382 die "drbd snapshot is not implemented";
383 }
384
385 sub volume_snapshot_rollback {
386 my ($class, $scfg, $storeid, $volname, $snap) = @_;
387
388 die "drbd snapshot rollback is not implemented";
389 }
390
391 sub volume_snapshot_delete {
392 my ($class, $scfg, $storeid, $volname, $snap) = @_;
393
394 die "drbd snapshot delete is not implemented";
395 }
396
397 sub volume_has_feature {
398 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
399
400 my $features = {
401 copy => { base => 1, current => 1},
402 };
403
404 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
405 $class->parse_volname($volname);
406
407 my $key = undef;
408 if($snapname){
409 $key = 'snap';
410 }else{
411 $key = $isBase ? 'base' : 'current';
412 }
413 return 1 if $features->{$feature}->{$key};
414
415 return undef;
416 }
417
418 1;