]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage.pm
pvesm add: pass storage type as first argument
[pve-storage.git] / PVE / Storage.pm
index 3d01e37997bc8728ccf6b73baf7390c3105e3806..e46bc7767cafad6209a2e8506de295c0db7ccf3e 100755 (executable)
@@ -1,6 +1,8 @@
 package PVE::Storage;
 
 use strict;
+use warnings;
+
 use POSIX;
 use IO::Select;
 use IO::File;
@@ -24,8 +26,9 @@ use PVE::Storage::ISCSIPlugin;
 use PVE::Storage::RBDPlugin;
 use PVE::Storage::SheepdogPlugin;
 use PVE::Storage::ISCSIDirectPlugin;
-use PVE::Storage::NexentaPlugin;
 use PVE::Storage::GlusterfsPlugin;
+use PVE::Storage::ZFSPoolPlugin;
+use PVE::Storage::ZFSPlugin;
 
 # load and initialize all plugins
 PVE::Storage::DirPlugin->register();
@@ -35,8 +38,9 @@ PVE::Storage::ISCSIPlugin->register();
 PVE::Storage::RBDPlugin->register();
 PVE::Storage::SheepdogPlugin->register();
 PVE::Storage::ISCSIDirectPlugin->register();
-PVE::Storage::NexentaPlugin->register();
 PVE::Storage::GlusterfsPlugin->register();
+PVE::Storage::ZFSPoolPlugin->register();
+PVE::Storage::ZFSPlugin->register();
 PVE::Storage::Plugin->init();
 
 my $UDEVADM = '/sbin/udevadm';
@@ -134,9 +138,24 @@ sub volume_resize {
         my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
         return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
     } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
-        die "resize device is not possible";
+        die "resize file/device '$volid' is not possible\n";
+    } else {
+       die "unable to parse volume ID '$volid'\n";
+    }
+}
+
+sub volume_rollback_is_possible {
+    my ($cfg, $volid, $snap) = @_;
+    
+    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_rollback_is_possible($scfg, $storeid, $volname, $snap);
+    } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
+        die "snapshot rollback file/device '$volid' is not possible\n";
     } else {
-        die "can't resize";
+       die "unable to parse volume ID '$volid'\n";
     }
 }
 
@@ -149,9 +168,9 @@ sub volume_snapshot {
         my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
         return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap, $running);
     } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
-        die "snapshot device is not possible";
+        die "snapshot file/device '$volid' is not possible\n";
     } else {
-        die "can't snapshot";
+       die "unable to parse volume ID '$volid'\n";
     }
 }
 
@@ -162,11 +181,12 @@ sub volume_snapshot_rollback {
     if ($storeid) {
         my $scfg = storage_config($cfg, $storeid);
         my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+       $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
         return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
     } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
-        die "snapshot rollback device is not possible";
+        die "snapshot rollback file/device '$volid' is not possible\n";
     } else {
-        die "can't snapshot";
+       die "unable to parse volume ID '$volid'\n";
     }
 }
 
@@ -179,9 +199,9 @@ sub volume_snapshot_delete {
         my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
         return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
     } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
-        die "snapshot delete device is not possible";
+        die "snapshot delete file/device '$volid' is not possible\n";
     } else {
-        die "can't delete snapshot";
+       die "unable to parse volume ID '$volid'\n";
     }
 }
 
@@ -259,6 +279,17 @@ sub parse_vmid {
     return int($vmid);
 }
 
+sub parse_volname {
+    my ($cfg, $volid) = @_;
+
+    my ($storeid, $volname) = parse_volume_id($volid);
+
+    my $scfg = storage_config($cfg, $storeid);
+
+    my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+    return $plugin->parse_volname($volname);
+}
+
 sub parse_volume_id {
     my ($volid, $noerr) = @_;
 
@@ -361,6 +392,27 @@ sub path {
     return wantarray ? ($path, $owner, $vtype) : $path;
 }
 
+sub abs_filesystem_path {
+    my ($cfg, $volid) = @_;
+
+    my $path;
+    if (PVE::Storage::parse_volume_id ($volid, 1)) {
+       PVE::Storage::activate_volumes($cfg, [ $volid ]);
+       $path = PVE::Storage::path($cfg, $volid);
+    } else {
+       if (-f $volid) {
+           my $abspath = abs_path($volid);
+           if ($abspath && $abspath =~ m|^(/.+)$|) {
+               $path = $1; # untaint any path
+           }
+       }
+    }
+
+    die "can't find file '$volid'\n" if !($path && -f $path);
+
+    return $path;
+}
+
 sub storage_migrate {
     my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
 
@@ -444,7 +496,7 @@ sub storage_migrate {
 }
 
 sub vdisk_clone {
-    my ($cfg, $volid, $vmid) = @_;
+    my ($cfg, $volid, $vmid, $snap) = @_;
 
     my ($storeid, $volname) = parse_volume_id($volid);
 
@@ -456,7 +508,7 @@ sub vdisk_clone {
 
     # lock shared storage
     return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
-       my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid);
+       my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
        return "$storeid:$volname";
     });
 }
@@ -543,7 +595,7 @@ sub vdisk_free {
                }
            }
        }
-       my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase);
+       $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase);
     });
 
     return if !$cleanup_worker;
@@ -875,6 +927,25 @@ sub scan_nfs {
     return $res;
 }
 
+sub scan_zfs {
+
+    my $cmd = ['zpool',  'list', '-H', '-o', 'name,size,free'];
+
+    my $res = [];
+    run_command($cmd, outfunc => sub {
+       my $line = shift;
+
+       if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
+           my ($pool, $size_str, $free_str) = ($1, $2, $3);
+           my $size = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($size_str);
+           my $free = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($free_str);
+           push @$res, { pool => $pool, size => $size, free => $free };
+       }
+    });
+
+    return $res;
+}
+
 sub resolv_portal {
     my ($portal, $noerr) = @_;