use strict;
use warnings;
+use Data::Dumper;
use IO::File;
use PVE::Tools qw(run_command trim);
use PVE::Storage::Plugin;
my ($pvname, $size, $vgname, $uuid) = split(':', $line);
- die "found multiple pvs entries for device '$device'\n"
+ die "found multiple pvs entries for device '$device'\n"
if $pvinfo;
$pvinfo = {
pvname => $pvname,
- size => $size,
+ size => int($size),
vgname => $vgname,
uuid => $uuid,
};
if (my $fh = IO::File->new($dev, "w")) {
my $buf = 0 x 512;
syswrite $fh, $buf;
- $fh->close();
+ $fh->close();
}
}
sub lvm_create_volume_group {
my ($device, $vgname, $shared) = @_;
-
+
my $res = lvm_pv_info($device);
-
+
if ($res->{vgname}) {
return if $res->{vgname} eq $vgname; # already created
die "device '$device' is already used by volume group '$res->{vgname}'\n";
return $vgs;
}
-sub lvm_lvs {
+sub lvm_list_volumes {
my ($vgname) = @_;
my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
'--unbuffered', '--nosuffix', '--options',
- 'vg_name,lv_name,lv_size,uuid,tags'];
+ 'vg_name,lv_name,lv_size,lv_attr,pool_lv,data_percent,metadata_percent,snap_percent,uuid,tags'];
push @$cmd, $vgname if $vgname;
$line = trim($line);
- my ($vg, $name, $size, $uuid, $tags) = split(':', $line);
+ my ($vg_name, $lv_name, $lv_size, $lv_attr, $pool_lv, $data_percent, $meta_percent, $snap_percent, $uuid, $tags) = split(':', $line);
+ return if !$vg_name;
+ return if !$lv_name;
- return if $name !~ m/^vm-(\d+)-/;
- my $nid = $1;
+ my $lv_type = substr($lv_attr, 0, 1);
- my $owner;
- foreach my $tag (split (/,/, $tags)) {
- if ($tag =~ m/^pve-vm-(\d+)$/) {
- $owner = $1;
- last;
- }
- }
-
- if ($owner) {
- if ($owner ne $nid) {
- warn "owner mismatch name = $name, owner = $owner\n";
- }
-
- $lvs->{$vg}->{$name} = { format => 'raw', size => $size,
- uuid => $uuid, tags => $tags,
- vmid => $owner };
+ my $d = {
+ lv_size => int($lv_size),
+ lv_type => $lv_type,
+ };
+ $d->{pool_lv} = $pool_lv if $pool_lv;
+ $d->{tags} = $tags if $tags;
+
+ if ($lv_type eq 't') {
+ $data_percent ||= 0;
+ $meta_percent ||= 0;
+ $snap_percent ||= 0;
+ $d->{used} = int(($data_percent * $lv_size)/100);
}
+ $lvs->{$vg_name}->{$lv_name} = $d;
});
return $lvs;
}
-# Configuration
-
-PVE::JSONSchema::register_format('pve-storage-vgname', \&parse_lvm_name);
-sub parse_lvm_name {
- my ($name, $noerr) = @_;
-
- if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
- return undef if $noerr;
- die "lvm name '$name' contains illegal characters\n";
- }
-
- return $name;
-}
+# Configuration
sub type {
return 'lvm';
description => "Wipe throughput (cstream -t parameter value).",
type => 'string',
},
+ tagged_only => {
+ description => "Only use logical volumes tagged with 'pve-vm-ID'.",
+ type => 'boolean',
+ }
};
}
sub options {
return {
vgname => { fixed => 1 },
- nodes => { optional => 1 },
+ nodes => { optional => 1 },
shared => { optional => 1 },
disable => { optional => 1 },
- saferemove => { optional => 1 },
- saferemove_throughput => { optional => 1 },
+ saferemove => { optional => 1 },
+ saferemove_throughput => { optional => 1 },
content => { optional => 1 },
- base => { fixed => 1, optional => 1 },
+ base => { fixed => 1, optional => 1 },
+ tagged_only => { optional => 1 },
+ bwlimit => { optional => 1 },
};
}
sub parse_volname {
my ($class, $volname) = @_;
- parse_lvm_name($volname);
+ PVE::Storage::Plugin::parse_lvm_name($volname);
if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
return ('images', $1, $2, undef, undef, undef, 'raw');
my ($vtype, $name, $vmid) = $class->parse_volname($volname);
my $vg = $scfg->{vgname};
-
+
my $path = "/dev/$vg/$name";
return wantarray ? ($path, $vmid, $vtype) : $path;
die "can't clone images in lvm storage\n";
}
+sub lvm_find_free_diskname {
+ my ($lvs, $vg, $storeid, $vmid) = @_;
+
+ my $name;
+
+ for (my $i = 1; $i < 100; $i++) {
+ my $tn = "vm-$vmid-disk-$i";
+ if (!defined ($lvs->{$vg}->{$tn})) {
+ $name = $tn;
+ last;
+ }
+ }
+
+ die "unable to allocate an image name for ID $vmid in storage '$storeid'\n"
+ if !$name;
+
+ return $name;
+}
+
sub alloc_image {
my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
die "unsupported format '$fmt'" if $fmt ne 'raw';
- die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
+ die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
if $name && $name !~ m/^vm-$vmid-/;
my $vgs = lvm_vgs();
die "not enough free space ($free < $size)\n" if $free < $size;
- if (!$name) {
- my $lvs = lvm_lvs($vg);
-
- for (my $i = 1; $i < 100; $i++) {
- my $tn = "vm-$vmid-disk-$i";
- if (!defined ($lvs->{$vg}->{$tn})) {
- $name = $tn;
- last;
- }
- }
- }
-
- die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
+ $name = lvm_find_free_diskname(lvm_list_volumes($vg), $vg, $storeid, $vmid)
if !$name;
my $cmd = ['/sbin/lvcreate', '-aly', '--addtag', "pve-vm-$vmid", '--size', "${size}k", '--name', $name, $vg];
return undef;
}
+my $check_tags = sub {
+ my ($tags) = @_;
+
+ return defined($tags) && $tags =~ /(^|,)pve-vm-\d+(,|$)/;
+};
+
sub list_images {
my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
my $vgname = $scfg->{vgname};
- $cache->{lvs} = lvm_lvs() if !$cache->{lvs};
+ $cache->{lvs} = lvm_list_volumes() if !$cache->{lvs};
my $res = [];
-
+
if (my $dat = $cache->{lvs}->{$vgname}) {
foreach my $volname (keys %$dat) {
- my $owner = $dat->{$volname}->{vmid};
+ next if $volname !~ m/^vm-(\d+)-/;
+ my $owner = $1;
+
+ my $info = $dat->{$volname};
+
+ next if $scfg->{tagged_only} && !&$check_tags($info->{tags});
+
+ next if $info->{lv_type} ne '-';
my $volid = "$storeid:$volname";
my $found = grep { $_ eq $volid } @$vollist;
next if !$found;
} else {
- next if defined ($vmid) && ($owner ne $vmid);
+ next if defined($vmid) && ($owner ne $vmid);
}
- my $info = $dat->{$volname};
- $info->{volid} = $volid;
-
- push @$res, $info;
+ push @$res, {
+ volid => $volid, format => 'raw', size => $info->{lv_size}, vmid => $owner,
+ };
}
}
# In LVM2, vgscans take place automatically;
# this is just to be sure
- if ($cache->{vgs} && !$cache->{vgscaned} &&
+ if ($cache->{vgs} && !$cache->{vgscaned} &&
!$cache->{vgs}->{$scfg->{vgname}}) {
$cache->{vgscaned} = 1;
my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
return undef;
}
+sub volume_export_formats {
+ my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
+ return () if defined($snapshot); # lvm-thin only
+ return volume_import_formats($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots);
+}
+
+sub volume_export {
+ my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
+ die "volume export format $format not available for $class\n"
+ if $format ne 'raw+size';
+ die "cannot export volumes together with their snapshots in $class\n"
+ if $with_snapshots;
+ die "cannot export a snapshot in $class\n" if defined($snapshot);
+ die "cannot export an incremental stream in $class\n" if defined($base_snapshot);
+ my $file = $class->path($scfg, $volname, $storeid);
+ my $size;
+ # should be faster than querying LVM, also checks for the device file's availability
+ run_command(['/sbin/blockdev', '--getsize64', $file], outfunc => sub {
+ my ($line) = @_;
+ die "unexpected output from /sbin/blockdev: $line\n" if $line !~ /^(\d+)$/;
+ $size = int($1);
+ });
+ PVE::Storage::Plugin::write_common_header($fh, $size);
+ run_command(['dd', "if=$file", "bs=64k"], output => '>&'.fileno($fh));
+}
+
+sub volume_import_formats {
+ my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
+ return () if $with_snapshots; # not supported
+ return () if defined($base_snapshot); # not supported
+ return ('raw+size');
+}
+
+sub volume_import {
+ my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
+ die "volume import format $format not available for $class\n"
+ if $format ne 'raw+size';
+ die "cannot import volumes together with their snapshots in $class\n"
+ if $with_snapshots;
+ die "cannot import an incremental stream in $class\n" if defined($base_snapshot);
+
+ my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
+ $class->parse_volname($volname);
+ die "cannot import format $format into a file of format $file_format\n"
+ if $file_format ne 'raw';
+
+ my $vg = $scfg->{vgname};
+ my $lvs = lvm_list_volumes($vg);
+ die "volume $vg/$volname already exists\n"
+ if $lvs->{$vg}->{$volname};
+
+ my ($size) = PVE::Storage::Plugin::read_common_header($fh);
+ $size = int($size/1024);
+
+ eval {
+ my $allocname = $class->alloc_image($storeid, $scfg, $vmid, 'raw', $name, $size);
+ if ($allocname ne $volname) {
+ my $oldname = $volname;
+ $volname = $allocname; # Let the cleanup code know what to free
+ die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
+ }
+ my $file = $class->path($scfg, $volname, $storeid)
+ or die "internal error: failed to get path to newly allocated volume $volname\n";
+ run_command(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
+ input => '<&'.fileno($fh));
+ };
+ if (my $err = $@) {
+ eval { $class->free_image($storeid, $scfg, $volname, 0) };
+ warn $@ if $@;
+ die $err;
+ }
+}
+
1;