]>
Commit | Line | Data |
---|---|---|
0ea9f384 DC |
1 | package PVE::API2::Disks::LVMThin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::Storage::LvmThinPlugin; | |
7 | use PVE::Diskmanage; | |
8 | use PVE::JSONSchema qw(get_standard_option); | |
9 | use PVE::API2::Storage::Config; | |
10 | use PVE::Storage; | |
11 | use PVE::Tools qw(run_command lock_file); | |
12 | ||
13 | use PVE::RPCEnvironment; | |
14 | use PVE::RESTHandler; | |
15 | ||
16 | use base qw(PVE::RESTHandler); | |
17 | ||
18 | __PACKAGE__->register_method ({ | |
19 | name => 'index', | |
20 | path => '', | |
21 | method => 'GET', | |
22 | proxyto => 'node', | |
23 | protected => 1, | |
24 | permissions => { | |
25 | check => ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1], | |
26 | }, | |
27 | description => "List LVM thinpools", | |
28 | parameters => { | |
29 | additionalProperties => 0, | |
30 | properties => { | |
31 | node => get_standard_option('pve-node'), | |
32 | }, | |
33 | }, | |
34 | returns => { | |
35 | type => 'array', | |
36 | items => { | |
37 | type => 'object', | |
38 | properties => { | |
39 | lv => { | |
40 | type => 'string', | |
41 | description => 'The name of the thinpool.', | |
42 | }, | |
a510449e FE |
43 | vg => { |
44 | type => 'string', | |
45 | description => 'The associated volume group.', | |
46 | }, | |
0ea9f384 DC |
47 | lv_size => { |
48 | type => 'integer', | |
49 | description => 'The size of the thinpool in bytes.', | |
50 | }, | |
51 | used => { | |
52 | type => 'integer', | |
53 | description => 'The used bytes of the thinpool.', | |
54 | }, | |
55 | metadata_size => { | |
56 | type => 'integer', | |
57 | description => 'The size of the metadata lv in bytes.', | |
58 | }, | |
59 | metadata_used => { | |
60 | type => 'integer', | |
61 | description => 'The used bytes of the metadata lv.', | |
62 | }, | |
63 | }, | |
64 | }, | |
65 | }, | |
66 | code => sub { | |
67 | my ($param) = @_; | |
68 | return PVE::Storage::LvmThinPlugin::list_thinpools(undef); | |
69 | }}); | |
70 | ||
71 | __PACKAGE__->register_method ({ | |
72 | name => 'create', | |
73 | path => '', | |
74 | method => 'POST', | |
75 | proxyto => 'node', | |
76 | protected => 1, | |
77 | permissions => { | |
78 | check => ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']], | |
79 | }, | |
80 | description => "Create an LVM thinpool", | |
81 | parameters => { | |
82 | additionalProperties => 0, | |
83 | properties => { | |
84 | node => get_standard_option('pve-node'), | |
85 | name => get_standard_option('pve-storage-id'), | |
86 | device => { | |
87 | type => 'string', | |
88 | description => 'The block device you want to create the thinpool on.', | |
89 | }, | |
90 | add_storage => { | |
91 | description => "Configure storage using the thinpool.", | |
92 | type => 'boolean', | |
93 | optional => 1, | |
94 | default => 0, | |
95 | }, | |
96 | }, | |
97 | }, | |
98 | returns => { type => 'string' }, | |
99 | code => sub { | |
100 | my ($param) = @_; | |
101 | ||
102 | my $rpcenv = PVE::RPCEnvironment::get(); | |
103 | my $user = $rpcenv->get_user(); | |
104 | ||
105 | my $name = $param->{name}; | |
106 | my $dev = $param->{device}; | |
107 | my $node = $param->{node}; | |
108 | ||
109 | $dev = PVE::Diskmanage::verify_blockdev_path($dev); | |
0370861c | 110 | PVE::Diskmanage::assert_disk_unused($dev); |
9280153e | 111 | PVE::Storage::assert_sid_unused($name) if $param->{add_storage}; |
0ea9f384 DC |
112 | |
113 | my $worker = sub { | |
e39e8ee2 | 114 | PVE::Diskmanage::locked_disk_action(sub { |
e99bc248 FE |
115 | PVE::Diskmanage::assert_disk_unused($dev); |
116 | ||
05d91712 FE |
117 | if (PVE::Diskmanage::is_partition($dev)) { |
118 | eval { PVE::Diskmanage::change_parttype($dev, '8E00'); }; | |
119 | warn $@ if $@; | |
120 | } | |
121 | ||
0ea9f384 | 122 | PVE::Storage::LVMPlugin::lvm_create_volume_group($dev, $name); |
3ea55f05 TM |
123 | my $pv = PVE::Storage::LVMPlugin::lvm_pv_info($dev); |
124 | # keep some free space just in case | |
125 | my $datasize = $pv->{size} - 128*1024; | |
126 | # default to 1% for metadata | |
127 | my $metadatasize = $datasize/100; | |
128 | # but at least 1G, as recommended in lvmthin man | |
129 | $metadatasize = 1024*1024 if $metadatasize < 1024*1024; | |
130 | # but at most 16G, which is the current lvm max | |
131 | $metadatasize = 16*1024*1024 if $metadatasize > 16*1024*1024; | |
132 | # shrink data by needed amount for metadata | |
133 | $datasize -= 2*$metadatasize; | |
0ea9f384 | 134 | |
3ea55f05 TM |
135 | run_command([ |
136 | '/sbin/lvcreate', | |
137 | '--type', 'thin-pool', | |
138 | "-L${datasize}K", | |
139 | '--poolmetadatasize', "${metadatasize}K", | |
140 | '-n', $name, | |
141 | $name | |
142 | ]); | |
0ea9f384 | 143 | |
26082b7d | 144 | PVE::Diskmanage::udevadm_trigger($dev); |
21a75847 | 145 | |
0ea9f384 DC |
146 | if ($param->{add_storage}) { |
147 | my $storage_params = { | |
148 | type => 'lvmthin', | |
149 | vgname => $name, | |
150 | thinpool => $name, | |
151 | storage => $name, | |
152 | content => 'rootdir,images', | |
153 | nodes => $node, | |
154 | }; | |
155 | ||
156 | PVE::API2::Storage::Config->create($storage_params); | |
157 | } | |
158 | }); | |
0ea9f384 DC |
159 | }; |
160 | ||
5e35281d | 161 | return $rpcenv->fork_worker('lvmthincreate', $name, $user, $worker); |
0ea9f384 DC |
162 | }}); |
163 | ||
a83d8eb1 FE |
164 | __PACKAGE__->register_method ({ |
165 | name => 'delete', | |
166 | path => '{name}', | |
167 | method => 'DELETE', | |
168 | proxyto => 'node', | |
169 | protected => 1, | |
170 | permissions => { | |
171 | check => ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']], | |
172 | }, | |
173 | description => "Remove an LVM thin pool.", | |
174 | parameters => { | |
175 | additionalProperties => 0, | |
176 | properties => { | |
177 | node => get_standard_option('pve-node'), | |
178 | name => get_standard_option('pve-storage-id'), | |
179 | 'volume-group' => get_standard_option('pve-storage-id'), | |
cde43c48 FE |
180 | 'cleanup-config' => { |
181 | description => "Marks associated storage(s) as not available on this node anymore ". | |
182 | "or removes them from the configuration (if configured for this node only).", | |
183 | type => 'boolean', | |
184 | optional => 1, | |
185 | default => 0, | |
186 | }, | |
f81908eb FE |
187 | 'cleanup-disks' => { |
188 | description => "Also wipe disks so they can be repurposed afterwards.", | |
189 | type => 'boolean', | |
190 | optional => 1, | |
191 | default => 0, | |
192 | }, | |
a83d8eb1 FE |
193 | }, |
194 | }, | |
195 | returns => { type => 'string' }, | |
196 | code => sub { | |
197 | my ($param) = @_; | |
198 | ||
199 | my $rpcenv = PVE::RPCEnvironment::get(); | |
200 | my $user = $rpcenv->get_user(); | |
201 | ||
202 | my $vg = $param->{'volume-group'}; | |
203 | my $lv = $param->{name}; | |
cde43c48 | 204 | my $node = $param->{node}; |
a83d8eb1 FE |
205 | |
206 | my $worker = sub { | |
207 | PVE::Diskmanage::locked_disk_action(sub { | |
208 | my $thinpools = PVE::Storage::LvmThinPlugin::list_thinpools(); | |
209 | ||
210 | die "no such thin pool ${vg}/${lv}\n" | |
211 | if !grep { $_->{lv} eq $lv && $_->{vg} eq $vg } $thinpools->@*; | |
212 | ||
213 | run_command(['lvremove', '-y', "${vg}/${lv}"]); | |
f81908eb | 214 | |
cde43c48 FE |
215 | my $config_err; |
216 | if ($param->{'cleanup-config'}) { | |
217 | my $match = sub { | |
218 | my ($scfg) = @_; | |
219 | return $scfg->{type} eq 'lvmthin' | |
220 | && $scfg->{vgname} eq $vg | |
221 | && $scfg->{thinpool} eq $lv; | |
222 | }; | |
223 | eval { PVE::API2::Storage::Config->cleanup_storages_for_node($match, $node); }; | |
224 | warn $config_err = $@ if $@; | |
225 | } | |
226 | ||
f81908eb FE |
227 | if ($param->{'cleanup-disks'}) { |
228 | my $vgs = PVE::Storage::LVMPlugin::lvm_vgs(1); | |
229 | ||
230 | die "no such volume group '$vg'\n" if !$vgs->{$vg}; | |
231 | die "volume group '$vg' still in use\n" if $vgs->{$vg}->{lvcount} > 0; | |
232 | ||
233 | my $wiped = []; | |
234 | eval { | |
235 | for my $pv ($vgs->{$vg}->{pvs}->@*) { | |
236 | my $dev = PVE::Diskmanage::verify_blockdev_path($pv->{name}); | |
237 | PVE::Diskmanage::wipe_blockdev($dev); | |
238 | push $wiped->@*, $dev; | |
239 | } | |
240 | }; | |
241 | my $err = $@; | |
242 | PVE::Diskmanage::udevadm_trigger($wiped->@*); | |
243 | die "cleanup failed - $err" if $err; | |
244 | } | |
cde43c48 FE |
245 | |
246 | die "config cleanup failed - $config_err" if $config_err; | |
a83d8eb1 FE |
247 | }); |
248 | }; | |
249 | ||
250 | return $rpcenv->fork_worker('lvmthinremove', "${vg}-${lv}", $user, $worker); | |
251 | }}); | |
252 | ||
0ea9f384 | 253 | 1; |