]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/LVMPlugin.pm
drbd: comment that the builtin plugin is depreacated
[pve-storage.git] / PVE / Storage / LVMPlugin.pm
index 3be680b4d605a2f6364406be8b34f4e830639ef9..73e8e487a397e24022d274fb5ef0870e16847ae1 100644 (file)
@@ -13,6 +13,15 @@ use base qw(PVE::Storage::Plugin);
 
 # lvm helper functions
 
+my $ignore_no_medium_warnings = sub {
+    my $line = shift;
+    # ignore those, most of the time they're from (virtual) IPMI/iKVM devices
+    # and just spam the log..
+    if ($line !~ /open failed: No medium found/) {
+       print STDERR "$line\n";
+    }
+};
+
 sub lvm_pv_info {
     my ($device) = @_;
 
@@ -85,7 +94,7 @@ sub lvm_create_volume_group {
     $cmd = ['/sbin/vgcreate', $vgname, $device];
     # push @$cmd, '-c', 'y' if $shared; # we do not use this yet
 
-    run_command($cmd, errmsg => "vgcreate $vgname $device error");
+    run_command($cmd, errmsg => "vgcreate $vgname $device error", errfunc => $ignore_no_medium_warnings, outfunc => $ignore_no_medium_warnings);
 }
 
 sub lvm_vgs {
@@ -106,13 +115,15 @@ sub lvm_vgs {
     eval {
        run_command($cmd, outfunc => sub {
            my $line = shift;
-
            $line = trim($line);
 
            my ($name, $size, $free, $lvcount, $pvname, $pvsize, $pvfree) = split (':', $line);
 
-           $vgs->{$name} = { size => int ($size), free => int ($free), lvcount => int($lvcount) }
-               if !$vgs->{$name};
+           $vgs->{$name} //= {
+               size => int ($size),
+               free => int ($free),
+               lvcount => int($lvcount)
+           };
 
            if (defined($pvname) && defined($pvsize) && defined($pvfree)) {
                push @{$vgs->{$name}->{pvs}}, {
@@ -121,7 +132,9 @@ sub lvm_vgs {
                    free => int($pvfree),
                };
            }
-        });
+       },
+       errfunc => $ignore_no_medium_warnings,
+       );
     };
     my $err = $@;
 
@@ -135,9 +148,14 @@ sub lvm_vgs {
 sub lvm_list_volumes {
     my ($vgname) = @_;
 
-    my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
-              '--unbuffered', '--nosuffix', '--options',
-              'vg_name,lv_name,lv_size,lv_attr,pool_lv,data_percent,metadata_percent,snap_percent,uuid,tags,metadata_size'];
+    my $option_list = 'vg_name,lv_name,lv_size,lv_attr,pool_lv,data_percent,metadata_percent,snap_percent,uuid,tags,metadata_size,time';
+
+    my $cmd = [
+       '/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
+       '--unbuffered', '--nosuffix',
+       '--config', 'report/time_format="%s"',
+       '--options', $option_list,
+    ];
 
     push @$cmd, $vgname if $vgname;
 
@@ -147,7 +165,7 @@ sub lvm_list_volumes {
 
        $line = trim($line);
 
-       my ($vg_name, $lv_name, $lv_size, $lv_attr, $pool_lv, $data_percent, $meta_percent, $snap_percent, $uuid, $tags, $meta_size) = split(':', $line);
+       my ($vg_name, $lv_name, $lv_size, $lv_attr, $pool_lv, $data_percent, $meta_percent, $snap_percent, $uuid, $tags, $meta_size, $ctime) = split(':', $line);
        return if !$vg_name;
        return if !$lv_name;
 
@@ -159,6 +177,7 @@ sub lvm_list_volumes {
        };
        $d->{pool_lv} = $pool_lv if $pool_lv;
        $d->{tags} = $tags if $tags;
+       $d->{ctime} = $ctime;
 
        if ($lv_type eq 't') {
            $data_percent ||= 0;
@@ -169,7 +188,9 @@ sub lvm_list_volumes {
            $d->{used} = int(($data_percent * $lv_size)/100);
        }
        $lvs->{$vg_name}->{$lv_name} = $d;
-    });
+    },
+    errfunc => $ignore_no_medium_warnings,
+    );
 
     return $lvs;
 }
@@ -248,6 +269,8 @@ sub on_add_hook {
 
        lvm_create_volume_group($path, $scfg->{vgname}, $scfg->{shared});
     }
+
+    return;
 }
 
 sub parse_volname {
@@ -288,14 +311,34 @@ sub clone_image {
     die "can't clone images in lvm storage\n";
 }
 
-sub lvm_find_free_diskname {
-    my ($lvs, $vg, $storeid, $vmid, $scfg) = @_;
+sub find_free_diskname {
+    my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
+
+    my $vg = $scfg->{vgname};
+
+    my $lvs = lvm_list_volumes($vg);
 
     my $disk_list = [ keys %{$lvs->{$vg}} ];
 
     return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, undef, $scfg);
 }
 
+sub lvcreate {
+    my ($vg, $name, $size, $tags) = @_;
+
+    if ($size =~ m/\d$/) { # no unit is given
+       $size .= "k"; # default to kilobytes
+    }
+
+    my $cmd = ['/sbin/lvcreate', '-aly', '--size', $size, '--name', $name];
+    for my $tag (@$tags) {
+       push @$cmd, '--addtag', $tag;
+    }
+    push @$cmd, $vg;
+
+    run_command($cmd, errmsg => "lvcreate '$vg/$name' error");
+}
+
 sub alloc_image {
     my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
 
@@ -314,12 +357,10 @@ sub alloc_image {
 
     die "not enough free space ($free < $size)\n" if $free < $size;
 
-    $name = lvm_find_free_diskname(lvm_list_volumes($vg), $vg, $storeid, $vmid, $scfg)
+    $name = $class->find_free_diskname($storeid, $scfg, $vmid)
        if !$name;
 
-    my $cmd = ['/sbin/lvcreate', '-aly', '--addtag', "pve-vm-$vmid", '--size', "${size}k", '--name', $name, $vg];
-
-    run_command($cmd, errmsg => "lvcreate '$vg/pve-vm-$vmid' error");
+    lvcreate($vg, $name, $size, ["pve-vm-$vmid"]);
 
     return $name;
 }
@@ -362,6 +403,8 @@ sub free_image {
 
     my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"];
     run_command($cmd, errmsg => "can't activate LV '$vg/$volname' to zero-out its data");
+    $cmd = ['/sbin/lvchange', '--refresh', "$vg/$volname"];
+    run_command($cmd, errmsg => "can't refresh LV '$vg/$volname' to zero-out its data");
 
     if ($scfg->{saferemove}) {
        # avoid long running task, so we only rename here
@@ -416,6 +459,7 @@ sub list_images {
 
            push @$res, {
                volid => $volid, format => 'raw', size => $info->{lv_size}, vmid => $owner,
+               ctime => $info->{ctime},
            };
        }
     }
@@ -472,6 +516,8 @@ sub activate_volume {
 
     my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path];
     run_command($cmd, errmsg => "can't activate LV '$path'");
+    $cmd = ['/sbin/lvchange', '--refresh', $path];
+    run_command($cmd, errmsg => "can't refresh LV '$path' for activation");
 }
 
 sub deactivate_volume {
@@ -587,7 +633,7 @@ sub volume_import_formats {
 }
 
 sub volume_import {
-    my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
+    my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
     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"
@@ -601,29 +647,40 @@ sub volume_import {
 
     my $vg = $scfg->{vgname};
     my $lvs = lvm_list_volumes($vg);
-    die "volume $vg/$volname already exists\n"
-       if $lvs->{$vg}->{$volname};
+    if ($lvs->{$vg}->{$volname}) {
+       die "volume $vg/$volname already exists\n" if !$allow_rename;
+       warn "volume $vg/$volname already exists - importing with a different name\n";
+       $name = undef;
+    }
 
     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
+       my $oldname = $volname;
+       $volname = $allocname;
+       if (defined($name) && $allocname ne $oldname) {
            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));
+
+       $class->volume_import_write($fh, $file);
     };
     if (my $err = $@) {
        eval { $class->free_image($storeid, $scfg, $volname, 0) };
        warn $@ if $@;
        die $err;
     }
+
+    return "$storeid:$volname";
+}
+
+sub volume_import_write {
+    my ($class, $input_fh, $output_file) = @_;
+    run_command(['dd', "of=$output_file", 'bs=64k'],
+       input => '<&'.fileno($input_fh));
 }
 
 1;