]>
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 = cfs_read_file
("storage.cfg");
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 = cfs_read_file
("storage.cfg");
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 = cfs_read_file
('storage.cfg');
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
});
167 # try to activate if enabled on local node,
168 # we only do this to detect errors/problems sooner
169 if (PVE
::Storage
::storage_check_enabled
($cfg, $storeid, undef, 1)) {
170 PVE
::Storage
::activate_storage
($cfg, $storeid);
173 cfs_write_file
('storage.cfg', $cfg);
175 }, "create storage failed");
180 __PACKAGE__-
>register_method ({
185 description
=> "Update storage configuration.",
187 check
=> ['perm', '/storage', ['Datastore.Allocate']],
189 parameters
=> PVE
::Storage
::Plugin-
>updateSchema(),
190 returns
=> { type
=> 'null' },
194 my $storeid = extract_param
($param, 'storage');
195 my $digest = extract_param
($param, 'digest');
197 PVE
::Storage
::lock_storage_config
(
200 my $cfg = cfs_read_file
('storage.cfg');
202 PVE
::SectionConfig
::assert_if_modified
($cfg, $digest);
204 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
206 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
207 my $opts = $plugin->check_config($storeid, $param, 0, 1);
209 foreach my $k (%$opts) {
210 $scfg->{$k} = $opts->{$k};
213 cfs_write_file
('storage.cfg', $cfg);
215 }, "update storage failed");
220 __PACKAGE__-
>register_method ({
223 path
=> '{storage}', # /storage/config/{storage}
225 description
=> "Delete storage configuration.",
227 check
=> ['perm', '/storage', ['Datastore.Allocate']],
230 additionalProperties
=> 0,
232 storage
=> get_standard_option
('pve-storage-id'),
235 returns
=> { type
=> 'null' },
239 my $storeid = extract_param
($param, 'storage');
241 PVE
::Storage
::lock_storage_config
(
244 my $cfg = cfs_read_file
('storage.cfg');
246 die "can't remove storage - storage is used as base of another storage\n"
247 if PVE
::Storage
::storage_is_used
($cfg, $storeid);
249 delete $cfg->{ids
}->{$storeid};
251 cfs_write_file
('storage.cfg', $cfg);
253 }, "delete storage failed");