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