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