]>
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 PVE
::Storage
::LVMPlugin
;
12 use PVE
::Storage
::CIFSPlugin
;
13 use HTTP
::Status
qw(:constants);
14 use Storable
qw(dclone);
15 use PVE
::JSONSchema
qw(get_standard_option);
16 use PVE
::RPCEnvironment
;
20 use base
qw(PVE::RESTHandler);
22 my @ctypes = qw(images vztmpl iso backup);
24 my $storage_type_enum = PVE
::Storage
::Plugin-
>lookup_types();
26 my $api_storage_config = sub {
27 my ($cfg, $storeid) = @_;
29 my $scfg = dclone
(PVE
::Storage
::storage_config
($cfg, $storeid));
30 $scfg->{storage
} = $storeid;
31 $scfg->{digest
} = $cfg->{digest
};
32 $scfg->{content
} = PVE
::Storage
::Plugin-
>encode_value($scfg->{type
}, 'content', $scfg->{content
});
35 $scfg->{nodes
} = PVE
::Storage
::Plugin-
>encode_value($scfg->{type
}, 'nodes', $scfg->{nodes
});
41 __PACKAGE__-
>register_method ({
45 description
=> "Storage index.",
47 description
=> "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'",
51 additionalProperties
=> 0,
54 description
=> "Only list storage of specific type",
56 enum
=> $storage_type_enum,
65 properties
=> { storage
=> { type
=> 'string'} },
67 links
=> [ { rel
=> 'child', href
=> "{storage}" } ],
72 my $rpcenv = PVE
::RPCEnvironment
::get
();
73 my $authuser = $rpcenv->get_user();
75 my $cfg = PVE
::Storage
::config
();
77 my @sids = PVE
::Storage
::storage_ids
($cfg);
80 foreach my $storeid (@sids) {
81 my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ];
82 next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1);
84 my $scfg = &$api_storage_config($cfg, $storeid);
85 next if $param->{type
} && $param->{type
} ne $scfg->{type
};
92 __PACKAGE__-
>register_method ({
96 description
=> "Read storage configuration.",
98 check
=> ['perm', '/storage/{storage}', ['Datastore.Allocate']],
101 additionalProperties
=> 0,
103 storage
=> get_standard_option
('pve-storage-id'),
106 returns
=> { type
=> 'object' },
110 my $cfg = PVE
::Storage
::config
();
112 return &$api_storage_config($cfg, $param->{storage
});
115 __PACKAGE__-
>register_method ({
120 description
=> "Create a new storage.",
122 check
=> ['perm', '/storage', ['Datastore.Allocate']],
124 parameters
=> PVE
::Storage
::Plugin-
>createSchema(),
125 returns
=> { type
=> 'null' },
129 my $type = extract_param
($param, 'type');
130 my $storeid = extract_param
($param, 'storage');
132 # revent an empty nodelist.
133 # fix me in section config create never need an empty entity.
134 delete $param->{nodes
} if !$param->{nodes
};
137 # always extract pw, else it gets written to the www-data readable scfg
138 if (my $tmp_pw = extract_param
($param, 'password')) {
139 if ($type eq 'cifs' && $param->{username
}) {
142 warn "ignore password parameter\n";
146 if ($param->{portal
}) {
147 $param->{portal
} = PVE
::Storage
::resolv_portal
($param->{portal
});
150 my $plugin = PVE
::Storage
::Plugin-
>lookup($type);
151 my $opts = $plugin->check_config($storeid, $param, 1, 1);
153 PVE
::Storage
::lock_storage_config
(
156 my $cfg = PVE
::Storage
::config
();
158 if (my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid, 1)) {
159 die "storage ID '$storeid' already defined\n";
162 $cfg->{ids
}->{$storeid} = $opts;
164 my $cred_file = undef;
166 if ($type eq 'lvm' && $opts->{base
}) {
168 my ($baseid, $volname) = PVE
::Storage
::parse_volume_id
($opts->{base
});
170 my $basecfg = PVE
::Storage
::storage_config
($cfg, $baseid, 1);
171 die "base storage ID '$baseid' does not exist\n" if !$basecfg;
173 # we only support iscsi for now
174 if (!($basecfg->{type
} eq 'iscsi')) {
175 die "unsupported base type '$basecfg->{type}'";
178 my $path = PVE
::Storage
::path
($cfg, $opts->{base
});
180 PVE
::Storage
::activate_storage
($cfg, $baseid);
182 PVE
::Storage
::LVMPlugin
::lvm_create_volume_group
($path, $opts->{vgname
}, $opts->{shared
});
183 } elsif ($type eq 'rbd' && !defined($opts->{monhost
})) {
184 my $ceph_admin_keyring = '/etc/pve/priv/ceph.client.admin.keyring';
185 my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
187 die "ceph authx keyring file for storage '$storeid' already exists!\n"
188 if -e
$ceph_storage_keyring;
191 mkdir '/etc/pve/priv/ceph';
192 PVE
::Tools
::file_copy
($ceph_admin_keyring, $ceph_storage_keyring);
195 unlink $ceph_storage_keyring;
196 die "failed to copy ceph authx keyring for storage '$storeid': $err\n";
198 } elsif ($type eq 'cifs' && defined($password)) {
199 # create a password file in /etc/pve/priv,
200 # this file is used as a cert_file at mount time.
201 $cred_file = PVE
::Storage
::CIFSPlugin
::cifs_set_credentials
($password, $storeid);
205 # try to activate if enabled on local node,
206 # we only do this to detect errors/problems sooner
207 if (PVE
::Storage
::storage_check_enabled
($cfg, $storeid, undef, 1)) {
208 PVE
::Storage
::activate_storage
($cfg, $storeid);
212 unlink $cred_file if defined($cred_file);
216 PVE
::Storage
::write_config
($cfg);
218 }, "create storage failed");
223 __PACKAGE__-
>register_method ({
228 description
=> "Update storage configuration.",
230 check
=> ['perm', '/storage', ['Datastore.Allocate']],
232 parameters
=> PVE
::Storage
::Plugin-
>updateSchema(),
233 returns
=> { type
=> 'null' },
237 my $storeid = extract_param
($param, 'storage');
238 my $digest = extract_param
($param, 'digest');
240 PVE
::Storage
::lock_storage_config
(
243 my $cfg = PVE
::Storage
::config
();
245 PVE
::SectionConfig
::assert_if_modified
($cfg, $digest);
247 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
249 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
250 my $opts = $plugin->check_config($storeid, $param, 0, 1);
252 foreach my $k (%$opts) {
253 $scfg->{$k} = $opts->{$k};
256 PVE
::Storage
::write_config
($cfg);
258 }, "update storage failed");
263 __PACKAGE__-
>register_method ({
266 path
=> '{storage}', # /storage/config/{storage}
268 description
=> "Delete storage configuration.",
270 check
=> ['perm', '/storage', ['Datastore.Allocate']],
273 additionalProperties
=> 0,
275 storage
=> get_standard_option
('pve-storage-id', {
276 completion
=> \
&PVE
::Storage
::complete_storage
,
280 returns
=> { type
=> 'null' },
284 my $storeid = extract_param
($param, 'storage');
286 PVE
::Storage
::lock_storage_config
(
289 my $cfg = PVE
::Storage
::config
();
291 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
293 die "can't remove storage - storage is used as base of another storage\n"
294 if PVE
::Storage
::storage_is_used
($cfg, $storeid);
296 if ($scfg->{type
} eq 'cifs') {
297 my $cred_file = PVE
::Storage
::CIFSPlugin
::cifs_cred_file_name
($storeid);
299 unlink($cred_file) or warn "removing cifs credientials '$cred_file' failed: $!\n";
301 } elsif ($scfg->{type
} eq 'rbd' && !defined($scfg->{monhost
})) {
302 my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
303 if (-f
$ceph_storage_keyring) {
304 unlink($ceph_storage_keyring) or warn "removing keyring of storage failed: $!\n";
308 delete $cfg->{ids
}->{$storeid};
310 PVE
::Storage
::write_config
($cfg);
312 }, "delete storage failed");
314 PVE
::AccessControl
::remove_storage_access
($storeid);