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

Wolfgang Bumiller w.bumiller at proxmox.com
Thu May 11 10:24:08 CEST 2017


---
 PVE/CLI/pvesm.pm             | 129 +++++++++++++++++++++++++++++++++++++++++++
 PVE/Storage.pm               |  41 ++++++++++++++
 PVE/Storage/Plugin.pm        |  11 ++++
 PVE/Storage/ZFSPoolPlugin.pm |  49 ++++++++++++++++
 4 files changed, 230 insertions(+)

diff --git a/PVE/CLI/pvesm.pm b/PVE/CLI/pvesm.pm
index 3b99436..da7b2ba 100755
--- a/PVE/CLI/pvesm.pm
+++ b/PVE/CLI/pvesm.pm
@@ -144,6 +144,133 @@ 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',
+	    },
+	    filename => {
+		description => "Destination file name",
+		type => 'string',
+	    },
+	    base => {
+		description => "Snapshot to start an incremental stream from",
+		type => 'string',
+		optional => 1,
+	    },
+	    snapshot => {
+		description => "Snapshot to export",
+		type => 'string',
+		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',
+	    },
+	    filename => {
+		description => "Source file name",
+		type => 'string',
+	    },
+	    base => {
+		description => "Base snapshot of an incremental stream",
+		type => 'string',
+		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 +344,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..1b34666 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..ceeb065 100644
--- a/PVE/Storage/ZFSPoolPlugin.pm
+++ b/PVE/Storage/ZFSPoolPlugin.pm
@@ -652,4 +652,53 @@ 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";
+
+    my $cmd = ['zfs', 'send', '-pv'];
+    push @$cmd, '-R' if $with_snapshots;
+    push @$cmd, '-I', $base_snapshot if defined($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 $cmd = ['zfs', 'recv', '-F'];
+    push @$cmd, '--', "$scfg->{pool}/$volname";
+
+    run_command($cmd, input => $fd);
+
+    return;
+}
+
 1;
-- 
2.11.0





More information about the pve-devel mailing list