[pve-devel] [PATCH 1/3] add sheepdog plugin
Alexandre Derumier
aderumier at odiso.com
Sat Jul 14 15:53:55 CEST 2012
storage definition
-------------------
portal 127.0.0.1:7000
Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
PVE/Storage/Makefile | 2 +-
PVE/Storage/Plugin.pm | 2 +-
PVE/Storage/SheepdogPlugin.pm | 198 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 200 insertions(+), 2 deletions(-)
create mode 100644 PVE/Storage/SheepdogPlugin.pm
diff --git a/PVE/Storage/Makefile b/PVE/Storage/Makefile
index 0f9950a..e13890f 100644
--- a/PVE/Storage/Makefile
+++ b/PVE/Storage/Makefile
@@ -1,4 +1,4 @@
-SOURCES=Plugin.pm DirPlugin.pm LVMPlugin.pm NFSPlugin.pm ISCSIPlugin.pm RBDPlugin.pm
+SOURCES=Plugin.pm DirPlugin.pm LVMPlugin.pm NFSPlugin.pm ISCSIPlugin.pm RBDPlugin.pm SheepdogPlugin.pm
.PHONY: install
install:
diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm
index 88d98bb..df994d5 100644
--- a/PVE/Storage/Plugin.pm
+++ b/PVE/Storage/Plugin.pm
@@ -300,7 +300,7 @@ sub parse_config {
$d->{content} = $def->{content}->[1] if !$d->{content};
}
- if ($type eq 'iscsi' || $type eq 'nfs' || $type eq 'rbd' ) {
+ if ($type eq 'iscsi' || $type eq 'nfs' || $type eq 'rbd' || $type eq 'sheepdog' ) {
$d->{shared} = 1;
}
}
diff --git a/PVE/Storage/SheepdogPlugin.pm b/PVE/Storage/SheepdogPlugin.pm
new file mode 100644
index 0000000..c6729df
--- /dev/null
+++ b/PVE/Storage/SheepdogPlugin.pm
@@ -0,0 +1,198 @@
+package PVE::Storage::SheepdogPlugin;
+
+use strict;
+use warnings;
+use IO::File;
+use PVE::Tools qw(run_command trim);
+use PVE::Storage::Plugin;
+use PVE::JSONSchema qw(get_standard_option);
+
+use base qw(PVE::Storage::Plugin);
+
+
+sub sheepdog_ls{
+ my ($scfg, $storeid) = @_;
+
+ my $portal = $scfg->{portal};
+ my ($server, $port) = split(':', $portal);
+ my $cmd = ['/usr/sbin/collie', 'vdi', 'list', '-a', $server ];
+ my $list = {};
+
+
+ run_command($cmd,outfunc => sub {
+ my $line = shift;
+ $line = trim($line);
+ if( $line =~ /(vm-(\d+)-\S+)\s+(\d+)\s+([\.0-9]*)\s(\w+)\s+([\.0-9]*)\s(\w+)\W+([\.0-9]*)\s(\w+)\s+([\-0-9]*)\s([:0-9]*)\W+/ ) {
+
+ my $image = $1;
+ my $owner = $2;
+ my $size = $4;
+
+ $list->{$storeid}->{$image} = {
+ name => $image,
+ size => $size,
+ vmid => $owner
+ };
+
+
+
+ }
+ });
+
+ return $list;
+
+}
+
+# Configuration
+
+
+sub type {
+ return 'sheepdog';
+}
+
+sub plugindata {
+ return {
+ content => [ {images => 1}, { images => 1 }],
+ };
+}
+
+
+sub options {
+ return {
+ portal => { fixed => 1 },
+ content => { optional => 1 },
+ };
+}
+
+# Storage implementation
+
+sub parse_volname {
+ my ($class, $volname) = @_;
+
+ if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
+ return ('images', $1, $2);
+ }
+
+ die "unable to parse rbd volume name '$volname'\n";
+}
+
+sub path {
+ my ($class, $scfg, $volname, $storeid) = @_;
+
+ my ($vtype, $name, $vmid) = $class->parse_volname($volname);
+
+ my $portal = $scfg->{portal};
+
+ my $path = "sheepdog:$portal:$name";
+
+ return ($path, $vmid, $vtype);
+}
+
+sub alloc_image {
+ my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
+
+
+ die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
+ if $name && $name !~ m/^vm-$vmid-/;
+ my $portal = $scfg->{portal};
+ my ($server, $port) = split(':', $portal);
+
+ if (!$name) {
+ my $sheepdog = sheepdog_ls($scfg, $storeid);
+
+ for (my $i = 1; $i < 100; $i++) {
+ my $tn = "vm-$vmid-disk-$i";
+ if (!defined ($sheepdog->{$storeid}->{$tn})) {
+ $name = $tn;
+ last;
+ }
+ }
+ }
+
+ die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
+ if !$name;
+ my $cmd = ['/usr/sbin/collie', 'vdi', 'create' , $name , $size.'KB', '-a', $server ];
+ run_command($cmd, errmsg => "sheepdog create $name' error");
+
+ return $name;
+}
+
+sub free_image {
+ my ($class, $storeid, $scfg, $volname) = @_;
+
+ my $portal = $scfg->{portal};
+ my ($server, $port) = split(':', $portal);
+
+ my $cmd = ['/usr/sbin/collie', 'vdi', 'delete' , $volname, '-a', $server ];
+
+ run_command($cmd, errmsg => "sheepdog delete $volname' error");
+
+ return undef;
+}
+
+sub list_images {
+ my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
+
+ $cache->{sheepdog} = sheepdog_ls($scfg, $storeid) if !$cache->{sheepdog};
+ my $res = [];
+
+ if (my $dat = $cache->{sheepdog}->{$storeid}) {
+ foreach my $image (keys %$dat) {
+
+ my $volname = $dat->{$image}->{name};
+
+ my $volid = "$storeid:$volname";
+
+
+ my $owner = $dat->{$volname}->{vmid};
+ if ($vollist) {
+ my $found = grep { $_ eq $volid } @$vollist;
+ next if !$found;
+ } else {
+ next if defined ($vmid) && ($owner ne $vmid);
+ }
+
+ my $info = $dat->{$volname};
+ $info->{volid} = $volid;
+
+ push @$res, $info;
+ }
+ }
+
+ return $res;
+}
+
+
+sub status {
+ my ($class, $storeid, $scfg, $cache) = @_;
+
+ my $total = 0;
+ my $free = 0;
+ my $used = 0;
+ my $active = 1;
+ return ($total,$free,$used,$active);
+
+ return undef;
+}
+
+sub activate_storage {
+ my ($class, $storeid, $scfg, $cache) = @_;
+ return 1;
+}
+
+sub deactivate_storage {
+ my ($class, $storeid, $scfg, $cache) = @_;
+ return 1;
+}
+
+sub activate_volume {
+ my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
+ return 1;
+}
+
+sub deactivate_volume {
+ my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
+ return 1;
+}
+
+1;
--
1.7.2.5
More information about the pve-devel
mailing list