]> git.proxmox.com Git - pve-storage.git/blame - PVE/API2/Storage/Config.pm
rbd: create ceph keyring dir before copying
[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 {
8143f490 173 mkdir '/etc/pve/priv/ceph';
5a39d0a1
FG
174 PVE::Tools::file_copy($ceph_admin_keyring, $ceph_storage_keyring);
175 };
176 if (my $err = $@) {
177 unlink $ceph_storage_keyring;
178 die "failed to copy ceph authx keyring for storage '$storeid': $err\n";
179 }
b6cf0a66
DM
180 }
181
182 # try to activate if enabled on local node,
183 # we only do this to detect errors/problems sooner
184 if (PVE::Storage::storage_check_enabled($cfg, $storeid, undef, 1)) {
185 PVE::Storage::activate_storage($cfg, $storeid);
186 }
187
83d7192f 188 PVE::Storage::write_config($cfg);
b6cf0a66
DM
189
190 }, "create storage failed");
191
1dc01b9f 192 return undef;
b6cf0a66
DM
193 }});
194
195__PACKAGE__->register_method ({
196 name => 'update',
197 protected => 1,
198 path => '{storage}',
199 method => 'PUT',
200 description => "Update storage configuration.",
5f642f73
DM
201 permissions => {
202 check => ['perm', '/storage', ['Datastore.Allocate']],
203 },
1dc01b9f 204 parameters => PVE::Storage::Plugin->updateSchema(),
b6cf0a66
DM
205 returns => { type => 'null' },
206 code => sub {
207 my ($param) = @_;
208
1dc01b9f
DM
209 my $storeid = extract_param($param, 'storage');
210 my $digest = extract_param($param, 'digest');
b6cf0a66
DM
211
212 PVE::Storage::lock_storage_config(
213 sub {
214
83d7192f 215 my $cfg = PVE::Storage::config();
b6cf0a66 216
1dc01b9f 217 PVE::SectionConfig::assert_if_modified($cfg, $digest);
b6cf0a66 218
1dc01b9f 219 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
b6cf0a66 220
1dc01b9f
DM
221 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
222 my $opts = $plugin->check_config($storeid, $param, 0, 1);
b6cf0a66
DM
223
224 foreach my $k (%$opts) {
225 $scfg->{$k} = $opts->{$k};
226 }
227
83d7192f 228 PVE::Storage::write_config($cfg);
b6cf0a66
DM
229
230 }, "update storage failed");
231
232 return undef;
233 }});
234
235__PACKAGE__->register_method ({
236 name => 'delete',
237 protected => 1,
238 path => '{storage}', # /storage/config/{storage}
239 method => 'DELETE',
240 description => "Delete storage configuration.",
5f642f73
DM
241 permissions => {
242 check => ['perm', '/storage', ['Datastore.Allocate']],
243 },
b6cf0a66
DM
244 parameters => {
245 additionalProperties => 0,
246 properties => {
f3bd890d
DM
247 storage => get_standard_option('pve-storage-id', {
248 completion => \&PVE::Storage::complete_storage,
249 }),
b6cf0a66
DM
250 },
251 },
252 returns => { type => 'null' },
253 code => sub {
254 my ($param) = @_;
255
1dc01b9f
DM
256 my $storeid = extract_param($param, 'storage');
257
b6cf0a66
DM
258 PVE::Storage::lock_storage_config(
259 sub {
260
83d7192f 261 my $cfg = PVE::Storage::config();
b6cf0a66 262
5a39d0a1 263 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
402df80b 264
b6cf0a66 265 die "can't remove storage - storage is used as base of another storage\n"
1dc01b9f 266 if PVE::Storage::storage_is_used($cfg, $storeid);
b6cf0a66 267
5a39d0a1
FG
268 if ($scfg->{type} eq 'rbd' && !defined($scfg->{monhost})) {
269 my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
270 if (-f $ceph_storage_keyring) {
271 unlink($ceph_storage_keyring) or warn "removing keyring of storage failed: $!\n";
272 }
273 }
274
1dc01b9f 275 delete $cfg->{ids}->{$storeid};
b6cf0a66 276
83d7192f 277 PVE::Storage::write_config($cfg);
b6cf0a66
DM
278
279 }, "delete storage failed");
2a2cf20a
AG
280
281 PVE::AccessControl::remove_storage_access($storeid);
282
b6cf0a66
DM
283 return undef;
284 }});
285
2861;