]>
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/bin/dog', $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 },
118 bwlimit
=> { optional
=> 1 },
122 # Storage implementation
125 my ($class, $volname) = @_;
127 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
128 return ('images', $4, $7, $2, $3, $5, 'raw');
131 die "unable to parse sheepdog volume name '$volname'\n";
135 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
137 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
139 my $portal = $scfg->{portal
};
140 my ($server, $port) = split(':', $portal);
141 $port = 7000 if !$port;
142 $name .= ':'.$snapname if $snapname;
144 my $path = "sheepdog:$server:$port:$name";
146 return ($path, $vmid, $vtype);
149 my $find_free_diskname = sub {
150 my ($storeid, $scfg, $vmid) = @_;
152 my $sheepdog = sheepdog_ls
($scfg, $storeid);
153 my $dat = $sheepdog->{$storeid};
156 foreach my $image (keys %$dat) {
157 my $volname = $dat->{$image}->{name
};
158 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
163 for (my $i = 1; $i < 100; $i++) {
164 if (!$disk_ids->{$i}) {
165 return "vm-$vmid-disk-$i";
169 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
173 my ($class, $storeid, $scfg, $volname) = @_;
175 my $snap = '__base__';
177 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
178 $class->parse_volname($volname);
180 die "create_base not possible with base image\n" if $isBase;
182 my $sheepdog = sheepdog_ls
($scfg, $storeid);
183 die "sheepdog volume info on '$name' failed\n" if !($sheepdog->{$storeid}->{$name});
184 my $parent = $sheepdog->{$storeid}->{$name}->{parent
};
186 die "volname '$volname' contains wrong information about parent $parent $basename\n"
187 if $basename && (!$parent || $parent ne $basename);
190 $newname =~ s/^vm-/base-/;
192 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
194 #sheepdog can't rename, so we clone then delete the parent
196 my $tempsnap = '__tempforbase__';
198 my $cmd = &$collie_cmd($scfg, 'vdi', 'snapshot', '-s', $tempsnap, $name);
199 run_command
($cmd, errmsg
=> "sheepdog snapshot $volname' error");
201 $cmd = &$collie_cmd($scfg, 'vdi', 'clone', '-s', $tempsnap, $name, $newname);
202 run_command
($cmd, errmsg
=> "sheepdog clone $volname' error");
204 $cmd = &$collie_cmd($scfg, 'vdi', 'delete', '-s', $tempsnap, $name);
205 run_command
($cmd, errmsg
=> "sheepdog delete snapshot $volname' error");
207 $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , $name);
208 run_command
($cmd, errmsg
=> "sheepdog delete $volname' error");
210 #create the base snapshot
211 my $running = undef; #fixme : is create_base always offline ?
213 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
219 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
221 $snap ||= '__base__';
223 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
224 $class->parse_volname($volname);
226 die "clone_image only works on base images\n" if !$isBase;
228 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
230 warn "clone $volname: $basename to $name\n";
232 my $newvol = "$basename/$name";
234 my $cmd = &$collie_cmd($scfg, 'vdi', 'clone', '-s', $snap, $basename, $name);
235 run_command
($cmd, errmsg
=> "sheepdog clone $volname' error");
241 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
243 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
244 if $name && $name !~ m/^vm-$vmid-/;
246 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
248 my $cmd = &$collie_cmd($scfg, 'vdi', 'create', $name , "${size}k");
250 run_command
($cmd, errmsg
=> "sheepdog create $name' error");
256 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
258 my ($vtype, $name, $vmid, undef, undef, undef) =
259 $class->parse_volname($volname);
261 my $snapshots = sheepdog_snapshot_ls
($scfg, $name);
262 while (my ($snapname) = each %$snapshots) {
263 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , '-s', $snapname, $name);
264 run_command
($cmd, errmsg
=> "sheepdog delete snapshot $snapname $name' error");
267 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , $name);
269 run_command
($cmd, errmsg
=> "sheepdog delete $name' error");
275 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
277 $cache->{sheepdog
} = sheepdog_ls
($scfg, $storeid) if !$cache->{sheepdog
};
280 if (my $dat = $cache->{sheepdog
}->{$storeid}) {
281 foreach my $image (keys %$dat) {
283 my $volname = $dat->{$image}->{name
};
284 my $parent = $dat->{$image}->{parent
};
287 if ($parent && $parent ne $volname) {
288 $volid = "$storeid:$parent/$volname";
290 $volid = "$storeid:$volname";
293 my $owner = $dat->{$volname}->{vmid
};
295 my $found = grep { $_ eq $volid } @$vollist;
298 next if defined ($vmid) && ($owner ne $vmid);
301 my $info = $dat->{$volname};
302 $info->{volid
} = $volid;
303 $info->{format
} = 'raw';
313 my ($class, $storeid, $scfg, $cache) = @_;
320 my $cmd = &$collie_cmd($scfg, 'node', 'info' , '-r');
324 if ($line =~ m/^Total\s(\d+)\s(\d+)\s/) {
327 $free = $total - $used;
331 run_command
($cmd, outfunc
=> $parser, errmsg
=> "sheepdog node info error");
333 return ($total,$free,$used,$active);
338 sub activate_storage
{
339 my ($class, $storeid, $scfg, $cache) = @_;
343 sub deactivate_storage
{
344 my ($class, $storeid, $scfg, $cache) = @_;
348 sub activate_volume
{
349 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
353 sub deactivate_volume
{
354 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
358 sub volume_size_info
{
359 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
361 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
362 $class->parse_volname($volname);
366 my $cmd = &$collie_cmd($scfg, 'vdi', 'list', '-r');
368 run_command
($cmd, outfunc
=> sub {
371 if ($line =~ /(=|c) $name\s+(\d+)\s+(\d+)\s(\d+)\s(\d+)\s/) {
381 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
383 return 1 if $running;
385 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
386 $class->parse_volname($volname);
388 my $cmd = &$collie_cmd($scfg, 'vdi', 'resize' , $name, $size);
389 run_command
($cmd, errmsg
=> "sheepdog resize $name' error");
394 sub volume_snapshot
{
395 my ($class, $scfg, $storeid, $volname, $snap) = @_;
397 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
398 $class->parse_volname($volname);
400 my $cmd = &$collie_cmd($scfg, 'vdi', 'snapshot', '-s', $snap, $name);
401 run_command
($cmd, errmsg
=> "sheepdog snapshot $volname' error");
406 sub volume_snapshot_rollback
{
407 my ($class, $scfg, $storeid, $volname, $snap) = @_;
409 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
410 $class->parse_volname($volname);
412 my $cmd = &$collie_cmd($scfg, 'vdi', 'rollback', '-f', '-s', $snap, $name);
413 run_command
($cmd, errmsg
=> "sheepdog snapshot $name' error");
417 sub volume_snapshot_delete
{
418 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
420 return 1 if $running;
422 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
424 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
425 $class->parse_volname($volname);
427 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete', '-s', $snap, $name);
428 run_command
($cmd, errmsg
=> "sheepdog snapshot $name' error");
433 sub volume_has_feature
{
434 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
437 snapshot
=> { current
=> 1, snap
=> 1},
438 clone
=> { base
=> 1},
439 template
=> { current
=> 1},
440 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
441 sparseinit
=> { base
=> 1, current
=> 1 },
444 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
445 $class->parse_volname($volname);
451 $key = $isBase ?
'base' : 'current';
453 return 1 if $features->{$feature}->{$key};