[pve-devel] [PATCH 05/09] Refactored code according to suggestions by Alexandre. Also fixed bug so that parent to an image is returned.

mir at datanom.net mir at datanom.net
Sun Aug 25 01:29:44 CEST 2013


From: Michael Rasmussen <mir at datanom.net>

The fix solves timeout problems in pve GUI since refactored code runs 5
times faster than original code.

Signed-off-by: Michael Rasmussen <mir at datanom.net>
---
 PVE/Storage/ZFSPlugin.pm | 70 ++++++++++++++++++++++++------------------------
 1 file changed, 35 insertions(+), 35 deletions(-)

diff --git a/PVE/Storage/ZFSPlugin.pm b/PVE/Storage/ZFSPlugin.pm
index ff32b97..0ec6632 100644
--- a/PVE/Storage/ZFSPlugin.pm
+++ b/PVE/Storage/ZFSPlugin.pm
@@ -96,22 +96,6 @@ sub zfs_parse_size {
     }
 }
 
-sub zfs_get_zvol_props {
-    my ($scfg, $zvol) = @_;
-    my $props = {};
-
-    my $text = zfs_request($scfg, 'get', '-H', 'all', $zvol);
-
-    my @lines = split /\n/, $text;
-    foreach my $line (@lines) {
-	if ($line =~ /^(.+)\s+(.+)\s+(.+)\s+(.+)$/) {
-            $props->{$2} = $3;
-        }
-    }
-    
-    return $props;
-}
-
 sub zfs_get_pool_stats {
 	my ($scfg) = @_;
 	my $map = {
@@ -133,23 +117,36 @@ sub zfs_get_pool_stats {
 
 sub zfs_parse_zvol_list {
     my ($text) = @_;
-    my @list = ();
-    my @zvols;
-
-    return \@list if !$text;
+	my $list = ();
+	
+    return $list if !$text;
 
     my @lines = split /\n/, $text;
     foreach my $line (@lines) {
-		@zvols = split /\//, $line;
-		my $pool = $zvols[0];
-		if (scalar(@zvols) == 2 && $zvols[0] !~ /^rpool$/) {
-		    my $disk = $zvols[1];
-		    next unless $disk =~ m!^(\w+)-(\d+)-(\w+)-(\d+)$!;
-		    push (@list, $pool . '/' . $disk);
+		if ($line =~ /^(.+)\s+([a-zA-Z0-9\.]+|\-)\s+(.+)$/) {
+			my $zvol = {};
+			my $name;
+			my $disk;
+			my @zvols = split /\//, $1;
+			my $pool = $zvols[0];
+			if (scalar(@zvols) == 2 && $zvols[0] !~ /^rpool$/) {
+				$disk = $zvols[1];
+				next unless $disk =~ m!^(\w+)-(\d+)-(\w+)-(\d+)$!;
+				$name = $pool . '/' . $disk;
+			}
+			else {
+				next;
+			}
+			$zvol->{name} = $name;
+			$zvol->{size} = zfs_parse_size($2);
+			if ($3 !~ /^-$/) {
+				$zvol->{origin} = $3;
+			}
+			push @$list, $zvol;
 		}
-    }
-
-    return \@list;
+	}
+	
+	return $list;
 }
 
 sub zfs_parse_lu_list {
@@ -264,13 +261,13 @@ sub zfs_delete_zvol {
 sub zfs_list_zvol {
     my ($scfg) = @_;
 
-    my $text = zfs_request($scfg, 'list', '-o', 'name', '-Hr');
+    my $text = zfs_request($scfg, 'list', '-o', 'name,volsize,origin', '-Hr');
     my $zvols = zfs_parse_zvol_list($text);
     return undef if !$zvols;
 
     my $list = {};
     foreach my $zvol (@$zvols) {
-		my @values = split('/', $zvol);
+		my @values = split('/', $zvol->{name});
 
 		my $pool = $values[0];
 		my $image = $values[1];
@@ -278,16 +275,19 @@ sub zfs_list_zvol {
 		if ($image =~ m/^((vm|base)-(\d+)-\S+)$/) {
 			$owner = $3;
 		}
+		else {
+			next;
+		}
 
-		my $props = zfs_get_zvol_props($scfg, $zvol);
-		my $parent = $props->{origin};
-		if($parent && $parent =~ m/^$scfg->{pool}\/(\S+)$/){
+		my $parent = $zvol->{origin};
+		if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
 			$parent = $1;
 		}
 
 		$list->{$pool}->{$image} = {
 			name => $image,
-			size => zfs_get_zvol_size($scfg, $image),
+			size => $zvol->{size},
+			parent => $parent,
 			format => 'raw',
 			vmid => $owner
 		};
-- 
1.8.4.rc3




More information about the pve-devel mailing list