]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Config.pm
Add write_config, drop cfs_read_file
[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 HTTP::Status qw(:constants);
12 use Storable qw(dclone);
13 use PVE::JSONSchema qw(get_standard_option);
14 use PVE::RPCEnvironment;
15
16 use PVE::RESTHandler;
17
18 use base qw(PVE::RESTHandler);
19
20 my @ctypes = qw(images vztmpl iso backup);
21
22 my $storage_type_enum = PVE::Storage::Plugin->lookup_types();
23
24 my $api_storage_config = sub {
25 my ($cfg, $storeid) = @_;
26
27 my $scfg = dclone(PVE::Storage::storage_config($cfg, $storeid));
28 $scfg->{storage} = $storeid;
29 $scfg->{digest} = $cfg->{digest};
30 $scfg->{content} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'content', $scfg->{content});
31
32 if ($scfg->{nodes}) {
33 $scfg->{nodes} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'nodes', $scfg->{nodes});
34 }
35
36 return $scfg;
37 };
38
39 __PACKAGE__->register_method ({
40 name => 'index',
41 path => '',
42 method => 'GET',
43 description => "Storage index.",
44 permissions => {
45 description => "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'",
46 user => 'all',
47 },
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 },
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
70 my $rpcenv = PVE::RPCEnvironment::get();
71 my $authuser = $rpcenv->get_user();
72
73 my $cfg = PVE::Storage::config();
74
75 my @sids = PVE::Storage::storage_ids($cfg);
76
77 my $res = [];
78 foreach my $storeid (@sids) {
79 my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ];
80 next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1);
81
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.",
95 permissions => {
96 check => ['perm', '/storage/{storage}', ['Datastore.Allocate']],
97 },
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
108 my $cfg = PVE::Storage::config();
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.",
119 permissions => {
120 check => ['perm', '/storage', ['Datastore.Allocate']],
121 },
122 parameters => PVE::Storage::Plugin->createSchema(),
123 returns => { type => 'null' },
124 code => sub {
125 my ($param) = @_;
126
127 my $type = extract_param($param, 'type');
128 my $storeid = extract_param($param, 'storage');
129
130 if ($param->{portal}) {
131 $param->{portal} = PVE::Storage::resolv_portal($param->{portal});
132 }
133
134 my $plugin = PVE::Storage::Plugin->lookup($type);
135 my $opts = $plugin->check_config($storeid, $param, 1, 1);
136
137 PVE::Storage::lock_storage_config(
138 sub {
139
140 my $cfg = PVE::Storage::config();
141
142 if (my $scfg = PVE::Storage::storage_config($cfg, $storeid, 1)) {
143 die "storage ID '$storeid' already defined\n";
144 }
145
146 $cfg->{ids}->{$storeid} = $opts;
147
148 if ($type eq 'lvm' && $opts->{base}) {
149
150 my ($baseid, $volname) = PVE::Storage::parse_volume_id($opts->{base});
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
160 my $path = PVE::Storage::path($cfg, $opts->{base});
161
162 PVE::Storage::activate_storage($cfg, $baseid);
163
164 PVE::Storage::LVMPlugin::lvm_create_volume_group($path, $opts->{vgname}, $opts->{shared});
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
173 PVE::Storage::write_config($cfg);
174
175 }, "create storage failed");
176
177 return undef;
178 }});
179
180 __PACKAGE__->register_method ({
181 name => 'update',
182 protected => 1,
183 path => '{storage}',
184 method => 'PUT',
185 description => "Update storage configuration.",
186 permissions => {
187 check => ['perm', '/storage', ['Datastore.Allocate']],
188 },
189 parameters => PVE::Storage::Plugin->updateSchema(),
190 returns => { type => 'null' },
191 code => sub {
192 my ($param) = @_;
193
194 my $storeid = extract_param($param, 'storage');
195 my $digest = extract_param($param, 'digest');
196
197 PVE::Storage::lock_storage_config(
198 sub {
199
200 my $cfg = PVE::Storage::config();
201
202 PVE::SectionConfig::assert_if_modified($cfg, $digest);
203
204 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
205
206 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
207 my $opts = $plugin->check_config($storeid, $param, 0, 1);
208
209 foreach my $k (%$opts) {
210 $scfg->{$k} = $opts->{$k};
211 }
212
213 PVE::Storage::write_config($cfg);
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.",
226 permissions => {
227 check => ['perm', '/storage', ['Datastore.Allocate']],
228 },
229 parameters => {
230 additionalProperties => 0,
231 properties => {
232 storage => get_standard_option('pve-storage-id', {
233 completion => \&PVE::Storage::complete_storage,
234 }),
235 },
236 },
237 returns => { type => 'null' },
238 code => sub {
239 my ($param) = @_;
240
241 my $storeid = extract_param($param, 'storage');
242
243 PVE::Storage::lock_storage_config(
244 sub {
245
246 my $cfg = PVE::Storage::config();
247
248 die "storage '$storeid' does not exist\n"
249 if !($cfg->{ids}->{$storeid});
250
251 die "can't remove storage - storage is used as base of another storage\n"
252 if PVE::Storage::storage_is_used($cfg, $storeid);
253
254 delete $cfg->{ids}->{$storeid};
255
256 PVE::Storage::write_config($cfg);
257
258 }, "delete storage failed");
259
260 PVE::AccessControl::remove_storage_access($storeid);
261
262 return undef;
263 }});
264
265 1;