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