]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/LvmThinPlugin.pm
Don't remove and recreate lun when changing a volume
[pve-storage.git] / PVE / Storage / LvmThinPlugin.pm
index 5c440324837758d32b49f9d0bc87bfe567148b9c..aafc202c043615852e1dbd9b7c2ebd0681914a12 100644 (file)
@@ -2,8 +2,9 @@ package PVE::Storage::LvmThinPlugin;
 
 use strict;
 use warnings;
-use Data::Dumper;
+
 use IO::File;
+
 use PVE::Tools qw(run_command trim);
 use PVE::Storage::Plugin;
 use PVE::Storage::LVMPlugin;
@@ -15,6 +16,12 @@ use PVE::JSONSchema qw(get_standard_option);
 # lvcreate -n pvepool -L 20G pve
 # lvconvert --type thin-pool pve/pvepool
 
+# NOTE: volumes which were created as linked clones of another base volume
+# are currently not tracking this relationship in their volume IDs. this is
+# generally not a problem, as LVM thin allows deletion of such base volumes
+# without affecting the linked clones. this leads to increased disk usage
+# when migrating LVM-thin volumes, which is normally prevented for linked clones.
+
 use base qw(PVE::Storage::LVMPlugin);
 
 sub type {
@@ -43,9 +50,13 @@ sub options {
         nodes => { optional => 1 },
        disable => { optional => 1 },
        content => { optional => 1 },
+       bwlimit => { optional => 1 },
     };
 }
 
+# NOTE: the fourth and fifth element of the returned array are always
+# undef, even if the volume is a linked clone of another volume. see note
+# at beginning of file.
 sub parse_volname {
     my ($class, $volname) = @_;
 
@@ -86,7 +97,7 @@ sub alloc_image {
 
     my $lvs = PVE::Storage::LVMPlugin::lvm_list_volumes($vg);
 
-    $name = PVE::Storage::LVMPlugin::lvm_find_free_diskname($lvs, $vg, $storeid, $vmid)
+    $name = PVE::Storage::LVMPlugin::lvm_find_free_diskname($lvs, $vg, $storeid, $vmid, $scfg)
        if !$name;
 
     my $cmd = ['/sbin/lvcreate', '-aly', '-V', "${size}k", '--name', $name,
@@ -163,6 +174,24 @@ sub list_images {
     return $res;
 }
 
+sub list_thinpools {
+    my ($vg) = @_;
+
+    my $lvs = PVE::Storage::LVMPlugin::lvm_list_volumes($vg);
+    my $thinpools = [];
+
+    foreach my $vg (keys %$lvs) {
+       foreach my $lvname (keys %{$lvs->{$vg}}) {
+           next if $lvs->{$vg}->{$lvname}->{lv_type} ne 't';
+           my $lv = $lvs->{$vg}->{$lvname};
+           $lv->{lv} = $lvname;
+           push @$thinpools, $lv;
+       }
+    }
+
+    return $thinpools;
+}
+
 sub status {
     my ($class, $storeid, $scfg, $cache) = @_;
 
@@ -195,6 +224,9 @@ sub activate_volume {
        my $snapvol = "snap_${volname}_$snapname";
        my $cmd = ['/sbin/lvchange', '-ay', '-K', "$vg/$snapvol"];
        run_command($cmd, errmsg => "activate_volume '$vg/$snapvol' error");
+    } elsif ($volname =~ /^base-/) {
+       my $cmd = ['/sbin/lvchange', '-ay', '-K', "$vg/$volname"];
+       run_command($cmd, errmsg => "activate_volume '$vg/$volname' error");
     } else {
        # other volumes are active by default
     }
@@ -210,6 +242,9 @@ sub deactivate_volume {
        my $snapvol = "snap_${volname}_$snapname";
        my $cmd = ['/sbin/lvchange', '-an', "$vg/$snapvol"];
        run_command($cmd, errmsg => "deactivate_volume '$vg/$snapvol' error");
+    } elsif ($volname =~ /^base-/) {
+       my $cmd = ['/sbin/lvchange', '-an', "$vg/$volname"];
+       run_command($cmd, errmsg => "deactivate_volume '$vg/$volname' error");
     } else {
        # other volumes are kept active
     }
@@ -235,7 +270,7 @@ sub clone_image {
 
     my $lvs = PVE::Storage::LVMPlugin::lvm_list_volumes($vg);
 
-    my $name =  PVE::Storage::LVMPlugin::lvm_find_free_diskname($lvs, $vg, $storeid, $vmid);
+    my $name =  PVE::Storage::LVMPlugin::lvm_find_free_diskname($lvs, $vg, $storeid, $vmid, $scfg);
 
     my $cmd = ['/sbin/lvcreate', '-n', $name, '-prw', '-kn', '-s', $lv];
     run_command($cmd, errmsg => "clone image '$lv' error");
@@ -322,6 +357,7 @@ sub volume_has_feature {
        clone => { base => 1, snap => 1},
        template => { current => 1},
        copy => { base => 1, current => 1, snap => 1},
+       sparseinit => { base => 1, current => 1},
     };
 
     my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
@@ -338,4 +374,11 @@ sub volume_has_feature {
     return undef;
 }
 
+# used in LVMPlugin->volume_import
+sub volume_import_write {
+    my ($class, $input_fh, $output_file) = @_;
+    run_command(['dd', "of=$output_file", 'conv=sparse', 'bs=64k'],
+       input => '<&'.fileno($input_fh));
+}
+
 1;