]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/ZFSPoolPlugin.pm
zfs: list images: don't use cache
[pve-storage.git] / PVE / Storage / ZFSPoolPlugin.pm
index 5f6befd23d57bc28b28f69eeca989aa826e19784..08998944721b016af8abe9b1fbf72052fa41d43a 100644 (file)
@@ -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,11 +254,11 @@ 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 $zfs_list = $class->zfs_list_zvol($scfg);
+
     my $res = [];
 
-    if (my $dat = $cache->{zfs}->{$zfspool}) {
+    if (my $dat = $zfs_list) {
 
        foreach my $image (keys %$dat) {
 
@@ -370,20 +375,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 $text = $class->zfs_request(
+       $scfg,
+       10,
+       'list',
+       '-o',
+       'name,volsize,origin,type,refquota',
+       '-t',
+       'volume,filesystem',
+       '-Hrp',
+       $scfg->{pool},
+    );
     my $zvols = zfs_parse_zvol_list($text);
     return undef if !$zvols;
 
     my $list = ();
     foreach my $zvol (@$zvols) {
-       my $pool = $zvol->{pool};
+       # The "pool" in $scfg is not the same as ZFS pool, so it's necessary to filter here.
+       next if $scfg->{pool} ne $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,
@@ -398,9 +415,11 @@ sub zfs_list_zvol {
 sub zfs_get_sorted_snapshot_list {
     my ($class, $scfg, $volname, $sort_params) = @_;
 
+    my @params = ('-H', '-r', '-t', 'snapshot', '-o', 'name', $sort_params->@*);
+
     my $vname = ($class->parse_volname($volname))[1];
+    push @params, "$scfg->{pool}\/$vname";
 
-    my @params = ('-H', '-t', 'snapshot', '-o', 'name', $sort_params->@*, "$scfg->{pool}\/$vname");
     my $text = $class->zfs_request($scfg, undef, 'list', @params);
     my @snapshots = split(/\n/, $text);
 
@@ -513,9 +532,11 @@ sub volume_rollback_is_possible {
 sub volume_snapshot_info {
     my ($class, $scfg, $storeid, $volname) = @_;
 
+    my @params = ('-Hp', '-r', '-t', 'snapshot', '-o', 'name,guid,creation');
+
     my $vname = ($class->parse_volname($volname))[1];
+    push @params, "$scfg->{pool}\/$vname";
 
-    my @params = ('-Hp', '-t', 'snapshot', '-o', 'name,guid,creation', "$scfg->{pool}\/$vname");
     my $text = $class->zfs_request($scfg, undef, 'list', @params);
     my @lines = split(/\n/, $text);
 
@@ -766,7 +787,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],
@@ -776,7 +799,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";
     }