]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/SheepdogPlugin.pm
sheepdog : sheepdog_ls : parse base volumes
[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);
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
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
5eab0272
DM
147sub create_base {
148 my ($class, $storeid, $scfg, $volname) = @_;
149
150 die "not implemented";
151}
152
153sub clone_image {
154 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
155
156 die "not implemented";
157}
158
e4fc8228
AD
159sub alloc_image {
160 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
161
162
163 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
164 if $name && $name !~ m/^vm-$vmid-/;
e4fc8228
AD
165
166 if (!$name) {
167 my $sheepdog = sheepdog_ls($scfg, $storeid);
168
169 for (my $i = 1; $i < 100; $i++) {
170 my $tn = "vm-$vmid-disk-$i";
171 if (!defined ($sheepdog->{$storeid}->{$tn})) {
172 $name = $tn;
173 last;
174 }
175 }
176 }
177
178 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
179 if !$name;
be6339fc
DM
180
181 my $cmd = &$collie_cmd($scfg, 'vdi', 'create', $name , "${size}KB");
182
e4fc8228
AD
183 run_command($cmd, errmsg => "sheepdog create $name' error");
184
185 return $name;
186}
187
188sub free_image {
32437ed2 189 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
e4fc8228 190
3cb21703
AD
191 my $snapshots = sheepdog_snapshot_ls($scfg, $volname);
192 while (my ($snapname) = each %$snapshots) {
193 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , '-s', $snapname, $volname);
194 run_command($cmd, errmsg => "sheepdog delete snapshot $snapname $volname' error");
195 }
196
be6339fc 197 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , $volname);
e4fc8228
AD
198
199 run_command($cmd, errmsg => "sheepdog delete $volname' error");
200
201 return undef;
202}
203
204sub list_images {
205 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
206
207 $cache->{sheepdog} = sheepdog_ls($scfg, $storeid) if !$cache->{sheepdog};
208 my $res = [];
209
210 if (my $dat = $cache->{sheepdog}->{$storeid}) {
211 foreach my $image (keys %$dat) {
212
213 my $volname = $dat->{$image}->{name};
214
215 my $volid = "$storeid:$volname";
216
e4fc8228
AD
217 my $owner = $dat->{$volname}->{vmid};
218 if ($vollist) {
219 my $found = grep { $_ eq $volid } @$vollist;
220 next if !$found;
221 } else {
222 next if defined ($vmid) && ($owner ne $vmid);
223 }
224
225 my $info = $dat->{$volname};
226 $info->{volid} = $volid;
be6339fc 227 $info->{format} = 'raw';
e4fc8228
AD
228 push @$res, $info;
229 }
230 }
7f8373d2 231
e4fc8228
AD
232 return $res;
233}
234
235
236sub status {
237 my ($class, $storeid, $scfg, $cache) = @_;
238
239 my $total = 0;
240 my $free = 0;
241 my $used = 0;
242 my $active = 1;
e9256323
DM
243
244 my $cmd = &$collie_cmd($scfg, 'node', 'info' , '-r');
245
246 my $parser = sub {
247 my $line = shift;
248 if ($line =~ m/^Total\s(\d+)\s(\d+)\s/) {
249 $total = $1;
250 $used = $2;
251 $free = $total - $used;
252 }
253 };
254
255 run_command($cmd, outfunc => $parser, errmsg => "sheepdog node info error");
256
e4fc8228
AD
257 return ($total,$free,$used,$active);
258
259 return undef;
260}
261
262sub activate_storage {
263 my ($class, $storeid, $scfg, $cache) = @_;
264 return 1;
265}
266
267sub deactivate_storage {
268 my ($class, $storeid, $scfg, $cache) = @_;
269 return 1;
270}
271
272sub activate_volume {
273 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
274 return 1;
275}
276
277sub deactivate_volume {
278 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
279 return 1;
280}
281
27923b43
AD
282sub volume_size_info {
283 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
284
c732d5db
AD
285 my $size = undef;
286
73b6f89b 287 my $cmd = &$collie_cmd($scfg, 'vdi', 'list', '-r');
c732d5db
AD
288
289 run_command($cmd, outfunc => sub {
290 my $line = shift;
291 $line = trim($line);
73b6f89b
AD
292 if ($line =~ /(=|c) $volname\s+(\d+)\s+(\d+)\s(\d+)\s(\d+)\s/) {
293 $size = $3;
c732d5db
AD
294
295 }
296 });
297
298 return $size;
27923b43
AD
299}
300
9ffffc2e
AD
301sub volume_resize {
302 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
303
304 my $cmd = &$collie_cmd($scfg, 'vdi', 'resize' , $volname, $size);
305 run_command($cmd, errmsg => "sheepdog resize $volname' error");
306
307 return undef;
308}
309
50a19c09
AD
310sub volume_snapshot {
311 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
312
313 return 1 if $running;
314
315 my $cmd = &$collie_cmd($scfg, 'vdi', 'snapshot', '-s', $snap, $volname);
316 run_command($cmd, errmsg => "sheepdog snapshot $volname' error");
317
318 return undef;
319}
320
948b2e22
AD
321sub volume_snapshot_rollback {
322 my ($class, $scfg, $storeid, $volname, $snap) = @_;
323
324 my $cmd = &$collie_cmd($scfg, 'vdi', 'rollback', '-s', $snap, $volname);
325 run_command($cmd, errmsg => "sheepdog snapshot $volname' error");
326
327}
328
9cd89ee3
AD
329sub volume_snapshot_delete {
330 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
331
332 return 1 if $running;
333
334 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete', '-s', $snap, $volname);
335 run_command($cmd, errmsg => "sheepdog snapshot $volname' error");
336
337 return undef;
338}
339
c0235499
AD
340sub volume_has_feature {
341 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
342
343 my $features = {
344 snapshot => { current => 1, snap => 1},
345 clone => { snap => 1},
346 };
347
348 my $snap = $snapname ? 'snap' : 'current';
349 return 1 if $features->{$feature}->{$snap};
350
351 return undef;
352}
353
e4fc8228 3541;