1 package PVE
::Storage
::DRBDPlugin
;
9 use PVE
::Tools
qw(run_command trim);
11 use PVE
::Storage
::Plugin
;
12 use PVE
::JSONSchema
qw(get_standard_option);
14 use base
qw(PVE::Storage::Plugin);
18 my $default_redundancy = 2;
26 content
=> [ {images
=> 1}, { images
=> 1 }],
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.",
37 default => $default_redundancy,
44 redundancy
=> { optional
=> 1 },
45 nodes
=> { optional
=> 1 },
46 disable
=> { optional
=> 1 },
55 return $scfg->{redundancy
} || $default_redundancy;
58 sub connect_drbdmanage_service
{
60 my $bus = Net
::DBus-
>system;
62 my $service = $bus->get_service("org.drbd.drbdmanaged");
64 my $hdl = $service->get_object("/interface", "org.drbd.drbdmanaged");
72 die "got undefined drbd rc\n" if !$rc;
74 my ($code, $msg, $details) = @$rc;
76 return undef if $code == 0;
78 $msg = "drbd error: got error code $code" if !$msg;
81 # fixme: add error details?
82 #print Dumper($details);
84 die "drbd error: $msg\n";
87 sub drbd_list_volumes
{
90 $hdl = connect_drbdmanage_service
() if !$hdl;
92 my ($rc, $res) = $hdl->list_volumes([], 0, {}, []);
93 check_drbd_rc
($rc->[0]);
97 foreach my $entry (@$res) {
98 my ($volname, $properties, $vol_list) = @$entry;
100 next if $volname !~ m/^vm-(\d+)-/;
103 # fixme: we always use volid 0 ?
105 foreach my $volentry (@$vol_list) {
106 my ($vol_id, $vol_properties) = @$volentry;
107 next if $vol_id != 0;
108 my $vol_size = $vol_properties->{vol_size
} * 1024;
109 $size = $vol_size if $vol_size > $size;
112 $volumes->{$volname} = { format
=> 'raw', size
=> $size,
119 # Storage implementation
122 my ($class, $volname) = @_;
124 if ($volname =~ m/^(vm-(\d+)-[a-z][a-z0-9\-\_\.]*[a-z0-9]+)$/) {
125 return ('images', $1, $2);
128 die "unable to parse lvm volume name '$volname'\n";
131 sub filesystem_path
{
132 my ($class, $scfg, $volname) = @_;
134 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
136 # fixme: always use volid 0?
137 my $path = "/dev/drbd/by-res/$volname/0";
139 return wantarray ?
($path, $vmid, $vtype) : $path;
143 my ($class, $storeid, $scfg, $volname) = @_;
145 die "can't create base images in drbd storage\n";
149 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
151 die "can't clone images in drbd storage\n";
155 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
157 die "unsupported format '$fmt'" if $fmt ne 'raw';
159 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
160 if defined($name) && $name !~ m/^vm-$vmid-/;
162 my $hdl = connect_drbdmanage_service
();
163 my $volumes = drbd_list_volumes
($hdl);
165 die "volume '$name' already exists\n" if defined($name) && $volumes->{$name};
167 if (!defined($name)) {
168 for (my $i = 1; $i < 100; $i++) {
169 my $tn = "vm-$vmid-disk-$i";
170 if (!defined ($volumes->{$tn})) {
177 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
180 my ($rc, $res) = $hdl->create_resource($name, {});
181 check_drbd_rc
($rc->[0]);
183 ($rc, $res) = $hdl->create_volume($name, $size, {});
184 check_drbd_rc
($rc->[0]);
186 my $redundancy = get_redundancy
($scfg);;
188 ($rc, $res) = $hdl->auto_deploy($name, $redundancy, 0, 0);
189 check_drbd_rc
($rc->[0]);
191 # hack: this is required to avoid bugs with set_drbdsetup_props(),
192 # which can result in a split brain if we do not wait.
193 # Note: wait until all volumes have cstate:deploy
195 for (my $i = 0; $i < $max_wait; $i++) {
196 ($rc, $res) = $hdl->list_assignments([], [$name], 0, { "cstate:deploy" => "true" }, []);
197 check_drbd_rc
($rc->[0]);
198 my $len = scalar(@$res);
199 last if $len == $redundancy;
202 sleep(1); # another sleep - it does not work without -no idea why?
204 ($rc, $res) = $hdl->set_drbdsetup_props(
206 target
=> "resource",
209 'allow-two-primaries' => 'yes',
211 check_drbd_rc
($rc->[0]);
217 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
219 my $hdl = connect_drbdmanage_service
();
220 my ($rc, $res) = $hdl->remove_resource($volname, 0);
221 check_drbd_rc
($rc->[0]);
227 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
229 my $vgname = $scfg->{vgname
};
231 $cache->{drbd_volumes
} = drbd_list_volumes
() if !$cache->{drbd_volumes
};
235 my $dat = $cache->{drbd_volumes
};
237 foreach my $volname (keys %$dat) {
239 my $owner = $dat->{$volname}->{vmid
};
241 my $volid = "$storeid:$volname";
244 my $found = grep { $_ eq $volid } @$vollist;
247 next if defined ($vmid) && ($owner ne $vmid);
250 my $info = $dat->{$volname};
251 $info->{volid
} = $volid;
260 my ($class, $storeid, $scfg, $cache) = @_;
262 my ($total, $avail, $used);
265 my $hdl = connect_drbdmanage_service
();
266 my $redundancy = get_redundancy
($scfg);;
267 my ($rc, $res) = $hdl->cluster_free_query($redundancy);
268 check_drbd_rc
($rc->[0]);
272 $total = $used + $avail;
277 # assume storage if offline
282 return ($total, $avail, $used, 1);
285 sub activate_storage
{
286 my ($class, $storeid, $scfg, $cache) = @_;
291 sub deactivate_storage
{
292 my ($class, $storeid, $scfg, $cache) = @_;
297 sub activate_volume
{
298 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
300 my $path = $class->path($scfg, $volname);
302 my $hdl = connect_drbdmanage_service
();
303 my $nodename = PVE
::INotify
::nodename
();
304 my ($rc, $res) = $hdl->list_assignments([$nodename], [], 0, {}, []);
305 check_drbd_rc
($rc->[0]);
307 foreach my $entry (@$res) {
308 my ($node, $res_name, $props, $voldata) = @$entry;
309 if (($node eq $nodename) && ($res_name eq $volname)) {
310 return undef; # assignment already exists
314 # create diskless assignment
315 ($rc, $res) = $hdl->assign($nodename, $volname, { diskless
=> 'true' });
316 check_drbd_rc
($rc->[0]);
318 # wait until device is acessitble
319 my $print_warning = 1;
320 my $max_wait_time = 20;
321 for (my $i = 0;; $i++) {
322 ($rc, $res) = $hdl->list_assignments([$nodename], [$volname], 0, { "cstate:deploy" => "true" }, []);
323 check_drbd_rc
($rc->[0]);
324 my $len = scalar(@$res);
326 die "aborting wait - device '$path' still not readable\n" if $i > $max_wait_time;
327 print "waiting for device '$path' to become ready...\n" if $print_warning;
335 sub deactivate_volume
{
336 my ($class, $storeid, $scfg, $volname, $cache) = @_;
338 # fixme: remove diskless assdignments
344 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
346 $size = ($size/1024/1024) . "M";
348 my $path = $class->path($scfg, $volname);
350 # fixme: howto implement this
351 die "drbd volume_resize is not implemented";
353 #my $cmd = ['/sbin/lvextend', '-L', $size, $path];
354 #run_command($cmd, errmsg => "error resizing volume '$path'");
359 sub volume_snapshot
{
360 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
362 die "drbd snapshot is not implemented";
365 sub volume_snapshot_rollback
{
366 my ($class, $scfg, $storeid, $volname, $snap) = @_;
368 die "drbd snapshot rollback is not implemented";
371 sub volume_snapshot_delete
{
372 my ($class, $scfg, $storeid, $volname, $snap) = @_;
374 die "drbd snapshot delete is not implemented";
377 sub volume_has_feature
{
378 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
381 copy
=> { base
=> 1, current
=> 1},
384 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
385 $class->parse_volname($volname);
391 $key = $isBase ?
'base' : 'current';
393 return 1 if $features->{$feature}->{$key};