]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/LVMPlugin.pm
f02c110d4d82f7f8a8dbd90e9d0b17c6b1737ac1
1 package PVE
::Storage
::LVMPlugin
;
8 use PVE
::Tools
qw(run_command trim);
9 use PVE
::Storage
::Plugin
;
10 use PVE
::JSONSchema
qw(get_standard_option);
12 use base
qw(PVE::Storage::Plugin);
14 # lvm helper functions
16 my $ignore_no_medium_warnings = sub {
18 # ignore those, most of the time they're from (virtual) IPMI/iKVM devices
19 # and just spam the log..
20 if ($line !~ /open failed: No medium found/) {
21 print STDERR
"$line\n";
28 die "no device specified" if !$device;
32 my $cmd = ['/usr/bin/file', '-L', '-s', $device];
33 run_command
($cmd, outfunc
=> sub {
35 $has_label = 1 if $line =~ m/LVM2/;
38 return undef if !$has_label;
40 $cmd = ['/sbin/pvs', '--separator', ':', '--noheadings', '--units', 'k',
41 '--unbuffered', '--nosuffix', '--options',
42 'pv_name,pv_size,vg_name,pv_uuid', $device];
45 run_command
($cmd, outfunc
=> sub {
50 my ($pvname, $size, $vgname, $uuid) = split(':', $line);
52 die "found multiple pvs entries for device '$device'\n"
66 sub clear_first_sector
{
69 if (my $fh = IO
::File-
>new($dev, "w")) {
76 sub lvm_create_volume_group
{
77 my ($device, $vgname, $shared) = @_;
79 my $res = lvm_pv_info
($device);
82 return if $res->{vgname
} eq $vgname; # already created
83 die "device '$device' is already used by volume group '$res->{vgname}'\n";
86 clear_first_sector
($device); # else pvcreate fails
88 # we use --metadatasize 250k, which reseults in "pe_start = 512"
89 # so pe_start is aligned on a 128k boundary (advantage for SSDs)
90 my $cmd = ['/sbin/pvcreate', '--metadatasize', '250k', $device];
92 run_command
($cmd, errmsg
=> "pvcreate '$device' error");
94 $cmd = ['/sbin/vgcreate', $vgname, $device];
95 # push @$cmd, '-c', 'y' if $shared; # we do not use this yet
97 run_command
($cmd, errmsg
=> "vgcreate $vgname $device error", errfunc
=> $ignore_no_medium_warnings, outfunc
=> $ignore_no_medium_warnings);
101 my ($includepvs) = @_;
103 my $cmd = ['/sbin/vgs', '--separator', ':', '--noheadings', '--units', 'b',
104 '--unbuffered', '--nosuffix', '--options'];
106 my $cols = [qw(vg_name vg_size vg_free lv_count)];
109 push @$cols, qw(pv_name pv_size pv_free);
112 push @$cmd, join(',', @$cols);
116 run_command
($cmd, outfunc
=> sub {
120 my ($name, $size, $free, $lvcount, $pvname, $pvsize, $pvfree) = split (':', $line);
125 lvcount
=> int($lvcount)
128 if (defined($pvname) && defined($pvsize) && defined($pvfree)) {
129 push @{$vgs->{$name}->{pvs
}}, {
131 size
=> int($pvsize),
132 free
=> int($pvfree),
136 errfunc
=> $ignore_no_medium_warnings,
141 # just warn (vgs return error code 5 if clvmd does not run)
142 # but output is still OK (list without clustered VGs)
148 sub lvm_list_volumes
{
151 my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
152 '--unbuffered', '--nosuffix', '--options',
153 'vg_name,lv_name,lv_size,lv_attr,pool_lv,data_percent,metadata_percent,snap_percent,uuid,tags,metadata_size'];
155 push @$cmd, $vgname if $vgname;
158 run_command
($cmd, outfunc
=> sub {
163 my ($vg_name, $lv_name, $lv_size, $lv_attr, $pool_lv, $data_percent, $meta_percent, $snap_percent, $uuid, $tags, $meta_size) = split(':', $line);
167 my $lv_type = substr($lv_attr, 0, 1);
170 lv_size
=> int($lv_size),
173 $d->{pool_lv
} = $pool_lv if $pool_lv;
174 $d->{tags
} = $tags if $tags;
176 if ($lv_type eq 't') {
180 $d->{metadata_size
} = int($meta_size);
181 $d->{metadata_used
} = int(($meta_percent * $meta_size)/100);
182 $d->{used
} = int(($data_percent * $lv_size)/100);
184 $lvs->{$vg_name}->{$lv_name} = $d;
186 errfunc
=> $ignore_no_medium_warnings,
200 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
207 description
=> "Volume group name.",
208 type
=> 'string', format
=> 'pve-storage-vgname',
211 description
=> "Base volume. This volume is automatically activated.",
212 type
=> 'string', format
=> 'pve-volume-id',
215 description
=> "Zero-out data when removing LVs.",
218 saferemove_throughput
=> {
219 description
=> "Wipe throughput (cstream -t parameter value).",
223 description
=> "Only use logical volumes tagged with 'pve-vm-ID'.",
231 vgname
=> { fixed
=> 1 },
232 nodes
=> { optional
=> 1 },
233 shared
=> { optional
=> 1 },
234 disable
=> { optional
=> 1 },
235 saferemove
=> { optional
=> 1 },
236 saferemove_throughput
=> { optional
=> 1 },
237 content
=> { optional
=> 1 },
238 base
=> { fixed
=> 1, optional
=> 1 },
239 tagged_only
=> { optional
=> 1 },
240 bwlimit
=> { optional
=> 1 },
244 # Storage implementation
247 my ($class, $storeid, $scfg, %param) = @_;
249 if (my $base = $scfg->{base
}) {
250 my ($baseid, $volname) = PVE
::Storage
::parse_volume_id
($base);
252 my $cfg = PVE
::Storage
::config
();
253 my $basecfg = PVE
::Storage
::storage_config
($cfg, $baseid, 1);
254 die "base storage ID '$baseid' does not exist\n" if !$basecfg;
256 # we only support iscsi for now
257 die "unsupported base type '$basecfg->{type}'"
258 if $basecfg->{type
} ne 'iscsi';
260 my $path = PVE
::Storage
::path
($cfg, $base);
262 PVE
::Storage
::activate_storage
($cfg, $baseid);
264 lvm_create_volume_group
($path, $scfg->{vgname
}, $scfg->{shared
});
269 my ($class, $volname) = @_;
271 PVE
::Storage
::Plugin
::parse_lvm_name
($volname);
273 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
274 return ('images', $1, $2, undef, undef, undef, 'raw');
277 die "unable to parse lvm volume name '$volname'\n";
280 sub filesystem_path
{
281 my ($class, $scfg, $volname, $snapname) = @_;
283 die "lvm snapshot is not implemented"if defined($snapname);
285 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
287 my $vg = $scfg->{vgname
};
289 my $path = "/dev/$vg/$name";
291 return wantarray ?
($path, $vmid, $vtype) : $path;
295 my ($class, $storeid, $scfg, $volname) = @_;
297 die "can't create base images in lvm storage\n";
301 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
303 die "can't clone images in lvm storage\n";
306 sub find_free_diskname
{
307 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
309 my $vg = $scfg->{vgname
};
311 my $lvs = lvm_list_volumes
($vg);
313 my $disk_list = [ keys %{$lvs->{$vg}} ];
315 return PVE
::Storage
::Plugin
::get_next_vm_diskname
($disk_list, $storeid, $vmid, undef, $scfg);
319 my ($vg, $name, $size, $tags) = @_;
321 if ($size =~ m/\d$/) { # no unit is given
322 $size .= "k"; # default to kilobytes
325 my $cmd = ['/sbin/lvcreate', '-aly', '--size', $size, '--name', $name];
326 for my $tag (@$tags) {
327 push @$cmd, '--addtag', $tag;
331 run_command
($cmd, errmsg
=> "lvcreate '$vg/$name' error");
335 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
337 die "unsupported format '$fmt'" if $fmt ne 'raw';
339 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
340 if $name && $name !~ m/^vm-$vmid-/;
344 my $vg = $scfg->{vgname
};
346 die "no such volume group '$vg'\n" if !defined ($vgs->{$vg});
348 my $free = int($vgs->{$vg}->{free
});
350 die "not enough free space ($free < $size)\n" if $free < $size;
352 $name = $class->find_free_diskname($storeid, $scfg, $vmid)
355 lvcreate
($vg, $name, $size, ["pve-vm-$vmid"]);
361 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
363 my $vg = $scfg->{vgname
};
365 # we need to zero out LVM data for security reasons
366 # and to allow thin provisioning
368 my $zero_out_worker = sub {
369 print "zero-out data on image $volname (/dev/$vg/del-$volname)\n";
371 # wipe throughput up to 10MB/s by default; may be overwritten with saferemove_throughput
372 my $throughput = '-10485760';
373 if ($scfg->{saferemove_throughput
}) {
374 $throughput = $scfg->{saferemove_throughput
};
380 '-o', "/dev/$vg/del-$volname",
386 eval { run_command
($cmd, errmsg
=> "zero out finished (note: 'No space left on device' is ok here)"); };
389 $class->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
390 my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"];
391 run_command
($cmd, errmsg
=> "lvremove '$vg/del-$volname' error");
393 print "successfully removed volume $volname ($vg/del-$volname)\n";
396 my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"];
397 run_command
($cmd, errmsg
=> "can't activate LV '$vg/$volname' to zero-out its data");
398 $cmd = ['/sbin/lvchange', '--refresh', "$vg/$volname"];
399 run_command
($cmd, errmsg
=> "can't refresh LV '$vg/$volname' to zero-out its data");
401 if ($scfg->{saferemove
}) {
402 # avoid long running task, so we only rename here
403 $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"];
404 run_command
($cmd, errmsg
=> "lvrename '$vg/$volname' error");
405 return $zero_out_worker;
407 my $tmpvg = $scfg->{vgname
};
408 $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"];
409 run_command
($cmd, errmsg
=> "lvremove '$tmpvg/$volname' error");
415 my $check_tags = sub {
418 return defined($tags) && $tags =~ /(^|,)pve-vm-\d+(,|$)/;
422 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
424 my $vgname = $scfg->{vgname
};
426 $cache->{lvs
} = lvm_list_volumes
() if !$cache->{lvs
};
430 if (my $dat = $cache->{lvs
}->{$vgname}) {
432 foreach my $volname (keys %$dat) {
434 next if $volname !~ m/^vm-(\d+)-/;
437 my $info = $dat->{$volname};
439 next if $scfg->{tagged_only
} && !&$check_tags($info->{tags
});
441 next if $info->{lv_type
} ne '-';
443 my $volid = "$storeid:$volname";
446 my $found = grep { $_ eq $volid } @$vollist;
449 next if defined($vmid) && ($owner ne $vmid);
453 volid
=> $volid, format
=> 'raw', size
=> $info->{lv_size
}, vmid
=> $owner,
462 my ($class, $storeid, $scfg, $cache) = @_;
464 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
466 my $vgname = $scfg->{vgname
};
468 if (my $info = $cache->{vgs
}->{$vgname}) {
469 return ($info->{size
}, $info->{free
}, $info->{size
} - $info->{free
}, 1);
475 sub activate_storage
{
476 my ($class, $storeid, $scfg, $cache) = @_;
478 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
480 # In LVM2, vgscans take place automatically;
481 # this is just to be sure
482 if ($cache->{vgs
} && !$cache->{vgscaned
} &&
483 !$cache->{vgs
}->{$scfg->{vgname
}}) {
484 $cache->{vgscaned
} = 1;
485 my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
486 eval { run_command
($cmd, outfunc
=> sub {}); };
490 # we do not acticate any volumes here ('vgchange -aly')
491 # instead, volumes are activate individually later
494 sub deactivate_storage
{
495 my ($class, $storeid, $scfg, $cache) = @_;
497 my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname
}];
498 run_command
($cmd, errmsg
=> "can't deactivate VG '$scfg->{vgname}'");
501 sub activate_volume
{
502 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
503 #fix me lvmchange is not provided on
504 my $path = $class->path($scfg, $volname, $snapname);
506 my $lvm_activate_mode = 'ey';
508 my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path];
509 run_command
($cmd, errmsg
=> "can't activate LV '$path'");
510 $cmd = ['/sbin/lvchange', '--refresh', $path];
511 run_command
($cmd, errmsg
=> "can't refresh LV '$path' for activation");
514 sub deactivate_volume
{
515 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
517 my $path = $class->path($scfg, $volname, $snapname);
518 return if ! -b
$path;
520 my $cmd = ['/sbin/lvchange', '-aln', $path];
521 run_command
($cmd, errmsg
=> "can't deactivate LV '$path'");
525 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
527 $size = ($size/1024/1024) . "M";
529 my $path = $class->path($scfg, $volname);
530 my $cmd = ['/sbin/lvextend', '-L', $size, $path];
532 $class->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
533 run_command
($cmd, errmsg
=> "error resizing volume '$path'");
539 sub volume_size_info
{
540 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
541 my $path = $class->filesystem_path($scfg, $volname);
543 my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
544 '--unbuffered', '--nosuffix', '--options', 'lv_size', $path];
547 run_command
($cmd, timeout
=> $timeout, errmsg
=> "can't get size of '$path'",
551 return wantarray ?
($size, 'raw', 0, undef) : $size;
554 sub volume_snapshot
{
555 my ($class, $scfg, $storeid, $volname, $snap) = @_;
557 die "lvm snapshot is not implemented";
560 sub volume_snapshot_rollback
{
561 my ($class, $scfg, $storeid, $volname, $snap) = @_;
563 die "lvm snapshot rollback is not implemented";
566 sub volume_snapshot_delete
{
567 my ($class, $scfg, $storeid, $volname, $snap) = @_;
569 die "lvm snapshot delete is not implemented";
572 sub volume_has_feature
{
573 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
576 copy
=> { base
=> 1, current
=> 1},
579 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
580 $class->parse_volname($volname);
586 $key = $isBase ?
'base' : 'current';
588 return 1 if $features->{$feature}->{$key};
593 sub volume_export_formats
{
594 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
595 return () if defined($snapshot); # lvm-thin only
596 return volume_import_formats
($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots);
600 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
601 die "volume export format $format not available for $class\n"
602 if $format ne 'raw+size';
603 die "cannot export volumes together with their snapshots in $class\n"
605 die "cannot export a snapshot in $class\n" if defined($snapshot);
606 die "cannot export an incremental stream in $class\n" if defined($base_snapshot);
607 my $file = $class->path($scfg, $volname, $storeid);
609 # should be faster than querying LVM, also checks for the device file's availability
610 run_command
(['/sbin/blockdev', '--getsize64', $file], outfunc
=> sub {
612 die "unexpected output from /sbin/blockdev: $line\n" if $line !~ /^(\d+)$/;
615 PVE
::Storage
::Plugin
::write_common_header
($fh, $size);
616 run_command
(['dd', "if=$file", "bs=64k"], output
=> '>&'.fileno($fh));
619 sub volume_import_formats
{
620 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
621 return () if $with_snapshots; # not supported
622 return () if defined($base_snapshot); # not supported
627 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
628 die "volume import format $format not available for $class\n"
629 if $format ne 'raw+size';
630 die "cannot import volumes together with their snapshots in $class\n"
632 die "cannot import an incremental stream in $class\n" if defined($base_snapshot);
634 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
635 $class->parse_volname($volname);
636 die "cannot import format $format into a file of format $file_format\n"
637 if $file_format ne 'raw';
639 my $vg = $scfg->{vgname
};
640 my $lvs = lvm_list_volumes
($vg);
641 die "volume $vg/$volname already exists\n"
642 if $lvs->{$vg}->{$volname};
644 my ($size) = PVE
::Storage
::Plugin
::read_common_header
($fh);
645 $size = int($size/1024);
648 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, 'raw', $name, $size);
649 if ($allocname ne $volname) {
650 my $oldname = $volname;
651 $volname = $allocname; # Let the cleanup code know what to free
652 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
654 my $file = $class->path($scfg, $volname, $storeid)
655 or die "internal error: failed to get path to newly allocated volume $volname\n";
657 $class->volume_import_write($fh, $file);
660 eval { $class->free_image($storeid, $scfg, $volname, 0) };
666 sub volume_import_write
{
667 my ($class, $input_fh, $output_file) = @_;
668 run_command
(['dd', "of=$output_file", 'bs=64k'],
669 input
=> '<&'.fileno($input_fh));