]>
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);
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 $option_list = 'vg_name,lv_name,lv_size,lv_attr,pool_lv,data_percent,metadata_percent,snap_percent,uuid,tags,metadata_size,time';
154 '/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
155 '--unbuffered', '--nosuffix',
156 '--config', 'report/time_format="%s"',
157 '--options', $option_list,
160 push @$cmd, $vgname if $vgname;
163 run_command
($cmd, outfunc
=> sub {
168 my ($vg_name, $lv_name, $lv_size, $lv_attr, $pool_lv, $data_percent, $meta_percent, $snap_percent, $uuid, $tags, $meta_size, $ctime) = split(':', $line);
172 my $lv_type = substr($lv_attr, 0, 1);
175 lv_size
=> int($lv_size),
178 $d->{pool_lv
} = $pool_lv if $pool_lv;
179 $d->{tags
} = $tags if $tags;
180 $d->{ctime
} = $ctime;
182 if ($lv_type eq 't') {
186 $d->{metadata_size
} = int($meta_size);
187 $d->{metadata_used
} = int(($meta_percent * $meta_size)/100);
188 $d->{used
} = int(($data_percent * $lv_size)/100);
190 $lvs->{$vg_name}->{$lv_name} = $d;
192 errfunc
=> $ignore_no_medium_warnings,
206 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
213 description
=> "Volume group name.",
214 type
=> 'string', format
=> 'pve-storage-vgname',
217 description
=> "Base volume. This volume is automatically activated.",
218 type
=> 'string', format
=> 'pve-volume-id',
221 description
=> "Zero-out data when removing LVs.",
224 saferemove_throughput
=> {
225 description
=> "Wipe throughput (cstream -t parameter value).",
229 description
=> "Only use logical volumes tagged with 'pve-vm-ID'.",
237 vgname
=> { fixed
=> 1 },
238 nodes
=> { optional
=> 1 },
239 shared
=> { optional
=> 1 },
240 disable
=> { optional
=> 1 },
241 saferemove
=> { optional
=> 1 },
242 saferemove_throughput
=> { optional
=> 1 },
243 content
=> { optional
=> 1 },
244 base
=> { fixed
=> 1, optional
=> 1 },
245 tagged_only
=> { optional
=> 1 },
246 bwlimit
=> { optional
=> 1 },
250 # Storage implementation
253 my ($class, $storeid, $scfg, %param) = @_;
255 if (my $base = $scfg->{base
}) {
256 my ($baseid, $volname) = PVE
::Storage
::parse_volume_id
($base);
258 my $cfg = PVE
::Storage
::config
();
259 my $basecfg = PVE
::Storage
::storage_config
($cfg, $baseid, 1);
260 die "base storage ID '$baseid' does not exist\n" if !$basecfg;
262 # we only support iscsi for now
263 die "unsupported base type '$basecfg->{type}'"
264 if $basecfg->{type
} ne 'iscsi';
266 my $path = PVE
::Storage
::path
($cfg, $base);
268 PVE
::Storage
::activate_storage
($cfg, $baseid);
270 lvm_create_volume_group
($path, $scfg->{vgname
}, $scfg->{shared
});
275 my ($class, $volname) = @_;
277 PVE
::Storage
::Plugin
::parse_lvm_name
($volname);
279 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
280 return ('images', $1, $2, undef, undef, undef, 'raw');
283 die "unable to parse lvm volume name '$volname'\n";
286 sub filesystem_path
{
287 my ($class, $scfg, $volname, $snapname) = @_;
289 die "lvm snapshot is not implemented"if defined($snapname);
291 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
293 my $vg = $scfg->{vgname
};
295 my $path = "/dev/$vg/$name";
297 return wantarray ?
($path, $vmid, $vtype) : $path;
301 my ($class, $storeid, $scfg, $volname) = @_;
303 die "can't create base images in lvm storage\n";
307 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
309 die "can't clone images in lvm storage\n";
312 sub find_free_diskname
{
313 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
315 my $vg = $scfg->{vgname
};
317 my $lvs = lvm_list_volumes
($vg);
319 my $disk_list = [ keys %{$lvs->{$vg}} ];
321 return PVE
::Storage
::Plugin
::get_next_vm_diskname
($disk_list, $storeid, $vmid, undef, $scfg);
325 my ($vg, $name, $size, $tags) = @_;
327 if ($size =~ m/\d$/) { # no unit is given
328 $size .= "k"; # default to kilobytes
331 my $cmd = ['/sbin/lvcreate', '-aly', '--size', $size, '--name', $name];
332 for my $tag (@$tags) {
333 push @$cmd, '--addtag', $tag;
337 run_command
($cmd, errmsg
=> "lvcreate '$vg/$name' error");
341 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
343 die "unsupported format '$fmt'" if $fmt ne 'raw';
345 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
346 if $name && $name !~ m/^vm-$vmid-/;
350 my $vg = $scfg->{vgname
};
352 die "no such volume group '$vg'\n" if !defined ($vgs->{$vg});
354 my $free = int($vgs->{$vg}->{free
});
356 die "not enough free space ($free < $size)\n" if $free < $size;
358 $name = $class->find_free_diskname($storeid, $scfg, $vmid)
361 lvcreate
($vg, $name, $size, ["pve-vm-$vmid"]);
367 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
369 my $vg = $scfg->{vgname
};
371 # we need to zero out LVM data for security reasons
372 # and to allow thin provisioning
374 my $zero_out_worker = sub {
375 print "zero-out data on image $volname (/dev/$vg/del-$volname)\n";
377 # wipe throughput up to 10MB/s by default; may be overwritten with saferemove_throughput
378 my $throughput = '-10485760';
379 if ($scfg->{saferemove_throughput
}) {
380 $throughput = $scfg->{saferemove_throughput
};
386 '-o', "/dev/$vg/del-$volname",
392 eval { run_command
($cmd, errmsg
=> "zero out finished (note: 'No space left on device' is ok here)"); };
395 $class->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
396 my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"];
397 run_command
($cmd, errmsg
=> "lvremove '$vg/del-$volname' error");
399 print "successfully removed volume $volname ($vg/del-$volname)\n";
402 my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"];
403 run_command
($cmd, errmsg
=> "can't activate LV '$vg/$volname' to zero-out its data");
404 $cmd = ['/sbin/lvchange', '--refresh', "$vg/$volname"];
405 run_command
($cmd, errmsg
=> "can't refresh LV '$vg/$volname' to zero-out its data");
407 if ($scfg->{saferemove
}) {
408 # avoid long running task, so we only rename here
409 $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"];
410 run_command
($cmd, errmsg
=> "lvrename '$vg/$volname' error");
411 return $zero_out_worker;
413 my $tmpvg = $scfg->{vgname
};
414 $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"];
415 run_command
($cmd, errmsg
=> "lvremove '$tmpvg/$volname' error");
421 my $check_tags = sub {
424 return defined($tags) && $tags =~ /(^|,)pve-vm-\d+(,|$)/;
428 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
430 my $vgname = $scfg->{vgname
};
432 $cache->{lvs
} = lvm_list_volumes
() if !$cache->{lvs
};
436 if (my $dat = $cache->{lvs
}->{$vgname}) {
438 foreach my $volname (keys %$dat) {
440 next if $volname !~ m/^vm-(\d+)-/;
443 my $info = $dat->{$volname};
445 next if $scfg->{tagged_only
} && !&$check_tags($info->{tags
});
447 next if $info->{lv_type
} ne '-';
449 my $volid = "$storeid:$volname";
452 my $found = grep { $_ eq $volid } @$vollist;
455 next if defined($vmid) && ($owner ne $vmid);
459 volid
=> $volid, format
=> 'raw', size
=> $info->{lv_size
}, vmid
=> $owner,
460 ctime
=> $info->{ctime
},
469 my ($class, $storeid, $scfg, $cache) = @_;
471 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
473 my $vgname = $scfg->{vgname
};
475 if (my $info = $cache->{vgs
}->{$vgname}) {
476 return ($info->{size
}, $info->{free
}, $info->{size
} - $info->{free
}, 1);
482 sub activate_storage
{
483 my ($class, $storeid, $scfg, $cache) = @_;
485 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
487 # In LVM2, vgscans take place automatically;
488 # this is just to be sure
489 if ($cache->{vgs
} && !$cache->{vgscaned
} &&
490 !$cache->{vgs
}->{$scfg->{vgname
}}) {
491 $cache->{vgscaned
} = 1;
492 my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
493 eval { run_command
($cmd, outfunc
=> sub {}); };
497 # we do not acticate any volumes here ('vgchange -aly')
498 # instead, volumes are activate individually later
501 sub deactivate_storage
{
502 my ($class, $storeid, $scfg, $cache) = @_;
504 my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname
}];
505 run_command
($cmd, errmsg
=> "can't deactivate VG '$scfg->{vgname}'");
508 sub activate_volume
{
509 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
510 #fix me lvmchange is not provided on
511 my $path = $class->path($scfg, $volname, $snapname);
513 my $lvm_activate_mode = 'ey';
515 my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path];
516 run_command
($cmd, errmsg
=> "can't activate LV '$path'");
517 $cmd = ['/sbin/lvchange', '--refresh', $path];
518 run_command
($cmd, errmsg
=> "can't refresh LV '$path' for activation");
521 sub deactivate_volume
{
522 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
524 my $path = $class->path($scfg, $volname, $snapname);
525 return if ! -b
$path;
527 my $cmd = ['/sbin/lvchange', '-aln', $path];
528 run_command
($cmd, errmsg
=> "can't deactivate LV '$path'");
532 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
534 $size = ($size/1024/1024) . "M";
536 my $path = $class->path($scfg, $volname);
537 my $cmd = ['/sbin/lvextend', '-L', $size, $path];
539 $class->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
540 run_command
($cmd, errmsg
=> "error resizing volume '$path'");
546 sub volume_size_info
{
547 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
548 my $path = $class->filesystem_path($scfg, $volname);
550 my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
551 '--unbuffered', '--nosuffix', '--options', 'lv_size', $path];
554 run_command
($cmd, timeout
=> $timeout, errmsg
=> "can't get size of '$path'",
558 return wantarray ?
($size, 'raw', 0, undef) : $size;
561 sub volume_snapshot
{
562 my ($class, $scfg, $storeid, $volname, $snap) = @_;
564 die "lvm snapshot is not implemented";
567 sub volume_snapshot_rollback
{
568 my ($class, $scfg, $storeid, $volname, $snap) = @_;
570 die "lvm snapshot rollback is not implemented";
573 sub volume_snapshot_delete
{
574 my ($class, $scfg, $storeid, $volname, $snap) = @_;
576 die "lvm snapshot delete is not implemented";
579 sub volume_has_feature
{
580 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
583 copy
=> { base
=> 1, current
=> 1},
586 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
587 $class->parse_volname($volname);
593 $key = $isBase ?
'base' : 'current';
595 return 1 if $features->{$feature}->{$key};
600 sub volume_export_formats
{
601 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
602 return () if defined($snapshot); # lvm-thin only
603 return volume_import_formats
($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots);
607 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
608 die "volume export format $format not available for $class\n"
609 if $format ne 'raw+size';
610 die "cannot export volumes together with their snapshots in $class\n"
612 die "cannot export a snapshot in $class\n" if defined($snapshot);
613 die "cannot export an incremental stream in $class\n" if defined($base_snapshot);
614 my $file = $class->path($scfg, $volname, $storeid);
616 # should be faster than querying LVM, also checks for the device file's availability
617 run_command
(['/sbin/blockdev', '--getsize64', $file], outfunc
=> sub {
619 die "unexpected output from /sbin/blockdev: $line\n" if $line !~ /^(\d+)$/;
622 PVE
::Storage
::Plugin
::write_common_header
($fh, $size);
623 run_command
(['dd', "if=$file", "bs=64k"], output
=> '>&'.fileno($fh));
626 sub volume_import_formats
{
627 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
628 return () if $with_snapshots; # not supported
629 return () if defined($base_snapshot); # not supported
634 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
635 die "volume import format $format not available for $class\n"
636 if $format ne 'raw+size';
637 die "cannot import volumes together with their snapshots in $class\n"
639 die "cannot import an incremental stream in $class\n" if defined($base_snapshot);
641 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
642 $class->parse_volname($volname);
643 die "cannot import format $format into a file of format $file_format\n"
644 if $file_format ne 'raw';
646 my $vg = $scfg->{vgname
};
647 my $lvs = lvm_list_volumes
($vg);
648 die "volume $vg/$volname already exists\n"
649 if $lvs->{$vg}->{$volname};
651 my ($size) = PVE
::Storage
::Plugin
::read_common_header
($fh);
652 $size = int($size/1024);
655 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, 'raw', $name, $size);
656 if ($allocname ne $volname) {
657 my $oldname = $volname;
658 $volname = $allocname; # Let the cleanup code know what to free
659 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
661 my $file = $class->path($scfg, $volname, $storeid)
662 or die "internal error: failed to get path to newly allocated volume $volname\n";
664 $class->volume_import_write($fh, $file);
667 eval { $class->free_image($storeid, $scfg, $volname, 0) };
673 sub volume_import_write
{
674 my ($class, $input_fh, $output_file) = @_;
675 run_command
(['dd', "of=$output_file", 'bs=64k'],
676 input
=> '<&'.fileno($input_fh));