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