[pve-devel] [PATCH v3 storage 1/6] pvesm: import/export commands

Wolfgang Bumiller w.bumiller at proxmox.com
Fri May 12 09:50:09 CEST 2017


---
Changes to v2:
  * stricter parameter types
  * typo fixes
  * zfs export always uses -R and -with-snapshots now switches between
    -I and -i.
 PVE/CLI/pvesm.pm             | 139 +++++++++++++++++++++++++++++++++++++++++++
 PVE/Storage.pm               |  41 +++++++++++++
 PVE/Storage/Plugin.pm        |  11 ++++
 PVE/Storage/ZFSPoolPlugin.pm |  61 +++++++++++++++++++
 4 files changed, 252 insertions(+)

diff --git a/PVE/CLI/pvesm.pm b/PVE/CLI/pvesm.pm
index 3b99436..ba2c91b 100755
--- a/PVE/CLI/pvesm.pm
+++ b/PVE/CLI/pvesm.pm
@@ -21,6 +21,8 @@ use PVE::CLIHandler;
 
 use base qw(PVE::CLIHandler);
 
+my $KNOWN_EXPORT_FORMATS = ['zfs'];
+
 my $nodename = PVE::INotify::nodename();
 
 sub setup_environment {
@@ -144,6 +146,141 @@ my $print_status = sub {
     }
 };
 
+__PACKAGE__->register_method ({
+    name => 'export',
+    path => 'export',
+    method => 'GET',
+    description => "Export a volume.",
+    protected => 1,
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    volume => {
+		description => "Volume identifier",
+		type => 'string',
+		completion => \&PVE::Storage::complete_volume,
+	    },
+	    format => {
+		description => "Export stream format",
+		type => 'string',
+		enum => $KNOWN_EXPORT_FORMATS,
+	    },
+	    filename => {
+		description => "Destination file name",
+		type => 'string',
+	    },
+	    base => {
+		description => "Snapshot to start an incremental stream from",
+		type => 'string',
+		pattern => qr/[a-z0-9_\-]{1,40}/,
+		maxLength => 40,
+		optional => 1,
+	    },
+	    snapshot => {
+		description => "Snapshot to export",
+		type => 'string',
+		pattern => qr/[a-z0-9_\-]{1,40}/,
+		maxLength => 40,
+		optional => 1,
+	    },
+	    'with-snapshots' => {
+		description =>
+		    "Whether to include intermediate snapshots in the stream",
+		type => 'boolean',
+		optional => 1,
+		default => 0,
+	    },
+	},
+    },
+    returns => { type => 'null' },
+    code => sub {
+	my ($param) = @_;
+
+	my $filename = $param->{filename};
+
+	my $outfh;
+	if ($filename eq '-') {
+	    $outfh = \*STDOUT;
+	} else {
+	    open($outfh, '>', $filename)
+		or die "open($filename): $!\n";
+	}
+
+	eval {
+	    my $cfg = PVE::Storage::config();
+	    PVE::Storage::volume_export($cfg, $outfh, $param->{volume}, $param->{format},
+		$param->{snapshot}, $param->{base}, $param->{'with-snapshots'});
+	};
+	my $err = $@;
+	if ($filename ne '-') {
+	    close($outfh);
+	    unlink($filename) if $err;
+	}
+	die $err if $err;
+	return;
+    }
+});
+
+__PACKAGE__->register_method ({
+    name => 'import',
+    path => 'import',
+    method => 'PUT',
+    description => "Import a volume.",
+    protected => 1,
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    volume => {
+		description => "Volume identifier",
+		type => 'string',
+		completion => \&PVE::Storage::complete_volume,
+	    },
+	    format => {
+		description => "Import stream format",
+		type => 'string',
+		enum => $KNOWN_EXPORT_FORMATS,
+	    },
+	    filename => {
+		description => "Source file name",
+		type => 'string',
+	    },
+	    base => {
+		description => "Base snapshot of an incremental stream",
+		type => 'string',
+		pattern => qr/[a-z0-9_\-]{1,40}/,
+		maxLength => 40,
+		optional => 1,
+	    },
+	    'with-snapshots' => {
+		description =>
+		    "Whether the stream includes intermediate snapshots",
+		type => 'boolean',
+		optional => 1,
+		default => 0,
+	    },
+	},
+    },
+    returns => { type => 'null' },
+    code => sub {
+	my ($param) = @_;
+
+	my $filename = $param->{filename};
+
+	my $infh;
+	if ($filename eq '-') {
+	    $infh = \*STDIN;
+	} else {
+	    open($infh, '<', $filename)
+		or die "open($filename): $!\n";
+	}
+
+	my $cfg = PVE::Storage::config();
+	PVE::Storage::volume_import($cfg, $infh, $param->{volume}, $param->{format},
+	    $param->{base}, $param->{'with-snapshots'});
+	return;
+    }
+});
+
 our $cmddef = {
     add => [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
     set => [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
@@ -217,6 +354,8 @@ our $cmddef = {
 		 }],
     path => [ __PACKAGE__, 'path', ['volume']],
     extractconfig => [__PACKAGE__, 'extractconfig', ['volume']],
+    export => [ __PACKAGE__, 'export', ['volume', 'format', 'filename']],
+    import => [ __PACKAGE__, 'import', ['volume', 'format', 'filename']],
 };
 
 1;
diff --git a/PVE/Storage.pm b/PVE/Storage.pm
index a4125d5..e1b50db 100755
--- a/PVE/Storage.pm
+++ b/PVE/Storage.pm
@@ -1453,6 +1453,47 @@ sub extract_vzdump_config {
     }
 }
 
+sub volume_export {
+    my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
+
+    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_export($scfg, $storeid, $fh, $volname, $format,
+	                              $snapshot, $base_snapshot, $with_snapshots);
+    } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
+	#die "TODO: cat raw files\n"
+	#    if $format eq 'raw'
+	#	&& !defined($snapshot)
+	#	&& !defined($base_snapshot)
+	#	&& !$with_snapshots;
+	die "volume export of raw files not supported\n";
+    } else {
+	die "cannot export volume '$volid'\n";
+    }
+}
+
+sub volume_import {
+    my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots) = @_;
+
+    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_import($scfg, $storeid, $fh, $volname, $format,
+	                              $base_snapshot, $with_snapshots);
+    } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
+	#die "TODO: cat raw files\n"
+	#    if $format eq 'raw'
+	#	&& !defined($base_snapshot)
+	#	&& !$with_snapshots;
+	die "volume import of raw files not supported\n";
+    } else {
+	die "cannot import volume '$volid'\n";
+    }
+}
+
 # bash completion helper
 
 sub complete_storage {
diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm
index b10e2d9..b7ec261 100644
--- a/PVE/Storage/Plugin.pm
+++ b/PVE/Storage/Plugin.pm
@@ -888,5 +888,16 @@ sub check_connection {
     return 1;
 }
 
+# Export a volume into a file handle as a stream of desired format.
+sub volume_export {
+    my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
+    die "volume export not implemented for $class";
+}
+
+# Import data from a stream, creating a new or replacing or adding to an existing volume.
+sub volume_import {
+    my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
+    die "volume import not implemented for $class";
+}
 
 1;
diff --git a/PVE/Storage/ZFSPoolPlugin.pm b/PVE/Storage/ZFSPoolPlugin.pm
index 0636ee1..faa232a 100644
--- a/PVE/Storage/ZFSPoolPlugin.pm
+++ b/PVE/Storage/ZFSPoolPlugin.pm
@@ -652,4 +652,65 @@ 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';
+
+    # FIXME: should we verify that $base_snapshot exists for nicer error
+    #        messages?
+
+    # FIXME: zfs recv doesn't actually support declaring $with_snapshots=false,
+    #        not sure if we actually need that case though?
+
+    my $fd = fileno($fh);
+    die "internal error: invalid file handle for volume_export\n"
+	if !defined($fd);
+    $fd = "<&$fd";
+
+    my $zfspath = "$scfg->{pool}/$volname";
+
+    my $cmd = ['zfs', 'recv', '-F'];
+    push @$cmd, '--', $zfspath;
+
+    eval { run_command($cmd, input => $fd) };
+    if (my $err = $@) {
+	$zfspath .= "\@$base_snapshot" if defined($base_snapshot);
+	eval { run_command(['zfs', 'rollback', '-r', $zfspath]) };
+	die $err;
+    }
+
+    return;
+}
+
 1;
-- 
2.11.0





More information about the pve-devel mailing list