]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/LVMPlugin.pm
lvmthin: fix used space (only use data%)
[pve-storage.git] / PVE / Storage / LVMPlugin.pm
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
144 if ($lv_type eq 't') {
145 $data_percent ||= 0;
146 $meta_percent ||= 0;
147 $snap_percent ||= 0;
148 $d->{used} = int(($data_percent * $lv_size)/100);
149 }
150 $lvs->{$vg_name}->{$lv_name} = $d;
151 });
152
153 return $lvs;
154 }
155
156 # Configuration
157
158 sub type {
159 return 'lvm';
160 }
161
162 sub plugindata {
163 return {
164 content => [ {images => 1, rootdir => 1}, { images => 1 }],
165 };
166 }
167
168 sub properties {
169 return {
170 vgname => {
171 description => "Volume group name.",
172 type => 'string', format => 'pve-storage-vgname',
173 },
174 base => {
175 description => "Base volume. This volume is automatically activated.",
176 type => 'string', format => 'pve-volume-id',
177 },
178 saferemove => {
179 description => "Zero-out data when removing LVs.",
180 type => 'boolean',
181 },
182 saferemove_throughput => {
183 description => "Wipe throughput (cstream -t parameter value).",
184 type => 'string',
185 },
186 };
187 }
188
189 sub options {
190 return {
191 vgname => { fixed => 1 },
192 nodes => { optional => 1 },
193 shared => { optional => 1 },
194 disable => { optional => 1 },
195 saferemove => { optional => 1 },
196 saferemove_throughput => { optional => 1 },
197 content => { optional => 1 },
198 base => { fixed => 1, optional => 1 },
199 };
200 }
201
202 # Storage implementation
203
204 sub parse_volname {
205 my ($class, $volname) = @_;
206
207 PVE::Storage::Plugin::parse_lvm_name($volname);
208
209 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
210 return ('images', $1, $2, undef, undef, undef, 'raw');
211 }
212
213 die "unable to parse lvm volume name '$volname'\n";
214 }
215
216 sub filesystem_path {
217 my ($class, $scfg, $volname, $snapname) = @_;
218
219 die "lvm snapshot is not implemented"if defined($snapname);
220
221 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
222
223 my $vg = $scfg->{vgname};
224
225 my $path = "/dev/$vg/$name";
226
227 return wantarray ? ($path, $vmid, $vtype) : $path;
228 }
229
230 sub create_base {
231 my ($class, $storeid, $scfg, $volname) = @_;
232
233 die "can't create base images in lvm storage\n";
234 }
235
236 sub clone_image {
237 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
238
239 die "can't clone images in lvm storage\n";
240 }
241
242 sub lvm_find_free_diskname {
243 my ($lvs, $vg, $storeid, $vmid) = @_;
244
245 my $name;
246
247 for (my $i = 1; $i < 100; $i++) {
248 my $tn = "vm-$vmid-disk-$i";
249 if (!defined ($lvs->{$vg}->{$tn})) {
250 $name = $tn;
251 last;
252 }
253 }
254
255 die "unable to allocate an image name for ID $vmid in storage '$storeid'\n"
256 if !$name;
257
258 return $name;
259 }
260
261 sub alloc_image {
262 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
263
264 die "unsupported format '$fmt'" if $fmt ne 'raw';
265
266 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
267 if $name && $name !~ m/^vm-$vmid-/;
268
269 my $vgs = lvm_vgs();
270
271 my $vg = $scfg->{vgname};
272
273 die "no such volume group '$vg'\n" if !defined ($vgs->{$vg});
274
275 my $free = int($vgs->{$vg}->{free});
276
277 die "not enough free space ($free < $size)\n" if $free < $size;
278
279 $name = lvm_find_free_diskname(lvm_list_volumes($vg), $vg, $storeid, $vmid)
280 if !$name;
281
282 my $cmd = ['/sbin/lvcreate', '-aly', '--addtag', "pve-vm-$vmid", '--size', "${size}k", '--name', $name, $vg];
283
284 run_command($cmd, errmsg => "lvcreate '$vg/pve-vm-$vmid' error");
285
286 return $name;
287 }
288
289 sub free_image {
290 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
291
292 my $vg = $scfg->{vgname};
293
294 # we need to zero out LVM data for security reasons
295 # and to allow thin provisioning
296
297 my $zero_out_worker = sub {
298 print "zero-out data on image $volname (/dev/$vg/del-$volname)\n";
299
300 # wipe throughput up to 10MB/s by default; may be overwritten with saferemove_throughput
301 my $throughput = '-10485760';
302 if ($scfg->{saferemove_throughput}) {
303 $throughput = $scfg->{saferemove_throughput};
304 }
305
306 my $cmd = [
307 '/usr/bin/cstream',
308 '-i', '/dev/zero',
309 '-o', "/dev/$vg/del-$volname",
310 '-T', '10',
311 '-v', '1',
312 '-b', '1048576',
313 '-t', "$throughput"
314 ];
315 eval { run_command($cmd, errmsg => "zero out finished (note: 'No space left on device' is ok here)"); };
316 warn $@ if $@;
317
318 $class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
319 my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"];
320 run_command($cmd, errmsg => "lvremove '$vg/del-$volname' error");
321 });
322 print "successfully removed volume $volname ($vg/del-$volname)\n";
323 };
324
325 my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"];
326 run_command($cmd, errmsg => "can't activate LV '$vg/$volname' to zero-out its data");
327
328 if ($scfg->{saferemove}) {
329 # avoid long running task, so we only rename here
330 $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"];
331 run_command($cmd, errmsg => "lvrename '$vg/$volname' error");
332 return $zero_out_worker;
333 } else {
334 my $tmpvg = $scfg->{vgname};
335 $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"];
336 run_command($cmd, errmsg => "lvremove '$tmpvg/$volname' error");
337 }
338
339 return undef;
340 }
341
342 sub list_images {
343 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
344
345 my $vgname = $scfg->{vgname};
346
347 $cache->{lvs} = lvm_list_volumes() if !$cache->{lvs};
348
349 my $res = [];
350
351 if (my $dat = $cache->{lvs}->{$vgname}) {
352
353 foreach my $volname (keys %$dat) {
354
355 next if $volname !~ m/^vm-(\d+)-/;
356 my $owner = $1;
357
358 my $info = $dat->{$volname};
359
360 next if $info->{lv_type} ne '-';
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 push @$res, {
372 volid => $volid, format => 'raw', size => $info->{lv_size}, vmid => $owner,
373 };
374 }
375 }
376
377 return $res;
378 }
379
380 sub status {
381 my ($class, $storeid, $scfg, $cache) = @_;
382
383 $cache->{vgs} = lvm_vgs() if !$cache->{vgs};
384
385 my $vgname = $scfg->{vgname};
386
387 if (my $info = $cache->{vgs}->{$vgname}) {
388 return ($info->{size}, $info->{free}, $info->{size} - $info->{free}, 1);
389 }
390
391 return undef;
392 }
393
394 sub activate_storage {
395 my ($class, $storeid, $scfg, $cache) = @_;
396
397 $cache->{vgs} = lvm_vgs() if !$cache->{vgs};
398
399 # In LVM2, vgscans take place automatically;
400 # this is just to be sure
401 if ($cache->{vgs} && !$cache->{vgscaned} &&
402 !$cache->{vgs}->{$scfg->{vgname}}) {
403 $cache->{vgscaned} = 1;
404 my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
405 eval { run_command($cmd, outfunc => sub {}); };
406 warn $@ if $@;
407 }
408
409 # we do not acticate any volumes here ('vgchange -aly')
410 # instead, volumes are activate individually later
411 }
412
413 sub deactivate_storage {
414 my ($class, $storeid, $scfg, $cache) = @_;
415
416 my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname}];
417 run_command($cmd, errmsg => "can't deactivate VG '$scfg->{vgname}'");
418 }
419
420 sub activate_volume {
421 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
422 #fix me lvmchange is not provided on
423 my $path = $class->path($scfg, $volname, $snapname);
424
425 my $lvm_activate_mode = 'ey';
426
427 my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path];
428 run_command($cmd, errmsg => "can't activate LV '$path'");
429 }
430
431 sub deactivate_volume {
432 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
433
434 my $path = $class->path($scfg, $volname, $snapname);
435 return if ! -b $path;
436
437 my $cmd = ['/sbin/lvchange', '-aln', $path];
438 run_command($cmd, errmsg => "can't deactivate LV '$path'");
439 }
440
441 sub volume_resize {
442 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
443
444 $size = ($size/1024/1024) . "M";
445
446 my $path = $class->path($scfg, $volname);
447 my $cmd = ['/sbin/lvextend', '-L', $size, $path];
448 run_command($cmd, errmsg => "error resizing volume '$path'");
449
450 return 1;
451 }
452
453 sub volume_snapshot {
454 my ($class, $scfg, $storeid, $volname, $snap) = @_;
455
456 die "lvm snapshot is not implemented";
457 }
458
459 sub volume_snapshot_rollback {
460 my ($class, $scfg, $storeid, $volname, $snap) = @_;
461
462 die "lvm snapshot rollback is not implemented";
463 }
464
465 sub volume_snapshot_delete {
466 my ($class, $scfg, $storeid, $volname, $snap) = @_;
467
468 die "lvm snapshot delete is not implemented";
469 }
470
471 sub volume_has_feature {
472 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
473
474 my $features = {
475 copy => { base => 1, current => 1},
476 };
477
478 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
479 $class->parse_volname($volname);
480
481 my $key = undef;
482 if($snapname){
483 $key = 'snap';
484 }else{
485 $key = $isBase ? 'base' : 'current';
486 }
487 return 1 if $features->{$feature}->{$key};
488
489 return undef;
490 }
491
492 1;