]> git.proxmox.com Git - pve-storage.git/blame - PVE/API2/Disks/LVM.pm
diskmanage: add helper for udev workaround
[pve-storage.git] / PVE / API2 / Disks / LVM.pm
CommitLineData
8b6842ca
DC
1package PVE::API2::Disks::LVM;
2
3use strict;
4use warnings;
5
6use PVE::Storage::LVMPlugin;
7use PVE::Diskmanage;
8use PVE::JSONSchema qw(get_standard_option);
9use PVE::API2::Storage::Config;
21a75847 10use PVE::Tools qw(lock_file run_command);
8b6842ca
DC
11
12use PVE::RPCEnvironment;
13use PVE::RESTHandler;
14
15use 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);
0370861c 152 PVE::Diskmanage::assert_disk_unused($dev);
9280153e 153 PVE::Storage::assert_sid_unused($name) if $param->{add_storage};
8b6842ca
DC
154
155 my $worker = sub {
e39e8ee2 156 PVE::Diskmanage::locked_disk_action(sub {
e99bc248
FE
157 PVE::Diskmanage::assert_disk_unused($dev);
158
05d91712
FE
159 if (PVE::Diskmanage::is_partition($dev)) {
160 eval { PVE::Diskmanage::change_parttype($dev, '8E00'); };
161 warn $@ if $@;
162 }
163
8b6842ca
DC
164 PVE::Storage::LVMPlugin::lvm_create_volume_group($dev, $name);
165
26082b7d 166 PVE::Diskmanage::udevadm_trigger($dev);
21a75847 167
8b6842ca
DC
168 if ($param->{add_storage}) {
169 my $storage_params = {
170 type => 'lvm',
171 vgname => $name,
172 storage => $name,
173 content => 'rootdir,images',
174 shared => 0,
175 nodes => $node,
176 };
177
178 PVE::API2::Storage::Config->create($storage_params);
179 }
180 });
8b6842ca
DC
181 };
182
183 return $rpcenv->fork_worker('lvmcreate', $name, $user, $worker);
184 }});
185
a83d8eb1
FE
186__PACKAGE__->register_method ({
187 name => 'delete',
188 path => '{name}',
189 method => 'DELETE',
190 proxyto => 'node',
191 protected => 1,
192 permissions => {
193 check => ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
194 },
195 description => "Remove an LVM Volume Group.",
196 parameters => {
197 additionalProperties => 0,
198 properties => {
199 node => get_standard_option('pve-node'),
200 name => get_standard_option('pve-storage-id'),
201 },
202 },
203 returns => { type => 'string' },
204 code => sub {
205 my ($param) = @_;
206
207 my $rpcenv = PVE::RPCEnvironment::get();
208 my $user = $rpcenv->get_user();
209
210 my $name = $param->{name};
211
212 my $worker = sub {
213 PVE::Diskmanage::locked_disk_action(sub {
214 PVE::Storage::LVMPlugin::lvm_destroy_volume_group($name);
215 });
216 };
217
218 return $rpcenv->fork_worker('lvmremove', $name, $user, $worker);
219 }});
220
8b6842ca 2211;