]>
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-(\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
};
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 =~ /s\s(\S+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\S+)\s(\d+)\s(\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/^(vm-(\d+)-\S+)$/) {
127 return ('images', $1, $2);
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);
148 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
151 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
152 if $name && $name !~ m/^vm-$vmid-/;
155 my $sheepdog = sheepdog_ls
($scfg, $storeid);
157 for (my $i = 1; $i < 100; $i++) {
158 my $tn = "vm-$vmid-disk-$i";
159 if (!defined ($sheepdog->{$storeid}->{$tn})) {
166 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
169 my $cmd = &$collie_cmd($scfg, 'vdi', 'create', $name , "${size}KB");
171 run_command
($cmd, errmsg
=> "sheepdog create $name' error");
177 my ($class, $storeid, $scfg, $volname) = @_;
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");
185 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete' , $volname);
187 run_command
($cmd, errmsg
=> "sheepdog delete $volname' error");
193 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
195 $cache->{sheepdog
} = sheepdog_ls
($scfg, $storeid) if !$cache->{sheepdog
};
198 if (my $dat = $cache->{sheepdog
}->{$storeid}) {
199 foreach my $image (keys %$dat) {
201 my $volname = $dat->{$image}->{name
};
203 my $volid = "$storeid:$volname";
205 my $owner = $dat->{$volname}->{vmid
};
207 my $found = grep { $_ eq $volid } @$vollist;
210 next if defined ($vmid) && ($owner ne $vmid);
213 my $info = $dat->{$volname};
214 $info->{volid
} = $volid;
215 $info->{format
} = 'raw';
225 my ($class, $storeid, $scfg, $cache) = @_;
232 my $cmd = &$collie_cmd($scfg, 'node', 'info' , '-r');
236 if ($line =~ m/^Total\s(\d+)\s(\d+)\s/) {
239 $free = $total - $used;
243 run_command
($cmd, outfunc
=> $parser, errmsg
=> "sheepdog node info error");
245 return ($total,$free,$used,$active);
250 sub activate_storage
{
251 my ($class, $storeid, $scfg, $cache) = @_;
255 sub deactivate_storage
{
256 my ($class, $storeid, $scfg, $cache) = @_;
260 sub activate_volume
{
261 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
265 sub deactivate_volume
{
266 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
270 sub volume_size_info
{
271 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
275 my $cmd = &$collie_cmd($scfg, 'vdi', 'list', '-r', $volname);
277 run_command
($cmd, outfunc
=> sub {
280 if ($line =~ /= (vm-(\d+)-\S+)\s+(\d+)\s+(\d+)\s(\d+)\s(\d+)\s/) {
290 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
292 my $cmd = &$collie_cmd($scfg, 'vdi', 'resize' , $volname, $size);
293 run_command
($cmd, errmsg
=> "sheepdog resize $volname' error");
298 sub volume_snapshot
{
299 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
301 return 1 if $running;
303 my $cmd = &$collie_cmd($scfg, 'vdi', 'snapshot', '-s', $snap, $volname);
304 run_command
($cmd, errmsg
=> "sheepdog snapshot $volname' error");
309 sub volume_snapshot_rollback
{
310 my ($class, $scfg, $storeid, $volname, $snap) = @_;
312 my $cmd = &$collie_cmd($scfg, 'vdi', 'rollback', '-s', $snap, $volname);
313 run_command
($cmd, errmsg
=> "sheepdog snapshot $volname' error");
317 sub volume_snapshot_delete
{
318 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
320 return 1 if $running;
322 my $cmd = &$collie_cmd($scfg, 'vdi', 'delete', '-s', $snap, $volname);
323 run_command
($cmd, errmsg
=> "sheepdog snapshot $volname' error");