]>
Commit | Line | Data |
---|---|---|
1dc01b9f DM |
1 | package PVE::Storage::LVMPlugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use IO::File; | |
6 | use PVE::Tools qw(run_command trim); | |
7 | use PVE::Storage::Plugin; | |
8 | use PVE::JSONSchema qw(get_standard_option); | |
9 | ||
10 | use base qw(PVE::Storage::Plugin); | |
11 | ||
12 | # lvm helper functions | |
13 | ||
14 | sub lvm_pv_info { | |
15 | my ($device) = @_; | |
16 | ||
17 | die "no device specified" if !$device; | |
18 | ||
19 | my $has_label = 0; | |
20 | ||
21 | my $cmd = ['/usr/bin/file', '-L', '-s', $device]; | |
22 | run_command($cmd, outfunc => sub { | |
23 | my $line = shift; | |
24 | $has_label = 1 if $line =~ m/LVM2/; | |
25 | }); | |
26 | ||
27 | return undef if !$has_label; | |
28 | ||
29 | $cmd = ['/sbin/pvs', '--separator', ':', '--noheadings', '--units', 'k', | |
30 | '--unbuffered', '--nosuffix', '--options', | |
31 | 'pv_name,pv_size,vg_name,pv_uuid', $device]; | |
32 | ||
33 | my $pvinfo; | |
34 | run_command($cmd, outfunc => sub { | |
35 | my $line = shift; | |
36 | ||
37 | $line = trim($line); | |
38 | ||
39 | my ($pvname, $size, $vgname, $uuid) = split(':', $line); | |
40 | ||
41 | die "found multiple pvs entries for device '$device'\n" | |
42 | if $pvinfo; | |
43 | ||
44 | $pvinfo = { | |
45 | pvname => $pvname, | |
46 | size => $size, | |
47 | vgname => $vgname, | |
48 | uuid => $uuid, | |
49 | }; | |
50 | }); | |
51 | ||
52 | return $pvinfo; | |
53 | } | |
54 | ||
55 | sub clear_first_sector { | |
56 | my ($dev) = shift; | |
57 | ||
58 | if (my $fh = IO::File->new($dev, "w")) { | |
59 | my $buf = 0 x 512; | |
60 | syswrite $fh, $buf; | |
61 | $fh->close(); | |
62 | } | |
63 | } | |
64 | ||
65 | sub lvm_create_volume_group { | |
66 | my ($device, $vgname, $shared) = @_; | |
67 | ||
68 | my $res = lvm_pv_info($device); | |
69 | ||
70 | if ($res->{vgname}) { | |
71 | return if $res->{vgname} eq $vgname; # already created | |
72 | die "device '$device' is already used by volume group '$res->{vgname}'\n"; | |
73 | } | |
74 | ||
75 | clear_first_sector($device); # else pvcreate fails | |
76 | ||
77 | # we use --metadatasize 250k, which reseults in "pe_start = 512" | |
78 | # so pe_start is aligned on a 128k boundary (advantage for SSDs) | |
79 | my $cmd = ['/sbin/pvcreate', '--metadatasize', '250k', $device]; | |
80 | ||
81 | run_command($cmd, errmsg => "pvcreate '$device' error"); | |
82 | ||
83 | $cmd = ['/sbin/vgcreate', $vgname, $device]; | |
84 | # push @$cmd, '-c', 'y' if $shared; # we do not use this yet | |
85 | ||
86 | run_command($cmd, errmsg => "vgcreate $vgname $device error"); | |
87 | } | |
88 | ||
89 | sub lvm_vgs { | |
90 | ||
91 | my $cmd = ['/sbin/vgs', '--separator', ':', '--noheadings', '--units', 'b', | |
92 | '--unbuffered', '--nosuffix', '--options', | |
93 | 'vg_name,vg_size,vg_free']; | |
94 | ||
95 | my $vgs = {}; | |
96 | eval { | |
97 | run_command($cmd, outfunc => sub { | |
98 | my $line = shift; | |
99 | ||
100 | $line = trim($line); | |
101 | ||
102 | my ($name, $size, $free) = split (':', $line); | |
103 | ||
104 | $vgs->{$name} = { size => int ($size), free => int ($free) }; | |
105 | }); | |
106 | }; | |
107 | my $err = $@; | |
108 | ||
109 | # just warn (vgs return error code 5 if clvmd does not run) | |
110 | # but output is still OK (list without clustered VGs) | |
111 | warn $err if $err; | |
112 | ||
113 | return $vgs; | |
114 | } | |
115 | ||
116 | sub lvm_lvs { | |
117 | my ($vgname) = @_; | |
118 | ||
119 | my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b', | |
120 | '--unbuffered', '--nosuffix', '--options', | |
121 | 'vg_name,lv_name,lv_size,uuid,tags']; | |
122 | ||
123 | push @$cmd, $vgname if $vgname; | |
124 | ||
125 | my $lvs = {}; | |
126 | run_command($cmd, outfunc => sub { | |
127 | my $line = shift; | |
128 | ||
129 | $line = trim($line); | |
130 | ||
131 | my ($vg, $name, $size, $uuid, $tags) = split(':', $line); | |
132 | ||
133 | return if $name !~ m/^vm-(\d+)-/; | |
134 | my $nid = $1; | |
135 | ||
136 | my $owner; | |
137 | foreach my $tag (split (/,/, $tags)) { | |
138 | if ($tag =~ m/^pve-vm-(\d+)$/) { | |
139 | $owner = $1; | |
140 | last; | |
141 | } | |
142 | } | |
143 | ||
144 | if ($owner) { | |
145 | if ($owner ne $nid) { | |
146 | warn "owner mismatch name = $name, owner = $owner\n"; | |
147 | } | |
148 | ||
149 | $lvs->{$vg}->{$name} = { format => 'raw', size => $size, | |
150 | uuid => $uuid, tags => $tags, | |
151 | vmid => $owner }; | |
152 | } | |
153 | }); | |
154 | ||
155 | return $lvs; | |
156 | } | |
157 | ||
158 | # Configuration | |
159 | ||
160 | PVE::JSONSchema::register_format('pve-storage-vgname', \&parse_lvm_name); | |
161 | sub parse_lvm_name { | |
162 | my ($name, $noerr) = @_; | |
163 | ||
164 | if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) { | |
165 | return undef if $noerr; | |
166 | die "lvm name '$name' contains illegal characters\n"; | |
167 | } | |
168 | ||
169 | return $name; | |
170 | } | |
171 | ||
172 | sub type { | |
173 | return 'lvm'; | |
174 | } | |
175 | ||
176 | sub plugindata { | |
177 | return { | |
178 | content => [ {images => 1}, { images => 1 }], | |
179 | }; | |
180 | } | |
181 | ||
182 | sub properties { | |
183 | return { | |
184 | vgname => { | |
185 | description => "Volume group name.", | |
186 | type => 'string', format => 'pve-storage-vgname', | |
187 | }, | |
188 | base => { | |
189 | description => "Base volume. This volume is automatically activated.", | |
190 | type => 'string', format => 'pve-volume-id', | |
191 | }, | |
192 | saferemove => { | |
193 | description => "Zero-out data when removing LVs.", | |
194 | type => 'boolean', | |
195 | }, | |
196 | }; | |
197 | } | |
198 | ||
199 | sub options { | |
200 | return { | |
201 | vgname => { fixed => 1 }, | |
202 | nodes => { optional => 1 }, | |
203 | shared => { optional => 1 }, | |
204 | disable => { optional => 1 }, | |
205 | saferemove => { optional => 1 }, | |
206 | content => { optional => 1 }, | |
207 | base => { fixed => 1, optional => 1 }, | |
208 | }; | |
209 | } | |
210 | ||
211 | # Storage implementation | |
212 | ||
213 | sub parse_volname { | |
214 | my ($class, $volname) = @_; | |
215 | ||
216 | parse_lvm_name($volname); | |
217 | ||
218 | if ($volname =~ m/^(vm-(\d+)-\S+)$/) { | |
219 | return ('images', $1, $2); | |
220 | } | |
221 | ||
222 | die "unable to parse lvm volume name '$volname'\n"; | |
223 | } | |
224 | ||
225 | sub path { | |
226 | my ($class, $scfg, $volname) = @_; | |
227 | ||
228 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
229 | ||
230 | my $vg = $scfg->{vgname}; | |
231 | ||
232 | my $path = "/dev/$vg/$name"; | |
233 | ||
5521b580 | 234 | return wantarray ? ($path, $vmid, $vtype) : $path; |
1dc01b9f DM |
235 | } |
236 | ||
237 | sub alloc_image { | |
238 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
239 | ||
240 | die "unsupported format '$fmt'" if $fmt ne 'raw'; | |
241 | ||
242 | die "illegal name '$name' - sould be 'vm-$vmid-*'\n" | |
243 | if $name && $name !~ m/^vm-$vmid-/; | |
244 | ||
245 | my $vgs = lvm_vgs(); | |
246 | ||
247 | my $vg = $scfg->{vgname}; | |
248 | ||
249 | die "no such volume gruoup '$vg'\n" if !defined ($vgs->{$vg}); | |
250 | ||
251 | my $free = int($vgs->{$vg}->{free}); | |
252 | ||
253 | die "not enough free space ($free < $size)\n" if $free < $size; | |
254 | ||
255 | if (!$name) { | |
256 | my $lvs = lvm_lvs($vg); | |
257 | ||
258 | for (my $i = 1; $i < 100; $i++) { | |
259 | my $tn = "vm-$vmid-disk-$i"; | |
260 | if (!defined ($lvs->{$vg}->{$tn})) { | |
261 | $name = $tn; | |
262 | last; | |
263 | } | |
264 | } | |
265 | } | |
266 | ||
267 | die "unable to allocate an image name for VM $vmid in storage '$storeid'\n" | |
268 | if !$name; | |
269 | ||
270 | my $cmd = ['/sbin/lvcreate', '-aly', '--addtag', "pve-vm-$vmid", '--size', "${size}k", '--name', $name, $vg]; | |
271 | ||
272 | run_command($cmd, errmsg => "lvcreate '$vg/pve-vm-$vmid' error"); | |
273 | ||
274 | return $name; | |
275 | } | |
276 | ||
277 | sub free_image { | |
278 | my ($class, $storeid, $scfg, $volname) = @_; | |
279 | ||
280 | my $vg = $scfg->{vgname}; | |
281 | ||
282 | # we need to zero out LVM data for security reasons | |
283 | # and to allow thin provisioning | |
284 | ||
285 | my $zero_out_worker = sub { | |
286 | print "zero-out data on image $volname\n"; | |
287 | my $cmd = ['dd', "if=/dev/zero", "of=/dev/$vg/del-$volname", "bs=1M"]; | |
288 | eval { run_command($cmd, errmsg => "zero out failed"); }; | |
289 | warn $@ if $@; | |
290 | ||
291 | $class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
292 | my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"]; | |
293 | run_command($cmd, errmsg => "lvremove '$vg/del-$volname' error"); | |
294 | }); | |
295 | print "successfully removed volume $volname\n"; | |
296 | }; | |
297 | ||
298 | if ($scfg->{saferemove}) { | |
299 | # avoid long running task, so we only rename here | |
300 | my $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"]; | |
301 | run_command($cmd, errmsg => "lvrename '$vg/$volname' error"); | |
302 | return $zero_out_worker; | |
303 | } else { | |
304 | my $tmpvg = $scfg->{vgname}; | |
305 | my $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"]; | |
306 | run_command($cmd, errmsg => "lvremove '$tmpvg/$volname' error"); | |
307 | } | |
308 | ||
309 | return undef; | |
310 | } | |
311 | ||
312 | sub list_images { | |
313 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
314 | ||
315 | my $vgname = $scfg->{vgname}; | |
316 | ||
317 | $cache->{lvs} = lvm_lvs() if !$cache->{lvs}; | |
318 | ||
319 | my $res = []; | |
320 | ||
321 | if (my $dat = $cache->{lvs}->{$vgname}) { | |
322 | ||
323 | foreach my $volname (keys %$dat) { | |
324 | ||
325 | my $owner = $dat->{$volname}->{vmid}; | |
326 | ||
327 | my $volid = "$storeid:$volname"; | |
328 | ||
329 | if ($vollist) { | |
330 | my $found = grep { $_ eq $volid } @$vollist; | |
331 | next if !$found; | |
332 | } else { | |
333 | next if defined ($vmid) && ($owner ne $vmid); | |
334 | } | |
335 | ||
336 | my $info = $dat->{$volname}; | |
337 | $info->{volid} = $volid; | |
338 | ||
339 | push @$res, $info; | |
340 | } | |
341 | } | |
342 | ||
343 | return $res; | |
344 | } | |
345 | ||
346 | sub status { | |
347 | my ($class, $storeid, $scfg, $cache) = @_; | |
348 | ||
349 | $cache->{vgs} = lvm_vgs() if !$cache->{vgs}; | |
350 | ||
351 | my $vgname = $scfg->{vgname}; | |
352 | ||
353 | my $total = 0; | |
354 | my $free = 0; | |
355 | my $used = 0; | |
356 | ||
357 | if (my $info = $cache->{vgs}->{$vgname}) { | |
358 | return ($info->{size}, $info->{free}, $total - $free, 1); | |
359 | } | |
360 | ||
361 | return undef; | |
362 | } | |
363 | ||
364 | sub activate_storage { | |
365 | my ($class, $storeid, $scfg, $cache) = @_; | |
366 | ||
367 | $cache->{vgs} = lvm_vgs() if !$cache->{vgs}; | |
368 | ||
369 | # In LVM2, vgscans take place automatically; | |
370 | # this is just to be sure | |
371 | if ($cache->{vgs} && !$cache->{vgscaned} && | |
372 | !$cache->{vgs}->{$scfg->{vgname}}) { | |
373 | $cache->{vgscaned} = 1; | |
374 | my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes']; | |
375 | eval { run_command($cmd, outfunc => sub {}); }; | |
376 | warn $@ if $@; | |
377 | } | |
378 | ||
379 | # we do not acticate any volumes here ('vgchange -aly') | |
380 | # instead, volumes are activate individually later | |
381 | } | |
382 | ||
383 | sub deactivate_storage { | |
384 | my ($class, $storeid, $scfg, $cache) = @_; | |
385 | ||
386 | my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname}]; | |
387 | run_command($cmd, errmsg => "can't deactivate VG '$scfg->{vgname}'"); | |
388 | } | |
389 | ||
390 | sub activate_volume { | |
391 | my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; | |
392 | ||
393 | my $path = $class->path($scfg, $volname); | |
394 | ||
395 | my $lvm_activate_mode = $exclusive ? 'ey' : 'ly'; | |
396 | ||
397 | my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path]; | |
398 | run_command($cmd, errmsg => "can't activate LV '$path'"); | |
399 | } | |
400 | ||
401 | sub deactivate_volume { | |
402 | my ($class, $storeid, $scfg, $volname, $cache) = @_; | |
403 | ||
404 | my $path = $class->path($scfg, $volname); | |
405 | return if ! -b $path; | |
406 | ||
407 | my $cmd = ['/sbin/lvchange', '-aln', $path]; | |
408 | run_command($cmd, errmsg => "can't deactivate LV '$path'"); | |
409 | } | |
410 | ||
530defb6 AD |
411 | sub volume_resize { |
412 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
413 | ||
414 | $size = ($size/1024/1024) . "M"; | |
415 | ||
416 | my $path = $class->path($scfg, $volname); | |
417 | my $cmd = ['/sbin/lvextend', '-L', $size, $path]; | |
418 | run_command($cmd, errmsg => "error resizing volume '$path'"); | |
419 | ||
420 | return 1; | |
421 | } | |
422 | ||
33818d16 AD |
423 | sub volume_snapshot { |
424 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
425 | ||
426 | die "lvm snapshot is not implemented"; | |
427 | } | |
428 | ||
051e85b8 AD |
429 | sub volume_snapshot_rollback { |
430 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
431 | ||
432 | die "lvm snapshot rollback is not implemented"; | |
433 | } | |
434 | ||
f57e796b AD |
435 | sub volume_snapshot_delete { |
436 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
437 | ||
438 | die "lvm snapshot delete is not implemented"; | |
439 | } | |
440 | ||
1dc01b9f | 441 | 1; |