]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/API2/Storage/Content.pm
api: storage/content: add ctime to return schema
[pve-storage.git] / PVE / API2 / Storage / Content.pm
index 239522d42716ba906c78af182fb421d02db8ce93..80c950146cf268922863852b3b199d08c12bcf2b 100644 (file)
@@ -2,52 +2,91 @@ package PVE::API2::Storage::Content;
 
 use strict;
 use warnings;
+use Data::Dumper;
 
 use PVE::SafeSyslog;
-use PVE::Cluster qw(cfs_read_file);
+use PVE::Cluster;
 use PVE::Storage;
 use PVE::INotify;
 use PVE::Exception qw(raise_param_exc);
 use PVE::RPCEnvironment;
 use PVE::RESTHandler;
 use PVE::JSONSchema qw(get_standard_option);
+use PVE::SSHInfo;
 
 use base qw(PVE::RESTHandler);
 
-my @ctypes = qw(images vztmpl iso backup);
-
 __PACKAGE__->register_method ({
-    name => 'index', 
+    name => 'index',
     path => '',
     method => 'GET',
     description => "List storage content.",
+    permissions => {
+       check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1],
+    },
     protected => 1,
     proxyto => 'node',
     parameters => {
        additionalProperties => 0,
-       properties => { 
+       properties => {
            node => get_standard_option('pve-node'),
-           storage => get_standard_option('pve-storage-id'),
-           content => { 
+           storage => get_standard_option('pve-storage-id', {
+               completion => \&PVE::Storage::complete_storage_enabled,
+            }),
+           content => {
                description => "Only list content of this type.",
                type => 'string', format => 'pve-storage-content',
                optional => 1,
+               completion => \&PVE::Storage::complete_content_type,
            },
-           vmid => get_standard_option
-               ('pve-vmid', { 
-                   description => "Only list images for this VM",
-                   optional => 1,              
-                }),
+           vmid => get_standard_option('pve-vmid', {
+               description => "Only list images for this VM",
+               optional => 1,
+               completion => \&PVE::Cluster::complete_vmid,
+           }),
        },
     },
     returns => {
        type => 'array',
        items => {
            type => "object",
-           properties => { 
-               volid => { 
-                   type => 'string' 
-               } 
+           properties => {
+               volid => {
+                   description => "Volume identifier.",
+                   type => 'string',
+               },
+               vmid => {
+                   description => "Associated Owner VMID.",
+                   type => 'integer',
+                   optional => 1,
+               },
+               parent => {
+                   description => "Volume identifier of parent (for linked cloned).",
+                   type => 'string',
+                   optional => 1,
+               },
+               'format' => {
+                   description => "Format identifier ('raw', 'qcow2', 'subvol', 'iso', 'tgz' ...)",
+                   type => 'string',
+               },
+               size => {
+                   description => "Volume size in bytes.",
+                   type => 'integer',
+                   renderer => 'bytes',
+               },
+               used => {
+                   description => "Used space. Please note that most storage plugins " .
+                       "does not report anything useful here.",
+                   type => 'integer',
+                   renderer => 'bytes',
+                   optional => 1,
+               },
+               ctime => {
+                   description => "Creation time (Unix epoch). Currently only set for backup volumes.",
+                   type => 'integer',
+                   minimum => 0,
+                   optional => 1,
+               },
            },
        },
        links => [ { rel => 'child', href => "{volid}" } ],
@@ -55,55 +94,52 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       my $cts = $param->{content} ? [ $param->{content} ] : [ @ctypes ];
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
 
        my $storeid = $param->{storage};
 
-       my $cfg = cfs_read_file("storage.cfg");
+       my $cfg = PVE::Storage::config();
 
-       my $scfg = PVE::Storage::storage_config ($cfg, $storeid);
+       my $vollist = PVE::Storage::volume_list($cfg, $storeid, $param->{vmid}, $param->{content});
 
        my $res = [];
-       foreach my $ct (@$cts) {
-           my $data;
-           if ($ct eq 'images' || defined($param->{vmid})) {
-               $data = PVE::Storage::vdisk_list ($cfg, $storeid, $param->{vmid});
-           } elsif ($ct eq 'iso') {
-               $data = PVE::Storage::template_list ($cfg, $storeid, 'iso');
-           } elsif ($ct eq 'vztmpl') {
-               $data = PVE::Storage::template_list ($cfg, $storeid, 'vztmpl');
-           } elsif ($ct eq 'backup') {
-               $data = PVE::Storage::template_list ($cfg, $storeid, 'backup');
-           }
-
-           next if !$data || !$data->{$storeid};
-
-           foreach my $item (@{$data->{$storeid}}) {
-               $item->{content} = $ct;
-               push @$res, $item;
-           }
+       foreach my $item (@$vollist) {
+           eval {  PVE::Storage::check_volume_access($rpcenv, $authuser, $cfg, undef, $item->{volid}); };
+           next if $@;
+           $item->{vmid} = int($item->{vmid}) if (defined($item->{vmid}));
+           push @$res, $item;
        }
 
-       return $res;    
+       return $res;
     }});
 
 __PACKAGE__->register_method ({
-    name => 'create', 
+    name => 'create',
     path => '',
     method => 'POST',
     description => "Allocate disk images.",
+    permissions => {
+       check => ['perm', '/storage/{storage}', ['Datastore.AllocateSpace']],
+    },
     protected => 1,
     proxyto => 'node',
     parameters => {
        additionalProperties => 0,
-       properties => { 
+       properties => {
            node => get_standard_option('pve-node'),
-           storage => get_standard_option('pve-storage-id'),
-           filename => { 
-               description => "The name of the file to create/upload.",
+           storage => get_standard_option('pve-storage-id', {
+               completion => \&PVE::Storage::complete_storage_enabled,
+           }),
+           filename => {
+               description => "The name of the file to create.",
                type => 'string',
            },
-           vmid => get_standard_option('pve-vmid', { description => "Specify owner VM" } ),
+           vmid => get_standard_option('pve-vmid', {
+               description => "Specify owner VM",
+               completion => \&PVE::Cluster::complete_vmid,
+           }),
            size => {
                description => "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)",
                type => 'string',
@@ -111,7 +147,7 @@ __PACKAGE__->register_method ({
            },
            'format' => {
                type => 'string',
-               enum => ['raw', 'qcow2'],
+               enum => ['raw', 'qcow2', 'subvol'],
                requires => 'size',
                optional => 1,
            },
@@ -140,19 +176,19 @@ __PACKAGE__->register_method ({
        }
 
        # extract FORMAT from name
-       if ($name =~ m/\.(raw|qcow2)$/) {
+       if ($name =~ m/\.(raw|qcow2|vmdk)$/) {
            my $fmt = $1;
 
-           raise_param_exc({ format => "different storage formats ($param->{format} != $fmt)" }) 
+           raise_param_exc({ format => "different storage formats ($param->{format} != $fmt)" })
                if $param->{format} && $param->{format} ne $fmt;
 
            $param->{format} = $fmt;
        }
 
-       my $cfg = cfs_read_file('storage.cfg');
-    
-       my $volid = PVE::Storage::vdisk_alloc ($cfg, $storeid, $param->{vmid}, 
-                                              $param->{format}, 
+       my $cfg = PVE::Storage::config();
+
+       my $volid = PVE::Storage::vdisk_alloc ($cfg, $storeid, $param->{vmid},
+                                              $param->{format},
                                               $name, $size);
 
        return $volid;
@@ -168,20 +204,21 @@ my $real_volume_id = sub {
     if ($volume =~ m/:/) {
        eval {
            my ($sid, $volname) = PVE::Storage::parse_volume_id ($volume);
-           raise_param_exc({ storage => "storage ID missmatch" }) 
+           die "storage ID missmatch ($sid != $storeid)\n"
                if $storeid && $sid ne $storeid;
            $volid = $volume;
+           $storeid = $sid;
        };
-       raise_param_exc({ volume => $@}) if $@; 
-          
+       raise_param_exc({ volume => $@ }) if $@;
+
     } else {
-       raise_param_exc({ volume => "no storage speficied - incomplete volume ID" }) 
+       raise_param_exc({ volume => "no storage speficied - incomplete volume ID" })
            if !$storeid;
-       
+
        $volid = "$storeid:$volume";
     }
 
-    return $volid;
+    return wantarray ? ($volid, $storeid) : $volid;
 };
 
 __PACKAGE__->register_method ({
@@ -189,16 +226,20 @@ __PACKAGE__->register_method ({
     path => '{volume}',
     method => 'GET',
     description => "Get volume attributes",
+    permissions => {
+       description => "You need read access for the volume.",
+       user => 'all',
+    },
     protected => 1,
     proxyto => 'node',
     parameters => {
        additionalProperties => 0,
-       properties => { 
+       properties => {
            node => get_standard_option('pve-node'),
            storage => get_standard_option('pve-storage-id', { optional => 1 }),
            volume => {
                description => "Volume identifier",
-               type => 'string', 
+               type => 'string',
            },
        },
     },
@@ -206,18 +247,25 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       my $volid = &$real_volume_id($param->{storage}, $param->{volume});
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $authuser = $rpcenv->get_user();
+
+       my ($volid, $storeid) = &$real_volume_id($param->{storage}, $param->{volume});
 
-       my $cfg = cfs_read_file('storage.cfg');
+       my $cfg = PVE::Storage::config();
+
+       PVE::Storage::check_volume_access($rpcenv, $authuser, $cfg, undef, $volid);
 
        my $path = PVE::Storage::path($cfg, $volid);
-       my ($size, $format, $used) = PVE::Storage::file_size_info ($path);
+       my ($size, $format, $used, $parent) =  PVE::Storage::file_size_info($path);
+       die "file_size_info on '$volid' failed\n" if !($format && $size);
 
        # fixme: return more attributes?
        return {
            path => $path,
            size => $size,
             used => $used,
+           format => $format,
        };
     }});
 
@@ -226,59 +274,112 @@ __PACKAGE__->register_method ({
     path => '{volume}',
     method => 'DELETE',
     description => "Delete volume",
+    permissions => {
+       description => "You need 'Datastore.Allocate' privilege on the storage (or 'Datastore.AllocateSpace' for backup volumes if you have VM.Backup privilege on the VM).",
+       user => 'all',
+    },
     protected => 1,
     proxyto => 'node',
     parameters => {
        additionalProperties => 0,
-       properties => { 
+       properties => {
            node => get_standard_option('pve-node'),
-           storage => get_standard_option('pve-storage-id', { optional => 1}),
+           storage => get_standard_option('pve-storage-id', {
+                optional => 1,
+                completion => \&PVE::Storage::complete_storage,
+            }),
            volume => {
                description => "Volume identifier",
-               type => 'string', 
+               type => 'string',
+               completion => \&PVE::Storage::complete_volume,
+           },
+           delay => {
+               type => 'integer',
+               description => "Time to wait for the task to finish. We return 'null' if the task finish within that time.",
+               minimum => 1,
+               maximum => 30,
+               optional => 1,
            },
        },
     },
-    returns => { type => 'null' },
+    returns => { type => 'string', optional => 1, },
     code => sub {
        my ($param) = @_;
 
-       my $volid = &$real_volume_id($param->{storage}, $param->{volume});
-       
-       my $cfg = cfs_read_file('storage.cfg');
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $authuser = $rpcenv->get_user();
+
+       my $cfg = PVE::Storage::config();
 
-       PVE::Storage::vdisk_free ($cfg, $volid);
+       my ($volid, $storeid) = &$real_volume_id($param->{storage}, $param->{volume});
 
-       return undef;
+       my ($path, $ownervm, $vtype) = PVE::Storage::path($cfg, $volid);
+       if ($vtype eq 'backup' && $ownervm) {
+           $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
+           $rpcenv->check($authuser, "/vms/$ownervm", ['VM.Backup']);
+       } else {
+           $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.Allocate']);
+       }
+
+       my $worker = sub {
+           PVE::Storage::vdisk_free ($cfg, $volid);
+           print "Removed volume '$volid'\n";
+           if ($vtype eq 'backup'
+               && $path =~ /(.*\/vzdump-\w+-\d+-\d{4}_\d{2}_\d{2}-\d{2}_\d{2}_\d{2})[^\/]+$/) {
+               my $logpath = "$1.log";
+               # try to cleanup our backup log file too, if still exisiting, #318
+               unlink($logpath) if -e $logpath;
+           }
+       };
+
+       my $id = (defined $ownervm ? "$ownervm@" : '') . $storeid;
+       my $upid = $rpcenv->fork_worker('imgdel', $id, $authuser, $worker);
+       my $background_delay = $param->{delay};
+       if ($background_delay) {
+           my $end_time = time() + $background_delay;
+           my $currently_deleting; # not necessarily true, e.g. sequential api call from cli
+           do {
+               my $task = PVE::Tools::upid_decode($upid);
+               $currently_deleting = PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart});
+               sleep 1 if $currently_deleting;
+           } while (time() < $end_time && $currently_deleting);
+
+           if (!$currently_deleting) {
+               my $status = PVE::Tools::upid_read_status($upid);
+               return undef if $status eq 'OK';
+               die $status;
+           }
+       }
+       return $upid;
     }});
 
 __PACKAGE__->register_method ({
     name => 'copy',
     path => '{volume}',
     method => 'POST',
-    description => "Copy a volume.",
+    description => "Copy a volume. This is experimental code - do not use.",
     protected => 1,
     proxyto => 'node',
     parameters => {
        additionalProperties => 0,
-       properties => { 
+       properties => {
            node => get_standard_option('pve-node'),
            storage => get_standard_option('pve-storage-id', { optional => 1}),
            volume => {
                description => "Source volume identifier",
-               type => 'string', 
+               type => 'string',
            },
            target => {
                description => "Target volume identifier",
-               type => 'string', 
+               type => 'string',
            },
-           target_node => get_standard_option('pve-node',  { 
+           target_node => get_standard_option('pve-node',  {
                description => "Target node. Default is local node.",
                optional => 1,
            }),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -299,7 +400,7 @@ __PACKAGE__->register_method ({
 
        print "DEBUG: COPY $src_volid TO $dst_volid\n";
 
-       my $cfg = cfs_read_file('storage.cfg');
+       my $cfg = PVE::Storage::config();
 
        # do all parameter checks first
 
@@ -316,7 +417,8 @@ __PACKAGE__->register_method ({
 
            # you need to get this working (fails currently, because storage_migrate() uses
            # ssh to connect to local host (which is not needed
-           PVE::Storage::storage_migrate($cfg, $src_volid, $target_node, $target_sid, $target_volname);
+           my $sshinfo = PVE::SSHInfo::get_ssh_info($target_node);
+           PVE::Storage::storage_migrate($cfg, $src_volid, $sshinfo, $target_sid, $target_volname);
 
            print "DEBUG: end worker $upid\n";