]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/SheepdogPlugin.pm
08945303cc1c3273d4b27761f587f6d447bd9214
[pve-storage.git] / PVE / Storage / SheepdogPlugin.pm
1 package PVE::Storage::SheepdogPlugin;
2
3 use strict;
4 use warnings;
5 use IO::File;
6 use PVE::Tools qw(run_command trim);
7 use PVE::Storage::Plugin;
8 use PVE::JSONSchema qw(get_standard_option);
9
10 use base qw(PVE::Storage::Plugin);
11
12 my $collie_cmd = sub {
13 my ($scfg, $class, $op, @options) = @_;
14
15 my $portal = $scfg->{portal};
16 my ($server, $port) = split(':', $portal);
17 my $cmd = ['/usr/sbin/collie', $class, $op, '-a', $server];
18 push @$cmd, '-p', $port if $port;
19
20 push @$cmd, @options if scalar(@options);
21
22 return $cmd;
23 };
24
25 sub sheepdog_ls {
26 my ($scfg, $storeid) = @_;
27
28 my $cmd = &$collie_cmd($scfg, 'vdi', 'graph');
29
30 my $relationship = {};
31 my $child = undef;
32
33 run_command($cmd, outfunc => sub {
34 my $line = shift;
35
36 my $parent = undef;
37 my $name = undef;
38
39 $line = trim($line);
40
41 if ($line =~ /\"(\S+)\"\s->\s\"(\S+)\"/) {
42 $parent = $1;
43 $child = $2;
44 $relationship->{$child}->{parent} = $parent;
45 }
46 elsif ($line =~ /group\s\=\s\"(\S+)\",/) {
47 $name = $1;
48 $relationship->{$child}->{name} = $name if $child;
49
50 }
51
52 });
53
54
55 $cmd = &$collie_cmd($scfg, 'vdi', 'list', '-r');
56
57 my $list = {};
58
59 run_command($cmd, outfunc => sub {
60 my $line = shift;
61 $line = trim($line);
62 if ($line =~ /(=|c) ((vm|base)-(\d+)-\S+)\s+(\d+)\s+(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\S+)\s(\d+)/) {
63 my $image = $2;
64 my $owner = $4;
65 my $size = $5;
66 my $idvdi = $10;
67 my $parentid = $relationship->{$idvdi}->{parent} if $relationship->{$idvdi}->{parent};
68 my $parent = $relationship->{$parentid}->{name} if $parentid;
69 $list->{$storeid}->{$image} = {
70 name => $image,
71 size => $size,
72 parent => $parent,
73 vmid => $owner
74 };
75 }
76 });
77
78 return $list;
79 }
80
81 sub sheepdog_snapshot_ls {
82 my ($scfg, $volname) = @_;
83
84 my $cmd = &$collie_cmd($scfg, 'vdi', 'list', '-r');
85
86 my $list = {};
87 run_command($cmd, outfunc => sub {
88 my $line = shift;
89 $line = trim($line);
90 if ($line =~ m/s $volname (\d+) (\d+) (\d+) (\d+) (\d+) (\S+) (\d+) (\S+)/) {
91 $list->{$8} = 1;
92 }
93 });
94
95 return $list;
96 }
97
98 # Configuration
99
100
101 sub type {
102 return 'sheepdog';
103 }
104
105 sub plugindata {
106 return {
107 content => [ {images => 1}, { images => 1 }],
108 };
109 }
110
111
112 sub options {
113 return {
114 nodes => { optional => 1 },
115 disable => { optional => 1 },
116 portal => { fixed => 1 },
117 content => { optional => 1 },
118 };
119 }
120
121 # Storage implementation
122
123 sub parse_volname {
124 my ($class, $volname) = @_;
125
126 if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
127 return ('images', $4, $7, $2, $3, $5);
128 }
129
130 die "unable to parse rbd volume name '$volname'\n";
131 }
132
133 sub path {
134 my ($class, $scfg, $volname, $storeid) = @_;
135
136 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
137
138 my $portal = $scfg->{portal};
139 my ($server, $port) = split(':', $portal);
140 $port = 7000 if !$port;
141
142 my $path = "sheepdog:$server:$port:$name";
143
144 return ($path, $vmid, $vtype);
145 }
146
147 my $find_free_diskname = sub {
148 my ($storeid, $scfg, $vmid) = @_;
149
150 my $sheepdog = sheepdog_ls($scfg, $storeid);
151 my $dat = $sheepdog->{sheepdog};
152 my $disk_ids = {};
153
154 foreach my $image (keys %$dat) {
155 my $volname = $dat->{$image}->{name};
156 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
157 $disk_ids->{$2} = 1;
158 }
159 }
160
161 for (my $i = 1; $i < 100; $i++) {
162 if (!$disk_ids->{$i}) {
163 return "vm-$vmid-disk-$i";
164 }
165 }
166
167 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
168 };
169
170 sub create_base {
171 my ($class, $storeid, $scfg, $volname) = @_;
172
173 my $snap = '__base__';
174
175 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
176 $class->parse_volname($volname);
177
178 die "create_base not possible with base image\n" if $isBase;
179
180 my $sheepdog = sheepdog_ls($scfg, $storeid);
181 die "sheepdog volume info on '$name' failed\n" if !($sheepdog->{sheepdog}->{$name});
182 my $parent = $sheepdog->{sheepdog}->{$name}->{parent};
183
184 die "volname '$volname' contains wrong information about parent $parent $basename\n"
185 if $basename && (!$parent || $parent ne $basename);
186
187 my $newname = $name;
188 $newname =~ s/^vm-/base-/;
189
190 my $newvolname = $basename ? "$basename/$newname" : "$newname";
191
192 #sheepdog can't rename, so we clone then delete the parent
193
194 my $tempsnap = '__tempforbase__';
195
196 my $cmd = &$collie_cmd($scfg, 'vdi', 'snapshot', '-s', $tempsnap, $name);
197 run_command($cmd, errmsg => "sheepdog snapshot $volname' error");
198
199 $cmd = &$collie_cmd($scfg, 'vdi', 'clone', '-s', $tempsnap, $name, $newname);
200 run_command($cmd, errmsg => "sheepdog clone $volname' error");
201
202 $cmd = &$collie_cmd($scfg, 'vdi', 'delete', '-s', $tempsnap, $name);
203 run_command($cmd, errmsg => "sheepdog delete snapshot $volname' error");
204
205 $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , $name);
206 run_command($cmd, errmsg => "sheepdog delete $volname' error");
207
208 #create the base snapshot
209 my $running = undef; #fixme : is create_base always offline ?
210
211 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
212
213 return $newvolname;
214 }
215
216 sub clone_image {
217 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
218
219 my $snap = '__base__';
220
221 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
222 $class->parse_volname($volname);
223
224 die "clone_image only works on base images\n" if !$isBase;
225
226 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
227
228 warn "clone $volname: $basename to $name\n";
229
230 my $newvol = "$basename/$name";
231
232 my $cmd = &$collie_cmd($scfg, 'vdi', 'clone', '-s', $snap, $basename, $name);
233 run_command($cmd, errmsg => "sheepdog clone $volname' error");
234
235 return $newvol;
236 }
237
238 sub alloc_image {
239 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
240
241 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
242 if $name && $name !~ m/^vm-$vmid-/;
243
244 $name = &$find_free_diskname($storeid, $scfg, $vmid);
245
246 my $cmd = &$collie_cmd($scfg, 'vdi', 'create', $name , "${size}KB");
247
248 run_command($cmd, errmsg => "sheepdog create $name' error");
249
250 return $name;
251 }
252
253 sub free_image {
254 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
255
256 my ($vtype, $name, $vmid, undef, undef, undef) =
257 $class->parse_volname($volname);
258
259 my $snapshots = sheepdog_snapshot_ls($scfg, $name);
260 while (my ($snapname) = each %$snapshots) {
261 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , '-s', $snapname, $name);
262 run_command($cmd, errmsg => "sheepdog delete snapshot $snapname $name' error");
263 }
264
265 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , $name);
266
267 run_command($cmd, errmsg => "sheepdog delete $name' error");
268
269 return undef;
270 }
271
272 sub list_images {
273 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
274
275 $cache->{sheepdog} = sheepdog_ls($scfg, $storeid) if !$cache->{sheepdog};
276 my $res = [];
277
278 if (my $dat = $cache->{sheepdog}->{$storeid}) {
279 foreach my $image (keys %$dat) {
280
281 my $volname = $dat->{$image}->{name};
282
283 my $volid = "$storeid:$volname";
284
285 my $owner = $dat->{$volname}->{vmid};
286 if ($vollist) {
287 my $found = grep { $_ eq $volid } @$vollist;
288 next if !$found;
289 } else {
290 next if defined ($vmid) && ($owner ne $vmid);
291 }
292
293 my $info = $dat->{$volname};
294 $info->{volid} = $volid;
295 $info->{format} = 'raw';
296 push @$res, $info;
297 }
298 }
299
300 return $res;
301 }
302
303
304 sub status {
305 my ($class, $storeid, $scfg, $cache) = @_;
306
307 my $total = 0;
308 my $free = 0;
309 my $used = 0;
310 my $active = 1;
311
312 my $cmd = &$collie_cmd($scfg, 'node', 'info' , '-r');
313
314 my $parser = sub {
315 my $line = shift;
316 if ($line =~ m/^Total\s(\d+)\s(\d+)\s/) {
317 $total = $1;
318 $used = $2;
319 $free = $total - $used;
320 }
321 };
322
323 run_command($cmd, outfunc => $parser, errmsg => "sheepdog node info error");
324
325 return ($total,$free,$used,$active);
326
327 return undef;
328 }
329
330 sub activate_storage {
331 my ($class, $storeid, $scfg, $cache) = @_;
332 return 1;
333 }
334
335 sub deactivate_storage {
336 my ($class, $storeid, $scfg, $cache) = @_;
337 return 1;
338 }
339
340 sub activate_volume {
341 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
342 return 1;
343 }
344
345 sub deactivate_volume {
346 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
347 return 1;
348 }
349
350 sub volume_size_info {
351 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
352
353 my $size = undef;
354
355 my $cmd = &$collie_cmd($scfg, 'vdi', 'list', '-r');
356
357 run_command($cmd, outfunc => sub {
358 my $line = shift;
359 $line = trim($line);
360 if ($line =~ /(=|c) $volname\s+(\d+)\s+(\d+)\s(\d+)\s(\d+)\s/) {
361 $size = $3;
362
363 }
364 });
365
366 return $size;
367 }
368
369 sub volume_resize {
370 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
371
372 my $cmd = &$collie_cmd($scfg, 'vdi', 'resize' , $volname, $size);
373 run_command($cmd, errmsg => "sheepdog resize $volname' error");
374
375 return undef;
376 }
377
378 sub volume_snapshot {
379 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
380
381 return 1 if $running;
382
383 my $cmd = &$collie_cmd($scfg, 'vdi', 'snapshot', '-s', $snap, $volname);
384 run_command($cmd, errmsg => "sheepdog snapshot $volname' error");
385
386 return undef;
387 }
388
389 sub volume_snapshot_rollback {
390 my ($class, $scfg, $storeid, $volname, $snap) = @_;
391
392 my $cmd = &$collie_cmd($scfg, 'vdi', 'rollback', '-s', $snap, $volname);
393 run_command($cmd, errmsg => "sheepdog snapshot $volname' error");
394
395 }
396
397 sub volume_snapshot_delete {
398 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
399
400 return 1 if $running;
401
402 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete', '-s', $snap, $volname);
403 run_command($cmd, errmsg => "sheepdog snapshot $volname' error");
404
405 return undef;
406 }
407
408 sub volume_has_feature {
409 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
410
411 my $features = {
412 snapshot => { current => 1, snap => 1},
413 clone => { snap => 1},
414 };
415
416 my $snap = $snapname ? 'snap' : 'current';
417 return 1 if $features->{$feature}->{$snap};
418
419 return undef;
420 }
421
422 1;