]>
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,
64 properties
=> { storage
=> { type
=> 'string'} },
66 links
=> [ { rel
=> 'child', href
=> "{storage}" } ],
71 my $rpcenv = PVE
::RPCEnvironment
::get
();
72 my $authuser = $rpcenv->get_user();
74 my $cfg = cfs_read_file
("storage.cfg");
76 my @sids = PVE
::Storage
::storage_ids
($cfg);
79 foreach my $storeid (@sids) {
80 my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ];
81 next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1);
83 my $scfg = &$api_storage_config($cfg, $storeid);
84 next if $param->{type
} && $param->{type
} ne $scfg->{type
};
91 __PACKAGE__-
>register_method ({
95 description
=> "Read storage configuration.",
97 check
=> ['perm', '/storage/{storage}', ['Datastore.Allocate']],
100 additionalProperties
=> 0,
102 storage
=> get_standard_option
('pve-storage-id'),
109 my $cfg = cfs_read_file
("storage.cfg");
111 return &$api_storage_config($cfg, $param->{storage
});
114 __PACKAGE__-
>register_method ({
119 description
=> "Create a new storage.",
121 check
=> ['perm', '/storage', ['Datastore.Allocate']],
123 parameters
=> PVE
::Storage
::Plugin-
>createSchema(),
124 returns
=> { type
=> 'null' },
128 my $type = extract_param
($param, 'type');
129 my $storeid = extract_param
($param, 'storage');
131 if ($param->{portal
}) {
132 $param->{portal
} = PVE
::Storage
::resolv_portal
($param->{portal
});
135 my $plugin = PVE
::Storage
::Plugin-
>lookup($type);
136 my $opts = $plugin->check_config($storeid, $param, 1, 1);
138 PVE
::Storage
::lock_storage_config
(
141 my $cfg = cfs_read_file
('storage.cfg');
143 if (my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid, 1)) {
144 die "storage ID '$storeid' already defined\n";
147 $cfg->{ids
}->{$storeid} = $opts;
149 if ($type eq 'lvm' && $opts->{base
}) {
151 my ($baseid, $volname) = PVE
::Storage
::parse_volume_id
($opts->{base
});
153 my $basecfg = PVE
::Storage
::storage_config
($cfg, $baseid, 1);
154 die "base storage ID '$baseid' does not exist\n" if !$basecfg;
156 # we only support iscsi for now
157 if (!($basecfg->{type
} eq 'iscsi')) {
158 die "unsupported base type '$basecfg->{type}'";
161 my $path = PVE
::Storage
::path
($cfg, $opts->{base
});
163 PVE
::Storage
::activate_storage
($cfg, $baseid);
165 PVE
::Storage
::LVMPlugin
::lvm_create_volume_group
($path, $opts->{vgname
}, $opts->{shared
});
168 # try to activate if enabled on local node,
169 # we only do this to detect errors/problems sooner
170 if (PVE
::Storage
::storage_check_enabled
($cfg, $storeid, undef, 1)) {
171 PVE
::Storage
::activate_storage
($cfg, $storeid);
174 cfs_write_file
('storage.cfg', $cfg);
176 }, "create storage failed");
181 __PACKAGE__-
>register_method ({
186 description
=> "Update storage configuration.",
188 check
=> ['perm', '/storage', ['Datastore.Allocate']],
190 parameters
=> PVE
::Storage
::Plugin-
>updateSchema(),
191 returns
=> { type
=> 'null' },
195 my $storeid = extract_param
($param, 'storage');
196 my $digest = extract_param
($param, 'digest');
198 PVE
::Storage
::lock_storage_config
(
201 my $cfg = cfs_read_file
('storage.cfg');
203 PVE
::SectionConfig
::assert_if_modified
($cfg, $digest);
205 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
207 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
208 my $opts = $plugin->check_config($storeid, $param, 0, 1);
210 foreach my $k (%$opts) {
211 $scfg->{$k} = $opts->{$k};
214 cfs_write_file
('storage.cfg', $cfg);
216 }, "update storage failed");
221 __PACKAGE__-
>register_method ({
224 path
=> '{storage}', # /storage/config/{storage}
226 description
=> "Delete storage configuration.",
228 check
=> ['perm', '/storage', ['Datastore.Allocate']],
231 additionalProperties
=> 0,
233 storage
=> get_standard_option
('pve-storage-id'),
236 returns
=> { type
=> 'null' },
240 my $storeid = extract_param
($param, 'storage');
242 PVE
::Storage
::lock_storage_config
(
245 my $cfg = cfs_read_file
('storage.cfg');
247 die "can't remove storage - storage is used as base of another storage\n"
248 if PVE
::Storage
::storage_is_used
($cfg, $storeid);
250 delete $cfg->{ids
}->{$storeid};
252 cfs_write_file
('storage.cfg', $cfg);
254 }, "delete storage failed");