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