]> git.proxmox.com Git - pve-storage.git/blame - PVE/API2/Storage/Config.pm
rbd: manage keyring for pveceph storages
[pve-storage.git] / PVE / API2 / Storage / Config.pm
CommitLineData
b6cf0a66
DM
1package PVE::API2::Storage::Config;
2
3use strict;
4use warnings;
5
6use PVE::SafeSyslog;
1dc01b9f 7use PVE::Tools qw(extract_param);
b6cf0a66
DM
8use PVE::Cluster qw(cfs_read_file cfs_write_file);
9use PVE::Storage;
1dc01b9f 10use PVE::Storage::Plugin;
b6cf0a66
DM
11use HTTP::Status qw(:constants);
12use Storable qw(dclone);
13use PVE::JSONSchema qw(get_standard_option);
5f642f73 14use PVE::RPCEnvironment;
b6cf0a66
DM
15
16use PVE::RESTHandler;
17
18use base qw(PVE::RESTHandler);
19
20my @ctypes = qw(images vztmpl iso backup);
21
1dc01b9f 22my $storage_type_enum = PVE::Storage::Plugin->lookup_types();
b6cf0a66
DM
23
24my $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});
5a39d0a1
FG
165 } elsif ($type eq 'rbd' && !defined($opts->{monhost})) {
166 my $ceph_admin_keyring = '/etc/pve/priv/ceph.client.admin.keyring';
167 my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
168
169 die "ceph authx keyring file for storage '$storeid' already exists!\n"
170 if -e $ceph_storage_keyring;
171
172 eval {
173 PVE::Tools::file_copy($ceph_admin_keyring, $ceph_storage_keyring);
174 };
175 if (my $err = $@) {
176 unlink $ceph_storage_keyring;
177 die "failed to copy ceph authx keyring for storage '$storeid': $err\n";
178 }
b6cf0a66
DM
179 }
180
181 # try to activate if enabled on local node,
182 # we only do this to detect errors/problems sooner
183 if (PVE::Storage::storage_check_enabled($cfg, $storeid, undef, 1)) {
184 PVE::Storage::activate_storage($cfg, $storeid);
185 }
186
83d7192f 187 PVE::Storage::write_config($cfg);
b6cf0a66
DM
188
189 }, "create storage failed");
190
1dc01b9f 191 return undef;
b6cf0a66
DM
192 }});
193
194__PACKAGE__->register_method ({
195 name => 'update',
196 protected => 1,
197 path => '{storage}',
198 method => 'PUT',
199 description => "Update storage configuration.",
5f642f73
DM
200 permissions => {
201 check => ['perm', '/storage', ['Datastore.Allocate']],
202 },
1dc01b9f 203 parameters => PVE::Storage::Plugin->updateSchema(),
b6cf0a66
DM
204 returns => { type => 'null' },
205 code => sub {
206 my ($param) = @_;
207
1dc01b9f
DM
208 my $storeid = extract_param($param, 'storage');
209 my $digest = extract_param($param, 'digest');
b6cf0a66
DM
210
211 PVE::Storage::lock_storage_config(
212 sub {
213
83d7192f 214 my $cfg = PVE::Storage::config();
b6cf0a66 215
1dc01b9f 216 PVE::SectionConfig::assert_if_modified($cfg, $digest);
b6cf0a66 217
1dc01b9f 218 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
b6cf0a66 219
1dc01b9f
DM
220 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
221 my $opts = $plugin->check_config($storeid, $param, 0, 1);
b6cf0a66
DM
222
223 foreach my $k (%$opts) {
224 $scfg->{$k} = $opts->{$k};
225 }
226
83d7192f 227 PVE::Storage::write_config($cfg);
b6cf0a66
DM
228
229 }, "update storage failed");
230
231 return undef;
232 }});
233
234__PACKAGE__->register_method ({
235 name => 'delete',
236 protected => 1,
237 path => '{storage}', # /storage/config/{storage}
238 method => 'DELETE',
239 description => "Delete storage configuration.",
5f642f73
DM
240 permissions => {
241 check => ['perm', '/storage', ['Datastore.Allocate']],
242 },
b6cf0a66
DM
243 parameters => {
244 additionalProperties => 0,
245 properties => {
f3bd890d
DM
246 storage => get_standard_option('pve-storage-id', {
247 completion => \&PVE::Storage::complete_storage,
248 }),
b6cf0a66
DM
249 },
250 },
251 returns => { type => 'null' },
252 code => sub {
253 my ($param) = @_;
254
1dc01b9f
DM
255 my $storeid = extract_param($param, 'storage');
256
b6cf0a66
DM
257 PVE::Storage::lock_storage_config(
258 sub {
259
83d7192f 260 my $cfg = PVE::Storage::config();
b6cf0a66 261
5a39d0a1 262 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
402df80b 263
b6cf0a66 264 die "can't remove storage - storage is used as base of another storage\n"
1dc01b9f 265 if PVE::Storage::storage_is_used($cfg, $storeid);
b6cf0a66 266
5a39d0a1
FG
267 if ($scfg->{type} eq 'rbd' && !defined($scfg->{monhost})) {
268 my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
269 if (-f $ceph_storage_keyring) {
270 unlink($ceph_storage_keyring) or warn "removing keyring of storage failed: $!\n";
271 }
272 }
273
1dc01b9f 274 delete $cfg->{ids}->{$storeid};
b6cf0a66 275
83d7192f 276 PVE::Storage::write_config($cfg);
b6cf0a66
DM
277
278 }, "delete storage failed");
2a2cf20a
AG
279
280 PVE::AccessControl::remove_storage_access($storeid);
281
b6cf0a66
DM
282 return undef;
283 }});
284
2851;