]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/SheepdogPlugin.pm
1 package PVE
::Storage
::SheepdogPlugin
;
6 use PVE
::Tools
qw(run_command trim);
7 use PVE
::Storage
::Plugin
;
8 use PVE
::JSONSchema
qw(get_standard_option);
10 use base
qw(PVE::Storage::Plugin);
12 my $collie_cmd = sub {
13 my ($scfg, $class, $op, @options) = @_;
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;
20 push @$cmd, @options if scalar(@options);
26 my ($scfg, $storeid) = @_;
28 my $cmd = &$collie_cmd($scfg, 'vdi', 'graph');
30 my $relationship = {};
33 run_command
($cmd, outfunc
=> sub {
41 if ($line =~ /\"(\S+)\"\s->\s\"(\S+)\"/) {
44 $relationship->{$child}->{parent
} = $parent;
46 elsif ($line =~ /group\s\=\s\"(\S+)\",/) {
48 $relationship->{$child}->{name
} = $name if $child;
55 $cmd = &$collie_cmd($scfg, 'vdi', 'list', '-r');
59 run_command
($cmd, outfunc
=> sub {
62 if ($line =~ /(=|c) ((vm|base)-(\d+)-\S+)\s+(\d+)\s+(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\S+)\s(\d+)/) {
67 my $parentid = $relationship->{$idvdi}->{parent
} if $relationship->{$idvdi}->{parent
};
68 my $parent = $relationship->{$parentid}->{name
} if $parentid;
69 $list->{$storeid}->{$image} = {
81 sub sheepdog_snapshot_ls
{
82 my ($scfg, $volname) = @_;
84 my $cmd = &$collie_cmd($scfg, 'vdi', 'list', '-r');
87 run_command
($cmd, outfunc
=> sub {
90 if ($line =~ m/s $volname (\d+) (\d+) (\d+) (\d+) (\d+) (\S+) (\d+) (\S+)/) {
107 content
=> [ {images
=> 1}, { images
=> 1 }],
114 nodes
=> { optional
=> 1 },
115 disable
=> { optional
=> 1 },
116 portal
=> { fixed
=> 1 },
117 content
=> { optional
=> 1 },
121 # Storage implementation
124 my ($class, $volname) = @_;
126 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
127 return ('images', $4, $7, $2, $3, $5);
130 die "unable to parse rbd volume name '$volname'\n";
134 my ($class, $scfg, $volname, $storeid) = @_;
136 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
138 my $portal = $scfg->{portal
};
139 my ($server, $port) = split(':', $portal);
140 $port = 7000 if !$port;
142 my $path = "sheepdog:$server:$port:$name";
144 return ($path, $vmid, $vtype);
147 my $find_free_diskname = sub {
148 my ($storeid, $scfg, $vmid) = @_;
150 my $sheepdog = sheepdog_ls
($scfg, $storeid);
151 my $dat = $sheepdog->{sheepdog
};
154 foreach my $image (keys %$dat) {
155 my $volname = $dat->{$image}->{name
};
156 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
161 for (my $i = 1; $i < 100; $i++) {
162 if (!$disk_ids->{$i}) {
163 return "vm-$vmid-disk-$i";
167 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
171 my ($class, $storeid, $scfg, $volname) = @_;
173 my $snap = '__base__';
175 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
176 $class->parse_volname($volname);
178 die "create_base not possible with base image\n" if $isBase;
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
};
184 die "volname '$volname' contains wrong information about parent $parent $basename\n"
185 if $basename && (!$parent || $parent ne $basename);
188 $newname =~ s/^vm-/base-/;
190 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
192 #sheepdog can't rename, so we clone then delete the parent
194 my $tempsnap = '__tempforbase__';
196 my $cmd = &$collie_cmd($scfg, 'vdi', 'snapshot', '-s', $tempsnap, $name);
197 run_command
($cmd, errmsg
=> "sheepdog snapshot $volname' error");
199 $cmd = &$collie_cmd($scfg, 'vdi', 'clone', '-s', $tempsnap, $name, $newname);
200 run_command
($cmd, errmsg
=> "sheepdog clone $volname' error");
202 $cmd = &$collie_cmd($scfg, 'vdi', 'delete', '-s', $tempsnap, $name);
203 run_command
($cmd, errmsg
=> "sheepdog delete snapshot $volname' error");
205 $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , $name);
206 run_command
($cmd, errmsg
=> "sheepdog delete $volname' error");
208 #create the base snapshot
209 my $running = undef; #fixme : is create_base always offline ?
211 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
217 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
219 my $snap = '__base__';
221 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
222 $class->parse_volname($volname);
224 die "clone_image only works on base images\n" if !$isBase;
226 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
228 warn "clone $volname: $basename to $name\n";
230 my $newvol = "$basename/$name";
232 my $cmd = &$collie_cmd($scfg, 'vdi', 'clone', '-s', $snap, $basename, $name);
233 run_command
($cmd, errmsg
=> "sheepdog clone $volname' error");
239 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
241 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
242 if $name && $name !~ m/^vm-$vmid-/;
244 $name = &$find_free_diskname($storeid, $scfg, $vmid);
246 my $cmd = &$collie_cmd($scfg, 'vdi', 'create', $name , "${size}KB");
248 run_command
($cmd, errmsg
=> "sheepdog create $name' error");
254 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
256 my ($vtype, $name, $vmid, undef, undef, undef) =
257 $class->parse_volname($volname);
259 my $snapshots = sheepdog_snapshot_ls
($scfg, $name);
260 while (my ($snapname) = each %$snapshots) {
261 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , '-s', $snapname, $name);
262 run_command
($cmd, errmsg
=> "sheepdog delete snapshot $snapname $name' error");
265 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , $name);
267 run_command
($cmd, errmsg
=> "sheepdog delete $name' error");
273 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
275 $cache->{sheepdog
} = sheepdog_ls
($scfg, $storeid) if !$cache->{sheepdog
};
278 if (my $dat = $cache->{sheepdog
}->{$storeid}) {
279 foreach my $image (keys %$dat) {
281 my $volname = $dat->{$image}->{name
};
282 my $parent = $dat->{$image}->{parent
};
285 if ($parent && $parent ne $volname) {
286 $volid = "$storeid:$parent/$volname";
288 $volid = "$storeid:$volname";
291 my $owner = $dat->{$volname}->{vmid
};
293 my $found = grep { $_ eq $volid } @$vollist;
296 next if defined ($vmid) && ($owner ne $vmid);
299 my $info = $dat->{$volname};
300 $info->{volid
} = $volid;
301 $info->{format
} = 'raw';
311 my ($class, $storeid, $scfg, $cache) = @_;
318 my $cmd = &$collie_cmd($scfg, 'node', 'info' , '-r');
322 if ($line =~ m/^Total\s(\d+)\s(\d+)\s/) {
325 $free = $total - $used;
329 run_command
($cmd, outfunc
=> $parser, errmsg
=> "sheepdog node info error");
331 return ($total,$free,$used,$active);
336 sub activate_storage
{
337 my ($class, $storeid, $scfg, $cache) = @_;
341 sub deactivate_storage
{
342 my ($class, $storeid, $scfg, $cache) = @_;
346 sub activate_volume
{
347 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
351 sub deactivate_volume
{
352 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
356 sub volume_size_info
{
357 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
361 my $cmd = &$collie_cmd($scfg, 'vdi', 'list', '-r');
363 run_command
($cmd, outfunc
=> sub {
366 if ($line =~ /(=|c) $volname\s+(\d+)\s+(\d+)\s(\d+)\s(\d+)\s/) {
376 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
378 my $cmd = &$collie_cmd($scfg, 'vdi', 'resize' , $volname, $size);
379 run_command
($cmd, errmsg
=> "sheepdog resize $volname' error");
384 sub volume_snapshot
{
385 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
387 return 1 if $running;
389 my $cmd = &$collie_cmd($scfg, 'vdi', 'snapshot', '-s', $snap, $volname);
390 run_command
($cmd, errmsg
=> "sheepdog snapshot $volname' error");
395 sub volume_snapshot_rollback
{
396 my ($class, $scfg, $storeid, $volname, $snap) = @_;
398 my $cmd = &$collie_cmd($scfg, 'vdi', 'rollback', '-s', $snap, $volname);
399 run_command
($cmd, errmsg
=> "sheepdog snapshot $volname' error");
403 sub volume_snapshot_delete
{
404 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
406 return 1 if $running;
408 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete', '-s', $snap, $volname);
409 run_command
($cmd, errmsg
=> "sheepdog snapshot $volname' error");
414 sub volume_has_feature
{
415 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
418 snapshot
=> { current
=> 1, snap
=> 1},
419 clone
=> { snap
=> 1},
422 my $snap = $snapname ?
'snap' : 'current';
423 return 1 if $features->{$feature}->{$snap};