]>
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' || defined($param->{vmid
})) {
70 $data = PVE
::Storage
::vdisk_list
($cfg, $storeid, $param->{vmid
});
71 } elsif ($ct eq 'iso') {
72 $data = PVE
::Storage
::template_list
($cfg, $storeid, 'iso');
73 } elsif ($ct eq 'vztmpl') {
74 $data = PVE
::Storage
::template_list
($cfg, $storeid, 'vztmpl');
75 } elsif ($ct eq 'backup') {
76 $data = PVE
::Storage
::template_list
($cfg, $storeid, 'backup');
79 next if !$data || !$data->{$storeid};
81 foreach my $item (@{$data->{$storeid}}) {
89 __PACKAGE__-
>register_method ({
93 description
=> "Allocate disk images.",
97 additionalProperties
=> 0,
99 node
=> get_standard_option
('pve-node'),
100 storage
=> get_standard_option
('pve-storage-id'),
102 description
=> "The name of the file to create/upload.",
105 vmid
=> get_standard_option
('pve-vmid', { description
=> "Specify owner VM" } ),
107 description
=> "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)",
109 pattern
=> '\d+[MG]?',
113 enum
=> ['raw', 'qcow2'],
120 description
=> "Volume identifier",
126 my $storeid = $param->{storage
};
127 my $name = $param->{filename
};
128 my $sizestr = $param->{size
};
131 if ($sizestr =~ m/^\d+$/) {
133 } elsif ($sizestr =~ m/^(\d+)M$/) {
135 } elsif ($sizestr =~ m/^(\d+)G$/) {
136 $size = $1 * 1024 * 1024;
138 raise_param_exc
({ size
=> "unable to parse size '$sizestr'" });
141 # extract FORMAT from name
142 if ($name =~ m/\.(raw|qcow2)$/) {
145 raise_param_exc
({ format
=> "different storage formats ($param->{format} != $fmt)" })
146 if $param->{format
} && $param->{format
} ne $fmt;
148 $param->{format
} = $fmt;
151 my $cfg = cfs_read_file
('storage.cfg');
153 my $volid = PVE
::Storage
::vdisk_alloc
($cfg, $storeid, $param->{vmid
},
160 # we allow to pass volume names (without storage prefix) if the storage
161 # is specified as separate parameter.
162 my $real_volume_id = sub {
163 my ($storeid, $volume) = @_;
167 if ($volume =~ m/:/) {
169 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volume);
170 raise_param_exc
({ storage
=> "storage ID missmatch" })
171 if $storeid && $sid ne $storeid;
174 raise_param_exc
({ volume
=> $@}) if $@;
177 raise_param_exc
({ volume
=> "no storage speficied - incomplete volume ID" })
180 $volid = "$storeid:$volume";
186 __PACKAGE__-
>register_method ({
190 description
=> "Get volume attributes",
194 additionalProperties
=> 0,
196 node
=> get_standard_option
('pve-node'),
197 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1 }),
199 description
=> "Volume identifier",
204 returns
=> { type
=> 'object' },
208 my $volid = &$real_volume_id($param->{storage
}, $param->{volume
});
210 my $cfg = cfs_read_file
('storage.cfg');
212 my $path = PVE
::Storage
::path
($cfg, $volid);
213 my ($size, $format, $used) = PVE
::Storage
::file_size_info
($path);
215 # fixme: return more attributes?
223 __PACKAGE__-
>register_method ({
227 description
=> "Delete volume",
231 additionalProperties
=> 0,
233 node
=> get_standard_option
('pve-node'),
234 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1}),
236 description
=> "Volume identifier",
241 returns
=> { type
=> 'null' },
245 my $volid = &$real_volume_id($param->{storage
}, $param->{volume
});
247 my $cfg = cfs_read_file
('storage.cfg');
249 PVE
::Storage
::vdisk_free
($cfg, $volid);
254 __PACKAGE__-
>register_method ({
258 description
=> "Copy a volume.",
262 additionalProperties
=> 0,
264 node
=> get_standard_option
('pve-node'),
265 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1}),
267 description
=> "Source volume identifier",
271 description
=> "Target volume identifier",
274 target_node
=> get_standard_option
('pve-node', {
275 description
=> "Target node. Default is local node.",
286 my $rpcenv = PVE
::RPCEnvironment
::get
();
288 my $user = $rpcenv->get_user();
290 my $target_node = $param->{target_node
} || PVE
::INotify
::nodename
();
292 # cd /nodes/localhost/storage/local/content
293 # pve:/> create local:103/vm-103-disk-1.raw -target local:103/vm-103-disk-2.raw
294 # pve:/> create 103/vm-103-disk-1.raw -target 103/vm-103-disk-3.raw
296 my $src_volid = &$real_volume_id($param->{storage
}, $param->{volume
});
297 my $dst_volid = &$real_volume_id($param->{storage
}, $param->{target
});
299 print "DEBUG: COPY $src_volid TO $dst_volid\n";
301 my $cfg = cfs_read_file
('storage.cfg');
303 # do all parameter checks first
305 # then do all short running task (to raise errors befor we go to background)
307 # then start the worker task
311 print "DEBUG: starting worker $upid\n";
313 my ($target_sid, $target_volname) = PVE
::Storage
::parse_volume_id
($dst_volid);
314 #my $target_ip = PVE::Cluster::remote_node_ip($target_node);
316 # you need to get this working (fails currently, because storage_migrate() uses
317 # ssh to connect to local host (which is not needed
318 PVE
::Storage
::storage_migrate
($cfg, $src_volid, $target_node, $target_sid, $target_volname);
320 print "DEBUG: end worker $upid\n";
324 return $rpcenv->fork_worker('imgcopy', undef, $user, $worker);