]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/ZFSPlugin.pm
rbd plugin: free image: use actual command in error message
[pve-storage.git] / PVE / Storage / ZFSPlugin.pm
index 69c6ecc0c669c3ad69f7ae5cb77dd011ee98ed26..5b84d85f2efca5f1d96fe003d47c21bd415857eb 100644 (file)
@@ -5,12 +5,15 @@ use warnings;
 use IO::File;
 use POSIX;
 use PVE::Tools qw(run_command);
-use PVE::Storage::ZFSDirPlugin;
+use PVE::Storage::ZFSPoolPlugin;
+use PVE::RPCEnvironment;
 
-use base qw(PVE::Storage::ZFSDirPlugin);
+use base qw(PVE::Storage::ZFSPoolPlugin);
 use PVE::Storage::LunCmd::Comstar;
 use PVE::Storage::LunCmd::Istgt;
 use PVE::Storage::LunCmd::Iet;
+use PVE::Storage::LunCmd::LIO;
+
 
 my @ssh_opts = ('-o', 'BatchMode=yes');
 my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
@@ -29,7 +32,7 @@ my $lun_cmds = {
 my $zfs_unknown_scsi_provider = sub {
     my ($provider) = @_;
 
-    die "$provider: unknown iscsi provider. Available [comstar, istgt, iet]";
+    die "$provider: unknown iscsi provider. Available [comstar, istgt, iet, LIO]";
 };
 
 my $zfs_get_base = sub {
@@ -41,6 +44,8 @@ my $zfs_get_base = sub {
         return PVE::Storage::LunCmd::Istgt::get_base;
     } elsif ($scfg->{iscsiprovider} eq 'iet') {
         return PVE::Storage::LunCmd::Iet::get_base;
+    } elsif ($scfg->{iscsiprovider} eq 'LIO') {
+        return PVE::Storage::LunCmd::LIO::get_base;
     } else {
         $zfs_unknown_scsi_provider->($scfg->{iscsiprovider});
     }
@@ -49,7 +54,8 @@ my $zfs_get_base = sub {
 sub zfs_request {
     my ($class, $scfg, $timeout, $method, @params) = @_;
 
-    $timeout = 5 if !$timeout;
+    $timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 10
+       if !$timeout;
 
     my $msg = '';
 
@@ -60,6 +66,8 @@ sub zfs_request {
             $msg = PVE::Storage::LunCmd::Istgt::run_lun_command($scfg, $timeout, $method, @params);
         } elsif ($scfg->{iscsiprovider} eq 'iet') {
             $msg = PVE::Storage::LunCmd::Iet::run_lun_command($scfg, $timeout, $method, @params);
+        } elsif ($scfg->{iscsiprovider} eq 'LIO') {
+            $msg = PVE::Storage::LunCmd::LIO::run_lun_command($scfg, $timeout, $method, @params);
         } else {
             $zfs_unknown_scsi_provider->($scfg->{iscsiprovider});
         }
@@ -90,14 +98,12 @@ sub zfs_request {
 
 sub zfs_get_lu_name {
     my ($class, $scfg, $zvol) = @_;
-    my $object;
 
     my $base = $zfs_get_base->($scfg);
-    if ($zvol =~ /^.+\/.+/) {
-        $object = "$base/$zvol";
-    } else {
-        $object = "$base/$scfg->{pool}/$zvol";
-    }
+
+    $zvol = ($class->parse_volname($zvol))[1];
+
+    my $object = ($zvol =~ /^.+\/.+/) ? "$base/$zvol" : "$base/$scfg->{pool}/$zvol";
 
     my $lu_name = $class->zfs_request($scfg, undef, 'list_lu', $object);
 
@@ -109,8 +115,8 @@ sub zfs_get_lu_name {
 sub zfs_add_lun_mapping_entry {
     my ($class, $scfg, $zvol, $guid) = @_;
 
-    if (! defined($guid)) {
-    $guid = $class->zfs_get_lu_name($scfg, $zvol);
+    if (!defined($guid)) {
+       $guid = $class->zfs_get_lu_name($scfg, $zvol);
     }
 
     $class->zfs_request($scfg, undef, 'add_view', $guid);
@@ -153,7 +159,11 @@ sub zfs_get_lun_number {
 
     die "could not find lun_number for guid $guid" if !$guid;
 
-    return $class->zfs_request($scfg, undef, 'list_view', $guid);
+    if ($class->zfs_request($scfg, undef, 'list_view', $guid) =~ /^(\d+)$/) {
+       return $1;
+    }
+
+    die "lun_number for guid $guid is not a number";
 }
 
 # Configuration
@@ -164,65 +174,64 @@ sub type {
 
 sub plugindata {
     return {
-    content => [ {images => 1}, { images => 1 }],
+       content => [ {images => 1}, { images => 1 }],
     };
 }
 
 sub properties {
     return {
-    iscsiprovider => {
-        description => "iscsi provider",
-        type => 'string',
-    },
-    # this will disable write caching on comstar and istgt.
-    # it is not implemented for iet. iet blockio always operates with 
-    # writethrough caching when not in readonly mode
-    nowritecache => {
-        description => "disable write caching on the target",
-        type => 'boolean',
-    },
-    comstar_tg => {
-        description => "target group for comstar views",
-        type => 'string',
-    },
-    comstar_hg => {
-        description => "host group for comstar views",
-        type => 'string',
-    },
+       iscsiprovider => {
+           description => "iscsi provider",
+           type => 'string',
+       },
+       # this will disable write caching on comstar and istgt.
+       # it is not implemented for iet. iet blockio always operates with
+       # writethrough caching when not in readonly mode
+       nowritecache => {
+           description => "disable write caching on the target",
+           type => 'boolean',
+       },
+       comstar_tg => {
+           description => "target group for comstar views",
+           type => 'string',
+       },
+       comstar_hg => {
+           description => "host group for comstar views",
+           type => 'string',
+       },
+       lio_tpg => {
+           description => "target portal group for Linux LIO targets",
+           type => 'string',
+       },
     };
 }
 
 sub options {
     return {
-    nodes => { optional => 1 },
-    disable => { optional => 1 },
-    portal => { fixed => 1 },
-    target => { fixed => 1 },
-    pool => { fixed => 1 },
-    blocksize => { fixed => 1 },
-    iscsiprovider => { fixed => 1 },
-    nowritecache => { optional => 1 },
-    sparse => { optional => 1 },
-    comstar_hg => { optional => 1 },
-    comstar_tg => { optional => 1 },
-    content => { optional => 1 },
+       nodes => { optional => 1 },
+       disable => { optional => 1 },
+       portal => { fixed => 1 },
+       target => { fixed => 1 },
+       pool => { fixed => 1 },
+       blocksize => { fixed => 1 },
+       iscsiprovider => { fixed => 1 },
+       nowritecache => { optional => 1 },
+       sparse => { optional => 1 },
+       comstar_hg => { optional => 1 },
+       comstar_tg => { optional => 1 },
+       lio_tpg => { optional => 1 },
+       content => { optional => 1 },
+       bwlimit => { optional => 1 },
     };
 }
 
 # Storage implementation
 
-sub parse_volname {
-    my ($class, $volname) = @_;
-
-    if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
-    return ('images', $5, $8, $2, $4, $6);
-    }
-
-    die "unable to parse zfs volume name '$volname'\n";
-}
-
 sub path {
-    my ($class, $scfg, $volname) = @_;
+    my ($class, $scfg, $volname, $storeid, $snapname) = @_;
+
+    die "direct access to snapshots not implemented"
+       if defined($snapname);
 
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
 
@@ -268,40 +277,35 @@ sub create_base {
 sub clone_image {
     my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
 
-    $snap ||= '__base__';
+    my $name = $class->SUPER::clone_image($scfg, $storeid, $volname, $vmid, $snap);
 
-    my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
-        $class->parse_volname($volname);
-
-    die "clone_image only works on base images\n" if !$isBase;
-
-    my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid);
-
-    warn "clone $volname: $basename to $name\n";
+    # get ZFS dataset name from PVE volname
+    my (undef, $clonedname) = $class->parse_volname($name);
 
-    $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
-
-    my $guid = $class->zfs_create_lu($scfg, $name);
-    $class->zfs_add_lun_mapping_entry($scfg, $name, $guid);
+    my $guid = $class->zfs_create_lu($scfg, $clonedname);
+    $class->zfs_add_lun_mapping_entry($scfg, $clonedname, $guid);
 
     return $name;
 }
 
 sub alloc_image {
     my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
-
+    
     die "unsupported format '$fmt'" if $fmt ne 'raw';
 
-    die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
+    die "illegal name '$name' - should be 'vm-$vmid-*'\n"
     if $name && $name !~ m/^vm-$vmid-/;
 
-    $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid) if !$name;
+    my $volname = $name;
 
-    $class->zfs_create_zvol($scfg, $name, $size);
-    my $guid = $class->zfs_create_lu($scfg, $name);
-    $class->zfs_add_lun_mapping_entry($scfg, $name, $guid);
+    $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt) if !$volname;
+    
+    $class->zfs_create_zvol($scfg, $volname, $size);
+    my $guid = $class->zfs_create_lu($scfg, $volname);
+    $class->zfs_add_lun_mapping_entry($scfg, $volname, $guid);
 
-    return $name;
+    return $volname;
 }
 
 sub free_image {
@@ -310,134 +314,41 @@ sub free_image {
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
 
     $class->zfs_delete_lu($scfg, $name);
-    eval {
-        $class->zfs_delete_zvol($scfg, $name);
-    };
-    do {
-        my $err = $@;
+
+    eval { $class->zfs_delete_zvol($scfg, $name); };
+    if (my $err = $@) {
         my $guid = $class->zfs_create_lu($scfg, $name);
         $class->zfs_add_lun_mapping_entry($scfg, $name, $guid);
         die $err;
-    } if $@;
-
-    return undef;
-}
-
-sub list_images {
-    my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
-
-    $cache->{zfs} = $class->zfs_list_zvol($scfg) if !$cache->{zfs};
-    my $zfspool = $scfg->{pool};
-    my $res = [];
-
-    if (my $dat = $cache->{zfs}->{$zfspool}) {
-
-    foreach my $image (keys %$dat) {
-
-        my $volname = $dat->{$image}->{name};
-        my $parent = $dat->{$image}->{parent};
-
-        my $volid = undef;
-            if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
-        my ($basename) = ($1);
-        $volid = "$storeid:$basename/$volname";
-        } else {
-        $volid = "$storeid:$volname";
-        }
-
-        my $owner = $dat->{$volname}->{vmid};
-        if ($vollist) {
-        my $found = grep { $_ eq $volid } @$vollist;
-        next if !$found;
-        } else {
-        next if defined ($vmid) && ($owner ne $vmid);
-        }
-
-        my $info = $dat->{$volname};
-        $info->{volid} = $volid;
-        push @$res, $info;
-    }
     }
 
-    return $res;
-}
-
-sub status {
-    my ($class, $storeid, $scfg, $cache) = @_;
-
-    my $total = 0;
-    my $free = 0;
-    my $used = 0;
-    my $active = 0;
-
-    eval {
-    ($free, $used) = $class->zfs_get_pool_stats($scfg);
-    $active = 1;
-    $total = $free + $used;
-    };
-    warn $@ if $@;
-
-    return ($total, $free, $used, $active);
-}
-
-sub activate_storage {
-    my ($class, $storeid, $scfg, $cache) = @_;
-    return 1;
-}
-
-sub deactivate_storage {
-    my ($class, $storeid, $scfg, $cache) = @_;
-    return 1;
-}
-
-sub activate_volume {
-    my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
-    return 1;
-}
-
-sub deactivate_volume {
-    my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
-    return 1;
-}
-
-sub volume_size_info {
-    my ($class, $scfg, $storeid, $volname, $timeout) = @_;
-
-    return $class->zfs_get_zvol_size($scfg, $volname);
+    return undef;
 }
 
 sub volume_resize {
     my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
 
-    my $new_size = ($size/1024);
+    $volname = ($class->parse_volname($volname))[1];
+
+    my $new_size = $class->SUPER::volume_resize($scfg, $storeid, $volname, $size, $running);
 
-    $class->zfs_request($scfg, undef, 'set', 'volsize=' . $new_size . 'k', "$scfg->{pool}/$volname");
     $class->zfs_resize_lu($scfg, $volname, $new_size);
+
+    return $new_size;
 }
 
-sub volume_snapshot {
+sub volume_snapshot_delete {
     my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
 
-    $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
+    $volname = ($class->parse_volname($volname))[1];
+
+    $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
 }
 
 sub volume_snapshot_rollback {
     my ($class, $scfg, $storeid, $volname, $snap) = @_;
 
-    # abort rollback if snapshot is not the latest
-    my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
-    my $text = $class->zfs_request($scfg, undef, 'list', @params);
-    my @snapshots = split(/\n/, $text);
-    my $recentsnap = undef;
-    foreach (@snapshots) {
-        if (/$scfg->{pool}\/$volname/) {
-            s/^.*@//;
-            $recentsnap = $_;
-        } 
-    }
-    if ($snap ne $recentsnap) {
-        die "cannot rollback, more recent snapshots exist\n";
-    }
+    $volname = ($class->parse_volname($volname))[1];
 
     $class->zfs_delete_lu($scfg, $volname);
 
@@ -448,31 +359,31 @@ sub volume_snapshot_rollback {
     $class->zfs_add_lun_mapping_entry($scfg, $volname);
 }
 
-sub volume_snapshot_delete {
-    my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
+sub storage_can_replicate {
+    my ($class, $scfg, $storeid, $format) = @_;
 
-    $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
+    return 0;
 }
 
 sub volume_has_feature {
     my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
 
     my $features = {
-    snapshot => { current => 1, snap => 1},
-    clone => { base => 1},
-    template => { current => 1},
-    copy => { base => 1, current => 1},
+       snapshot => { current => 1, snap => 1},
+       clone => { base => 1},
+       template => { current => 1},
+       copy => { base => 1, current => 1},
     };
 
     my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
-    $class->parse_volname($volname);
+       $class->parse_volname($volname);
 
     my $key = undef;
 
     if ($snapname) {
-    $key = 'snap';
+       $key = 'snap';
     } else {
-    $key = $isBase ? 'base' : 'current';
+       $key = $isBase ? 'base' : 'current';
     }
 
     return 1 if $features->{$feature}->{$key};
@@ -480,4 +391,38 @@ sub volume_has_feature {
     return undef;
 }
 
+sub volume_snapshot_list {
+    my ($class, $scfg, $storeid, $volname) = @_;
+    # return an empty array if dataset does not exist.
+    die "Volume_snapshot_list is not implemented for ZFS over iSCSI.\n";
+}
+
+sub activate_storage {
+    my ($class, $storeid, $scfg, $cache) = @_;
+
+    return 1;
+}
+
+sub deactivate_storage {
+    my ($class, $storeid, $scfg, $cache) = @_;
+
+    return 1;
+}
+
+sub activate_volume {
+    my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
+
+    die "unable to activate snapshot from remote zfs storage" if $snapname;
+
+    return 1;
+}
+
+sub deactivate_volume {
+    my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
+
+    die "unable to deactivate snapshot from remote zfs storage" if $snapname;
+
+    return 1;
+}
+
 1;