]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/API2/Storage/Status.pm
followup: reword comment a bit
[pve-storage.git] / PVE / API2 / Storage / Status.pm
index 49bb58cb36198ea81f4a61c1625f74cc72023101..ce7e040b27fe62fefa94857ac91da76e8977867c 100644 (file)
@@ -7,7 +7,7 @@ use File::Path;
 use File::Basename;
 use PVE::Tools;
 use PVE::INotify;
-use PVE::Cluster qw(cfs_read_file);
+use PVE::Cluster;
 use PVE::Storage;
 use PVE::API2::Storage::Content;
 use PVE::RESTHandler;
@@ -63,13 +63,68 @@ __PACKAGE__->register_method ({
                optional => 1,
                completion => \&PVE::Cluster::get_nodelist,
            }),
+           'format' => {
+               description => "Include information about formats",
+               type => 'boolean',
+               optional => 1,
+               default => 0,
+           },
        },
     },
     returns => {
        type => 'array',
        items => {
            type => "object",
-           properties => { storage => { type => 'string' } },
+           properties => {
+               storage => get_standard_option('pve-storage-id'),
+               type => {
+                   description => "Storage type.",
+                   type => 'string',
+               },
+               content => {
+                   description => "Allowed storage content types.",
+                   type => 'string', format => 'pve-storage-content-list',
+               },
+               enabled => {
+                   description => "Set when storage is enabled (not disabled).",
+                   type => 'boolean',
+                   optional => 1,
+               },
+               active => {
+                   description => "Set when storage is accessible.",
+                   type => 'boolean',
+                   optional => 1,
+               },
+               shared => {
+                   description => "Shared flag from storage configuration.",
+                   type => 'boolean',
+                   optional => 1,
+               },
+               total => {
+                   description => "Total storage space in bytes.",
+                   type => 'integer',
+                   renderer => 'bytes',
+                   optional => 1,
+               },
+               used => {
+                   description => "Used storage space in bytes.",
+                   type => 'integer',
+                   renderer => 'bytes',
+                   optional => 1,
+               },
+               avail => {
+                   description => "Available storage space in bytes.",
+                   type => 'integer',
+                   renderer => 'bytes',
+                   optional => 1,
+               },
+               used_fraction => {
+                   description => "Used fraction (used/total).",
+                   type => 'number',
+                   renderer => 'fraction_as_percentage',
+                   optional => 1,
+               },
+           },
        },
        links => [ { rel => 'child', href => "{storage}" } ],
     },
@@ -84,18 +139,19 @@ __PACKAGE__->register_method ({
        my $target = $param->{target};
 
        undef $target if $target && ($target eq $localnode || $target eq 'localhost');
-       
-       my $cfg = cfs_read_file("storage.cfg");
 
-       my $info = PVE::Storage::storage_info($cfg, $param->{content});
+       my $cfg = PVE::Storage::config();
+
+       my $info = PVE::Storage::storage_info($cfg, $param->{content}, $param->{format});
 
        raise_param_exc({ storage => "No such storage." })
            if $param->{storage} && !defined($info->{$param->{storage}});
-       
+
        my $res = {};
        my @sids = PVE::Storage::storage_ids($cfg);
        foreach my $storeid (@sids) {
-           next if !$info->{$storeid};
+           my $data = $info->{$storeid};
+           next if !$data;
            my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ];
            next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1);
            next if $param->{storage} && $param->{storage} ne $storeid;
@@ -103,7 +159,7 @@ __PACKAGE__->register_method ({
            my $scfg = PVE::Storage::storage_config($cfg, $storeid);
 
            next if $param->{enabled} && $scfg->{disable};
+
            if ($target) {
                # check if storage content is accessible on local node and specified target node
                # we use this on the Clone GUI
@@ -113,7 +169,11 @@ __PACKAGE__->register_method ({
                next if !PVE::Storage::storage_check_node($cfg, $storeid, $target, 1);
            }
 
-           $res->{$storeid} = $info->{$storeid};
+           if ($data->{total}) {
+               $data->{used_fraction} = ($data->{used} // 0) / $data->{total};
+           }
+
+           $res->{$storeid} = $data;
        }
 
        return PVE::RESTHandler::hash_to_array($res, 'storage');
@@ -182,7 +242,7 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       my $cfg = cfs_read_file("storage.cfg");
+       my $cfg = PVE::Storage::config();
 
        my $info = PVE::Storage::storage_info($cfg, $param->{content});
 
@@ -309,7 +369,7 @@ __PACKAGE__->register_method ({
                type => 'string',
            },
            tmpfilename => { 
-               description => "The source file name. This parameter is usually set by the REST handler. You can only overwrite it when connecting to the trustet port on localhost.",
+               description => "The source file name. This parameter is usually set by the REST handler. You can only overwrite it when connecting to the trusted port on localhost.",
                type => 'string',
                optional => 1,
            },
@@ -323,13 +383,13 @@ __PACKAGE__->register_method ({
 
        my $user = $rpcenv->get_user();
 
-       my $cfg = cfs_read_file("storage.cfg");
+       my $cfg = PVE::Storage::config();
 
        my $node = $param->{node};
        my $scfg = PVE::Storage::storage_check_enabled($cfg, $param->{storage}, $node);
 
-       die "cant upload to storage type '$scfg->{type}'\n" 
-           if !($scfg->{type} eq 'dir' || $scfg->{type} eq 'nfs' || $scfg->{type} eq 'glusterfs');
+       die "can't upload to storage type '$scfg->{type}'\n"
+           if !defined($scfg->{path});
 
        my $content = $param->{content};
 
@@ -343,7 +403,7 @@ __PACKAGE__->register_method ({
 
        chomp $filename;
        $filename =~ s/^.*[\/\\]//;
-       $filename =~ s/[;:,=\s\x80-\xff]/_/g;
+       $filename =~ s/[^-a-zA-Z0-9_.]/_/g;
 
        my $path;
 
@@ -353,21 +413,24 @@ __PACKAGE__->register_method ({
            }
            $path = PVE::Storage::get_iso_dir($cfg, $param->{storage});
        } elsif ($content eq 'vztmpl') {
-           if ($filename !~ m![^/]+\.tar\.gz$!) {
-               raise_param_exc({ filename => "missing '.tar.gz' extension" });
+           if ($filename !~ m![^/]+\.tar\.[gx]z$!) {
+               raise_param_exc({ filename => "missing '.tar.gz' or '.tar.xz' extension" });
            }
            $path = PVE::Storage::get_vztmpl_dir($cfg, $param->{storage});
        } else {
            raise_param_exc({ content => "upload content type '$content' not allowed" });
        }
 
-       die "storage '$param->{storage}' does not support '$content' content\n" 
+       die "storage '$param->{storage}' does not support '$content' content\n"
            if !$scfg->{content}->{$content};
 
        my $dest = "$path/$filename";
        my $dirname = dirname($dest);
 
-       # we simply overwrite when destination when file already exists
+       # best effort to match apl_download behaviour
+       chmod 0644, $tmpfilename;
+
+       # we simply overwrite the destination file if it already exists
 
        my $cmd;
        if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
@@ -382,12 +445,12 @@ __PACKAGE__->register_method ({
                PVE::Tools::run_command([@remcmd, '/usr/sbin/pvesm', 'status', 
                                         '--storage', $param->{storage}]); 
            };
-           die "can't activate storage '$param->{storage}' on node '$node'\n" if $@;
+           die "can't activate storage '$param->{storage}' on node '$node': $@\n" if $@;
 
            PVE::Tools::run_command([@remcmd, '/bin/mkdir', '-p', '--', PVE::Tools::shell_quote($dirname)],
                                    errmsg => "mkdir failed");
  
-           $cmd = ['/usr/bin/scp', @ssh_options, '--', $tmpfilename, "[$remip]:" . PVE::Tools::shell_quote($dest)];
+           $cmd = ['/usr/bin/scp', @ssh_options, '-p', '--', $tmpfilename, "[$remip]:" . PVE::Tools::shell_quote($dest)];
        } else {
            PVE::Storage::activate_storage($cfg, $param->{storage});
            File::Path::make_path($dirname);