]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/LVMPlugin.pm
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);
100 sub lvm_destroy_volume_group
{
104 ['vgremove', '-y', $vgname],
105 errmsg
=> "unable to remove volume group $vgname",
106 errfunc
=> $ignore_no_medium_warnings,
107 outfunc
=> $ignore_no_medium_warnings,
112 my ($includepvs) = @_;
114 my $cmd = ['/sbin/vgs', '--separator', ':', '--noheadings', '--units', 'b',
115 '--unbuffered', '--nosuffix', '--options'];
117 my $cols = [qw(vg_name vg_size vg_free lv_count)];
120 push @$cols, qw(pv_name pv_size pv_free);
123 push @$cmd, join(',', @$cols);
127 run_command
($cmd, outfunc
=> sub {
131 my ($name, $size, $free, $lvcount, $pvname, $pvsize, $pvfree) = split (':', $line);
136 lvcount
=> int($lvcount)
139 if (defined($pvname) && defined($pvsize) && defined($pvfree)) {
140 push @{$vgs->{$name}->{pvs
}}, {
142 size
=> int($pvsize),
143 free
=> int($pvfree),
147 errfunc
=> $ignore_no_medium_warnings,
152 # just warn (vgs return error code 5 if clvmd does not run)
153 # but output is still OK (list without clustered VGs)
159 sub lvm_list_volumes
{
162 my $option_list = 'vg_name,lv_name,lv_size,lv_attr,pool_lv,data_percent,metadata_percent,snap_percent,uuid,tags,metadata_size,time';
165 '/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
166 '--unbuffered', '--nosuffix',
167 '--config', 'report/time_format="%s"',
168 '--options', $option_list,
171 push @$cmd, $vgname if $vgname;
174 run_command
($cmd, outfunc
=> sub {
179 my ($vg_name, $lv_name, $lv_size, $lv_attr, $pool_lv, $data_percent, $meta_percent, $snap_percent, $uuid, $tags, $meta_size, $ctime) = split(':', $line);
183 my $lv_type = substr($lv_attr, 0, 1);
186 lv_size
=> int($lv_size),
189 $d->{pool_lv
} = $pool_lv if $pool_lv;
190 $d->{tags
} = $tags if $tags;
191 $d->{ctime
} = $ctime;
193 if ($lv_type eq 't') {
197 $d->{metadata_size
} = int($meta_size);
198 $d->{metadata_used
} = int(($meta_percent * $meta_size)/100);
199 $d->{used
} = int(($data_percent * $lv_size)/100);
201 $lvs->{$vg_name}->{$lv_name} = $d;
203 errfunc
=> $ignore_no_medium_warnings,
217 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
224 description
=> "Volume group name.",
225 type
=> 'string', format
=> 'pve-storage-vgname',
228 description
=> "Base volume. This volume is automatically activated.",
229 type
=> 'string', format
=> 'pve-volume-id',
232 description
=> "Zero-out data when removing LVs.",
235 saferemove_throughput
=> {
236 description
=> "Wipe throughput (cstream -t parameter value).",
240 description
=> "Only use logical volumes tagged with 'pve-vm-ID'.",
248 vgname
=> { fixed
=> 1 },
249 nodes
=> { optional
=> 1 },
250 shared
=> { optional
=> 1 },
251 disable
=> { optional
=> 1 },
252 saferemove
=> { optional
=> 1 },
253 saferemove_throughput
=> { optional
=> 1 },
254 content
=> { optional
=> 1 },
255 base
=> { fixed
=> 1, optional
=> 1 },
256 tagged_only
=> { optional
=> 1 },
257 bwlimit
=> { optional
=> 1 },
261 # Storage implementation
264 my ($class, $storeid, $scfg, %param) = @_;
266 if (my $base = $scfg->{base
}) {
267 my ($baseid, $volname) = PVE
::Storage
::parse_volume_id
($base);
269 my $cfg = PVE
::Storage
::config
();
270 my $basecfg = PVE
::Storage
::storage_config
($cfg, $baseid, 1);
271 die "base storage ID '$baseid' does not exist\n" if !$basecfg;
273 # we only support iscsi for now
274 die "unsupported base type '$basecfg->{type}'"
275 if $basecfg->{type
} ne 'iscsi';
277 my $path = PVE
::Storage
::path
($cfg, $base);
279 PVE
::Storage
::activate_storage
($cfg, $baseid);
281 lvm_create_volume_group
($path, $scfg->{vgname
}, $scfg->{shared
});
288 my ($class, $volname) = @_;
290 PVE
::Storage
::Plugin
::parse_lvm_name
($volname);
292 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
293 return ('images', $1, $2, undef, undef, undef, 'raw');
296 die "unable to parse lvm volume name '$volname'\n";
299 sub filesystem_path
{
300 my ($class, $scfg, $volname, $snapname) = @_;
302 die "lvm snapshot is not implemented"if defined($snapname);
304 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
306 my $vg = $scfg->{vgname
};
308 my $path = "/dev/$vg/$name";
310 return wantarray ?
($path, $vmid, $vtype) : $path;
314 my ($class, $storeid, $scfg, $volname) = @_;
316 die "can't create base images in lvm storage\n";
320 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
322 die "can't clone images in lvm storage\n";
325 sub find_free_diskname
{
326 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
328 my $vg = $scfg->{vgname
};
330 my $lvs = lvm_list_volumes
($vg);
332 my $disk_list = [ keys %{$lvs->{$vg}} ];
334 return PVE
::Storage
::Plugin
::get_next_vm_diskname
($disk_list, $storeid, $vmid, undef, $scfg);
338 my ($vg, $name, $size, $tags) = @_;
340 if ($size =~ m/\d$/) { # no unit is given
341 $size .= "k"; # default to kilobytes
344 my $cmd = ['/sbin/lvcreate', '-aly', '-Wy', '--yes', '--size', $size, '--name', $name];
345 for my $tag (@$tags) {
346 push @$cmd, '--addtag', $tag;
350 run_command
($cmd, errmsg
=> "lvcreate '$vg/$name' error");
354 my ($vg, $oldname, $newname) = @_;
357 ['/sbin/lvrename', $vg, $oldname, $newname],
358 errmsg
=> "lvrename '${vg}/${oldname}' to '${newname}' error",
363 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
365 die "unsupported format '$fmt'" if $fmt ne 'raw';
367 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
368 if $name && $name !~ m/^vm-$vmid-/;
372 my $vg = $scfg->{vgname
};
374 die "no such volume group '$vg'\n" if !defined ($vgs->{$vg});
376 my $free = int($vgs->{$vg}->{free
});
378 die "not enough free space ($free < $size)\n" if $free < $size;
380 $name = $class->find_free_diskname($storeid, $scfg, $vmid)
383 lvcreate
($vg, $name, $size, ["pve-vm-$vmid"]);
389 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
391 my $vg = $scfg->{vgname
};
393 # we need to zero out LVM data for security reasons
394 # and to allow thin provisioning
396 my $zero_out_worker = sub {
397 print "zero-out data on image $volname (/dev/$vg/del-$volname)\n";
399 # wipe throughput up to 10MB/s by default; may be overwritten with saferemove_throughput
400 my $throughput = '-10485760';
401 if ($scfg->{saferemove_throughput
}) {
402 $throughput = $scfg->{saferemove_throughput
};
408 '-o', "/dev/$vg/del-$volname",
414 eval { run_command
($cmd, errmsg
=> "zero out finished (note: 'No space left on device' is ok here)"); };
417 $class->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
418 my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"];
419 run_command
($cmd, errmsg
=> "lvremove '$vg/del-$volname' error");
421 print "successfully removed volume $volname ($vg/del-$volname)\n";
424 my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"];
425 run_command
($cmd, errmsg
=> "can't activate LV '$vg/$volname' to zero-out its data");
426 $cmd = ['/sbin/lvchange', '--refresh', "$vg/$volname"];
427 run_command
($cmd, errmsg
=> "can't refresh LV '$vg/$volname' to zero-out its data");
429 if ($scfg->{saferemove
}) {
430 # avoid long running task, so we only rename here
431 $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"];
432 run_command
($cmd, errmsg
=> "lvrename '$vg/$volname' error");
433 return $zero_out_worker;
435 my $tmpvg = $scfg->{vgname
};
436 $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"];
437 run_command
($cmd, errmsg
=> "lvremove '$tmpvg/$volname' error");
443 my $check_tags = sub {
446 return defined($tags) && $tags =~ /(^|,)pve-vm-\d+(,|$)/;
450 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
452 my $vgname = $scfg->{vgname
};
454 $cache->{lvs
} = lvm_list_volumes
() if !$cache->{lvs
};
458 if (my $dat = $cache->{lvs
}->{$vgname}) {
460 foreach my $volname (keys %$dat) {
462 next if $volname !~ m/^vm-(\d+)-/;
465 my $info = $dat->{$volname};
467 next if $scfg->{tagged_only
} && !&$check_tags($info->{tags
});
469 # Allow mirrored and RAID LVs
470 next if $info->{lv_type
} !~ m/^[-mMrR]$/;
472 my $volid = "$storeid:$volname";
475 my $found = grep { $_ eq $volid } @$vollist;
478 next if defined($vmid) && ($owner ne $vmid);
482 volid
=> $volid, format
=> 'raw', size
=> $info->{lv_size
}, vmid
=> $owner,
483 ctime
=> $info->{ctime
},
492 my ($class, $storeid, $scfg, $cache) = @_;
494 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
496 my $vgname = $scfg->{vgname
};
498 if (my $info = $cache->{vgs
}->{$vgname}) {
499 return ($info->{size
}, $info->{free
}, $info->{size
} - $info->{free
}, 1);
505 sub activate_storage
{
506 my ($class, $storeid, $scfg, $cache) = @_;
508 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
510 # In LVM2, vgscans take place automatically;
511 # this is just to be sure
512 if ($cache->{vgs
} && !$cache->{vgscaned
} &&
513 !$cache->{vgs
}->{$scfg->{vgname
}}) {
514 $cache->{vgscaned
} = 1;
515 my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
516 eval { run_command
($cmd, outfunc
=> sub {}); };
520 # we do not acticate any volumes here ('vgchange -aly')
521 # instead, volumes are activate individually later
524 sub deactivate_storage
{
525 my ($class, $storeid, $scfg, $cache) = @_;
527 my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname
}];
528 run_command
($cmd, errmsg
=> "can't deactivate VG '$scfg->{vgname}'");
531 sub activate_volume
{
532 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
533 #fix me lvmchange is not provided on
534 my $path = $class->path($scfg, $volname, $snapname);
536 my $lvm_activate_mode = 'ey';
538 my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path];
539 run_command
($cmd, errmsg
=> "can't activate LV '$path'");
540 $cmd = ['/sbin/lvchange', '--refresh', $path];
541 run_command
($cmd, errmsg
=> "can't refresh LV '$path' for activation");
544 sub deactivate_volume
{
545 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
547 my $path = $class->path($scfg, $volname, $snapname);
548 return if ! -b
$path;
550 my $cmd = ['/sbin/lvchange', '-aln', $path];
551 run_command
($cmd, errmsg
=> "can't deactivate LV '$path'");
555 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
557 $size = ($size/1024/1024) . "M";
559 my $path = $class->path($scfg, $volname);
560 my $cmd = ['/sbin/lvextend', '-L', $size, $path];
562 $class->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
563 run_command
($cmd, errmsg
=> "error resizing volume '$path'");
569 sub volume_size_info
{
570 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
571 my $path = $class->filesystem_path($scfg, $volname);
573 my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
574 '--unbuffered', '--nosuffix', '--options', 'lv_size', $path];
577 run_command
($cmd, timeout
=> $timeout, errmsg
=> "can't get size of '$path'",
581 return wantarray ?
($size, 'raw', 0, undef) : $size;
584 sub volume_snapshot
{
585 my ($class, $scfg, $storeid, $volname, $snap) = @_;
587 die "lvm snapshot is not implemented";
590 sub volume_snapshot_rollback
{
591 my ($class, $scfg, $storeid, $volname, $snap) = @_;
593 die "lvm snapshot rollback is not implemented";
596 sub volume_snapshot_delete
{
597 my ($class, $scfg, $storeid, $volname, $snap) = @_;
599 die "lvm snapshot delete is not implemented";
602 sub volume_has_feature
{
603 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
606 copy
=> { base
=> 1, current
=> 1},
607 rename => {current
=> 1},
610 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
611 $class->parse_volname($volname);
617 $key = $isBase ?
'base' : 'current';
619 return 1 if $features->{$feature}->{$key};
624 sub volume_export_formats
{
625 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
626 return () if defined($snapshot); # lvm-thin only
627 return volume_import_formats
($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots);
631 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
632 die "volume export format $format not available for $class\n"
633 if $format ne 'raw+size';
634 die "cannot export volumes together with their snapshots in $class\n"
636 die "cannot export a snapshot in $class\n" if defined($snapshot);
637 die "cannot export an incremental stream in $class\n" if defined($base_snapshot);
638 my $file = $class->path($scfg, $volname, $storeid);
640 # should be faster than querying LVM, also checks for the device file's availability
641 run_command
(['/sbin/blockdev', '--getsize64', $file], outfunc
=> sub {
643 die "unexpected output from /sbin/blockdev: $line\n" if $line !~ /^(\d+)$/;
646 PVE
::Storage
::Plugin
::write_common_header
($fh, $size);
647 run_command
(['dd', "if=$file", "bs=64k"], output
=> '>&'.fileno($fh));
650 sub volume_import_formats
{
651 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
652 return () if $with_snapshots; # not supported
653 return () if defined($base_snapshot); # not supported
658 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots, $allow_rename) = @_;
659 die "volume import format $format not available for $class\n"
660 if $format ne 'raw+size';
661 die "cannot import volumes together with their snapshots in $class\n"
663 die "cannot import an incremental stream in $class\n" if defined($base_snapshot);
665 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
666 $class->parse_volname($volname);
667 die "cannot import format $format into a file of format $file_format\n"
668 if $file_format ne 'raw';
670 my $vg = $scfg->{vgname
};
671 my $lvs = lvm_list_volumes
($vg);
672 if ($lvs->{$vg}->{$volname}) {
673 die "volume $vg/$volname already exists\n" if !$allow_rename;
674 warn "volume $vg/$volname already exists - importing with a different name\n";
678 my ($size) = PVE
::Storage
::Plugin
::read_common_header
($fh);
679 $size = int($size/1024);
682 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, 'raw', $name, $size);
683 my $oldname = $volname;
684 $volname = $allocname;
685 if (defined($name) && $allocname ne $oldname) {
686 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
688 my $file = $class->path($scfg, $volname, $storeid)
689 or die "internal error: failed to get path to newly allocated volume $volname\n";
691 $class->volume_import_write($fh, $file);
694 my $cleanup_worker = eval { $class->free_image($storeid, $scfg, $volname, 0) };
697 if ($cleanup_worker) {
698 my $rpcenv = PVE
::RPCEnvironment
::get
();
699 my $authuser = $rpcenv->get_user();
701 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
707 return "$storeid:$volname";
710 sub volume_import_write
{
711 my ($class, $input_fh, $output_file) = @_;
712 run_command
(['dd', "of=$output_file", 'bs=64k'],
713 input
=> '<&'.fileno($input_fh));
717 my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
727 ) = $class->parse_volname($source_volname);
728 $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format)
731 my $vg = $scfg->{vgname
};
732 my $lvs = lvm_list_volumes
($vg);
733 die "target volume '${target_volname}' already exists\n"
734 if ($lvs->{$vg}->{$target_volname});
736 lvrename
($vg, $source_volname, $target_volname);
737 return "${storeid}:${target_volname}";