<div dir="ltr">it depend on the features we will implement in this!<div>This is not all.<br><div>But your right it would be better extend on from an other.</div><div><br></div><div>regards </div><div><br></div><div>Wolfgang Link</div><div><br></div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">2015-01-20 18:29 GMT+01:00 Adrian Costin <span dir="ltr"><<a href="mailto:adrian@goat.fish" target="_blank">adrian@goat.fish</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I’ve been using a similar plugin for local zfs, basically a stripped down version of the ZFSPlugin.pm, however I’ve been thinking if there’s a way to implement this as a LunCmd for the actual ZFS Plugin instead of a separate plugin.<br>
<br>
Do you think it’s worth pursuing or is a separate plugin better?<br>
<br>
Best regards,<br>
Adrian Costin<br>
<div><div class="h5"><br>
<br>
> On Jan 20, 2015, at 4:37 PM, Wolfgang Link <<a href="mailto:wolfgang@linksystems.org">wolfgang@linksystems.org</a>> wrote:<br>
><br>
><br>
> Signed-off-by: Wolfgang Link <<a href="mailto:wolfgang@linksystems.org">wolfgang@linksystems.org</a>><br>
> ---<br>
> PVE/Storage.pm | 2 +<br>
> PVE/Storage/Makefile | 2 +-<br>
> PVE/Storage/ZFSLocalPlugin.pm | 517 +++++++++++++++++++++++++++++++++++++++++<br>
> 3 files changed, 520 insertions(+), 1 deletion(-)<br>
> create mode 100644 PVE/Storage/ZFSLocalPlugin.pm<br>
><br>
> diff --git a/PVE/Storage.pm b/PVE/Storage.pm<br>
> index 3cc9581..b978b66 100755<br>
> --- a/PVE/Storage.pm<br>
> +++ b/PVE/Storage.pm<br>
> @@ -28,6 +28,7 @@ use PVE::Storage::SheepdogPlugin;<br>
> use PVE::Storage::ISCSIDirectPlugin;<br>
> use PVE::Storage::GlusterfsPlugin;<br>
> use PVE::Storage::ZFSPlugin;<br>
> +use PVE::Storage::ZFSLocalPlugin;<br>
><br>
> # load and initialize all plugins<br>
> PVE::Storage::DirPlugin->register();<br>
> @@ -39,6 +40,7 @@ PVE::Storage::SheepdogPlugin->register();<br>
> PVE::Storage::ISCSIDirectPlugin->register();<br>
> PVE::Storage::GlusterfsPlugin->register();<br>
> PVE::Storage::ZFSPlugin->register();<br>
> +PVE::Storage::ZFSLocalPlugin->register();<br>
> PVE::Storage::Plugin->init();<br>
><br>
> my $UDEVADM = '/sbin/udevadm';<br>
> diff --git a/PVE/Storage/Makefile b/PVE/Storage/Makefile<br>
> index 919c486..475f757 100644<br>
> --- a/PVE/Storage/Makefile<br>
> +++ b/PVE/Storage/Makefile<br>
> @@ -1,4 +1,4 @@<br>
> -SOURCES=Plugin.pm DirPlugin.pm LVMPlugin.pm NFSPlugin.pm ISCSIPlugin.pm RBDPlugin.pm SheepdogPlugin.pm ISCSIDirectPlugin.pm GlusterfsPlugin.pm ZFSPlugin.pm<br>
> +SOURCES=Plugin.pm DirPlugin.pm LVMPlugin.pm NFSPlugin.pm ISCSIPlugin.pm RBDPlugin.pm SheepdogPlugin.pm ISCSIDirectPlugin.pm GlusterfsPlugin.pm ZFSPlugin.pm ZFSLocalPlugin.pm<br>
><br>
> .PHONY: install<br>
> install:<br>
> diff --git a/PVE/Storage/ZFSLocalPlugin.pm b/PVE/Storage/ZFSLocalPlugin.pm<br>
> new file mode 100644<br>
> index 0000000..df211f7<br>
> --- /dev/null<br>
> +++ b/PVE/Storage/ZFSLocalPlugin.pm<br>
> @@ -0,0 +1,517 @@<br>
> +package PVE::Storage::ZFSLocalPlugin;<br>
> +<br>
> +use strict;<br>
> +use warnings;<br>
> +use IO::File;<br>
> +use POSIX;<br>
> +use PVE::Tools qw(run_command);<br>
> +use PVE::Storage::Plugin;<br>
> +<br>
> +use base qw(PVE::Storage::Plugin);<br>
> +<br>
> +sub zfs_request {<br>
> + my ($scfg, $timeout, $method, @params) = @_;<br>
> +<br>
> + my $cmdmap;<br>
> + my $zfscmd;<br>
> + my $msg;<br>
> +<br>
> + $timeout = 5 if !$timeout;<br>
> +<br>
> + if ($method eq 'zpool_list') {<br>
> + $zfscmd = 'zpool';<br>
> + $method = 'list',<br>
> + } else {<br>
> + $zfscmd = 'zfs';<br>
> + }<br>
> +<br>
> + my $cmd = [$zfscmd, $method, @params];<br>
> +<br>
> + $msg = '';<br>
> +<br>
> + my $output = sub {<br>
> + my $line = shift;<br>
> + $msg .= "$line\n";<br>
> + };<br>
> +<br>
> + run_command($cmd, outfunc => $output, timeout => $timeout);<br>
> +<br>
> + return $msg;<br>
> +}<br>
> +<br>
> +sub zfs_parse_size {<br>
> + my ($text) = @_;<br>
> +<br>
> + return 0 if !$text;<br>
> +<br>
> + if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) {<br>
> + my ($size, $reminder, $unit) = ($1, $2, $3);<br>
> + return $size if !$unit;<br>
> + if ($unit eq 'K') {<br>
> + $size *= 1024;<br>
> + } elsif ($unit eq 'M') {<br>
> + $size *= 1024*1024;<br>
> + } elsif ($unit eq 'G') {<br>
> + $size *= 1024*1024*1024;<br>
> + } elsif ($unit eq 'T') {<br>
> + $size *= 1024*1024*1024*1024;<br>
> + }<br>
> +<br>
> + if ($reminder) {<br>
> + $size = ceil($size);<br>
> + }<br>
> + return $size;<br>
> + } else {<br>
> + return 0;<br>
> + }<br>
> +}<br>
> +<br>
> +sub zfs_get_pool_stats {<br>
> + my ($scfg) = @_;<br>
> +<br>
> + my $available = 0;<br>
> + my $used = 0;<br>
> +<br>
> + my $text = '';<br>
> + eval {<br>
> + $text = zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp','available,used', $scfg->{pool});<br>
> + };<br>
> + warn $@ if $@;<br>
> +<br>
> + my @lines = split /\n/, $text;<br>
> +<br>
> + if($lines[0] =~ /^(\d+)$/) {<br>
> + $available = $1;<br>
> + }<br>
> +<br>
> + if($lines[1] =~ /^(\d+)$/) {<br>
> + $used = $1;<br>
> + }<br>
> +<br>
> + return ($available, $used);<br>
> +}<br>
> +<br>
> +sub zfs_parse_zvol_list {<br>
> + my ($text) = @_;<br>
> +<br>
> + my $list = ();<br>
> +<br>
> + return $list if !$text;<br>
> +<br>
> + my @lines = split /\n/, $text;<br>
> + foreach my $line (@lines) {<br>
> + if ($line =~ /^(.+)\s+([a-zA-Z0-9\.]+|\-)\s+(.+)$/) {<br>
> + my $zvol = {};<br>
> + my @parts = split /\//, $1;<br>
> + my $name = pop @parts;<br>
> + my $pool = join('/', @parts);<br>
> +<br>
> + $zvol->{pool} = $pool;<br>
> + $zvol->{name} = $name;<br>
> + $zvol->{size} = zfs_parse_size($2);<br>
> + if ($3 !~ /^-$/) {<br>
> + $zvol->{origin} = $3;<br>
> + }<br>
> + push @$list, $zvol;<br>
> + }<br>
> + }<br>
> +<br>
> + return $list;<br>
> +}<br>
> +<br>
> +sub zfs_get_zvol_size {<br>
> + my ($scfg, $zvol) = @_;<br>
> +<br>
> + my $text = '';<br>
> + eval {<br>
> + $text = zfs_request($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol");<br>
> + };<br>
> + warn $@ if $@;<br>
> + if($text =~ /volsize\s(\d+)/){<br>
> + return $1;<br>
> + }<br>
> +<br>
> + die "Could not get zvol size";<br>
> +}<br>
> +<br>
> +sub zfs_create_zvol {<br>
> + my ($scfg, $zvol, $size) = @_;<br>
> +<br>
> + my $blocksize = "8kb";<br>
> + if ($scfg->{blocksize}) {<br>
> + $blocksize = $scfg->{blocksize};<br>
> + }<br>
> +<br>
> + eval {<br>
> + if ($scfg->{sparse}) {<br>
> + zfs_request($scfg, undef, 'create', '-s', '-b', $blocksize, '-V', "${size}k", "$scfg->{pool}/$zvol");<br>
> + } else {<br>
> + zfs_request($scfg, undef, 'create', '-b', $blocksize, '-V', "${size}k", "$scfg->{pool}/$zvol");<br>
> + }<br>
> + };<br>
> + warn $@ if $@;<br>
> +}<br>
> +<br>
> +sub zfs_delete_zvol {<br>
> + my ($scfg, $zvol) = @_;<br>
> + eval {<br>
> + zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol");<br>
> + };<br>
> + warn $@ if $@;<br>
> +}<br>
> +<br>
> +sub zfs_list_zvol {<br>
> + my ($scfg) = @_;<br>
> +<br>
> + my $text = '';<br>
> + eval {<br>
> + $text = zfs_request($scfg, 5, 'list', '-o', 'name,volsize,origin', '-t', 'volume', '-Hr');<br>
> + };<br>
> + warn $@ if $@;<br>
> +<br>
> + my $zvols = zfs_parse_zvol_list($text);<br>
> + return undef if !$zvols;<br>
> +<br>
> + my $list = ();<br>
> + foreach my $zvol (@$zvols) {<br>
> + my @values = split('/', $zvol->{name});<br>
> + my $image = pop @values;<br>
> + my $pool = $zvol->{pool};<br>
> +<br>
> + next if $image !~ m/^((vm|base)-(\d+)-\S+)$/;<br>
> + my $owner = $3;<br>
> +<br>
> + my $parent = $zvol->{origin};<br>
> + if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){<br>
> + $parent = $1;<br>
> + }<br>
> +<br>
> + $list->{$pool}->{$image} = {<br>
> + name => $image,<br>
> + size => $zvol->{size},<br>
> + parent => $parent,<br>
> + format => 'raw',<br>
> + vmid => $owner<br>
> + };<br>
> + }<br>
> +<br>
> + return $list;<br>
> +}<br>
> +<br>
> +# Configuration<br>
> +<br>
> +sub type {<br>
> + return 'zfslocal';<br>
> +}<br>
> +<br>
> +sub plugindata {<br>
> + return {<br>
> + content => [ {images => 1}, { images => 1 }],<br>
> + };<br>
> +}<br>
> +<br>
> +sub properties {<br>
> + return {<br>
> + };<br>
> +}<br>
> +<br>
> +sub options {<br>
> + return {<br>
> + nodes => { optional => 1 },<br>
> + disable => { optional => 1 },<br>
> + pool => { fixed => 1 },<br>
> + blocksize => { optional => 1 },<br>
> + nowritecache => { optional => 1 },<br>
> + sparse => { optional => 1 },<br>
> + content => { optional => 1 },<br>
> + };<br>
> +}<br>
> +<br>
> +# Storage implementation<br>
> +<br>
> +sub parse_volname {<br>
> + my ($class, $volname) = @_;<br>
> +<br>
> + if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\_)?((base)?(vm)?-(\d+)-\S+)$/) {<br>
> + return ('images', $5, $8, $2, $4, $6);<br>
> + }<br>
> +<br>
> + die "unable to parse zfs volume name '$volname'\n";<br>
> +}<br>
> +<br>
> +sub path {<br>
> + my ($class, $scfg, $volname) = @_;<br>
> +<br>
> + my ($vtype, $name, $vmid) = $class->parse_volname($volname);<br>
> +<br>
> + my $path = "/dev/zvol/$scfg->{pool}/$volname";<br>
> +<br>
> + return ($path, $vmid, $vtype);<br>
> +}<br>
> +<br>
> +my $find_free_diskname = sub {<br>
> + my ($storeid, $scfg, $vmid) = @_;<br>
> +<br>
> + my $name = undef;<br>
> + my $volumes = zfs_list_zvol($scfg);<br>
> +<br>
> + my $disk_ids = {};<br>
> + my $dat = $volumes->{$scfg->{pool}};<br>
> +<br>
> + foreach my $image (keys %$dat) {<br>
> + my $volname = $dat->{$image}->{name};<br>
> + if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){<br>
> + $disk_ids->{$2} = 1;<br>
> + }<br>
> + }<br>
> +<br>
> + for (my $i = 1; $i < 100; $i++) {<br>
> + if (!$disk_ids->{$i}) {<br>
> + return "vm-$vmid-disk-$i";<br>
> + }<br>
> + }<br>
> +<br>
> + die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";<br>
> +};<br>
> +sub create_base {<br>
> + my ($class, $storeid, $scfg, $volname) = @_;<br>
> +<br>
> + my $snap = '__base__';<br>
> +<br>
> + my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =<br>
> + $class->parse_volname($volname);<br>
> +<br>
> + die "create_base not possible with base image\n" if $isBase;<br>
> +<br>
> + my $newname = $name;<br>
> + $newname =~ s/^vm-/base-/;<br>
> +<br>
> + my $newvolname = $basename ? "$basename/$newname" : "$newname";<br>
> + eval {<br>
> + zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");<br>
> + };<br>
> + warn $@ if $@;<br>
> + my $running = undef; #fixme : is create_base always offline ?<br>
> +<br>
> + $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);<br>
> +<br>
> + return $newvolname;<br>
> +}<br>
> +<br>
> +sub clone_image {<br>
> + my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;<br>
> +<br>
> + $snap ||= '__base__';<br>
> +<br>
> + my ($vtype, $basename, $basevmid, undef, undef, $isBase) =<br>
> + $class->parse_volname($volname);<br>
> +<br>
> + die "clone_image only works on base images\n" if !$isBase;<br>
> +<br>
> + my $name = &$find_free_diskname($storeid, $scfg, $vmid);<br>
> +<br>
> + warn "clone $volname: $basename to $name\n";<br>
> + eval {<br>
> + zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$basename\_$name");<br>
> + };<br>
> + warn $@ if $@;<br>
> + return "$basename\_$name";<br>
> +}<br>
> +<br>
> +sub alloc_image {<br>
> + my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;<br>
> +<br>
> + die "unsupported format '$fmt'" if $fmt ne 'raw';<br>
> +<br>
> + die "illegal name '$name' - sould be 'vm-$vmid-*'\n"<br>
> + if $name && $name !~ m/^vm-$vmid-/;<br>
> +<br>
> + $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;<br>
> + eval {<br>
> + zfs_create_zvol($scfg, $name, $size);<br>
> + };<br>
> + warn $@ if $@;<br>
> +<br>
> + run_command ("udevadm trigger --subsystem-match block");<br>
> + run_command ("udevadm settle --timeout 3");<br>
> +<br>
> + my $file = "/dev/zvol/".$scfg->{pool}."/$name" ;<br>
> + for (my $i = 5; $i > 0; $i--) {<br>
> + last if -e $file;<br>
> + Time::HiRes::usleep(100);<br>
> + }<br>
> +<br>
> + return $name;<br>
> +}<br>
> +<br>
> +sub free_image {<br>
> + my ($class, $storeid, $scfg, $volname, $isBase) = @_;<br>
> +<br>
> + my ($vtype, $name, $vmid) = $class->parse_volname($volname);<br>
> +<br>
> + eval {<br>
> + zfs_delete_zvol($scfg, $volname);<br>
> + };<br>
> + warn $@ if $@;<br>
> +<br>
> + return undef;<br>
> +}<br>
> +<br>
> +sub list_images {<br>
> + my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;<br>
> +<br>
> + $cache->{zfs} = zfs_list_zvol($scfg) if !$cache->{zfs};<br>
> + my $zfspool = $scfg->{pool};<br>
> + my $res = [];<br>
> + if (my $dat = $cache->{zfs}->{$zfspool}) {<br>
> +<br>
> + foreach my $image (keys %$dat) {<br>
> +<br>
> + my $volname = $dat->{$image}->{name};<br>
> + my $parent = $dat->{$image}->{parent};<br>
> + my $owner = $dat->{$volname}->{vmid};<br>
> +<br>
> + next if ($volname =~ "state");<br>
> +<br>
> + my $volid = "$storeid:$volname";<br>
> +<br>
> + if ($vollist) {<br>
> + my $found = grep { $_ eq $volid } @$vollist;<br>
> + next if !$found;<br>
> + } else {<br>
> + next if defined ($vmid) && ($owner ne $vmid);<br>
> + }<br>
> +<br>
> + my $info = $dat->{$volname};<br>
> + $info->{volid} = $volid;<br>
> + push @$res, $info;<br>
> + }<br>
> + }<br>
> + return $res;<br>
> +}<br>
> +<br>
> +sub status {<br>
> + my ($class, $storeid, $scfg, $cache) = @_;<br>
> +<br>
> + my $total = 0;<br>
> + my $free = 0;<br>
> + my $used = 0;<br>
> + my $active = 0;<br>
> +<br>
> + eval {<br>
> + ($free, $used) = zfs_get_pool_stats($scfg);<br>
> + $active = 1;<br>
> + $total = $free + $used;<br>
> + };<br>
> + warn $@ if $@;<br>
> +<br>
> + return ($total, $free, $used, $active);<br>
> +}<br>
> +<br>
> +sub activate_storage {<br>
> + my ($class, $storeid, $scfg, $cache) = @_;<br>
> + return 1;<br>
> +}<br>
> +<br>
> +sub deactivate_storage {<br>
> + my ($class, $storeid, $scfg, $cache) = @_;<br>
> + return 1;<br>
> +}<br>
> +<br>
> +sub activate_volume {<br>
> + my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;<br>
> + return 1;<br>
> +}<br>
> +<br>
> +sub deactivate_volume {<br>
> + my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;<br>
> + return 1;<br>
> +}<br>
> +<br>
> +sub volume_size_info {<br>
> + my ($class, $scfg, $storeid, $volname, $timeout) = @_;<br>
> + my $res = '';<br>
> + eval {<br>
> + $res = zfs_get_zvol_size($scfg, $volname);<br>
> + };<br>
> + warn $@ if $@;<br>
> + return $res;<br>
> +}<br>
> +<br>
> +sub volume_resize {<br>
> + my ($class, $scfg, $storeid, $volname, $size, $running) = @_;<br>
> +<br>
> + my $new_size = ($size/1024);<br>
> +<br>
> + eval {<br>
> + zfs_request($scfg, undef, 'set', 'volsize=' . $new_size . 'k', "$scfg->{pool}/$volname");<br>
> + };<br>
> + warn $@ if $@;<br>
> +}<br>
> +<br>
> +sub volume_snapshot {<br>
> + my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;<br>
> + eval {<br>
> + zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");<br>
> + };<br>
> + warn $@ if $@;<br>
> +}<br>
> +<br>
> +sub volume_snapshot_rollback {<br>
> + my ($class, $scfg, $storeid, $volname, $snap) = @_;<br>
> +<br>
> + # abort rollback if snapshot is not the latest<br>
> + my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');<br>
> + my $text = zfs_request($scfg, undef, 'list', @params);<br>
> + my @snapshots = split(/\n/, $text);<br>
> + my $recentsnap = undef;<br>
> + foreach (@snapshots) {<br>
> + if (/$scfg->{pool}\/$volname/) {<br>
> + s/^.*@//;<br>
> + $recentsnap = $_;<br>
> + }<br>
> + }<br>
> + if ($snap ne $recentsnap) {<br>
> + die "cannot rollback, more recent snapshots exist\n";<br>
> + }<br>
> + eval {<br>
> + zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");<br>
> + };<br>
> + warn $@ if $@;<br>
> +}<br>
> +<br>
> +sub volume_snapshot_delete {<br>
> + my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;<br>
> + eval {<br>
> + zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");<br>
> + };<br>
> + warn $@ if $@;<br>
> +}<br>
> +<br>
> +sub volume_has_feature {<br>
> + my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;<br>
> +<br>
> + my $features = {<br>
> + snapshot => { current => 1, snap => 1},<br>
> + clone => { base => 1},<br>
> + template => { current => 1},<br>
> + copy => { base => 1, current => 1},<br>
> + };<br>
> +<br>
> + my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =<br>
> + $class->parse_volname($volname);<br>
> +<br>
> + my $key = undef;<br>
> +<br>
> + if ($snapname) {<br>
> + $key = 'snap';<br>
> + } else {<br>
> + $key = $isBase ? 'base' : 'current';<br>
> + }<br>
> +<br>
> + return 1 if $features->{$feature}->{$key};<br>
> +<br>
> + return undef;<br>
> +}<br>
> +<br>
> +1;<br>
> --<br>
> 1.7.10.4<br>
><br>
><br>
</div></div>> _______________________________________________<br>
> pve-devel mailing list<br>
> <a href="mailto:pve-devel@pve.proxmox.com">pve-devel@pve.proxmox.com</a><br>
> <a href="http://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel" target="_blank">http://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel</a><br>
<br>
</blockquote></div><br></div>