]>
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.",
27 additionalProperties
=> 0,
29 node
=> get_standard_option
('pve-node'),
30 storage
=> get_standard_option
('pve-storage-id'),
32 description
=> "Only list content of this type.",
33 type
=> 'string', format
=> 'pve-storage-content',
36 vmid
=> get_standard_option
38 description
=> "Only list images for this VM",
53 links
=> [ { rel
=> 'child', href
=> "{volid}" } ],
58 my $cts = $param->{content
} ?
[ $param->{content
} ] : [ @ctypes ];
60 my $storeid = $param->{storage
};
62 my $cfg = cfs_read_file
("storage.cfg");
64 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
67 foreach my $ct (@$cts) {
69 if ($ct eq 'images') {
70 $data = PVE
::Storage
::vdisk_list
($cfg, $storeid, $param->{vmid
});
71 } elsif ($ct eq 'iso') {
72 $data = PVE
::Storage
::template_list
($cfg, $storeid, 'iso')
74 } elsif ($ct eq 'vztmpl') {
75 $data = PVE
::Storage
::template_list
($cfg, $storeid, 'vztmpl')
77 } elsif ($ct eq 'backup') {
78 $data = PVE
::Storage
::template_list
($cfg, $storeid, 'backup')
82 next if !$data || !$data->{$storeid};
84 foreach my $item (@{$data->{$storeid}}) {
92 __PACKAGE__-
>register_method ({
96 description
=> "Allocate disk images.",
100 additionalProperties
=> 0,
102 node
=> get_standard_option
('pve-node'),
103 storage
=> get_standard_option
('pve-storage-id'),
105 description
=> "The name of the file to create/upload.",
108 vmid
=> get_standard_option
('pve-vmid', { description
=> "Specify owner VM" } ),
110 description
=> "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)",
112 pattern
=> '\d+[MG]?',
116 enum
=> ['raw', 'qcow2'],
123 description
=> "Volume identifier",
129 my $storeid = $param->{storage
};
130 my $name = $param->{filename
};
131 my $sizestr = $param->{size
};
134 if ($sizestr =~ m/^\d+$/) {
136 } elsif ($sizestr =~ m/^(\d+)M$/) {
138 } elsif ($sizestr =~ m/^(\d+)G$/) {
139 $size = $1 * 1024 * 1024;
141 raise_param_exc
({ size
=> "unable to parse size '$sizestr'" });
144 # extract FORMAT from name
145 if ($name =~ m/\.(raw|qcow2)$/) {
148 raise_param_exc
({ format
=> "different storage formats ($param->{format} != $fmt)" })
149 if $param->{format
} && $param->{format
} ne $fmt;
151 $param->{format
} = $fmt;
154 my $cfg = cfs_read_file
('storage.cfg');
156 my $volid = PVE
::Storage
::vdisk_alloc
($cfg, $storeid, $param->{vmid
},
163 # we allow to pass volume names (without storage prefix) if the storage
164 # is specified as separate parameter.
165 my $real_volume_id = sub {
166 my ($storeid, $volume) = @_;
170 if ($volume =~ m/:/) {
172 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volume);
173 raise_param_exc
({ storage
=> "storage ID missmatch" })
174 if $storeid && $sid ne $storeid;
177 raise_param_exc
({ volume
=> $@}) if $@;
180 raise_param_exc
({ volume
=> "no storage speficied - incomplete volume ID" })
183 $volid = "$storeid:$volume";
189 __PACKAGE__-
>register_method ({
193 description
=> "Get volume attributes",
197 additionalProperties
=> 0,
199 node
=> get_standard_option
('pve-node'),
200 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1 }),
202 description
=> "Volume identifier",
207 returns
=> { type
=> 'object' },
211 my $volid = &$real_volume_id($param->{storage
}, $param->{volume
});
213 my $cfg = cfs_read_file
('storage.cfg');
215 my $path = PVE
::Storage
::path
($cfg, $volid);
216 my ($size, $format, $used) = PVE
::Storage
::file_size_info
($path);
218 # fixme: return more attributes?
226 __PACKAGE__-
>register_method ({
230 description
=> "Delete volume",
234 additionalProperties
=> 0,
236 node
=> get_standard_option
('pve-node'),
237 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1}),
239 description
=> "Volume identifier",
244 returns
=> { type
=> 'null' },
248 my $volid = &$real_volume_id($param->{storage
}, $param->{volume
});
250 my $cfg = cfs_read_file
('storage.cfg');
252 PVE
::Storage
::vdisk_free
($cfg, $volid);
257 __PACKAGE__-
>register_method ({
261 description
=> "Copy a volume.",
265 additionalProperties
=> 0,
267 node
=> get_standard_option
('pve-node'),
268 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1}),
270 description
=> "Source volume identifier",
274 description
=> "Target volume identifier",
277 target_node
=> get_standard_option
('pve-node', {
278 description
=> "Target node. Default is local node.",
289 my $rpcenv = PVE
::RPCEnvironment
::get
();
291 my $user = $rpcenv->get_user();
293 my $target_node = $param->{target_node
} || PVE
::INotify
::nodename
();
295 # cd /nodes/localhost/storage/local/content
296 # pve:/> create local:103/vm-103-disk-1.raw -target local:103/vm-103-disk-2.raw
297 # pve:/> create 103/vm-103-disk-1.raw -target 103/vm-103-disk-3.raw
299 my $src_volid = &$real_volume_id($param->{storage
}, $param->{volume
});
300 my $dst_volid = &$real_volume_id($param->{storage
}, $param->{target
});
302 print "DEBUG: COPY $src_volid TO $dst_volid\n";
304 my $cfg = cfs_read_file
('storage.cfg');
306 # do all parameter checks first
308 # then do all short running task (to raise errors befor we go to background)
310 # then start the worker task
314 print "DEBUG: starting worker $upid\n";
316 my ($target_sid, $target_volname) = PVE
::Storage
::parse_volume_id
($dst_volid);
317 #my $target_ip = PVE::Cluster::remote_node_ip($target_node);
319 # you need to get this working (fails currently, because storage_migrate() uses
320 # ssh to connect to local host (which is not needed
321 PVE
::Storage
::storage_migrate
($cfg, $src_volid, $target_node, $target_sid, $target_volname);
323 print "DEBUG: end worker $upid\n";
327 return $rpcenv->fork_worker('imgcopy', undef, $user, $worker);