]>
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 __PACKAGE__-
>register_method ({
43 description
=> "Storage index.",
45 description
=> "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'",
49 additionalProperties
=> 0,
52 description
=> "Only list storage of specific type",
54 enum
=> $storage_type_enum,
63 properties
=> { storage
=> { type
=> 'string'} },
65 links
=> [ { rel
=> 'child', href
=> "{storage}" } ],
70 my $rpcenv = PVE
::RPCEnvironment
::get
();
71 my $authuser = $rpcenv->get_user();
73 my $cfg = PVE
::Storage
::config
();
75 my @sids = PVE
::Storage
::storage_ids
($cfg);
78 foreach my $storeid (@sids) {
79 my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ];
80 next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1);
82 my $scfg = &$api_storage_config($cfg, $storeid);
83 next if $param->{type
} && $param->{type
} ne $scfg->{type
};
90 __PACKAGE__-
>register_method ({
94 description
=> "Read storage configuration.",
96 check
=> ['perm', '/storage/{storage}', ['Datastore.Allocate']],
99 additionalProperties
=> 0,
101 storage
=> get_standard_option
('pve-storage-id'),
108 my $cfg = PVE
::Storage
::config
();
110 return &$api_storage_config($cfg, $param->{storage
});
113 __PACKAGE__-
>register_method ({
118 description
=> "Create a new storage.",
120 check
=> ['perm', '/storage', ['Datastore.Allocate']],
122 parameters
=> PVE
::Storage
::Plugin-
>createSchema(),
123 returns
=> { type
=> 'null' },
127 my $type = extract_param
($param, 'type');
128 my $storeid = extract_param
($param, 'storage');
130 if ($param->{portal
}) {
131 $param->{portal
} = PVE
::Storage
::resolv_portal
($param->{portal
});
134 my $plugin = PVE
::Storage
::Plugin-
>lookup($type);
135 my $opts = $plugin->check_config($storeid, $param, 1, 1);
137 PVE
::Storage
::lock_storage_config
(
140 my $cfg = PVE
::Storage
::config
();
142 if (my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid, 1)) {
143 die "storage ID '$storeid' already defined\n";
146 $cfg->{ids
}->{$storeid} = $opts;
148 if ($type eq 'lvm' && $opts->{base
}) {
150 my ($baseid, $volname) = PVE
::Storage
::parse_volume_id
($opts->{base
});
152 my $basecfg = PVE
::Storage
::storage_config
($cfg, $baseid, 1);
153 die "base storage ID '$baseid' does not exist\n" if !$basecfg;
155 # we only support iscsi for now
156 if (!($basecfg->{type
} eq 'iscsi')) {
157 die "unsupported base type '$basecfg->{type}'";
160 my $path = PVE
::Storage
::path
($cfg, $opts->{base
});
162 PVE
::Storage
::activate_storage
($cfg, $baseid);
164 PVE
::Storage
::LVMPlugin
::lvm_create_volume_group
($path, $opts->{vgname
}, $opts->{shared
});
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";
169 die "ceph authx keyring file for storage '$storeid' already exists!\n"
170 if -e
$ceph_storage_keyring;
173 PVE
::Tools
::file_copy
($ceph_admin_keyring, $ceph_storage_keyring);
176 unlink $ceph_storage_keyring;
177 die "failed to copy ceph authx keyring for storage '$storeid': $err\n";
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);
187 PVE
::Storage
::write_config
($cfg);
189 }, "create storage failed");
194 __PACKAGE__-
>register_method ({
199 description
=> "Update storage configuration.",
201 check
=> ['perm', '/storage', ['Datastore.Allocate']],
203 parameters
=> PVE
::Storage
::Plugin-
>updateSchema(),
204 returns
=> { type
=> 'null' },
208 my $storeid = extract_param
($param, 'storage');
209 my $digest = extract_param
($param, 'digest');
211 PVE
::Storage
::lock_storage_config
(
214 my $cfg = PVE
::Storage
::config
();
216 PVE
::SectionConfig
::assert_if_modified
($cfg, $digest);
218 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
220 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
221 my $opts = $plugin->check_config($storeid, $param, 0, 1);
223 foreach my $k (%$opts) {
224 $scfg->{$k} = $opts->{$k};
227 PVE
::Storage
::write_config
($cfg);
229 }, "update storage failed");
234 __PACKAGE__-
>register_method ({
237 path
=> '{storage}', # /storage/config/{storage}
239 description
=> "Delete storage configuration.",
241 check
=> ['perm', '/storage', ['Datastore.Allocate']],
244 additionalProperties
=> 0,
246 storage
=> get_standard_option
('pve-storage-id', {
247 completion
=> \
&PVE
::Storage
::complete_storage
,
251 returns
=> { type
=> 'null' },
255 my $storeid = extract_param
($param, 'storage');
257 PVE
::Storage
::lock_storage_config
(
260 my $cfg = PVE
::Storage
::config
();
262 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
264 die "can't remove storage - storage is used as base of another storage\n"
265 if PVE
::Storage
::storage_is_used
($cfg, $storeid);
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";
274 delete $cfg->{ids
}->{$storeid};
276 PVE
::Storage
::write_config
($cfg);
278 }, "delete storage failed");
280 PVE
::AccessControl
::remove_storage_access
($storeid);