]> 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 cd4746be60ff3bb70acbe3c338acd502b3c9b1ea..80c950146cf268922863852b3b199d08c12bcf2b 100644 (file)
@@ -12,27 +12,28 @@ 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);
 
 __PACKAGE__->register_method ({
-    name => 'index', 
+    name => 'index',
     path => '',
     method => 'GET',
     description => "List storage content.",
-    permissions => { 
+    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', {
                completion => \&PVE::Storage::complete_storage_enabled,
             }),
-           content => { 
+           content => {
                description => "Only list content of this type.",
                type => 'string', format => 'pve-storage-content',
                optional => 1,
@@ -49,7 +50,7 @@ __PACKAGE__->register_method ({
        type => 'array',
        items => {
            type => "object",
-           properties => { 
+           properties => {
                volid => {
                    description => "Volume identifier.",
                    type => 'string',
@@ -80,6 +81,12 @@ __PACKAGE__->register_method ({
                    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}" } ],
@@ -101,30 +108,31 @@ __PACKAGE__->register_method ({
        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 => { 
+    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', {
                completion => \&PVE::Storage::complete_storage_enabled,
            }),
-           filename => { 
+           filename => {
                description => "The name of the file to create.",
                type => 'string',
            },
@@ -171,16 +179,16 @@ __PACKAGE__->register_method ({
        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 = PVE::Storage::config();
-    
-       my $volid = PVE::Storage::vdisk_alloc ($cfg, $storeid, $param->{vmid}, 
-                                              $param->{format}, 
+
+       my $volid = PVE::Storage::vdisk_alloc ($cfg, $storeid, $param->{vmid},
+                                              $param->{format},
                                               $name, $size);
 
        return $volid;
@@ -201,12 +209,12 @@ my $real_volume_id = sub {
            $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";
     }
 
@@ -218,7 +226,7 @@ __PACKAGE__->register_method ({
     path => '{volume}',
     method => 'GET',
     description => "Get volume attributes",
-    permissions => { 
+    permissions => {
        description => "You need read access for the volume.",
        user => 'all',
     },
@@ -226,12 +234,12 @@ __PACKAGE__->register_method ({
     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',
            },
        },
     },
@@ -266,7 +274,7 @@ __PACKAGE__->register_method ({
     path => '{volume}',
     method => 'DELETE',
     description => "Delete volume",
-    permissions => { 
+    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',
     },
@@ -274,7 +282,7 @@ __PACKAGE__->register_method ({
     proxyto => 'node',
     parameters => {
        additionalProperties => 0,
-       properties => { 
+       properties => {
            node => get_standard_option('pve-node'),
            storage => get_standard_option('pve-storage-id', {
                 optional => 1,
@@ -285,9 +293,16 @@ __PACKAGE__->register_method ({
                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) = @_;
 
@@ -306,16 +321,36 @@ __PACKAGE__->register_method ({
            $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.Allocate']);
        }
 
-       PVE::Storage::vdisk_free ($cfg, $volid);
+       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;
+           }
+       };
 
-       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 undef;
+       return $upid;
     }});
 
 __PACKAGE__->register_method ({
@@ -327,24 +362,24 @@ __PACKAGE__->register_method ({
     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 {
@@ -382,7 +417,7 @@ __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
-           my $sshinfo = PVE::Cluster::get_ssh_info($target_node);
+           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";