]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Content.pm
1 package PVE
::API2
::Storage
::Content
;
7 use PVE
::Cluster
qw(cfs_read_file);
10 use PVE
::Exception
qw(raise_param_exc);
11 use PVE
::RPCEnvironment
;
13 use PVE
::JSONSchema
qw(get_standard_option);
15 use base
qw(PVE::RESTHandler);
17 my @ctypes = qw(images vztmpl iso backup);
19 __PACKAGE__-
>register_method ({
23 description
=> "List storage content.",
25 check
=> ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any
=> 1],
30 additionalProperties
=> 0,
32 node
=> get_standard_option
('pve-node'),
33 storage
=> get_standard_option
('pve-storage-id'),
35 description
=> "Only list content of this type.",
36 type
=> 'string', format
=> 'pve-storage-content',
39 vmid
=> get_standard_option
41 description
=> "Only list images for this VM",
56 links
=> [ { rel
=> 'child', href
=> "{volid}" } ],
61 my $cts = $param->{content
} ?
[ $param->{content
} ] : [ @ctypes ];
63 my $storeid = $param->{storage
};
65 my $cfg = cfs_read_file
("storage.cfg");
67 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
70 foreach my $ct (@$cts) {
72 if ($ct eq 'images' || defined($param->{vmid
})) {
73 $data = PVE
::Storage
::vdisk_list
($cfg, $storeid, $param->{vmid
});
74 } elsif ($ct eq 'iso') {
75 $data = PVE
::Storage
::template_list
($cfg, $storeid, 'iso');
76 } elsif ($ct eq 'vztmpl') {
77 $data = PVE
::Storage
::template_list
($cfg, $storeid, 'vztmpl');
78 } elsif ($ct eq 'backup') {
79 $data = PVE
::Storage
::template_list
($cfg, $storeid, 'backup');
82 next if !$data || !$data->{$storeid};
84 foreach my $item (@{$data->{$storeid}}) {
85 $item->{content
} = $ct;
93 __PACKAGE__-
>register_method ({
97 description
=> "Allocate disk images.",
99 check
=> ['perm', '/storage/{storage}', ['Datastore.AllocateSpace']],
104 additionalProperties
=> 0,
106 node
=> get_standard_option
('pve-node'),
107 storage
=> get_standard_option
('pve-storage-id'),
109 description
=> "The name of the file to create.",
112 vmid
=> get_standard_option
('pve-vmid', { description
=> "Specify owner VM" } ),
114 description
=> "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)",
116 pattern
=> '\d+[MG]?',
120 enum
=> ['raw', 'qcow2'],
127 description
=> "Volume identifier",
133 my $storeid = $param->{storage
};
134 my $name = $param->{filename
};
135 my $sizestr = $param->{size
};
138 if ($sizestr =~ m/^\d+$/) {
140 } elsif ($sizestr =~ m/^(\d+)M$/) {
142 } elsif ($sizestr =~ m/^(\d+)G$/) {
143 $size = $1 * 1024 * 1024;
145 raise_param_exc
({ size
=> "unable to parse size '$sizestr'" });
148 # extract FORMAT from name
149 if ($name =~ m/\.(raw|qcow2)$/) {
152 raise_param_exc
({ format
=> "different storage formats ($param->{format} != $fmt)" })
153 if $param->{format
} && $param->{format
} ne $fmt;
155 $param->{format
} = $fmt;
158 my $cfg = cfs_read_file
('storage.cfg');
160 my $volid = PVE
::Storage
::vdisk_alloc
($cfg, $storeid, $param->{vmid
},
167 # we allow to pass volume names (without storage prefix) if the storage
168 # is specified as separate parameter.
169 my $real_volume_id = sub {
170 my ($storeid, $volume) = @_;
174 if ($volume =~ m/:/) {
176 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volume);
177 raise_param_exc
({ storage
=> "storage ID missmatch" })
178 if $storeid && $sid ne $storeid;
182 raise_param_exc
({ volume
=> $@}) if $@;
185 raise_param_exc
({ volume
=> "no storage speficied - incomplete volume ID" })
188 $volid = "$storeid:$volume";
191 return wantarray ?
($volid, $storeid) : $volid;
194 __PACKAGE__-
>register_method ({
198 description
=> "Get volume attributes",
200 description
=> "You need 'Datastore.Audit' or 'Datastore.AllocateSpace' privilege on the storage.",
206 additionalProperties
=> 0,
208 node
=> get_standard_option
('pve-node'),
209 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1 }),
211 description
=> "Volume identifier",
216 returns
=> { type
=> 'object' },
220 my $rpcenv = PVE
::RPCEnvironment
::get
();
221 my $authuser = $rpcenv->get_user();
223 my ($volid, $storeid) = &$real_volume_id($param->{storage
}, $param->{volume
});
225 $rpcenv->check_any($authuser, "/storage/$storeid", ['Datastore.Audit', 'Datastore.AllocateSpace']);
227 my $cfg = cfs_read_file
('storage.cfg');
229 my $path = PVE
::Storage
::path
($cfg, $volid);
230 my ($size, $format, $used) = PVE
::Storage
::file_size_info
($path);
232 # fixme: return more attributes?
240 __PACKAGE__-
>register_method ({
244 description
=> "Delete volume",
246 description
=> "You need 'Datastore.Allocate' privilege on the storage ('Datastore.AllocateSpace' is not enough).",
252 additionalProperties
=> 0,
254 node
=> get_standard_option
('pve-node'),
255 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1}),
257 description
=> "Volume identifier",
262 returns
=> { type
=> 'null' },
266 my $rpcenv = PVE
::RPCEnvironment
::get
();
267 my $authuser = $rpcenv->get_user();
269 my ($volid, $storeid) = &$real_volume_id($param->{storage
}, $param->{volume
});
271 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.Allocate']);
273 my $cfg = cfs_read_file
('storage.cfg');
275 PVE
::Storage
::vdisk_free
($cfg, $volid);
280 __PACKAGE__-
>register_method ({
284 description
=> "Copy a volume. This is experimental code - do not use.",
288 additionalProperties
=> 0,
290 node
=> get_standard_option
('pve-node'),
291 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1}),
293 description
=> "Source volume identifier",
297 description
=> "Target volume identifier",
300 target_node
=> get_standard_option
('pve-node', {
301 description
=> "Target node. Default is local node.",
312 my $rpcenv = PVE
::RPCEnvironment
::get
();
314 my $user = $rpcenv->get_user();
316 my $target_node = $param->{target_node
} || PVE
::INotify
::nodename
();
318 # cd /nodes/localhost/storage/local/content
319 # pve:/> create local:103/vm-103-disk-1.raw -target local:103/vm-103-disk-2.raw
320 # pve:/> create 103/vm-103-disk-1.raw -target 103/vm-103-disk-3.raw
322 my $src_volid = &$real_volume_id($param->{storage
}, $param->{volume
});
323 my $dst_volid = &$real_volume_id($param->{storage
}, $param->{target
});
325 print "DEBUG: COPY $src_volid TO $dst_volid\n";
327 my $cfg = cfs_read_file
('storage.cfg');
329 # do all parameter checks first
331 # then do all short running task (to raise errors befor we go to background)
333 # then start the worker task
337 print "DEBUG: starting worker $upid\n";
339 my ($target_sid, $target_volname) = PVE
::Storage
::parse_volume_id
($dst_volid);
340 #my $target_ip = PVE::Cluster::remote_node_ip($target_node);
342 # you need to get this working (fails currently, because storage_migrate() uses
343 # ssh to connect to local host (which is not needed
344 PVE
::Storage
::storage_migrate
($cfg, $src_volid, $target_node, $target_sid, $target_volname);
346 print "DEBUG: end worker $upid\n";
350 return $rpcenv->fork_worker('imgcopy', undef, $user, $worker);