]>
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),
187 lv_state
=> substr($lv_attr, 4, 1),
190 $d->{pool_lv
} = $pool_lv if $pool_lv;
191 $d->{tags
} = $tags if $tags;
192 $d->{ctime
} = $ctime;
194 if ($lv_type eq 't') {
198 $d->{metadata_size
} = int($meta_size);
199 $d->{metadata_used
} = int(($meta_percent * $meta_size)/100);
200 $d->{used
} = int(($data_percent * $lv_size)/100);
202 $lvs->{$vg_name}->{$lv_name} = $d;
204 errfunc
=> $ignore_no_medium_warnings,
218 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
225 description
=> "Volume group name.",
226 type
=> 'string', format
=> 'pve-storage-vgname',
229 description
=> "Base volume. This volume is automatically activated.",
230 type
=> 'string', format
=> 'pve-volume-id',
233 description
=> "Zero-out data when removing LVs.",
236 saferemove_throughput
=> {
237 description
=> "Wipe throughput (cstream -t parameter value).",
241 description
=> "Only use logical volumes tagged with 'pve-vm-ID'.",
249 vgname
=> { fixed
=> 1 },
250 nodes
=> { optional
=> 1 },
251 shared
=> { optional
=> 1 },
252 disable
=> { optional
=> 1 },
253 saferemove
=> { optional
=> 1 },
254 saferemove_throughput
=> { optional
=> 1 },
255 content
=> { optional
=> 1 },
256 base
=> { fixed
=> 1, optional
=> 1 },
257 tagged_only
=> { optional
=> 1 },
258 bwlimit
=> { optional
=> 1 },
262 # Storage implementation
265 my ($class, $storeid, $scfg, %param) = @_;
267 if (my $base = $scfg->{base
}) {
268 my ($baseid, $volname) = PVE
::Storage
::parse_volume_id
($base);
270 my $cfg = PVE
::Storage
::config
();
271 my $basecfg = PVE
::Storage
::storage_config
($cfg, $baseid, 1);
272 die "base storage ID '$baseid' does not exist\n" if !$basecfg;
274 # we only support iscsi for now
275 die "unsupported base type '$basecfg->{type}'"
276 if $basecfg->{type
} ne 'iscsi';
278 my $path = PVE
::Storage
::path
($cfg, $base);
280 PVE
::Storage
::activate_storage
($cfg, $baseid);
282 lvm_create_volume_group
($path, $scfg->{vgname
}, $scfg->{shared
});
289 my ($class, $volname) = @_;
291 PVE
::Storage
::Plugin
::parse_lvm_name
($volname);
293 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
294 return ('images', $1, $2, undef, undef, undef, 'raw');
297 die "unable to parse lvm volume name '$volname'\n";
300 sub filesystem_path
{
301 my ($class, $scfg, $volname, $snapname) = @_;
303 die "lvm snapshot is not implemented"if defined($snapname);
305 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
307 my $vg = $scfg->{vgname
};
309 my $path = "/dev/$vg/$name";
311 return wantarray ?
($path, $vmid, $vtype) : $path;
315 my ($class, $storeid, $scfg, $volname) = @_;
317 die "can't create base images in lvm storage\n";
321 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
323 die "can't clone images in lvm storage\n";
326 sub find_free_diskname
{
327 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
329 my $vg = $scfg->{vgname
};
331 my $lvs = lvm_list_volumes
($vg);
333 my $disk_list = [ keys %{$lvs->{$vg}} ];
335 return PVE
::Storage
::Plugin
::get_next_vm_diskname
($disk_list, $storeid, $vmid, undef, $scfg);
339 my ($vg, $name, $size, $tags) = @_;
341 if ($size =~ m/\d$/) { # no unit is given
342 $size .= "k"; # default to kilobytes
345 my $cmd = ['/sbin/lvcreate', '-aly', '-Wy', '--yes', '--size', $size, '--name', $name];
346 for my $tag (@$tags) {
347 push @$cmd, '--addtag', $tag;
351 run_command
($cmd, errmsg
=> "lvcreate '$vg/$name' error");
355 my ($vg, $oldname, $newname) = @_;
358 ['/sbin/lvrename', $vg, $oldname, $newname],
359 errmsg
=> "lvrename '${vg}/${oldname}' to '${newname}' error",
364 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
366 die "unsupported format '$fmt'" if $fmt ne 'raw';
368 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
369 if $name && $name !~ m/^vm-$vmid-/;
373 my $vg = $scfg->{vgname
};
375 die "no such volume group '$vg'\n" if !defined ($vgs->{$vg});
377 my $free = int($vgs->{$vg}->{free
});
379 die "not enough free space ($free < $size)\n" if $free < $size;
381 $name = $class->find_free_diskname($storeid, $scfg, $vmid)
384 lvcreate
($vg, $name, $size, ["pve-vm-$vmid"]);
390 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
392 my $vg = $scfg->{vgname
};
394 # we need to zero out LVM data for security reasons
395 # and to allow thin provisioning
397 my $zero_out_worker = sub {
398 print "zero-out data on image $volname (/dev/$vg/del-$volname)\n";
400 # wipe throughput up to 10MB/s by default; may be overwritten with saferemove_throughput
401 my $throughput = '-10485760';
402 if ($scfg->{saferemove_throughput
}) {
403 $throughput = $scfg->{saferemove_throughput
};
409 '-o', "/dev/$vg/del-$volname",
415 eval { run_command
($cmd, errmsg
=> "zero out finished (note: 'No space left on device' is ok here)"); };
418 $class->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
419 my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"];
420 run_command
($cmd, errmsg
=> "lvremove '$vg/del-$volname' error");
422 print "successfully removed volume $volname ($vg/del-$volname)\n";
425 my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"];
426 run_command
($cmd, errmsg
=> "can't activate LV '$vg/$volname' to zero-out its data");
427 $cmd = ['/sbin/lvchange', '--refresh', "$vg/$volname"];
428 run_command
($cmd, errmsg
=> "can't refresh LV '$vg/$volname' to zero-out its data");
430 if ($scfg->{saferemove
}) {
431 # avoid long running task, so we only rename here
432 $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"];
433 run_command
($cmd, errmsg
=> "lvrename '$vg/$volname' error");
434 return $zero_out_worker;
436 my $tmpvg = $scfg->{vgname
};
437 $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"];
438 run_command
($cmd, errmsg
=> "lvremove '$tmpvg/$volname' error");
444 my $check_tags = sub {
447 return defined($tags) && $tags =~ /(^|,)pve-vm-\d+(,|$)/;
451 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
453 my $vgname = $scfg->{vgname
};
455 $cache->{lvs
} = lvm_list_volumes
() if !$cache->{lvs
};
459 if (my $dat = $cache->{lvs
}->{$vgname}) {
461 foreach my $volname (keys %$dat) {
463 next if $volname !~ m/^vm-(\d+)-/;
466 my $info = $dat->{$volname};
468 next if $scfg->{tagged_only
} && !&$check_tags($info->{tags
});
470 # Allow mirrored and RAID LVs
471 next if $info->{lv_type
} !~ m/^[-mMrR]$/;
473 my $volid = "$storeid:$volname";
476 my $found = grep { $_ eq $volid } @$vollist;
479 next if defined($vmid) && ($owner ne $vmid);
483 volid
=> $volid, format
=> 'raw', size
=> $info->{lv_size
}, vmid
=> $owner,
484 ctime
=> $info->{ctime
},
493 my ($class, $storeid, $scfg, $cache) = @_;
495 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
497 my $vgname = $scfg->{vgname
};
499 if (my $info = $cache->{vgs
}->{$vgname}) {
500 return ($info->{size
}, $info->{free
}, $info->{size
} - $info->{free
}, 1);
506 sub activate_storage
{
507 my ($class, $storeid, $scfg, $cache) = @_;
509 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
511 # In LVM2, vgscans take place automatically;
512 # this is just to be sure
513 if ($cache->{vgs
} && !$cache->{vgscaned
} &&
514 !$cache->{vgs
}->{$scfg->{vgname
}}) {
515 $cache->{vgscaned
} = 1;
516 my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
517 eval { run_command
($cmd, outfunc
=> sub {}); };
521 # we do not acticate any volumes here ('vgchange -aly')
522 # instead, volumes are activate individually later
525 sub deactivate_storage
{
526 my ($class, $storeid, $scfg, $cache) = @_;
528 my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname
}];
529 run_command
($cmd, errmsg
=> "can't deactivate VG '$scfg->{vgname}'");
532 sub activate_volume
{
533 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
534 #fix me lvmchange is not provided on
535 my $path = $class->path($scfg, $volname, $snapname);
537 my $lvm_activate_mode = 'ey';
539 my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path];
540 run_command
($cmd, errmsg
=> "can't activate LV '$path'");
541 $cmd = ['/sbin/lvchange', '--refresh', $path];
542 run_command
($cmd, errmsg
=> "can't refresh LV '$path' for activation");
545 sub deactivate_volume
{
546 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
548 my $path = $class->path($scfg, $volname, $snapname);
549 return if ! -b
$path;
551 my $cmd = ['/sbin/lvchange', '-aln', $path];
552 run_command
($cmd, errmsg
=> "can't deactivate LV '$path'");
556 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
558 $size = ($size/1024/1024) . "M";
560 my $path = $class->path($scfg, $volname);
561 my $cmd = ['/sbin/lvextend', '-L', $size, $path];
563 $class->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
564 run_command
($cmd, errmsg
=> "error resizing volume '$path'");
570 sub volume_size_info
{
571 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
572 my $path = $class->filesystem_path($scfg, $volname);
574 my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
575 '--unbuffered', '--nosuffix', '--options', 'lv_size', $path];
578 run_command
($cmd, timeout
=> $timeout, errmsg
=> "can't get size of '$path'",
582 return wantarray ?
($size, 'raw', 0, undef) : $size;
585 sub volume_snapshot
{
586 my ($class, $scfg, $storeid, $volname, $snap) = @_;
588 die "lvm snapshot is not implemented";
591 sub volume_snapshot_rollback
{
592 my ($class, $scfg, $storeid, $volname, $snap) = @_;
594 die "lvm snapshot rollback is not implemented";
597 sub volume_snapshot_delete
{
598 my ($class, $scfg, $storeid, $volname, $snap) = @_;
600 die "lvm snapshot delete is not implemented";
603 sub volume_has_feature
{
604 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
607 copy
=> { base
=> 1, current
=> 1},
608 rename => {current
=> 1},
611 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
612 $class->parse_volname($volname);
618 $key = $isBase ?
'base' : 'current';
620 return 1 if $features->{$feature}->{$key};
625 sub volume_export_formats
{
626 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
627 return () if defined($snapshot); # lvm-thin only
628 return volume_import_formats
($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots);
632 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
633 die "volume export format $format not available for $class\n"
634 if $format ne 'raw+size';
635 die "cannot export volumes together with their snapshots in $class\n"
637 die "cannot export a snapshot in $class\n" if defined($snapshot);
638 die "cannot export an incremental stream in $class\n" if defined($base_snapshot);
639 my $file = $class->path($scfg, $volname, $storeid);
641 # should be faster than querying LVM, also checks for the device file's availability
642 run_command
(['/sbin/blockdev', '--getsize64', $file], outfunc
=> sub {
644 die "unexpected output from /sbin/blockdev: $line\n" if $line !~ /^(\d+)$/;
647 PVE
::Storage
::Plugin
::write_common_header
($fh, $size);
648 run_command
(['dd', "if=$file", "bs=64k"], output
=> '>&'.fileno($fh));
651 sub volume_import_formats
{
652 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
653 return () if $with_snapshots; # not supported
654 return () if defined($base_snapshot); # not supported
659 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots, $allow_rename) = @_;
660 die "volume import format $format not available for $class\n"
661 if $format ne 'raw+size';
662 die "cannot import volumes together with their snapshots in $class\n"
664 die "cannot import an incremental stream in $class\n" if defined($base_snapshot);
666 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
667 $class->parse_volname($volname);
668 die "cannot import format $format into a file of format $file_format\n"
669 if $file_format ne 'raw';
671 my $vg = $scfg->{vgname
};
672 my $lvs = lvm_list_volumes
($vg);
673 if ($lvs->{$vg}->{$volname}) {
674 die "volume $vg/$volname already exists\n" if !$allow_rename;
675 warn "volume $vg/$volname already exists - importing with a different name\n";
679 my ($size) = PVE
::Storage
::Plugin
::read_common_header
($fh);
680 $size = int($size/1024);
683 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, 'raw', $name, $size);
684 my $oldname = $volname;
685 $volname = $allocname;
686 if (defined($name) && $allocname ne $oldname) {
687 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
689 my $file = $class->path($scfg, $volname, $storeid)
690 or die "internal error: failed to get path to newly allocated volume $volname\n";
692 $class->volume_import_write($fh, $file);
695 my $cleanup_worker = eval { $class->free_image($storeid, $scfg, $volname, 0) };
698 if ($cleanup_worker) {
699 my $rpcenv = PVE
::RPCEnvironment
::get
();
700 my $authuser = $rpcenv->get_user();
702 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
708 return "$storeid:$volname";
711 sub volume_import_write
{
712 my ($class, $input_fh, $output_file) = @_;
713 run_command
(['dd', "of=$output_file", 'bs=64k'],
714 input
=> '<&'.fileno($input_fh));
718 my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
728 ) = $class->parse_volname($source_volname);
729 $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format)
732 my $vg = $scfg->{vgname
};
733 my $lvs = lvm_list_volumes
($vg);
734 die "target volume '${target_volname}' already exists\n"
735 if ($lvs->{$vg}->{$target_volname});
737 lvrename
($vg, $source_volname, $target_volname);
738 return "${storeid}:${target_volname}";