]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/SheepdogPlugin.pm
sheepdog : retrieve parent of a clone
[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-(\d+)-\S+)\s+(\d+)\s+(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\S+)\s(\d+)/) {
63 my $image = $2;
64 my $owner = $3;
65 my $size = $4;
66 my $idvdi = $9;
67 my $parentid = $relationship->{$idvdi}->{parent} if $relationship->{$idvdi}->{parent};
68 my $parent = $relationship->{$parentid}->{name};
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 =~ /s\s(\S+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\S+)\s(\d+)\s(\S+)/) {
91 $list->{$9} = 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/^(vm-(\d+)-\S+)$/) {
127 return ('images', $1, $2);
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 sub alloc_image {
148 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
149
150
151 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
152 if $name && $name !~ m/^vm-$vmid-/;
153
154 if (!$name) {
155 my $sheepdog = sheepdog_ls($scfg, $storeid);
156
157 for (my $i = 1; $i < 100; $i++) {
158 my $tn = "vm-$vmid-disk-$i";
159 if (!defined ($sheepdog->{$storeid}->{$tn})) {
160 $name = $tn;
161 last;
162 }
163 }
164 }
165
166 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
167 if !$name;
168
169 my $cmd = &$collie_cmd($scfg, 'vdi', 'create', $name , "${size}KB");
170
171 run_command($cmd, errmsg => "sheepdog create $name' error");
172
173 return $name;
174 }
175
176 sub free_image {
177 my ($class, $storeid, $scfg, $volname) = @_;
178
179 my $snapshots = sheepdog_snapshot_ls($scfg, $volname);
180 while (my ($snapname) = each %$snapshots) {
181 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , '-s', $snapname, $volname);
182 run_command($cmd, errmsg => "sheepdog delete snapshot $snapname $volname' error");
183 }
184
185 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , $volname);
186
187 run_command($cmd, errmsg => "sheepdog delete $volname' error");
188
189 return undef;
190 }
191
192 sub list_images {
193 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
194
195 $cache->{sheepdog} = sheepdog_ls($scfg, $storeid) if !$cache->{sheepdog};
196 my $res = [];
197
198 if (my $dat = $cache->{sheepdog}->{$storeid}) {
199 foreach my $image (keys %$dat) {
200
201 my $volname = $dat->{$image}->{name};
202
203 my $volid = "$storeid:$volname";
204
205 my $owner = $dat->{$volname}->{vmid};
206 if ($vollist) {
207 my $found = grep { $_ eq $volid } @$vollist;
208 next if !$found;
209 } else {
210 next if defined ($vmid) && ($owner ne $vmid);
211 }
212
213 my $info = $dat->{$volname};
214 $info->{volid} = $volid;
215 $info->{format} = 'raw';
216 push @$res, $info;
217 }
218 }
219
220 return $res;
221 }
222
223
224 sub status {
225 my ($class, $storeid, $scfg, $cache) = @_;
226
227 my $total = 0;
228 my $free = 0;
229 my $used = 0;
230 my $active = 1;
231
232 my $cmd = &$collie_cmd($scfg, 'node', 'info' , '-r');
233
234 my $parser = sub {
235 my $line = shift;
236 if ($line =~ m/^Total\s(\d+)\s(\d+)\s/) {
237 $total = $1;
238 $used = $2;
239 $free = $total - $used;
240 }
241 };
242
243 run_command($cmd, outfunc => $parser, errmsg => "sheepdog node info error");
244
245 return ($total,$free,$used,$active);
246
247 return undef;
248 }
249
250 sub activate_storage {
251 my ($class, $storeid, $scfg, $cache) = @_;
252 return 1;
253 }
254
255 sub deactivate_storage {
256 my ($class, $storeid, $scfg, $cache) = @_;
257 return 1;
258 }
259
260 sub activate_volume {
261 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
262 return 1;
263 }
264
265 sub deactivate_volume {
266 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
267 return 1;
268 }
269
270 sub volume_size_info {
271 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
272
273 my $size = undef;
274
275 my $cmd = &$collie_cmd($scfg, 'vdi', 'list', '-r', $volname);
276
277 run_command($cmd, outfunc => sub {
278 my $line = shift;
279 $line = trim($line);
280 if ($line =~ /= (vm-(\d+)-\S+)\s+(\d+)\s+(\d+)\s(\d+)\s(\d+)\s/) {
281 $size = $4;
282
283 }
284 });
285
286 return $size;
287 }
288
289 sub volume_resize {
290 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
291
292 my $cmd = &$collie_cmd($scfg, 'vdi', 'resize' , $volname, $size);
293 run_command($cmd, errmsg => "sheepdog resize $volname' error");
294
295 return undef;
296 }
297
298 sub volume_snapshot {
299 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
300
301 return 1 if $running;
302
303 my $cmd = &$collie_cmd($scfg, 'vdi', 'snapshot', '-s', $snap, $volname);
304 run_command($cmd, errmsg => "sheepdog snapshot $volname' error");
305
306 return undef;
307 }
308
309 sub volume_snapshot_rollback {
310 my ($class, $scfg, $storeid, $volname, $snap) = @_;
311
312 my $cmd = &$collie_cmd($scfg, 'vdi', 'rollback', '-s', $snap, $volname);
313 run_command($cmd, errmsg => "sheepdog snapshot $volname' error");
314
315 }
316
317 sub volume_snapshot_delete {
318 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
319
320 return 1 if $running;
321
322 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete', '-s', $snap, $volname);
323 run_command($cmd, errmsg => "sheepdog snapshot $volname' error");
324
325 return undef;
326 }
327
328 1;