]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Config.pm
1 package PVE
::API2
::Storage
::Config
;
7 use PVE
::Tools
qw(extract_param);
8 use PVE
::Cluster
qw(cfs_read_file cfs_write_file);
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
;
18 use base
qw(PVE::RESTHandler);
20 my @ctypes = qw(images vztmpl iso backup);
22 my $storage_type_enum = PVE
::Storage
::Plugin-
>lookup_types();
24 my $api_storage_config = sub {
25 my ($cfg, $storeid) = @_;
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
});
33 $scfg->{nodes
} = PVE
::Storage
::Plugin-
>encode_value($scfg->{type
}, 'nodes', $scfg->{nodes
});
39 my $cifs_cred_file_name = sub {
42 return "/etc/pve/priv/${storeid}.cred";
45 my $set_cifs_credentials = sub {
46 my ($password, $storeid) = @_;
48 my $cred_file = $cifs_cred_file_name->($storeid);
50 PVE
::Tools
::file_set_contents
($cred_file, "password=$password\n");
55 __PACKAGE__-
>register_method ({
59 description
=> "Storage index.",
61 description
=> "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'",
65 additionalProperties
=> 0,
68 description
=> "Only list storage of specific type",
70 enum
=> $storage_type_enum,
79 properties
=> { storage
=> { type
=> 'string'} },
81 links
=> [ { rel
=> 'child', href
=> "{storage}" } ],
86 my $rpcenv = PVE
::RPCEnvironment
::get
();
87 my $authuser = $rpcenv->get_user();
89 my $cfg = PVE
::Storage
::config
();
91 my @sids = PVE
::Storage
::storage_ids
($cfg);
94 foreach my $storeid (@sids) {
95 my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ];
96 next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1);
98 my $scfg = &$api_storage_config($cfg, $storeid);
99 next if $param->{type
} && $param->{type
} ne $scfg->{type
};
106 __PACKAGE__-
>register_method ({
110 description
=> "Read storage configuration.",
112 check
=> ['perm', '/storage/{storage}', ['Datastore.Allocate']],
115 additionalProperties
=> 0,
117 storage
=> get_standard_option
('pve-storage-id'),
124 my $cfg = PVE
::Storage
::config
();
126 return &$api_storage_config($cfg, $param->{storage
});
129 __PACKAGE__-
>register_method ({
134 description
=> "Create a new storage.",
136 check
=> ['perm', '/storage', ['Datastore.Allocate']],
138 parameters
=> PVE
::Storage
::Plugin-
>createSchema(),
139 returns
=> { type
=> 'null' },
143 my $type = extract_param
($param, 'type');
144 my $storeid = extract_param
($param, 'storage');
146 # revent an empty nodelist.
147 # fix me in section config create never need an empty entity.
148 delete $param->{nodes
} if !$param->{nodes
};
150 my $password = extract_param
($param, 'password')
151 if $type eq 'cifs' && $param->{username
};
153 if ($param->{portal
}) {
154 $param->{portal
} = PVE
::Storage
::resolv_portal
($param->{portal
});
157 my $plugin = PVE
::Storage
::Plugin-
>lookup($type);
158 my $opts = $plugin->check_config($storeid, $param, 1, 1);
160 PVE
::Storage
::lock_storage_config
(
163 my $cfg = PVE
::Storage
::config
();
165 if (my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid, 1)) {
166 die "storage ID '$storeid' already defined\n";
169 $cfg->{ids
}->{$storeid} = $opts;
171 if ($type eq 'lvm' && $opts->{base
}) {
173 my ($baseid, $volname) = PVE
::Storage
::parse_volume_id
($opts->{base
});
175 my $basecfg = PVE
::Storage
::storage_config
($cfg, $baseid, 1);
176 die "base storage ID '$baseid' does not exist\n" if !$basecfg;
178 # we only support iscsi for now
179 if (!($basecfg->{type
} eq 'iscsi')) {
180 die "unsupported base type '$basecfg->{type}'";
183 my $path = PVE
::Storage
::path
($cfg, $opts->{base
});
185 PVE
::Storage
::activate_storage
($cfg, $baseid);
187 PVE
::Storage
::LVMPlugin
::lvm_create_volume_group
($path, $opts->{vgname
}, $opts->{shared
});
188 } elsif ($type eq 'rbd' && !defined($opts->{monhost
})) {
189 my $ceph_admin_keyring = '/etc/pve/priv/ceph.client.admin.keyring';
190 my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
192 die "ceph authx keyring file for storage '$storeid' already exists!\n"
193 if -e
$ceph_storage_keyring;
196 mkdir '/etc/pve/priv/ceph';
197 PVE
::Tools
::file_copy
($ceph_admin_keyring, $ceph_storage_keyring);
200 unlink $ceph_storage_keyring;
201 die "failed to copy ceph authx keyring for storage '$storeid': $err\n";
204 # create a password file in /etc/pve/priv,
205 # this file is used as a cert_file at mount time.
206 my $cred_file = &$set_cifs_credentials($password, $storeid)
207 if defined($password);
210 # try to activate if enabled on local node,
211 # we only do this to detect errors/problems sooner
212 if (PVE
::Storage
::storage_check_enabled
($cfg, $storeid, undef, 1)) {
213 PVE
::Storage
::activate_storage
($cfg, $storeid);
217 unlink $cred_file if defined($cred_file);
221 PVE
::Storage
::write_config
($cfg);
223 }, "create storage failed");
228 __PACKAGE__-
>register_method ({
233 description
=> "Update storage configuration.",
235 check
=> ['perm', '/storage', ['Datastore.Allocate']],
237 parameters
=> PVE
::Storage
::Plugin-
>updateSchema(),
238 returns
=> { type
=> 'null' },
242 my $storeid = extract_param
($param, 'storage');
243 my $digest = extract_param
($param, 'digest');
245 PVE
::Storage
::lock_storage_config
(
248 my $cfg = PVE
::Storage
::config
();
250 PVE
::SectionConfig
::assert_if_modified
($cfg, $digest);
252 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
254 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
255 my $opts = $plugin->check_config($storeid, $param, 0, 1);
257 foreach my $k (%$opts) {
258 $scfg->{$k} = $opts->{$k};
261 PVE
::Storage
::write_config
($cfg);
263 }, "update storage failed");
268 __PACKAGE__-
>register_method ({
271 path
=> '{storage}', # /storage/config/{storage}
273 description
=> "Delete storage configuration.",
275 check
=> ['perm', '/storage', ['Datastore.Allocate']],
278 additionalProperties
=> 0,
280 storage
=> get_standard_option
('pve-storage-id', {
281 completion
=> \
&PVE
::Storage
::complete_storage
,
285 returns
=> { type
=> 'null' },
289 my $storeid = extract_param
($param, 'storage');
291 PVE
::Storage
::lock_storage_config
(
294 my $cfg = PVE
::Storage
::config
();
296 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
298 die "can't remove storage - storage is used as base of another storage\n"
299 if PVE
::Storage
::storage_is_used
($cfg, $storeid);
301 if ($scfg->{type
} eq 'cifs') {
302 my $cred_file = $cifs_cred_file_name->($storeid);
304 unlink($cred_file) or warn "removing cifs credientials '$cred_file' failed: $!\n";
306 } elsif ($scfg->{type
} eq 'rbd' && !defined($scfg->{monhost
})) {
307 my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
308 if (-f
$ceph_storage_keyring) {
309 unlink($ceph_storage_keyring) or warn "removing keyring of storage failed: $!\n";
313 delete $cfg->{ids
}->{$storeid};
315 PVE
::Storage
::write_config
($cfg);
317 }, "delete storage failed");
319 PVE
::AccessControl
::remove_storage_access
($storeid);