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