]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Config.pm
53c21077cc5ac6768d557cb8b65de6ceb7a62b65
[pve-storage.git] / PVE / API2 / Storage / Config.pm
1 package PVE::API2::Storage::Config;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::Tools qw(extract_param);
8 use PVE::Cluster qw(cfs_read_file cfs_write_file);
9 use PVE::Storage;
10 use PVE::Storage::Plugin;
11 use PVE::Storage::LVMPlugin;
12 use PVE::Storage::CIFSPlugin;
13 use HTTP::Status qw(:constants);
14 use Storable qw(dclone);
15 use PVE::JSONSchema qw(get_standard_option);
16 use PVE::RPCEnvironment;
17
18 use PVE::RESTHandler;
19
20 use base qw(PVE::RESTHandler);
21
22 my @ctypes = qw(images vztmpl iso backup);
23
24 my $storage_type_enum = PVE::Storage::Plugin->lookup_types();
25
26 my $api_storage_config = sub {
27 my ($cfg, $storeid) = @_;
28
29 my $scfg = dclone(PVE::Storage::storage_config($cfg, $storeid));
30 $scfg->{storage} = $storeid;
31 $scfg->{digest} = $cfg->{digest};
32 $scfg->{content} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'content', $scfg->{content});
33
34 if ($scfg->{nodes}) {
35 $scfg->{nodes} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'nodes', $scfg->{nodes});
36 }
37
38 return $scfg;
39 };
40
41 __PACKAGE__->register_method ({
42 name => 'index',
43 path => '',
44 method => 'GET',
45 description => "Storage index.",
46 permissions => {
47 description => "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'",
48 user => 'all',
49 },
50 parameters => {
51 additionalProperties => 0,
52 properties => {
53 type => {
54 description => "Only list storage of specific type",
55 type => 'string',
56 enum => $storage_type_enum,
57 optional => 1,
58 },
59 },
60 },
61 returns => {
62 type => 'array',
63 items => {
64 type => "object",
65 properties => { storage => { type => 'string'} },
66 },
67 links => [ { rel => 'child', href => "{storage}" } ],
68 },
69 code => sub {
70 my ($param) = @_;
71
72 my $rpcenv = PVE::RPCEnvironment::get();
73 my $authuser = $rpcenv->get_user();
74
75 my $cfg = PVE::Storage::config();
76
77 my @sids = PVE::Storage::storage_ids($cfg);
78
79 my $res = [];
80 foreach my $storeid (@sids) {
81 my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ];
82 next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1);
83
84 my $scfg = &$api_storage_config($cfg, $storeid);
85 next if $param->{type} && $param->{type} ne $scfg->{type};
86 push @$res, $scfg;
87 }
88
89 return $res;
90 }});
91
92 __PACKAGE__->register_method ({
93 name => 'read',
94 path => '{storage}',
95 method => 'GET',
96 description => "Read storage configuration.",
97 permissions => {
98 check => ['perm', '/storage/{storage}', ['Datastore.Allocate']],
99 },
100 parameters => {
101 additionalProperties => 0,
102 properties => {
103 storage => get_standard_option('pve-storage-id'),
104 },
105 },
106 returns => {},
107 code => sub {
108 my ($param) = @_;
109
110 my $cfg = PVE::Storage::config();
111
112 return &$api_storage_config($cfg, $param->{storage});
113 }});
114
115 __PACKAGE__->register_method ({
116 name => 'create',
117 protected => 1,
118 path => '',
119 method => 'POST',
120 description => "Create a new storage.",
121 permissions => {
122 check => ['perm', '/storage', ['Datastore.Allocate']],
123 },
124 parameters => PVE::Storage::Plugin->createSchema(),
125 returns => { type => 'null' },
126 code => sub {
127 my ($param) = @_;
128
129 my $type = extract_param($param, 'type');
130 my $storeid = extract_param($param, 'storage');
131
132 # revent an empty nodelist.
133 # fix me in section config create never need an empty entity.
134 delete $param->{nodes} if !$param->{nodes};
135
136 my $password = extract_param($param, 'password')
137 if $type eq 'cifs' && $param->{username};
138
139 if ($param->{portal}) {
140 $param->{portal} = PVE::Storage::resolv_portal($param->{portal});
141 }
142
143 my $plugin = PVE::Storage::Plugin->lookup($type);
144 my $opts = $plugin->check_config($storeid, $param, 1, 1);
145
146 PVE::Storage::lock_storage_config(
147 sub {
148
149 my $cfg = PVE::Storage::config();
150
151 if (my $scfg = PVE::Storage::storage_config($cfg, $storeid, 1)) {
152 die "storage ID '$storeid' already defined\n";
153 }
154
155 $cfg->{ids}->{$storeid} = $opts;
156
157 if ($type eq 'lvm' && $opts->{base}) {
158
159 my ($baseid, $volname) = PVE::Storage::parse_volume_id($opts->{base});
160
161 my $basecfg = PVE::Storage::storage_config ($cfg, $baseid, 1);
162 die "base storage ID '$baseid' does not exist\n" if !$basecfg;
163
164 # we only support iscsi for now
165 if (!($basecfg->{type} eq 'iscsi')) {
166 die "unsupported base type '$basecfg->{type}'";
167 }
168
169 my $path = PVE::Storage::path($cfg, $opts->{base});
170
171 PVE::Storage::activate_storage($cfg, $baseid);
172
173 PVE::Storage::LVMPlugin::lvm_create_volume_group($path, $opts->{vgname}, $opts->{shared});
174 } elsif ($type eq 'rbd' && !defined($opts->{monhost})) {
175 my $ceph_admin_keyring = '/etc/pve/priv/ceph.client.admin.keyring';
176 my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
177
178 die "ceph authx keyring file for storage '$storeid' already exists!\n"
179 if -e $ceph_storage_keyring;
180
181 eval {
182 mkdir '/etc/pve/priv/ceph';
183 PVE::Tools::file_copy($ceph_admin_keyring, $ceph_storage_keyring);
184 };
185 if (my $err = $@) {
186 unlink $ceph_storage_keyring;
187 die "failed to copy ceph authx keyring for storage '$storeid': $err\n";
188 }
189 }
190 # create a password file in /etc/pve/priv,
191 # this file is used as a cert_file at mount time.
192 my $cred_file = PVE::Storage::cifs_set_credentials($password, $storeid)
193 if $type eq 'cifs' && defined($password);
194
195 eval {
196 # try to activate if enabled on local node,
197 # we only do this to detect errors/problems sooner
198 if (PVE::Storage::storage_check_enabled($cfg, $storeid, undef, 1)) {
199 PVE::Storage::activate_storage($cfg, $storeid);
200 }
201 };
202 if(my $err = $@) {
203 unlink $cred_file if defined($cred_file);
204 die $err;
205 }
206
207 PVE::Storage::write_config($cfg);
208
209 }, "create storage failed");
210
211 return undef;
212 }});
213
214 __PACKAGE__->register_method ({
215 name => 'update',
216 protected => 1,
217 path => '{storage}',
218 method => 'PUT',
219 description => "Update storage configuration.",
220 permissions => {
221 check => ['perm', '/storage', ['Datastore.Allocate']],
222 },
223 parameters => PVE::Storage::Plugin->updateSchema(),
224 returns => { type => 'null' },
225 code => sub {
226 my ($param) = @_;
227
228 my $storeid = extract_param($param, 'storage');
229 my $digest = extract_param($param, 'digest');
230
231 PVE::Storage::lock_storage_config(
232 sub {
233
234 my $cfg = PVE::Storage::config();
235
236 PVE::SectionConfig::assert_if_modified($cfg, $digest);
237
238 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
239
240 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
241 my $opts = $plugin->check_config($storeid, $param, 0, 1);
242
243 foreach my $k (%$opts) {
244 $scfg->{$k} = $opts->{$k};
245 }
246
247 PVE::Storage::write_config($cfg);
248
249 }, "update storage failed");
250
251 return undef;
252 }});
253
254 __PACKAGE__->register_method ({
255 name => 'delete',
256 protected => 1,
257 path => '{storage}', # /storage/config/{storage}
258 method => 'DELETE',
259 description => "Delete storage configuration.",
260 permissions => {
261 check => ['perm', '/storage', ['Datastore.Allocate']],
262 },
263 parameters => {
264 additionalProperties => 0,
265 properties => {
266 storage => get_standard_option('pve-storage-id', {
267 completion => \&PVE::Storage::complete_storage,
268 }),
269 },
270 },
271 returns => { type => 'null' },
272 code => sub {
273 my ($param) = @_;
274
275 my $storeid = extract_param($param, 'storage');
276
277 PVE::Storage::lock_storage_config(
278 sub {
279
280 my $cfg = PVE::Storage::config();
281
282 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
283
284 die "can't remove storage - storage is used as base of another storage\n"
285 if PVE::Storage::storage_is_used($cfg, $storeid);
286
287 if ($scfg->{type} eq 'cifs') {
288 my $cred_file = PVE::Storage::cifs_cred_file_name($storeid);
289 if (-f $cred_file) {
290 unlink($cred_file) or warn "removing cifs credientials '$cred_file' failed: $!\n";
291 }
292 } elsif ($scfg->{type} eq 'rbd' && !defined($scfg->{monhost})) {
293 my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
294 if (-f $ceph_storage_keyring) {
295 unlink($ceph_storage_keyring) or warn "removing keyring of storage failed: $!\n";
296 }
297 }
298
299 delete $cfg->{ids}->{$storeid};
300
301 PVE::Storage::write_config($cfg);
302
303 }, "delete storage failed");
304
305 PVE::AccessControl::remove_storage_access($storeid);
306
307 return undef;
308 }});
309
310 1;