]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/SheepdogPlugin.pm
fix #1691: increase timeout in worker
[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/bin/dog', $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 = $6;
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 bwlimit => { optional => 1 },
119 };
120 }
121
122 # Storage implementation
123
124 sub parse_volname {
125 my ($class, $volname) = @_;
126
127 if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
128 return ('images', $4, $7, $2, $3, $5, 'raw');
129 }
130
131 die "unable to parse sheepdog volume name '$volname'\n";
132 }
133
134 sub path {
135 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
136
137 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
138
139 my $portal = $scfg->{portal};
140 my ($server, $port) = split(':', $portal);
141 $port = 7000 if !$port;
142 $name .= ':'.$snapname if $snapname;
143
144 my $path = "sheepdog:$server:$port:$name";
145
146 return ($path, $vmid, $vtype);
147 }
148
149 my $find_free_diskname = sub {
150 my ($storeid, $scfg, $vmid) = @_;
151
152 my $sheepdog = sheepdog_ls($scfg, $storeid);
153 my $dat = $sheepdog->{$storeid};
154 my $disk_ids = {};
155
156 foreach my $image (keys %$dat) {
157 my $volname = $dat->{$image}->{name};
158 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
159 $disk_ids->{$2} = 1;
160 }
161 }
162
163 for (my $i = 1; $i < 100; $i++) {
164 if (!$disk_ids->{$i}) {
165 return "vm-$vmid-disk-$i";
166 }
167 }
168
169 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
170 };
171
172 sub create_base {
173 my ($class, $storeid, $scfg, $volname) = @_;
174
175 my $snap = '__base__';
176
177 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
178 $class->parse_volname($volname);
179
180 die "create_base not possible with base image\n" if $isBase;
181
182 my $sheepdog = sheepdog_ls($scfg, $storeid);
183 die "sheepdog volume info on '$name' failed\n" if !($sheepdog->{$storeid}->{$name});
184 my $parent = $sheepdog->{$storeid}->{$name}->{parent};
185
186 die "volname '$volname' contains wrong information about parent $parent $basename\n"
187 if $basename && (!$parent || $parent ne $basename);
188
189 my $newname = $name;
190 $newname =~ s/^vm-/base-/;
191
192 my $newvolname = $basename ? "$basename/$newname" : "$newname";
193
194 #sheepdog can't rename, so we clone then delete the parent
195
196 my $tempsnap = '__tempforbase__';
197
198 my $cmd = &$collie_cmd($scfg, 'vdi', 'snapshot', '-s', $tempsnap, $name);
199 run_command($cmd, errmsg => "sheepdog snapshot $volname' error");
200
201 $cmd = &$collie_cmd($scfg, 'vdi', 'clone', '-s', $tempsnap, $name, $newname);
202 run_command($cmd, errmsg => "sheepdog clone $volname' error");
203
204 $cmd = &$collie_cmd($scfg, 'vdi', 'delete', '-s', $tempsnap, $name);
205 run_command($cmd, errmsg => "sheepdog delete snapshot $volname' error");
206
207 $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , $name);
208 run_command($cmd, errmsg => "sheepdog delete $volname' error");
209
210 #create the base snapshot
211 my $running = undef; #fixme : is create_base always offline ?
212
213 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
214
215 return $newvolname;
216 }
217
218 sub clone_image {
219 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
220
221 $snap ||= '__base__';
222
223 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
224 $class->parse_volname($volname);
225
226 die "clone_image only works on base images\n" if !$isBase;
227
228 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
229
230 warn "clone $volname: $basename to $name\n";
231
232 my $newvol = "$basename/$name";
233
234 my $cmd = &$collie_cmd($scfg, 'vdi', 'clone', '-s', $snap, $basename, $name);
235 run_command($cmd, errmsg => "sheepdog clone $volname' error");
236
237 return $newvol;
238 }
239
240 sub alloc_image {
241 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
242
243 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
244 if $name && $name !~ m/^vm-$vmid-/;
245
246 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
247
248 my $cmd = &$collie_cmd($scfg, 'vdi', 'create', $name , "${size}k");
249
250 run_command($cmd, errmsg => "sheepdog create $name' error");
251
252 return $name;
253 }
254
255 sub free_image {
256 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
257
258 my ($vtype, $name, $vmid, undef, undef, undef) =
259 $class->parse_volname($volname);
260
261 my $snapshots = sheepdog_snapshot_ls($scfg, $name);
262 while (my ($snapname) = each %$snapshots) {
263 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , '-s', $snapname, $name);
264 run_command($cmd, errmsg => "sheepdog delete snapshot $snapname $name' error");
265 }
266
267 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , $name);
268
269 run_command($cmd, errmsg => "sheepdog delete $name' error");
270
271 return undef;
272 }
273
274 sub list_images {
275 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
276
277 $cache->{sheepdog} = sheepdog_ls($scfg, $storeid) if !$cache->{sheepdog};
278 my $res = [];
279
280 if (my $dat = $cache->{sheepdog}->{$storeid}) {
281 foreach my $image (keys %$dat) {
282
283 my $volname = $dat->{$image}->{name};
284 my $parent = $dat->{$image}->{parent};
285
286 my $volid = undef;
287 if ($parent && $parent ne $volname) {
288 $volid = "$storeid:$parent/$volname";
289 } else {
290 $volid = "$storeid:$volname";
291 }
292
293 my $owner = $dat->{$volname}->{vmid};
294 if ($vollist) {
295 my $found = grep { $_ eq $volid } @$vollist;
296 next if !$found;
297 } else {
298 next if defined ($vmid) && ($owner ne $vmid);
299 }
300
301 my $info = $dat->{$volname};
302 $info->{volid} = $volid;
303 $info->{format} = 'raw';
304 push @$res, $info;
305 }
306 }
307
308 return $res;
309 }
310
311
312 sub status {
313 my ($class, $storeid, $scfg, $cache) = @_;
314
315 my $total = 0;
316 my $free = 0;
317 my $used = 0;
318 my $active = 1;
319
320 my $cmd = &$collie_cmd($scfg, 'node', 'info' , '-r');
321
322 my $parser = sub {
323 my $line = shift;
324 if ($line =~ m/^Total\s(\d+)\s(\d+)\s/) {
325 $total = $1;
326 $used = $2;
327 $free = $total - $used;
328 }
329 };
330
331 run_command($cmd, outfunc => $parser, errmsg => "sheepdog node info error");
332
333 return ($total,$free,$used,$active);
334
335 return undef;
336 }
337
338 sub activate_storage {
339 my ($class, $storeid, $scfg, $cache) = @_;
340 return 1;
341 }
342
343 sub deactivate_storage {
344 my ($class, $storeid, $scfg, $cache) = @_;
345 return 1;
346 }
347
348 sub activate_volume {
349 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
350 return 1;
351 }
352
353 sub deactivate_volume {
354 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
355 return 1;
356 }
357
358 sub volume_size_info {
359 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
360
361 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
362 $class->parse_volname($volname);
363
364 my $size = undef;
365
366 my $cmd = &$collie_cmd($scfg, 'vdi', 'list', '-r');
367
368 run_command($cmd, outfunc => sub {
369 my $line = shift;
370 $line = trim($line);
371 if ($line =~ /(=|c) $name\s+(\d+)\s+(\d+)\s(\d+)\s(\d+)\s/) {
372 $size = $3;
373
374 }
375 });
376
377 return $size;
378 }
379
380 sub volume_resize {
381 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
382
383 return 1 if $running;
384
385 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
386 $class->parse_volname($volname);
387
388 my $cmd = &$collie_cmd($scfg, 'vdi', 'resize' , $name, $size);
389 run_command($cmd, errmsg => "sheepdog resize $name' error");
390
391 return undef;
392 }
393
394 sub volume_snapshot {
395 my ($class, $scfg, $storeid, $volname, $snap) = @_;
396
397 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
398 $class->parse_volname($volname);
399
400 my $cmd = &$collie_cmd($scfg, 'vdi', 'snapshot', '-s', $snap, $name);
401 run_command($cmd, errmsg => "sheepdog snapshot $volname' error");
402
403 return undef;
404 }
405
406 sub volume_snapshot_rollback {
407 my ($class, $scfg, $storeid, $volname, $snap) = @_;
408
409 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
410 $class->parse_volname($volname);
411
412 my $cmd = &$collie_cmd($scfg, 'vdi', 'rollback', '-f', '-s', $snap, $name);
413 run_command($cmd, errmsg => "sheepdog snapshot $name' error");
414
415 }
416
417 sub volume_snapshot_delete {
418 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
419
420 return 1 if $running;
421
422 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
423
424 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
425 $class->parse_volname($volname);
426
427 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete', '-s', $snap, $name);
428 run_command($cmd, errmsg => "sheepdog snapshot $name' error");
429
430 return undef;
431 }
432
433 sub volume_has_feature {
434 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
435
436 my $features = {
437 snapshot => { current => 1, snap => 1},
438 clone => { base => 1},
439 template => { current => 1},
440 copy => { base => 1, current => 1, snap => 1},
441 sparseinit => { base => 1, current => 1 },
442 };
443
444 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
445 $class->parse_volname($volname);
446
447 my $key = undef;
448 if($snapname){
449 $key = 'snap';
450 }else{
451 $key = $isBase ? 'base' : 'current';
452 }
453 return 1 if $features->{$feature}->{$key};
454
455 return undef;
456 }
457
458 1;