]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage.pm
Include new storage function volume_snapshot_list.
[pve-storage.git] / PVE / Storage.pm
index 25ff545527f3de376db5aa1786561d1b053d0f0e..8fe9f4b013a873a8101deaec27ce7380b8b1cf93 100755 (executable)
@@ -12,7 +12,7 @@ use File::Path;
 use Cwd 'abs_path';
 use Socket;
 
-use PVE::Tools qw(run_command file_read_firstline $IPV6RE);
+use PVE::Tools qw(run_command file_read_firstline dir_glob_foreach $IPV6RE);
 use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
 use PVE::Exception qw(raise_param_exc);
 use PVE::JSONSchema;
@@ -33,7 +33,10 @@ use PVE::Storage::ZFSPoolPlugin;
 use PVE::Storage::ZFSPlugin;
 use PVE::Storage::DRBDPlugin;
 
-# load and initialize all plugins
+# Storage API version. Icrement it on changes in storage API interface.
+use constant APIVER => 1;
+
+# load standard plugins
 PVE::Storage::DirPlugin->register();
 PVE::Storage::LVMPlugin->register();
 PVE::Storage::LvmThinPlugin->register();
@@ -46,6 +49,34 @@ PVE::Storage::GlusterfsPlugin->register();
 PVE::Storage::ZFSPoolPlugin->register();
 PVE::Storage::ZFSPlugin->register();
 PVE::Storage::DRBDPlugin->register();
+
+# load third-party plugins
+if ( -d '/usr/share/perl5/PVE/Storage/Custom' ) {
+    dir_glob_foreach('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub {
+       my ($file) = @_;
+       my $modname = 'PVE::Storage::Custom::' . $file;
+       $modname =~ s!\.pm$!!;
+       $file = 'PVE/Storage/Custom/' . $file;
+
+       eval {
+           require $file;
+       };
+       if ($@) {
+           warn $@;
+       # Check storage API version and that file is really storage plugin.
+       } elsif ($modname->isa('PVE::Storage::Plugin') && $modname->can('api') && $modname->api() == APIVER) {
+            eval {
+               import $file;
+               $modname->register();
+            };
+            warn $@ if $@;
+       } else {
+           warn "Error loading storage plugin \"$modname\" because of API version mismatch. Please, update it.\n"
+       }
+    });
+}
+
+# initialize all plugins
 PVE::Storage::Plugin->init();
 
 my $UDEVADM = '/sbin/udevadm';
@@ -185,6 +216,24 @@ sub volume_snapshot {
     }
 }
 
+sub volume_send {
+    my ($cfg, $volid, $snap, $ip, $incremental_snap, $verbose, $limit,
+       $target_path) = @_;
+
+    my ($storeid, $volname) = parse_volume_id($volid, 1);
+    if ($storeid) {
+       my $scfg = storage_config($cfg, $storeid);
+       my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+       return $plugin->volume_send($scfg, $storeid, $volname, $ip, $snap,
+                                   $incremental_snap, $verbose, $limit, $target_path);
+
+    } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
+       die "send file/device '$volid' is not possible\n";
+    } else {
+       die "unable to parse volume ID '$volid'\n";
+    }
+}
+
 sub volume_snapshot_rollback {
     my ($cfg, $volid, $snap) = @_;
 
@@ -231,6 +280,23 @@ sub volume_has_feature {
     }
 }
 
+sub volume_snapshot_list {
+    my ($cfg, $volid, $prefix, $ip) = @_;
+
+    my ($storeid, $volname) = parse_volume_id($volid, 1);
+    if ($storeid) {
+       my $scfg = storage_config($cfg, $storeid);
+       my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+       return $plugin->volume_snapshot_list($scfg, $storeid, $volname, $prefix, $ip);
+    } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
+       die "send file/device '$volid' is not possible\n";
+    } else {
+       die "unable to parse volume ID '$volid'\n";
+    }
+    # return an empty array if dataset does not exist.
+    # youngest snap first
+}
+
 sub get_image_dir {
     my ($cfg, $storeid, $vmid) = @_;
 
@@ -290,6 +356,9 @@ sub parse_vmid {
     return int($vmid);
 }
 
+# NOTE: basename and basevmid are always undef for LVM-thin, where the
+# clone -> base reference is not encoded in the volume ID.
+# see note in PVE::Storage::LvmThinPlugin for details.
 sub parse_volname {
     my ($cfg, $volid) = @_;
 
@@ -310,25 +379,71 @@ sub parse_volume_id {
     return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
 }
 
-sub volume_is_base {
-    my ($cfg, $volid) = @_;
+# test if we have read access to volid
+sub check_volume_access {
+    my ($rpcenv, $user, $cfg, $vmid, $volid) = @_;
 
     my ($sid, $volname) = parse_volume_id($volid, 1);
-    return 0 if !$sid;
-
-    if (my $scfg = $cfg->{ids}->{$sid}) {
-       my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
-       my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
-           $plugin->parse_volname($volname);
-       return $isBase ? 1 : 0;
-    } else {
-       # stale volid with undefined storage - so we can just guess
-       if ($volid =~ m/base-/) {
-           return 1;
+    if ($sid) {
+       my ($vtype, undef, $ownervm) = parse_volname($cfg, $volid);
+       if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
+           # we simply allow access
+       } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
+           # we are owner - allow access
+       } elsif ($vtype eq 'backup' && $ownervm) {
+           $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
+           $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']);
+       } else {
+           # allow if we are Datastore administrator
+           $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']);
        }
+    } else {
+       die "Only root can pass arbitrary filesystem paths."
+           if $user ne 'root@pam';
     }
 
+    return undef;
+}
+
+my $volume_is_base_and_used__no_lock = sub {
+    my ($scfg, $storeid, $plugin, $volname) = @_;
+
+    my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
+       $plugin->parse_volname($volname);
+
+    if ($isBase) {
+       my $vollist = $plugin->list_images($storeid, $scfg);
+       foreach my $info (@$vollist) {
+           my (undef, $tmpvolname) = parse_volume_id($info->{volid});
+           my $basename = undef;
+           my $basevmid = undef;
+
+           eval{
+               (undef, undef, undef, $basename, $basevmid) =
+                   $plugin->parse_volname($tmpvolname);
+           };
+
+           if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
+               return 1;
+           }
+       }
+    }
     return 0;
+};
+
+# NOTE: this check does not work for LVM-thin, where the clone -> base
+# reference is not encoded in the volume ID.
+# see note in PVE::Storage::LvmThinPlugin for details.
+sub volume_is_base_and_used {
+    my ($cfg, $volid) = @_;
+
+    my ($storeid, $volname) = parse_volume_id($volid);
+    my $scfg = storage_config($cfg, $storeid);
+    my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+
+    $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
+       return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
+    });
 }
 
 # try to map a filesystem path to a volume identifier
@@ -532,12 +647,13 @@ sub storage_migrate {
            eval{
                run_command($send);
            };
-           my $err;
-           if ($err = $@){
-               run_command(['zfs', 'destroy', "$zfspath\@__migration__"]);
-               die $err;
-           }
-           run_command($destroy_target);
+           my $err = $@;
+           warn "zfs send/receive failed, cleaning up snapshot(s)..\n" if $err;
+           eval { run_command(['zfs', 'destroy', "$zfspath\@__migration__"]); };
+           warn "could not remove source snapshot: $@\n" if $@;
+           eval { run_command($destroy_target); };
+           warn "could not remove target snapshot: $@\n" if $@;
+           die $err if $err;
 
        } else {
            die "$errstr - target type $tcfg->{type} is not valid\n";
@@ -559,11 +675,11 @@ sub storage_migrate {
 
            eval {
                if ($tcfg->{type} eq 'lvmthin') {
-                   run_command([["dd", "if=$src"],["/usr/bin/ssh", "root\@${target_host}",
-                             "dd", 'conv=sparse', "of=$dst"]]);
+                   run_command([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
+                             "dd", 'conv=sparse', "of=$dst", "bs=4k"]]);
                } else {
-                   run_command([["dd", "if=$src"],["/usr/bin/ssh", "root\@${target_host}",
-                             "dd", "of=$dst"]]);
+                   run_command([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
+                             "dd", "of=$dst", "bs=4k"]]);
                }
            };
            if (my $err = $@) {
@@ -651,9 +767,7 @@ sub vdisk_free {
     my ($cfg, $volid) = @_;
 
     my ($storeid, $volname) = parse_volume_id($volid);
-
     my $scfg = storage_config($cfg, $storeid);
-
     my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
 
     activate_storage($cfg, $storeid);
@@ -662,27 +776,12 @@ sub vdisk_free {
 
     # lock shared storage
     $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
+       # LVM-thin allows deletion of still referenced base volumes!
+       die "base volume '$volname' is still in use by linked clones\n"
+           if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
 
-       my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
+       my (undef, undef, undef, undef, undef, $isBase, $format) =
            $plugin->parse_volname($volname);
-       if ($isBase) {
-           my $vollist = $plugin->list_images($storeid, $scfg);
-           foreach my $info (@$vollist) {
-               my (undef, $tmpvolname) = parse_volume_id($info->{volid});
-               my $basename = undef;
-               my $basevmid = undef;
-
-               eval{
-                   (undef, undef, undef, $basename, $basevmid) =
-                       $plugin->parse_volname($tmpvolname);
-               };
-
-               if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
-                   die "base volume '$volname' is still in use " .
-                       "(use by '$tmpvolname')\n";
-               }
-           }
-       }
        $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
     });
 
@@ -971,9 +1070,9 @@ sub storage_info {
     my $ids = $cfg->{ids};
 
     my $info = {};
-    
+
     my @ctypes = PVE::Tools::split_list($content);
-    
+
     my $slist = [];
     foreach my $storeid (keys %$ids) {
 
@@ -989,7 +1088,7 @@ sub storage_info {
            }
            next if !$want_ctype;
        }
-       
+
        my $type = $ids->{$storeid}->{type};
 
        $info->{$storeid} = {
@@ -1022,9 +1121,9 @@ sub storage_info {
        eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
        warn $@ if $@;
        next if !$active;
-       $info->{$storeid}->{total} = $total;
-       $info->{$storeid}->{avail} = $avail;
-       $info->{$storeid}->{used} = $used;
+       $info->{$storeid}->{total} = int($total);
+       $info->{$storeid}->{avail} = int($avail);
+       $info->{$storeid}->{used} = int($used);
        $info->{$storeid}->{active} = $active;
     }
 
@@ -1263,7 +1362,7 @@ sub extract_vzdump_config_tar {
 
     my $file;
     while (defined($file = <$fh>)) {
-       if ($file =~ m!$conf_re!) {
+       if ($file =~ $conf_re) {
            $file = $1; # untaint
            last;
        }
@@ -1331,7 +1430,7 @@ sub extract_vzdump_config_vma {
        my $rerr = $@;
 
        # use exit code if no stderr output and not just broken pipe
-       if (!$errstring && !$broken_pipe && $rc > 0 && $rc != 141) {
+       if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) {
            die "$rerr\n" if $rerr;
            die "config extraction failed with exit code $rc\n";
        }
@@ -1349,9 +1448,9 @@ sub extract_vzdump_config {
 
     my $archive = abs_filesystem_path($cfg, $volid);
 
-    if ($volid =~ /\/vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
-       return extract_vzdump_config_tar($archive,'^(\./etc/vzdump/(pct|vps)\.conf)$');
-    } elsif ($volid =~ /\/vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
+    if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
+       return extract_vzdump_config_tar($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
+    } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
        my $format;
        my $comp;
        if ($7 eq 'tgz') {