]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/LVMPlugin.pm
add Storage::get_bandwidth_limit helper
[pve-storage.git] / PVE / Storage / LVMPlugin.pm
index ddae51709f420e516791dfc456662dc4b72d20b6..94d3a33c2a1b3a6e3669eb42c59a2e8c0e9984b7 100644 (file)
@@ -44,7 +44,7 @@ sub lvm_pv_info {
 
        $pvinfo = {
            pvname => $pvname,
-           size => $size,
+           size => int($size),
            vgname => $vgname,
            uuid => $uuid,
        };
@@ -136,16 +136,17 @@ sub lvm_list_volumes {
        my $lv_type = substr($lv_attr, 0, 1);
 
        my $d = {
-           lv_size => $lv_size,
+           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 + $meta_percent + $snap_percent) * $lv_size)/100);
+           $d->{used} = int(($data_percent * $lv_size)/100);
        }
        $lvs->{$vg_name}->{$lv_name} = $d;
     });
@@ -183,19 +184,25 @@ sub properties {
            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 },
     };
 }
 
@@ -339,6 +346,12 @@ sub free_image {
     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) = @_;
 
@@ -357,6 +370,8 @@ sub list_images {
 
            my $info = $dat->{$volname};
 
+           next if $scfg->{tagged_only} && !&$check_tags($info->{tags});
+
            next if $info->{lv_type} ne '-';
 
            my $volid = "$storeid:$volname";
@@ -489,4 +504,77 @@ sub volume_has_feature {
     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;