]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage.pm
pvesm iscsiscan: fix cli parameters
[pve-storage.git] / PVE / Storage.pm
index 9829fbdd4c8e25e2db0cda2be5fcfec4ff577a65..f9732feec778b1623e19362341a77baf068f7eb1 100755 (executable)
@@ -25,8 +25,10 @@ use PVE::Storage::DirPlugin;
 use PVE::Storage::LVMPlugin;
 use PVE::Storage::LvmThinPlugin;
 use PVE::Storage::NFSPlugin;
+use PVE::Storage::CIFSPlugin;
 use PVE::Storage::ISCSIPlugin;
 use PVE::Storage::RBDPlugin;
+use PVE::Storage::CephFSPlugin;
 use PVE::Storage::SheepdogPlugin;
 use PVE::Storage::ISCSIDirectPlugin;
 use PVE::Storage::GlusterfsPlugin;
@@ -42,8 +44,10 @@ PVE::Storage::DirPlugin->register();
 PVE::Storage::LVMPlugin->register();
 PVE::Storage::LvmThinPlugin->register();
 PVE::Storage::NFSPlugin->register();
+PVE::Storage::CIFSPlugin->register();
 PVE::Storage::ISCSIPlugin->register();
 PVE::Storage::RBDPlugin->register();
+PVE::Storage::CephFSPlugin->register();
 PVE::Storage::SheepdogPlugin->register();
 PVE::Storage::ISCSIDirectPlugin->register();
 PVE::Storage::GlusterfsPlugin->register();
@@ -536,7 +540,7 @@ sub abs_filesystem_path {
 }
 
 sub storage_migrate {
-    my ($cfg, $volid, $target_sshinfo, $target_storeid, $target_volname, $base_snapshot, $snapshot, $ratelimit_bps, $insecure, $with_snapshots) = @_;
+    my ($cfg, $volid, $target_sshinfo, $target_storeid, $target_volname, $base_snapshot, $snapshot, $ratelimit_bps, $insecure, $with_snapshots, $logfunc) = @_;
 
     my ($storeid, $volname) = parse_volume_id($volid);
     $target_volname = $volname if !$target_volname;
@@ -619,7 +623,7 @@ sub storage_migrate {
                die "import failed: exit code ".($?>>8)."\n";
            }
        } else {
-           run_command([$send, @cstream, $recv]);
+           run_command([$send, @cstream, $recv], logfunc => $logfunc);
        }
     };
     my $err = $@;
@@ -1001,7 +1005,7 @@ sub deactivate_volumes {
 }
 
 sub storage_info {
-    my ($cfg, $content) = @_;
+    my ($cfg, $content, $includeformat) = @_;
 
     my $ids = $cfg->{ids};
 
@@ -1011,7 +1015,7 @@ sub storage_info {
 
     my $slist = [];
     foreach my $storeid (keys %$ids) {
-       my $storage_enabled = storage_check_enabled($cfg, $storeid, undef, 1);
+       my $storage_enabled = defined(storage_check_enabled($cfg, $storeid, undef, 1));
 
        if (defined($content)) {
            my $want_ctype = 0;
@@ -1021,7 +1025,7 @@ sub storage_info {
                    last;
                }
            }
-           next if !$want_ctype || !defined($storage_enabled);
+           next if !$want_ctype || !$storage_enabled;
        }
 
        my $type = $ids->{$storeid}->{type};
@@ -1034,7 +1038,7 @@ sub storage_info {
            shared => $ids->{$storeid}->{shared} ? 1 : 0,
            content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}),
            active => 0,
-           enabled => defined($storage_enabled) ? 1 : 0,
+           enabled => $storage_enabled ? 1 : 0,
        };
 
        push @$slist, $storeid;
@@ -1048,15 +1052,22 @@ sub storage_info {
        next if !$info->{$storeid};
        next if !$info->{$storeid}->{enabled};
 
+       my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+       if ($includeformat) {
+           my $pd = $plugin->plugindata();
+           $info->{$storeid}->{format} = $pd->{format}
+               if $pd->{format};
+           $info->{$storeid}->{select_existing} = $pd->{select_existing}
+               if $pd->{select_existing};
+       }
+
        eval { activate_storage($cfg, $storeid, $cache); };
        if (my $err = $@) {
            warn $err;
            next;
        }
 
-       my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
-       my ($total, $avail, $used, $active);
-       eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
+       my ($total, $avail, $used, $active) = eval { $plugin->status($storeid, $scfg, $cache); };
        warn $@ if $@;
        next if !$active;
        $info->{$storeid}->{total} = int($total);
@@ -1106,6 +1117,41 @@ sub scan_nfs {
     return $res;
 }
 
+sub scan_cifs {
+    my ($server_in, $user, $password, $domain) = @_;
+
+    my $server;
+    if (!($server = resolv_server ($server_in))) {
+       die "unable to resolve address for server '${server_in}'\n";
+    }
+
+    # we support only Windows grater than 2012 cifsscan so use smb3
+    my $cmd = ['/usr/bin/smbclient', '-m', 'smb3', '-d', '0', '-L', $server];
+    if (defined($user)) {
+       die "password is required" if !defined($password);
+       push @$cmd, '-U', "$user\%$password";
+       push @$cmd, '-W', $domain if defined($domain);
+    } else {
+       push @$cmd, '-N';
+    }
+
+    my $res = {};
+    run_command($cmd,
+               outfunc => sub {
+                   my $line = shift;
+                   if ($line =~ m/(\S+)\s*Disk\s*(\S*)/) {
+                       $res->{$1} = $2;
+                   } elsif ($line =~ m/(NT_STATUS_(\S*))/) {
+                       $res->{$1} = '';
+                   }
+               },
+               errfunc => sub {},
+               noerr => 1
+    );
+
+    return $res;
+}
+
 sub scan_zfs {
 
     my $cmd = ['zfs',  'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
@@ -1526,4 +1572,79 @@ sub complete_volume {
     return $res;
 }
 
+# Various io-heavy operations require io/bandwidth limits which can be
+# configured on multiple levels: The global defaults in datacenter.cfg, and
+# per-storage overrides. When we want to do a restore from storage A to storage
+# B, we should take the smaller limit defined for storages A and B, and if no
+# such limit was specified, use the one from datacenter.cfg.
+sub get_bandwidth_limit {
+    my ($operation, $storage_list, $override) = @_;
+
+    # called for each limit (global, per-storage) with the 'default' and the
+    # $operation limit and should udpate $override for every limit affecting
+    # us.
+    my $use_global_limits = 0;
+    my $apply_limit = sub {
+       my ($bwlimit) = @_;
+       if (defined($bwlimit)) {
+           my $limits = PVE::JSONSchema::parse_property_string('bwlimit', $bwlimit);
+           my $limit = $limits->{$operation} // $limits->{default};
+           if (defined($limit)) {
+               if (!$override || $limit < $override) {
+                   $override = $limit;
+               }
+               return;
+           }
+       }
+       # If there was no applicable limit, try to apply the global ones.
+       $use_global_limits = 1;
+    };
+
+    my ($rpcenv, $authuser);
+    if (defined($override)) {
+       $rpcenv = PVE::RPCEnvironment->get();
+       $authuser = $rpcenv->get_user();
+    }
+
+    # Apply per-storage limits - if there are storages involved.
+    if (@$storage_list) {
+       my $config = config();
+
+       # The Datastore.Allocate permission allows us to modify the per-storage
+       # limits, therefore it also allows us to override them.
+       # Since we have most likely multiple storages to check, do a quick check on
+       # the general '/storage' path to see if we can skip the checks entirely:
+       return $override if $rpcenv && $rpcenv->check($authuser, '/storage', ['Datastore.Allocate'], 1);
+
+       my %done;
+       foreach my $storage (@$storage_list) {
+           # Avoid duplicate checks:
+           next if $done{$storage};
+           $done{$storage} = 1;
+
+           # Otherwise we may still have individual /storage/$ID permissions:
+           if (!$rpcenv || !$rpcenv->check($authuser, "/storage/$storage", ['Datastore.Allocate'], 1)) {
+               # And if not: apply the limits.
+               my $storecfg = storage_config($config, $storage);
+               $apply_limit->($storecfg->{bwlimit});
+           }
+       }
+
+       # Storage limits take precedence over the datacenter defaults, so if
+       # a limit was applied:
+       return $override if !$use_global_limits;
+    }
+
+    # Sys.Modify on '/' means we can change datacenter.cfg which contains the
+    # global default limits.
+    if (!$rpcenv || !$rpcenv->check($authuser, '/', ['Sys.Modify'], 1)) {
+       # So if we cannot modify global limits, apply them to our currently
+       # requested override.
+       my $dc = cfs_read_file('datacenter.cfg');
+       $apply_limit->($dc->{bwlimit});
+    }
+
+    return $override;
+}
+
 1;