]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Content.pm
9f34819ac473820eba4aa582bcb2fa88708fdd9e
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);