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