]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/ZFSPoolPlugin.pm
cifs: small line bloat reduction
[pve-storage.git] / PVE / Storage / ZFSPoolPlugin.pm
index e952a5cc36c99f6f545712132649170ce88da33e..9fbd1497fc49090de1aa6a9fdda9738f7fd6ff71 100644 (file)
@@ -58,7 +58,7 @@ sub options {
 # static zfs helper methods
 
 sub zfs_parse_zvol_list {
-    my ($text) = @_;
+    my ($text, $pool) = @_;
 
     my $list = ();
 
@@ -73,12 +73,12 @@ sub zfs_parse_zvol_list {
        my @parts = split /\//, $dataset;
        next if scalar(@parts) < 2; # we need pool/name
        my $name = pop @parts;
-       my $pool = join('/', @parts);
+       my $parsed_pool = join('/', @parts);
+       next if $parsed_pool ne $pool;
 
        next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!;
        $zvol->{owner} = $2;
 
-       $zvol->{pool} = $pool;
        $zvol->{name} = $name;
        if ($type eq 'filesystem') {
            if ($refquota eq 'none') {
@@ -178,7 +178,12 @@ sub zfs_request {
     my $msg = '';
     my $output = sub { $msg .= "$_[0]\n" };
 
-    $timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 5 if !$timeout;
+    if (PVE::RPCEnvironment->is_worker()) {
+       $timeout = 60*60 if !$timeout;
+       $timeout = 60*5 if $timeout < 60*5;
+    } else {
+       $timeout = 10 if !$timeout;
+    }
 
     run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout);
 
@@ -249,36 +254,30 @@ sub free_image {
 sub list_images {
     my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
 
-    $cache->{zfs} = $class->zfs_list_zvol($scfg) if !$cache->{zfs};
-    my $zfspool = $scfg->{pool};
-    my $res = [];
-
-    if (my $dat = $cache->{zfs}->{$zfspool}) {
-
-       foreach my $image (keys %$dat) {
+    my $zfs_list = $class->zfs_list_zvol($scfg);
 
-           my $info = $dat->{$image};
+    my $res = [];
 
-           my $volname = $info->{name};
-           my $parent = $info->{parent};
-           my $owner = $info->{vmid};
+    for my $info (values $zfs_list->%*) {
+       my $volname = $info->{name};
+       my $parent = $info->{parent};
+       my $owner = $info->{vmid};
 
-           if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
-               my ($basename) = ($1);
-               $info->{volid} = "$storeid:$basename/$volname";
-           } else {
-               $info->{volid} = "$storeid:$volname";
-           }
-
-           if ($vollist) {
-               my $found = grep { $_ eq $info->{volid} } @$vollist;
-               next if !$found;
-           } else {
-               next if defined ($vmid) && ($owner ne $vmid);
-           }
+       if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
+           my ($basename) = ($1);
+           $info->{volid} = "$storeid:$basename/$volname";
+       } else {
+           $info->{volid} = "$storeid:$volname";
+       }
 
-           push @$res, $info;
+       if ($vollist) {
+           my $found = grep { $_ eq $info->{volid} } @$vollist;
+           next if !$found;
+       } else {
+           next if defined ($vmid) && ($owner ne $vmid);
        }
+
+       push @$res, $info;
     }
     return $res;
 }
@@ -370,20 +369,32 @@ sub zfs_delete_zvol {
 sub zfs_list_zvol {
     my ($class, $scfg) = @_;
 
-    my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hrp');
-    my $zvols = zfs_parse_zvol_list($text);
-    return undef if !$zvols;
-
-    my $list = ();
+    my $text = $class->zfs_request(
+       $scfg,
+       10,
+       'list',
+       '-o',
+       'name,volsize,origin,type,refquota',
+       '-t',
+       'volume,filesystem',
+       '-d1',
+       '-Hp',
+       $scfg->{pool},
+    );
+    # It's still required to have zfs_parse_zvol_list filter by pool, because -d1 lists
+    # $scfg->{pool} too and while unlikely, it could be named to be mistaken for a volume.
+    my $zvols = zfs_parse_zvol_list($text, $scfg->{pool});
+    return {} if !$zvols;
+
+    my $list = {};
     foreach my $zvol (@$zvols) {
-       my $pool = $zvol->{pool};
        my $name = $zvol->{name};
        my $parent = $zvol->{origin};
        if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
            $parent = $1;
        }
 
-       $list->{$pool}->{$name} = {
+       $list->{$name} = {
            name => $name,
            size => $zvol->{size},
            parent => $parent,
@@ -770,7 +781,9 @@ sub volume_import {
     die "internal error: invalid file handle for volume_import\n"
        if !defined($fd);
 
-    my (undef, $dataset, $vmid) = $class->parse_volname($volname);
+    my (undef, $dataset, $vmid, undef, undef, undef, $volume_format) =
+       $class->parse_volname($volname);
+
     my $zfspath = "$scfg->{pool}/$dataset";
     my $suffix = defined($base_snapshot) ? "\@$base_snapshot" : '';
     my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
@@ -780,7 +793,7 @@ sub volume_import {
     } elsif ($exists) {
        die "volume '$zfspath' already exists\n" if !$allow_rename;
        warn "volume '$zfspath' already exists - importing with a different name\n";
-       $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
+       $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $volume_format);
        $zfspath = "$scfg->{pool}/$dataset";
     }