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