]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/LVMPlugin.pm
Don't remove and recreate lun when changing a volume
[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, $scfg) = @_;
293
294 my $disk_list = [ keys %{$lvs->{$vg}} ];
295
296 return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, undef, $scfg);
297 }
298
299 sub lvcreate {
300 my ($vg, $name, $size, $tags) = @_;
301
302 if ($size =~ m/\d$/) { # no unit is given
303 $size .= "k"; # default to kilobytes
304 }
305
306 my $cmd = ['/sbin/lvcreate', '-aly', '--size', $size, '--name', $name];
307 for my $tag (@$tags) {
308 push @$cmd, '--addtag', $tag;
309 }
310 push @$cmd, $vg;
311
312 run_command($cmd, errmsg => "lvcreate '$vg/$name' error");
313 }
314
315 sub alloc_image {
316 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
317
318 die "unsupported format '$fmt'" if $fmt ne 'raw';
319
320 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
321 if $name && $name !~ m/^vm-$vmid-/;
322
323 my $vgs = lvm_vgs();
324
325 my $vg = $scfg->{vgname};
326
327 die "no such volume group '$vg'\n" if !defined ($vgs->{$vg});
328
329 my $free = int($vgs->{$vg}->{free});
330
331 die "not enough free space ($free < $size)\n" if $free < $size;
332
333 $name = lvm_find_free_diskname(lvm_list_volumes($vg), $vg, $storeid, $vmid, $scfg)
334 if !$name;
335
336 lvcreate($vg, $name, $size, ["pve-vm-$vmid"]);
337
338 return $name;
339 }
340
341 sub free_image {
342 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
343
344 my $vg = $scfg->{vgname};
345
346 # we need to zero out LVM data for security reasons
347 # and to allow thin provisioning
348
349 my $zero_out_worker = sub {
350 print "zero-out data on image $volname (/dev/$vg/del-$volname)\n";
351
352 # wipe throughput up to 10MB/s by default; may be overwritten with saferemove_throughput
353 my $throughput = '-10485760';
354 if ($scfg->{saferemove_throughput}) {
355 $throughput = $scfg->{saferemove_throughput};
356 }
357
358 my $cmd = [
359 '/usr/bin/cstream',
360 '-i', '/dev/zero',
361 '-o', "/dev/$vg/del-$volname",
362 '-T', '10',
363 '-v', '1',
364 '-b', '1048576',
365 '-t', "$throughput"
366 ];
367 eval { run_command($cmd, errmsg => "zero out finished (note: 'No space left on device' is ok here)"); };
368 warn $@ if $@;
369
370 $class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
371 my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"];
372 run_command($cmd, errmsg => "lvremove '$vg/del-$volname' error");
373 });
374 print "successfully removed volume $volname ($vg/del-$volname)\n";
375 };
376
377 my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"];
378 run_command($cmd, errmsg => "can't activate LV '$vg/$volname' to zero-out its data");
379 $cmd = ['/sbin/lvchange', '--refresh', "$vg/$volname"];
380 run_command($cmd, errmsg => "can't refresh LV '$vg/$volname' to zero-out its data");
381
382 if ($scfg->{saferemove}) {
383 # avoid long running task, so we only rename here
384 $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"];
385 run_command($cmd, errmsg => "lvrename '$vg/$volname' error");
386 return $zero_out_worker;
387 } else {
388 my $tmpvg = $scfg->{vgname};
389 $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"];
390 run_command($cmd, errmsg => "lvremove '$tmpvg/$volname' error");
391 }
392
393 return undef;
394 }
395
396 my $check_tags = sub {
397 my ($tags) = @_;
398
399 return defined($tags) && $tags =~ /(^|,)pve-vm-\d+(,|$)/;
400 };
401
402 sub list_images {
403 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
404
405 my $vgname = $scfg->{vgname};
406
407 $cache->{lvs} = lvm_list_volumes() if !$cache->{lvs};
408
409 my $res = [];
410
411 if (my $dat = $cache->{lvs}->{$vgname}) {
412
413 foreach my $volname (keys %$dat) {
414
415 next if $volname !~ m/^vm-(\d+)-/;
416 my $owner = $1;
417
418 my $info = $dat->{$volname};
419
420 next if $scfg->{tagged_only} && !&$check_tags($info->{tags});
421
422 next if $info->{lv_type} ne '-';
423
424 my $volid = "$storeid:$volname";
425
426 if ($vollist) {
427 my $found = grep { $_ eq $volid } @$vollist;
428 next if !$found;
429 } else {
430 next if defined($vmid) && ($owner ne $vmid);
431 }
432
433 push @$res, {
434 volid => $volid, format => 'raw', size => $info->{lv_size}, vmid => $owner,
435 };
436 }
437 }
438
439 return $res;
440 }
441
442 sub status {
443 my ($class, $storeid, $scfg, $cache) = @_;
444
445 $cache->{vgs} = lvm_vgs() if !$cache->{vgs};
446
447 my $vgname = $scfg->{vgname};
448
449 if (my $info = $cache->{vgs}->{$vgname}) {
450 return ($info->{size}, $info->{free}, $info->{size} - $info->{free}, 1);
451 }
452
453 return undef;
454 }
455
456 sub activate_storage {
457 my ($class, $storeid, $scfg, $cache) = @_;
458
459 $cache->{vgs} = lvm_vgs() if !$cache->{vgs};
460
461 # In LVM2, vgscans take place automatically;
462 # this is just to be sure
463 if ($cache->{vgs} && !$cache->{vgscaned} &&
464 !$cache->{vgs}->{$scfg->{vgname}}) {
465 $cache->{vgscaned} = 1;
466 my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
467 eval { run_command($cmd, outfunc => sub {}); };
468 warn $@ if $@;
469 }
470
471 # we do not acticate any volumes here ('vgchange -aly')
472 # instead, volumes are activate individually later
473 }
474
475 sub deactivate_storage {
476 my ($class, $storeid, $scfg, $cache) = @_;
477
478 my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname}];
479 run_command($cmd, errmsg => "can't deactivate VG '$scfg->{vgname}'");
480 }
481
482 sub activate_volume {
483 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
484 #fix me lvmchange is not provided on
485 my $path = $class->path($scfg, $volname, $snapname);
486
487 my $lvm_activate_mode = 'ey';
488
489 my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path];
490 run_command($cmd, errmsg => "can't activate LV '$path'");
491 $cmd = ['/sbin/lvchange', '--refresh', $path];
492 run_command($cmd, errmsg => "can't refresh LV '$path' for activation");
493 }
494
495 sub deactivate_volume {
496 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
497
498 my $path = $class->path($scfg, $volname, $snapname);
499 return if ! -b $path;
500
501 my $cmd = ['/sbin/lvchange', '-aln', $path];
502 run_command($cmd, errmsg => "can't deactivate LV '$path'");
503 }
504
505 sub volume_resize {
506 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
507
508 $size = ($size/1024/1024) . "M";
509
510 my $path = $class->path($scfg, $volname);
511 my $cmd = ['/sbin/lvextend', '-L', $size, $path];
512
513 $class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
514 run_command($cmd, errmsg => "error resizing volume '$path'");
515 });
516
517 return 1;
518 }
519
520 sub volume_size_info {
521 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
522 my $path = $class->filesystem_path($scfg, $volname);
523
524 my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
525 '--unbuffered', '--nosuffix', '--options', 'lv_size', $path];
526
527 my $size;
528 run_command($cmd, timeout => $timeout, errmsg => "can't get size of '$path'",
529 outfunc => sub {
530 $size = int(shift);
531 });
532 return wantarray ? ($size, 'raw', 0, undef) : $size;
533 }
534
535 sub volume_snapshot {
536 my ($class, $scfg, $storeid, $volname, $snap) = @_;
537
538 die "lvm snapshot is not implemented";
539 }
540
541 sub volume_snapshot_rollback {
542 my ($class, $scfg, $storeid, $volname, $snap) = @_;
543
544 die "lvm snapshot rollback is not implemented";
545 }
546
547 sub volume_snapshot_delete {
548 my ($class, $scfg, $storeid, $volname, $snap) = @_;
549
550 die "lvm snapshot delete is not implemented";
551 }
552
553 sub volume_has_feature {
554 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
555
556 my $features = {
557 copy => { base => 1, current => 1},
558 };
559
560 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
561 $class->parse_volname($volname);
562
563 my $key = undef;
564 if($snapname){
565 $key = 'snap';
566 }else{
567 $key = $isBase ? 'base' : 'current';
568 }
569 return 1 if $features->{$feature}->{$key};
570
571 return undef;
572 }
573
574 sub volume_export_formats {
575 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
576 return () if defined($snapshot); # lvm-thin only
577 return volume_import_formats($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots);
578 }
579
580 sub volume_export {
581 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
582 die "volume export format $format not available for $class\n"
583 if $format ne 'raw+size';
584 die "cannot export volumes together with their snapshots in $class\n"
585 if $with_snapshots;
586 die "cannot export a snapshot in $class\n" if defined($snapshot);
587 die "cannot export an incremental stream in $class\n" if defined($base_snapshot);
588 my $file = $class->path($scfg, $volname, $storeid);
589 my $size;
590 # should be faster than querying LVM, also checks for the device file's availability
591 run_command(['/sbin/blockdev', '--getsize64', $file], outfunc => sub {
592 my ($line) = @_;
593 die "unexpected output from /sbin/blockdev: $line\n" if $line !~ /^(\d+)$/;
594 $size = int($1);
595 });
596 PVE::Storage::Plugin::write_common_header($fh, $size);
597 run_command(['dd', "if=$file", "bs=64k"], output => '>&'.fileno($fh));
598 }
599
600 sub volume_import_formats {
601 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
602 return () if $with_snapshots; # not supported
603 return () if defined($base_snapshot); # not supported
604 return ('raw+size');
605 }
606
607 sub volume_import {
608 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
609 die "volume import format $format not available for $class\n"
610 if $format ne 'raw+size';
611 die "cannot import volumes together with their snapshots in $class\n"
612 if $with_snapshots;
613 die "cannot import an incremental stream in $class\n" if defined($base_snapshot);
614
615 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
616 $class->parse_volname($volname);
617 die "cannot import format $format into a file of format $file_format\n"
618 if $file_format ne 'raw';
619
620 my $vg = $scfg->{vgname};
621 my $lvs = lvm_list_volumes($vg);
622 die "volume $vg/$volname already exists\n"
623 if $lvs->{$vg}->{$volname};
624
625 my ($size) = PVE::Storage::Plugin::read_common_header($fh);
626 $size = int($size/1024);
627
628 eval {
629 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, 'raw', $name, $size);
630 if ($allocname ne $volname) {
631 my $oldname = $volname;
632 $volname = $allocname; # Let the cleanup code know what to free
633 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
634 }
635 my $file = $class->path($scfg, $volname, $storeid)
636 or die "internal error: failed to get path to newly allocated volume $volname\n";
637
638 $class->volume_import_write($fh, $file);
639 };
640 if (my $err = $@) {
641 eval { $class->free_image($storeid, $scfg, $volname, 0) };
642 warn $@ if $@;
643 die $err;
644 }
645 }
646
647 sub volume_import_write {
648 my ($class, $input_fh, $output_file) = @_;
649 run_command(['dd', "of=$output_file", 'bs=64k'],
650 input => '<&'.fileno($input_fh));
651 }
652
653 1;