]>
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; |
b6cf0a66 DM |
15 | |
16 | use PVE::RESTHandler; | |
17 | ||
18 | use base qw(PVE::RESTHandler); | |
19 | ||
20 | my @ctypes = qw(images vztmpl iso backup); | |
21 | ||
1dc01b9f | 22 | my $storage_type_enum = PVE::Storage::Plugin->lookup_types(); |
b6cf0a66 DM |
23 | |
24 | my $api_storage_config = sub { | |
25 | my ($cfg, $storeid) = @_; | |
26 | ||
1dc01b9f | 27 | my $scfg = dclone(PVE::Storage::storage_config($cfg, $storeid)); |
b6cf0a66 | 28 | $scfg->{storage} = $storeid; |
b6cf0a66 | 29 | $scfg->{digest} = $cfg->{digest}; |
1dc01b9f | 30 | $scfg->{content} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'content', $scfg->{content}); |
b6cf0a66 DM |
31 | |
32 | if ($scfg->{nodes}) { | |
1dc01b9f | 33 | $scfg->{nodes} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'nodes', $scfg->{nodes}); |
b6cf0a66 DM |
34 | } |
35 | ||
36 | return $scfg; | |
37 | }; | |
38 | ||
39 | __PACKAGE__->register_method ({ | |
40 | name => 'index', | |
41 | path => '', | |
42 | method => 'GET', | |
43 | description => "Storage index.", | |
5f642f73 DM |
44 | permissions => { |
45 | description => "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'", | |
46 | user => 'all', | |
47 | }, | |
b6cf0a66 DM |
48 | parameters => { |
49 | additionalProperties => 0, | |
50 | properties => { | |
51 | type => { | |
52 | description => "Only list storage of specific type", | |
53 | type => 'string', | |
54 | enum => $storage_type_enum, | |
55 | optional => 1, | |
56 | }, | |
b6cf0a66 DM |
57 | }, |
58 | }, | |
59 | returns => { | |
60 | type => 'array', | |
61 | items => { | |
62 | type => "object", | |
63 | properties => { storage => { type => 'string'} }, | |
64 | }, | |
65 | links => [ { rel => 'child', href => "{storage}" } ], | |
66 | }, | |
67 | code => sub { | |
68 | my ($param) = @_; | |
69 | ||
5f642f73 DM |
70 | my $rpcenv = PVE::RPCEnvironment::get(); |
71 | my $authuser = $rpcenv->get_user(); | |
72 | ||
83d7192f | 73 | my $cfg = PVE::Storage::config(); |
b6cf0a66 | 74 | |
5f642f73 | 75 | my @sids = PVE::Storage::storage_ids($cfg); |
b6cf0a66 DM |
76 | |
77 | my $res = []; | |
78 | foreach my $storeid (@sids) { | |
5f642f73 DM |
79 | my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ]; |
80 | next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1); | |
81 | ||
b6cf0a66 DM |
82 | my $scfg = &$api_storage_config($cfg, $storeid); |
83 | next if $param->{type} && $param->{type} ne $scfg->{type}; | |
84 | push @$res, $scfg; | |
85 | } | |
86 | ||
87 | return $res; | |
88 | }}); | |
89 | ||
90 | __PACKAGE__->register_method ({ | |
91 | name => 'read', | |
92 | path => '{storage}', | |
93 | method => 'GET', | |
94 | description => "Read storage configuration.", | |
5f642f73 DM |
95 | permissions => { |
96 | check => ['perm', '/storage/{storage}', ['Datastore.Allocate']], | |
97 | }, | |
b6cf0a66 DM |
98 | parameters => { |
99 | additionalProperties => 0, | |
100 | properties => { | |
101 | storage => get_standard_option('pve-storage-id'), | |
102 | }, | |
103 | }, | |
104 | returns => {}, | |
105 | code => sub { | |
106 | my ($param) = @_; | |
107 | ||
83d7192f | 108 | my $cfg = PVE::Storage::config(); |
b6cf0a66 DM |
109 | |
110 | return &$api_storage_config($cfg, $param->{storage}); | |
111 | }}); | |
112 | ||
113 | __PACKAGE__->register_method ({ | |
114 | name => 'create', | |
115 | protected => 1, | |
116 | path => '', | |
117 | method => 'POST', | |
118 | description => "Create a new storage.", | |
5f642f73 DM |
119 | permissions => { |
120 | check => ['perm', '/storage', ['Datastore.Allocate']], | |
121 | }, | |
1dc01b9f | 122 | parameters => PVE::Storage::Plugin->createSchema(), |
b6cf0a66 DM |
123 | returns => { type => 'null' }, |
124 | code => sub { | |
125 | my ($param) = @_; | |
126 | ||
1dc01b9f DM |
127 | my $type = extract_param($param, 'type'); |
128 | my $storeid = extract_param($param, 'storage'); | |
b6cf0a66 DM |
129 | |
130 | if ($param->{portal}) { | |
131 | $param->{portal} = PVE::Storage::resolv_portal($param->{portal}); | |
132 | } | |
133 | ||
1dc01b9f DM |
134 | my $plugin = PVE::Storage::Plugin->lookup($type); |
135 | my $opts = $plugin->check_config($storeid, $param, 1, 1); | |
b6cf0a66 DM |
136 | |
137 | PVE::Storage::lock_storage_config( | |
138 | sub { | |
139 | ||
83d7192f | 140 | my $cfg = PVE::Storage::config(); |
b6cf0a66 | 141 | |
1dc01b9f | 142 | if (my $scfg = PVE::Storage::storage_config($cfg, $storeid, 1)) { |
b6cf0a66 DM |
143 | die "storage ID '$storeid' already defined\n"; |
144 | } | |
145 | ||
146 | $cfg->{ids}->{$storeid} = $opts; | |
147 | ||
148 | if ($type eq 'lvm' && $opts->{base}) { | |
149 | ||
1dc01b9f | 150 | my ($baseid, $volname) = PVE::Storage::parse_volume_id($opts->{base}); |
b6cf0a66 DM |
151 | |
152 | my $basecfg = PVE::Storage::storage_config ($cfg, $baseid, 1); | |
153 | die "base storage ID '$baseid' does not exist\n" if !$basecfg; | |
154 | ||
155 | # we only support iscsi for now | |
156 | if (!($basecfg->{type} eq 'iscsi')) { | |
157 | die "unsupported base type '$basecfg->{type}'"; | |
158 | } | |
159 | ||
1dc01b9f | 160 | my $path = PVE::Storage::path($cfg, $opts->{base}); |
b6cf0a66 DM |
161 | |
162 | PVE::Storage::activate_storage($cfg, $baseid); | |
163 | ||
1dc01b9f | 164 | PVE::Storage::LVMPlugin::lvm_create_volume_group($path, $opts->{vgname}, $opts->{shared}); |
b6cf0a66 DM |
165 | } |
166 | ||
167 | # try to activate if enabled on local node, | |
168 | # we only do this to detect errors/problems sooner | |
169 | if (PVE::Storage::storage_check_enabled($cfg, $storeid, undef, 1)) { | |
170 | PVE::Storage::activate_storage($cfg, $storeid); | |
171 | } | |
172 | ||
83d7192f | 173 | PVE::Storage::write_config($cfg); |
b6cf0a66 DM |
174 | |
175 | }, "create storage failed"); | |
176 | ||
1dc01b9f | 177 | return undef; |
b6cf0a66 DM |
178 | }}); |
179 | ||
180 | __PACKAGE__->register_method ({ | |
181 | name => 'update', | |
182 | protected => 1, | |
183 | path => '{storage}', | |
184 | method => 'PUT', | |
185 | description => "Update storage configuration.", | |
5f642f73 DM |
186 | permissions => { |
187 | check => ['perm', '/storage', ['Datastore.Allocate']], | |
188 | }, | |
1dc01b9f | 189 | parameters => PVE::Storage::Plugin->updateSchema(), |
b6cf0a66 DM |
190 | returns => { type => 'null' }, |
191 | code => sub { | |
192 | my ($param) = @_; | |
193 | ||
1dc01b9f DM |
194 | my $storeid = extract_param($param, 'storage'); |
195 | my $digest = extract_param($param, 'digest'); | |
b6cf0a66 DM |
196 | |
197 | PVE::Storage::lock_storage_config( | |
198 | sub { | |
199 | ||
83d7192f | 200 | my $cfg = PVE::Storage::config(); |
b6cf0a66 | 201 | |
1dc01b9f | 202 | PVE::SectionConfig::assert_if_modified($cfg, $digest); |
b6cf0a66 | 203 | |
1dc01b9f | 204 | my $scfg = PVE::Storage::storage_config($cfg, $storeid); |
b6cf0a66 | 205 | |
1dc01b9f DM |
206 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
207 | my $opts = $plugin->check_config($storeid, $param, 0, 1); | |
b6cf0a66 DM |
208 | |
209 | foreach my $k (%$opts) { | |
210 | $scfg->{$k} = $opts->{$k}; | |
211 | } | |
212 | ||
83d7192f | 213 | PVE::Storage::write_config($cfg); |
b6cf0a66 DM |
214 | |
215 | }, "update storage failed"); | |
216 | ||
217 | return undef; | |
218 | }}); | |
219 | ||
220 | __PACKAGE__->register_method ({ | |
221 | name => 'delete', | |
222 | protected => 1, | |
223 | path => '{storage}', # /storage/config/{storage} | |
224 | method => 'DELETE', | |
225 | description => "Delete storage configuration.", | |
5f642f73 DM |
226 | permissions => { |
227 | check => ['perm', '/storage', ['Datastore.Allocate']], | |
228 | }, | |
b6cf0a66 DM |
229 | parameters => { |
230 | additionalProperties => 0, | |
231 | properties => { | |
f3bd890d DM |
232 | storage => get_standard_option('pve-storage-id', { |
233 | completion => \&PVE::Storage::complete_storage, | |
234 | }), | |
b6cf0a66 DM |
235 | }, |
236 | }, | |
237 | returns => { type => 'null' }, | |
238 | code => sub { | |
239 | my ($param) = @_; | |
240 | ||
1dc01b9f DM |
241 | my $storeid = extract_param($param, 'storage'); |
242 | ||
b6cf0a66 DM |
243 | PVE::Storage::lock_storage_config( |
244 | sub { | |
245 | ||
83d7192f | 246 | my $cfg = PVE::Storage::config(); |
b6cf0a66 | 247 | |
402df80b AG |
248 | die "storage '$storeid' does not exist\n" |
249 | if !($cfg->{ids}->{$storeid}); | |
250 | ||
b6cf0a66 | 251 | die "can't remove storage - storage is used as base of another storage\n" |
1dc01b9f | 252 | if PVE::Storage::storage_is_used($cfg, $storeid); |
b6cf0a66 | 253 | |
1dc01b9f | 254 | delete $cfg->{ids}->{$storeid}; |
b6cf0a66 | 255 | |
83d7192f | 256 | PVE::Storage::write_config($cfg); |
b6cf0a66 DM |
257 | |
258 | }, "delete storage failed"); | |
2a2cf20a AG |
259 | |
260 | PVE::AccessControl::remove_storage_access($storeid); | |
261 | ||
b6cf0a66 DM |
262 | return undef; |
263 | }}); | |
264 | ||
265 | 1; |