]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Disks/LVM.pm
ef341d1ce67ebd54bb4d9ee9c341f35a8fda385c
[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 PVE::Storage::assert_sid_unused($name) if $param->{add_storage};
154
155 my $worker = sub {
156 PVE::Diskmanage::locked_disk_action(sub {
157 PVE::Diskmanage::assert_disk_unused($dev);
158 die "volume group with name '${name}' already exists on node '${node}'\n"
159 if PVE::Storage::LVMPlugin::lvm_vgs()->{$name};
160
161 if (PVE::Diskmanage::is_partition($dev)) {
162 eval { PVE::Diskmanage::change_parttype($dev, '8E00'); };
163 warn $@ if $@;
164 }
165
166 PVE::Storage::LVMPlugin::lvm_create_volume_group($dev, $name);
167
168 PVE::Diskmanage::udevadm_trigger($dev);
169
170 if ($param->{add_storage}) {
171 my $storage_params = {
172 type => 'lvm',
173 vgname => $name,
174 storage => $name,
175 content => 'rootdir,images',
176 shared => 0,
177 nodes => $node,
178 };
179
180 PVE::API2::Storage::Config->create($storage_params);
181 }
182 });
183 };
184
185 return $rpcenv->fork_worker('lvmcreate', $name, $user, $worker);
186 }});
187
188 __PACKAGE__->register_method ({
189 name => 'delete',
190 path => '{name}',
191 method => 'DELETE',
192 proxyto => 'node',
193 protected => 1,
194 permissions => {
195 check => ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
196 },
197 description => "Remove an LVM Volume Group.",
198 parameters => {
199 additionalProperties => 0,
200 properties => {
201 node => get_standard_option('pve-node'),
202 name => get_standard_option('pve-storage-id'),
203 'cleanup-config' => {
204 description => "Marks associated storage(s) as not available on this node anymore ".
205 "or removes them from the configuration (if configured for this node only).",
206 type => 'boolean',
207 optional => 1,
208 default => 0,
209 },
210 'cleanup-disks' => {
211 description => "Also wipe disks so they can be repurposed afterwards.",
212 type => 'boolean',
213 optional => 1,
214 default => 0,
215 },
216 },
217 },
218 returns => { type => 'string' },
219 code => sub {
220 my ($param) = @_;
221
222 my $rpcenv = PVE::RPCEnvironment::get();
223 my $user = $rpcenv->get_user();
224
225 my $name = $param->{name};
226 my $node = $param->{node};
227
228 my $worker = sub {
229 PVE::Diskmanage::locked_disk_action(sub {
230 my $vgs = PVE::Storage::LVMPlugin::lvm_vgs(1);
231 die "no such volume group '$name'\n" if !$vgs->{$name};
232
233 PVE::Storage::LVMPlugin::lvm_destroy_volume_group($name);
234
235 my $config_err;
236 if ($param->{'cleanup-config'}) {
237 my $match = sub {
238 my ($scfg) = @_;
239 return $scfg->{type} eq 'lvm' && $scfg->{vgname} eq $name;
240 };
241 eval { PVE::API2::Storage::Config->cleanup_storages_for_node($match, $node); };
242 warn $config_err = $@ if $@;
243 }
244
245 if ($param->{'cleanup-disks'}) {
246 my $wiped = [];
247 eval {
248 for my $pv ($vgs->{$name}->{pvs}->@*) {
249 my $dev = PVE::Diskmanage::verify_blockdev_path($pv->{name});
250 PVE::Diskmanage::wipe_blockdev($dev);
251 push $wiped->@*, $dev;
252 }
253 };
254 my $err = $@;
255 PVE::Diskmanage::udevadm_trigger($wiped->@*);
256 die "cleanup failed - $err" if $err;
257 }
258
259 die "config cleanup failed - $config_err" if $config_err;
260 });
261 };
262
263 return $rpcenv->fork_worker('lvmremove', $name, $user, $worker);
264 }});
265
266 1;