]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/SheepdogPlugin.pm
sheepdog : clone_image
[pve-storage.git] / PVE / Storage / SheepdogPlugin.pm
CommitLineData
e4fc8228
AD
1package PVE::Storage::SheepdogPlugin;
2
3use strict;
4use warnings;
5use IO::File;
6use PVE::Tools qw(run_command trim);
7use PVE::Storage::Plugin;
8use PVE::JSONSchema qw(get_standard_option);
9
10use base qw(PVE::Storage::Plugin);
11
be6339fc
DM
12my $collie_cmd = sub {
13 my ($scfg, $class, $op, @options) = @_;
e4fc8228
AD
14
15 my $portal = $scfg->{portal};
16 my ($server, $port) = split(':', $portal);
be6339fc
DM
17 my $cmd = ['/usr/sbin/collie', $class, $op, '-a', $server];
18 push @$cmd, '-p', $port if $port;
7f8373d2 19
be6339fc
DM
20 push @$cmd, @options if scalar(@options);
21
22 return $cmd;
23};
e4fc8228 24
be6339fc
DM
25sub sheepdog_ls {
26 my ($scfg, $storeid) = @_;
e4fc8228 27
ba4ee9ba
AD
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');
be6339fc
DM
56
57 my $list = {};
58
59 run_command($cmd, outfunc => sub {
e4fc8228
AD
60 my $line = shift;
61 $line = trim($line);
a76d3c97 62 if ($line =~ /(=|c) ((vm|base)-(\d+)-\S+)\s+(\d+)\s+(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\S+)\s(\d+)/) {
ba4ee9ba 63 my $image = $2;
a76d3c97
AD
64 my $owner = $4;
65 my $size = $5;
66 my $idvdi = $10;
ba4ee9ba 67 my $parentid = $relationship->{$idvdi}->{parent} if $relationship->{$idvdi}->{parent};
73b6f89b 68 my $parent = $relationship->{$parentid}->{name} if $parentid;
e4fc8228
AD
69 $list->{$storeid}->{$image} = {
70 name => $image,
71 size => $size,
ba4ee9ba 72 parent => $parent,
e4fc8228
AD
73 vmid => $owner
74 };
e4fc8228
AD
75 }
76 });
77
78 return $list;
e4fc8228
AD
79}
80
3cb21703
AD
81sub 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);
a602d957
AD
90 if ($line =~ m/s $volname (\d+) (\d+) (\d+) (\d+) (\d+) (\S+) (\d+) (\S+)/) {
91 $list->{$8} = 1;
3cb21703
AD
92 }
93 });
94
95 return $list;
96}
97
e4fc8228
AD
98# Configuration
99
100
101sub type {
102 return 'sheepdog';
103}
104
105sub plugindata {
106 return {
107 content => [ {images => 1}, { images => 1 }],
108 };
109}
110
111
112sub options {
113 return {
911ce850
AD
114 nodes => { optional => 1 },
115 disable => { optional => 1 },
e4fc8228
AD
116 portal => { fixed => 1 },
117 content => { optional => 1 },
118 };
119}
120
121# Storage implementation
122
123sub parse_volname {
124 my ($class, $volname) = @_;
125
648095a9
AD
126 if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
127 return ('images', $4, $7, $2, $3, $5);
e4fc8228
AD
128 }
129
130 die "unable to parse rbd volume name '$volname'\n";
131}
132
133sub path {
134 my ($class, $scfg, $volname, $storeid) = @_;
135
136 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
137
138 my $portal = $scfg->{portal};
f7a1e35f
DM
139 my ($server, $port) = split(':', $portal);
140 $port = 7000 if !$port;
7f8373d2 141
f7a1e35f 142 my $path = "sheepdog:$server:$port:$name";
e4fc8228
AD
143
144 return ($path, $vmid, $vtype);
145}
146
19860f5b
AD
147my $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
5eab0272
DM
170sub create_base {
171 my ($class, $storeid, $scfg, $volname) = @_;
172
8a3c8d0e
AD
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;
5eab0272
DM
214}
215
216sub clone_image {
217 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
218
87157341
AD
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;
5eab0272
DM
236}
237
e4fc8228
AD
238sub alloc_image {
239 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
240
e4fc8228
AD
241 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
242 if $name && $name !~ m/^vm-$vmid-/;
e4fc8228 243
19860f5b 244 $name = &$find_free_diskname($storeid, $scfg, $vmid);
be6339fc
DM
245
246 my $cmd = &$collie_cmd($scfg, 'vdi', 'create', $name , "${size}KB");
247
e4fc8228
AD
248 run_command($cmd, errmsg => "sheepdog create $name' error");
249
250 return $name;
251}
252
253sub free_image {
32437ed2 254 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
e4fc8228 255
3cb21703
AD
256 my $snapshots = sheepdog_snapshot_ls($scfg, $volname);
257 while (my ($snapname) = each %$snapshots) {
258 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , '-s', $snapname, $volname);
259 run_command($cmd, errmsg => "sheepdog delete snapshot $snapname $volname' error");
260 }
261
be6339fc 262 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , $volname);
e4fc8228
AD
263
264 run_command($cmd, errmsg => "sheepdog delete $volname' error");
265
266 return undef;
267}
268
269sub list_images {
270 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
271
272 $cache->{sheepdog} = sheepdog_ls($scfg, $storeid) if !$cache->{sheepdog};
273 my $res = [];
274
275 if (my $dat = $cache->{sheepdog}->{$storeid}) {
276 foreach my $image (keys %$dat) {
277
278 my $volname = $dat->{$image}->{name};
279
280 my $volid = "$storeid:$volname";
281
e4fc8228
AD
282 my $owner = $dat->{$volname}->{vmid};
283 if ($vollist) {
284 my $found = grep { $_ eq $volid } @$vollist;
285 next if !$found;
286 } else {
287 next if defined ($vmid) && ($owner ne $vmid);
288 }
289
290 my $info = $dat->{$volname};
291 $info->{volid} = $volid;
be6339fc 292 $info->{format} = 'raw';
e4fc8228
AD
293 push @$res, $info;
294 }
295 }
7f8373d2 296
e4fc8228
AD
297 return $res;
298}
299
300
301sub status {
302 my ($class, $storeid, $scfg, $cache) = @_;
303
304 my $total = 0;
305 my $free = 0;
306 my $used = 0;
307 my $active = 1;
e9256323
DM
308
309 my $cmd = &$collie_cmd($scfg, 'node', 'info' , '-r');
310
311 my $parser = sub {
312 my $line = shift;
313 if ($line =~ m/^Total\s(\d+)\s(\d+)\s/) {
314 $total = $1;
315 $used = $2;
316 $free = $total - $used;
317 }
318 };
319
320 run_command($cmd, outfunc => $parser, errmsg => "sheepdog node info error");
321
e4fc8228
AD
322 return ($total,$free,$used,$active);
323
324 return undef;
325}
326
327sub activate_storage {
328 my ($class, $storeid, $scfg, $cache) = @_;
329 return 1;
330}
331
332sub deactivate_storage {
333 my ($class, $storeid, $scfg, $cache) = @_;
334 return 1;
335}
336
337sub activate_volume {
338 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
339 return 1;
340}
341
342sub deactivate_volume {
343 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
344 return 1;
345}
346
27923b43
AD
347sub volume_size_info {
348 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
349
c732d5db
AD
350 my $size = undef;
351
73b6f89b 352 my $cmd = &$collie_cmd($scfg, 'vdi', 'list', '-r');
c732d5db
AD
353
354 run_command($cmd, outfunc => sub {
355 my $line = shift;
356 $line = trim($line);
73b6f89b
AD
357 if ($line =~ /(=|c) $volname\s+(\d+)\s+(\d+)\s(\d+)\s(\d+)\s/) {
358 $size = $3;
c732d5db
AD
359
360 }
361 });
362
363 return $size;
27923b43
AD
364}
365
9ffffc2e
AD
366sub volume_resize {
367 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
368
369 my $cmd = &$collie_cmd($scfg, 'vdi', 'resize' , $volname, $size);
370 run_command($cmd, errmsg => "sheepdog resize $volname' error");
371
372 return undef;
373}
374
50a19c09
AD
375sub volume_snapshot {
376 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
377
378 return 1 if $running;
379
380 my $cmd = &$collie_cmd($scfg, 'vdi', 'snapshot', '-s', $snap, $volname);
381 run_command($cmd, errmsg => "sheepdog snapshot $volname' error");
382
383 return undef;
384}
385
948b2e22
AD
386sub volume_snapshot_rollback {
387 my ($class, $scfg, $storeid, $volname, $snap) = @_;
388
389 my $cmd = &$collie_cmd($scfg, 'vdi', 'rollback', '-s', $snap, $volname);
390 run_command($cmd, errmsg => "sheepdog snapshot $volname' error");
391
392}
393
9cd89ee3
AD
394sub volume_snapshot_delete {
395 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
396
397 return 1 if $running;
398
399 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete', '-s', $snap, $volname);
400 run_command($cmd, errmsg => "sheepdog snapshot $volname' error");
401
402 return undef;
403}
404
c0235499
AD
405sub volume_has_feature {
406 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
407
408 my $features = {
409 snapshot => { current => 1, snap => 1},
410 clone => { snap => 1},
411 };
412
413 my $snap = $snapname ? 'snap' : 'current';
414 return 1 if $features->{$feature}->{$snap};
415
416 return undef;
417}
418
e4fc8228 4191;