]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Disks/LVM.pm
fe875452bdaa170040b36a98f493f7876ff51f4b
[pve-storage.git] / PVE / API2 / Disks / LVM.pm
1 package PVE::API2::Disks::LVM;
2
3 use strict;
4 use warnings;
5
6 use PVE::Storage::LVMPlugin;
7 use PVE::Diskmanage;
8 use PVE::JSONSchema qw(get_standard_option);
9 use PVE::API2::Storage::Config;
10 use PVE::Tools qw(lock_file run_command);
11
12 use PVE::RPCEnvironment;
13 use PVE::RESTHandler;
14
15 use base qw(PVE::RESTHandler);
16
17 __PACKAGE__->register_method ({
18 name => 'index',
19 path => '',
20 method => 'GET',
21 proxyto => 'node',
22 protected => 1,
23 permissions => {
24 check => ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1],
25 },
26 description => "List LVM Volume Groups",
27 parameters => {
28 additionalProperties => 0,
29 properties => {
30 node => get_standard_option('pve-node'),
31 },
32 },
33 returns => {
34 type => 'object',
35 properties => {
36 leaf => {
37 type => 'boolean',
38 },
39 children => {
40 type => 'array',
41 items => {
42 type => "object",
43 properties => {
44 leaf => {
45 type => 'boolean',
46 },
47 name => {
48 type => 'string',
49 description => 'The name of the volume group',
50 },
51 size => {
52 type => 'integer',
53 description => 'The size of the volume group in bytes',
54 },
55 free => {
56 type => 'integer',
57 description => 'The free bytes in the volume group',
58 },
59 children => {
60 optional => 1,
61 type => 'array',
62 description => 'The underlying physical volumes',
63 items => {
64 type => 'object',
65 properties => {
66 leaf => {
67 type => 'boolean',
68 },
69 name => {
70 type => 'string',
71 description => 'The name of the physical volume',
72 },
73 size => {
74 type => 'integer',
75 description => 'The size of the physical volume in bytes',
76 },
77 free => {
78 type => 'integer',
79 description => 'The free bytes in the physical volume',
80 },
81 },
82 },
83 },
84 },
85 },
86 },
87 },
88 },
89 code => sub {
90 my ($param) = @_;
91
92 my $result = [];
93
94 my $vgs = PVE::Storage::LVMPlugin::lvm_vgs(1);
95
96 foreach my $vg_name (sort keys %$vgs) {
97 my $vg = $vgs->{$vg_name};
98 $vg->{name} = $vg_name;
99 $vg->{leaf} = 0;
100 foreach my $pv (@{$vg->{pvs}}) {
101 $pv->{leaf} = 1;
102 }
103 $vg->{children} = delete $vg->{pvs};
104 push @$result, $vg;
105 }
106
107 return {
108 leaf => 0,
109 children => $result,
110 };
111 }});
112
113 __PACKAGE__->register_method ({
114 name => 'create',
115 path => '',
116 method => 'POST',
117 proxyto => 'node',
118 protected => 1,
119 permissions => {
120 check => ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
121 },
122 description => "Create an LVM Volume Group",
123 parameters => {
124 additionalProperties => 0,
125 properties => {
126 node => get_standard_option('pve-node'),
127 name => get_standard_option('pve-storage-id'),
128 device => {
129 type => 'string',
130 description => 'The block device you want to create the volume group on',
131 },
132 add_storage => {
133 description => "Configure storage using the Volume Group",
134 type => 'boolean',
135 optional => 1,
136 default => 0,
137 },
138 },
139 },
140 returns => { type => 'string' },
141 code => sub {
142 my ($param) = @_;
143
144 my $rpcenv = PVE::RPCEnvironment::get();
145 my $user = $rpcenv->get_user();
146
147 my $name = $param->{name};
148 my $dev = $param->{device};
149 my $node = $param->{node};
150
151 $dev = PVE::Diskmanage::verify_blockdev_path($dev);
152 PVE::Diskmanage::assert_disk_unused($dev);
153
154 my $storage_params = {
155 type => 'lvm',
156 vgname => $name,
157 storage => $name,
158 content => 'rootdir,images',
159 shared => 0,
160 nodes => $node,
161 };
162 my $verify_params = [qw(vgname)];
163
164 if ($param->{add_storage}) {
165 PVE::API2::Storage::Config->create_or_update(
166 $name,
167 $node,
168 $storage_params,
169 $verify_params,
170 1,
171 );
172 }
173
174 my $worker = sub {
175 PVE::Diskmanage::locked_disk_action(sub {
176 PVE::Diskmanage::assert_disk_unused($dev);
177 die "volume group with name '${name}' already exists on node '${node}'\n"
178 if PVE::Storage::LVMPlugin::lvm_vgs()->{$name};
179
180 if (PVE::Diskmanage::is_partition($dev)) {
181 eval { PVE::Diskmanage::change_parttype($dev, '8E00'); };
182 warn $@ if $@;
183 }
184
185 PVE::Storage::LVMPlugin::lvm_create_volume_group($dev, $name);
186
187 PVE::Diskmanage::udevadm_trigger($dev);
188
189 if ($param->{add_storage}) {
190 PVE::API2::Storage::Config->create_or_update(
191 $name,
192 $node,
193 $storage_params,
194 $verify_params,
195 );
196 }
197 });
198 };
199
200 return $rpcenv->fork_worker('lvmcreate', $name, $user, $worker);
201 }});
202
203 __PACKAGE__->register_method ({
204 name => 'delete',
205 path => '{name}',
206 method => 'DELETE',
207 proxyto => 'node',
208 protected => 1,
209 permissions => {
210 check => ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
211 },
212 description => "Remove an LVM Volume Group.",
213 parameters => {
214 additionalProperties => 0,
215 properties => {
216 node => get_standard_option('pve-node'),
217 name => get_standard_option('pve-storage-id'),
218 'cleanup-config' => {
219 description => "Marks associated storage(s) as not available on this node anymore ".
220 "or removes them from the configuration (if configured for this node only).",
221 type => 'boolean',
222 optional => 1,
223 default => 0,
224 },
225 'cleanup-disks' => {
226 description => "Also wipe disks so they can be repurposed afterwards.",
227 type => 'boolean',
228 optional => 1,
229 default => 0,
230 },
231 },
232 },
233 returns => { type => 'string' },
234 code => sub {
235 my ($param) = @_;
236
237 my $rpcenv = PVE::RPCEnvironment::get();
238 my $user = $rpcenv->get_user();
239
240 my $name = $param->{name};
241 my $node = $param->{node};
242
243 my $worker = sub {
244 PVE::Diskmanage::locked_disk_action(sub {
245 my $vgs = PVE::Storage::LVMPlugin::lvm_vgs(1);
246 die "no such volume group '$name'\n" if !$vgs->{$name};
247
248 PVE::Storage::LVMPlugin::lvm_destroy_volume_group($name);
249
250 my $config_err;
251 if ($param->{'cleanup-config'}) {
252 my $match = sub {
253 my ($scfg) = @_;
254 return $scfg->{type} eq 'lvm' && $scfg->{vgname} eq $name;
255 };
256 eval { PVE::API2::Storage::Config->cleanup_storages_for_node($match, $node); };
257 warn $config_err = $@ if $@;
258 }
259
260 if ($param->{'cleanup-disks'}) {
261 my $wiped = [];
262 eval {
263 for my $pv ($vgs->{$name}->{pvs}->@*) {
264 my $dev = PVE::Diskmanage::verify_blockdev_path($pv->{name});
265 PVE::Diskmanage::wipe_blockdev($dev);
266 push $wiped->@*, $dev;
267 }
268 };
269 my $err = $@;
270 PVE::Diskmanage::udevadm_trigger($wiped->@*);
271 die "cleanup failed - $err" if $err;
272 }
273
274 die "config cleanup failed - $config_err" if $config_err;
275 });
276 };
277
278 return $rpcenv->fork_worker('lvmremove', $name, $user, $worker);
279 }});
280
281 1;