]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/ZFSPoolPlugin.pm
pvesm: import/export commands
[pve-storage.git] / PVE / Storage / ZFSPoolPlugin.pm
index ca0a2cf8c84d0e1f17e875ae1ad09941b67d2e7e..72da44dc38fbbd62cc71cb4a63b0f5513047982a 100644 (file)
@@ -7,6 +7,7 @@ use POSIX;
 use PVE::Tools qw(run_command);
 use PVE::Storage::Plugin;
 use PVE::RPCEnvironment;
+use Net::IP;
 
 use base qw(PVE::Storage::Plugin);
 
@@ -493,6 +494,33 @@ sub volume_rollback_is_possible {
     return 1; 
 }
 
+sub volume_snapshot_list {
+    my ($class, $scfg, $storeid, $volname, $prefix) = @_;
+
+    my ($vtype, $name, $vmid) = $class->parse_volname($volname);
+
+    my $zpath = "$scfg->{pool}/$name";
+
+    $prefix = '' if !defined($prefix);
+    my $snaps = [];
+
+    my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
+              'name', $zpath];
+
+    my $outfunc = sub {
+       my $line = shift;
+
+       if ($line =~ m/^\Q$zpath\E@(\Q$prefix\E.*)$/) {
+           push @$snaps, $1;
+       }
+    };
+
+    eval { run_command( [$cmd], outfunc => $outfunc , errfunc => sub{}); };
+
+    # return an empty array if dataset does not exist.
+    return $snaps;
+}
+
 sub activate_storage {
     my ($class, $storeid, $scfg, $cache) = @_;
 
@@ -605,6 +633,7 @@ sub volume_has_feature {
        template => { current => 1},
        copy => { base => 1, current => 1},
        sparseinit => { base => 1, current => 1},
+       replicate => { base => 1, current => 1},
     };
 
     my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
@@ -623,4 +652,66 @@ sub volume_has_feature {
     return undef;
 }
 
+sub volume_export {
+    my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
+
+    die "unsupported export stream format for $class: $format\n"
+       if $format ne 'zfs';
+
+    die "$class storage can only export snapshots\n"
+       if !defined($snapshot);
+
+    my $fd = fileno($fh);
+    die "internal error: invalid file handle for volume_export\n"
+       if !defined($fd);
+    $fd = ">&$fd";
+
+    # For zfs we always create a replication stream (-R) which means the remote
+    # side will always delete non-existing source snapshots. This should work
+    # for all our use cases.
+    my $cmd = ['zfs', 'send', '-Rpv'];
+    if (defined($base_snapshot)) {
+       my $arg = $with_snapshots ? '-I' : '-i';
+       push @$cmd, $arg, $base_snapshot;
+    }
+    push @$cmd, '--', "$scfg->{pool}/$volname\@$snapshot";
+
+    run_command($cmd, output => $fd);
+
+    return;
+}
+
+sub volume_import {
+    my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
+
+    die "unsupported import stream format for $class: $format\n"
+       if $format ne 'zfs';
+
+    my $fd = fileno($fh);
+    die "internal error: invalid file handle for volume_import\n"
+       if !defined($fd);
+
+    my $zfspath = "$scfg->{pool}/$volname";
+    my $suffix = defined($base_snapshot) ? "\@$base_snapshot" : '';
+    my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
+                            noerr => 1, errfunc => sub {});
+    if (defined($base_snapshot)) {
+       die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
+    } else {
+       die "volume '$zfspath' already exists\n" if $exists;
+    }
+
+    eval { run_command(['zfs', 'recv', '-F', '--', $zfspath], input => "<&$fd") };
+    if (my $err = $@) {
+       if (defined($base_snapshot)) {
+           eval { run_command(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
+       } else {
+           eval { run_command(['zfs', 'destroy', '-r', '--', $zfspath]) };
+       }
+       die $err;
+    }
+
+    return;
+}
+
 1;