]>
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 | }, | |
399ab2b6 PB |
196 | saferemove_throughput => { |
197 | description => "Wipe throughput (cstream -t parameter value).", | |
198 | type => 'string', | |
199 | }, | |
1dc01b9f DM |
200 | }; |
201 | } | |
202 | ||
203 | sub options { | |
204 | return { | |
205 | vgname => { fixed => 1 }, | |
206 | nodes => { optional => 1 }, | |
207 | shared => { optional => 1 }, | |
208 | disable => { optional => 1 }, | |
209 | saferemove => { optional => 1 }, | |
399ab2b6 | 210 | saferemove_throughput => { optional => 1 }, |
1dc01b9f DM |
211 | content => { optional => 1 }, |
212 | base => { fixed => 1, optional => 1 }, | |
213 | }; | |
214 | } | |
215 | ||
216 | # Storage implementation | |
217 | ||
218 | sub parse_volname { | |
219 | my ($class, $volname) = @_; | |
220 | ||
221 | parse_lvm_name($volname); | |
222 | ||
223 | if ($volname =~ m/^(vm-(\d+)-\S+)$/) { | |
7800e84d | 224 | return ('images', $1, $2, undef, undef, undef, 'raw'); |
1dc01b9f DM |
225 | } |
226 | ||
227 | die "unable to parse lvm volume name '$volname'\n"; | |
228 | } | |
229 | ||
452e3ee7 | 230 | sub filesystem_path { |
1dc01b9f DM |
231 | my ($class, $scfg, $volname) = @_; |
232 | ||
233 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
234 | ||
235 | my $vg = $scfg->{vgname}; | |
236 | ||
237 | my $path = "/dev/$vg/$name"; | |
238 | ||
5521b580 | 239 | return wantarray ? ($path, $vmid, $vtype) : $path; |
1dc01b9f DM |
240 | } |
241 | ||
5eab0272 DM |
242 | sub create_base { |
243 | my ($class, $storeid, $scfg, $volname) = @_; | |
244 | ||
245 | die "can't create base images in lvm storage\n"; | |
246 | } | |
247 | ||
248 | sub clone_image { | |
f236eaf8 | 249 | my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; |
5eab0272 DM |
250 | |
251 | die "can't clone images in lvm storage\n"; | |
252 | } | |
253 | ||
1dc01b9f DM |
254 | sub alloc_image { |
255 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
256 | ||
257 | die "unsupported format '$fmt'" if $fmt ne 'raw'; | |
258 | ||
259 | die "illegal name '$name' - sould be 'vm-$vmid-*'\n" | |
260 | if $name && $name !~ m/^vm-$vmid-/; | |
261 | ||
262 | my $vgs = lvm_vgs(); | |
263 | ||
264 | my $vg = $scfg->{vgname}; | |
265 | ||
e8acaa3c | 266 | die "no such volume group '$vg'\n" if !defined ($vgs->{$vg}); |
1dc01b9f DM |
267 | |
268 | my $free = int($vgs->{$vg}->{free}); | |
269 | ||
270 | die "not enough free space ($free < $size)\n" if $free < $size; | |
271 | ||
272 | if (!$name) { | |
273 | my $lvs = lvm_lvs($vg); | |
274 | ||
275 | for (my $i = 1; $i < 100; $i++) { | |
276 | my $tn = "vm-$vmid-disk-$i"; | |
277 | if (!defined ($lvs->{$vg}->{$tn})) { | |
278 | $name = $tn; | |
279 | last; | |
280 | } | |
281 | } | |
282 | } | |
283 | ||
284 | die "unable to allocate an image name for VM $vmid in storage '$storeid'\n" | |
285 | if !$name; | |
286 | ||
287 | my $cmd = ['/sbin/lvcreate', '-aly', '--addtag', "pve-vm-$vmid", '--size', "${size}k", '--name', $name, $vg]; | |
288 | ||
289 | run_command($cmd, errmsg => "lvcreate '$vg/pve-vm-$vmid' error"); | |
290 | ||
291 | return $name; | |
292 | } | |
293 | ||
294 | sub free_image { | |
32437ed2 | 295 | my ($class, $storeid, $scfg, $volname, $isBase) = @_; |
1dc01b9f DM |
296 | |
297 | my $vg = $scfg->{vgname}; | |
399ab2b6 | 298 | |
1dc01b9f DM |
299 | # we need to zero out LVM data for security reasons |
300 | # and to allow thin provisioning | |
301 | ||
302 | my $zero_out_worker = sub { | |
399ab2b6 PB |
303 | print "zero-out data on image $volname (/dev/$vg/del-$volname)\n"; |
304 | ||
305 | # wipe throughput up to 10MB/s by default; may be overwritten with saferemove_throughput | |
306 | my $throughput = '-10485760'; | |
307 | if ($scfg->{saferemove_throughput}) { | |
308 | $throughput = $scfg->{saferemove_throughput}; | |
309 | } | |
310 | ||
311 | my $cmd = [ | |
312 | '/usr/bin/cstream', | |
313 | '-i', '/dev/zero', | |
314 | '-o', "/dev/$vg/del-$volname", | |
315 | '-T', '10', | |
316 | '-v', '1', | |
317 | '-b', '1048576', | |
318 | '-t', "$throughput" | |
319 | ]; | |
320 | eval { run_command($cmd, errmsg => "zero out finished (note: 'No space left on device' is ok here)"); }; | |
1dc01b9f DM |
321 | warn $@ if $@; |
322 | ||
323 | $class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
324 | my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"]; | |
325 | run_command($cmd, errmsg => "lvremove '$vg/del-$volname' error"); | |
326 | }); | |
399ab2b6 | 327 | print "successfully removed volume $volname ($vg/del-$volname)\n"; |
1dc01b9f DM |
328 | }; |
329 | ||
399ab2b6 PB |
330 | my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"]; |
331 | run_command($cmd, errmsg => "can't activate LV '$vg/$volname' to zero-out its data"); | |
332 | ||
1dc01b9f DM |
333 | if ($scfg->{saferemove}) { |
334 | # avoid long running task, so we only rename here | |
399ab2b6 | 335 | $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"]; |
1dc01b9f DM |
336 | run_command($cmd, errmsg => "lvrename '$vg/$volname' error"); |
337 | return $zero_out_worker; | |
338 | } else { | |
339 | my $tmpvg = $scfg->{vgname}; | |
399ab2b6 | 340 | $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"]; |
1dc01b9f DM |
341 | run_command($cmd, errmsg => "lvremove '$tmpvg/$volname' error"); |
342 | } | |
343 | ||
344 | return undef; | |
345 | } | |
346 | ||
347 | sub list_images { | |
348 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
349 | ||
350 | my $vgname = $scfg->{vgname}; | |
351 | ||
352 | $cache->{lvs} = lvm_lvs() if !$cache->{lvs}; | |
353 | ||
354 | my $res = []; | |
355 | ||
356 | if (my $dat = $cache->{lvs}->{$vgname}) { | |
357 | ||
358 | foreach my $volname (keys %$dat) { | |
359 | ||
360 | my $owner = $dat->{$volname}->{vmid}; | |
361 | ||
362 | my $volid = "$storeid:$volname"; | |
363 | ||
364 | if ($vollist) { | |
365 | my $found = grep { $_ eq $volid } @$vollist; | |
366 | next if !$found; | |
367 | } else { | |
368 | next if defined ($vmid) && ($owner ne $vmid); | |
369 | } | |
370 | ||
371 | my $info = $dat->{$volname}; | |
372 | $info->{volid} = $volid; | |
373 | ||
374 | push @$res, $info; | |
375 | } | |
376 | } | |
377 | ||
378 | return $res; | |
379 | } | |
380 | ||
381 | sub status { | |
382 | my ($class, $storeid, $scfg, $cache) = @_; | |
383 | ||
384 | $cache->{vgs} = lvm_vgs() if !$cache->{vgs}; | |
385 | ||
386 | my $vgname = $scfg->{vgname}; | |
387 | ||
388 | my $total = 0; | |
389 | my $free = 0; | |
390 | my $used = 0; | |
391 | ||
392 | if (my $info = $cache->{vgs}->{$vgname}) { | |
393 | return ($info->{size}, $info->{free}, $total - $free, 1); | |
394 | } | |
395 | ||
396 | return undef; | |
397 | } | |
398 | ||
399 | sub activate_storage { | |
400 | my ($class, $storeid, $scfg, $cache) = @_; | |
401 | ||
402 | $cache->{vgs} = lvm_vgs() if !$cache->{vgs}; | |
403 | ||
404 | # In LVM2, vgscans take place automatically; | |
405 | # this is just to be sure | |
406 | if ($cache->{vgs} && !$cache->{vgscaned} && | |
407 | !$cache->{vgs}->{$scfg->{vgname}}) { | |
408 | $cache->{vgscaned} = 1; | |
409 | my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes']; | |
410 | eval { run_command($cmd, outfunc => sub {}); }; | |
411 | warn $@ if $@; | |
412 | } | |
413 | ||
414 | # we do not acticate any volumes here ('vgchange -aly') | |
415 | # instead, volumes are activate individually later | |
416 | } | |
417 | ||
418 | sub deactivate_storage { | |
419 | my ($class, $storeid, $scfg, $cache) = @_; | |
420 | ||
421 | my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname}]; | |
422 | run_command($cmd, errmsg => "can't deactivate VG '$scfg->{vgname}'"); | |
423 | } | |
424 | ||
425 | sub activate_volume { | |
426 | my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; | |
427 | ||
428 | my $path = $class->path($scfg, $volname); | |
429 | ||
430 | my $lvm_activate_mode = $exclusive ? 'ey' : 'ly'; | |
431 | ||
432 | my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path]; | |
433 | run_command($cmd, errmsg => "can't activate LV '$path'"); | |
434 | } | |
435 | ||
436 | sub deactivate_volume { | |
437 | my ($class, $storeid, $scfg, $volname, $cache) = @_; | |
438 | ||
439 | my $path = $class->path($scfg, $volname); | |
440 | return if ! -b $path; | |
441 | ||
442 | my $cmd = ['/sbin/lvchange', '-aln', $path]; | |
443 | run_command($cmd, errmsg => "can't deactivate LV '$path'"); | |
444 | } | |
445 | ||
530defb6 AD |
446 | sub volume_resize { |
447 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
448 | ||
449 | $size = ($size/1024/1024) . "M"; | |
450 | ||
451 | my $path = $class->path($scfg, $volname); | |
452 | my $cmd = ['/sbin/lvextend', '-L', $size, $path]; | |
453 | run_command($cmd, errmsg => "error resizing volume '$path'"); | |
454 | ||
455 | return 1; | |
456 | } | |
457 | ||
33818d16 | 458 | sub volume_snapshot { |
f5640e7d | 459 | my ($class, $scfg, $storeid, $volname, $snap) = @_; |
33818d16 AD |
460 | |
461 | die "lvm snapshot is not implemented"; | |
462 | } | |
463 | ||
051e85b8 AD |
464 | sub volume_snapshot_rollback { |
465 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
466 | ||
467 | die "lvm snapshot rollback is not implemented"; | |
468 | } | |
469 | ||
f57e796b AD |
470 | sub volume_snapshot_delete { |
471 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
472 | ||
473 | die "lvm snapshot delete is not implemented"; | |
474 | } | |
475 | ||
f7d4064f AD |
476 | sub volume_has_feature { |
477 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
478 | ||
9bb4abf6 AD |
479 | my $features = { |
480 | copy => { base => 1, current => 1}, | |
481 | }; | |
482 | ||
483 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
484 | $class->parse_volname($volname); | |
485 | ||
486 | my $key = undef; | |
487 | if($snapname){ | |
2c5a7097 | 488 | $key = 'snap'; |
9bb4abf6 AD |
489 | }else{ |
490 | $key = $isBase ? 'base' : 'current'; | |
491 | } | |
492 | return 1 if $features->{$feature}->{$key}; | |
493 | ||
f7d4064f AD |
494 | return undef; |
495 | } | |
496 | ||
1dc01b9f | 497 | 1; |