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