From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id 375F01FF15E
	for <inbox@lore.proxmox.com>; Tue, 11 Mar 2025 15:24:18 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 4ECC512403;
	Tue, 11 Mar 2025 15:23:41 +0100 (CET)
From: Filip Schauer <f.schauer@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue, 11 Mar 2025 15:23:23 +0100
Message-Id: <20250311142328.112538-5-f.schauer@proxmox.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20250311142328.112538-1-f.schauer@proxmox.com>
References: <20250311142328.112538-1-f.schauer@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.168 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 POISEN_SPAM_PILL          0.1 Meta: its spam
 POISEN_SPAM_PILL_1        0.1 random spam to be learned in bayes
 POISEN_SPAM_PILL_3        0.1 random spam to be learned in bayes
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH storage v7 4/9] api: content: implement copying
 volumes between storages
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

Add the ability to copy an iso, snippet or vztmpl between storages and
nodes.

Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
---
 src/PVE/API2/Storage/Content.pm | 117 ++++++++++++++++++++------------
 1 file changed, 75 insertions(+), 42 deletions(-)

diff --git a/src/PVE/API2/Storage/Content.pm b/src/PVE/API2/Storage/Content.pm
index fe0ad4a..5996540 100644
--- a/src/PVE/API2/Storage/Content.pm
+++ b/src/PVE/API2/Storage/Content.pm
@@ -2,7 +2,6 @@ package PVE::API2::Storage::Content;
 
 use strict;
 use warnings;
-use Data::Dumper;
 
 use PVE::SafeSyslog;
 use PVE::Cluster;
@@ -399,6 +398,19 @@ __PACKAGE__->register_method ({
 	return undef;
     }});
 
+my $volume_remove = sub {
+    my ($cfg, $volid) = @_;
+
+    my ($path, undef, $vtype) = PVE::Storage::path($cfg, $volid);
+
+    PVE::Storage::vdisk_free ($cfg, $volid);
+    if ($vtype eq 'backup'
+	&& $path =~ /(.*\/vzdump-\w+-\d+-\d{4}_\d{2}_\d{2}-\d{2}_\d{2}_\d{2})[^\/]+$/) {
+	# Remove log file #318 and notes file #3972 if they still exist
+	PVE::Storage::archive_auxiliaries_remove($path);
+    }
+};
+
 __PACKAGE__->register_method ({
     name => 'delete',
     path => '{volume}',
@@ -452,13 +464,8 @@ __PACKAGE__->register_method ({
 	}
 
 	my $worker = sub {
-	    PVE::Storage::vdisk_free ($cfg, $volid);
+	    &$volume_remove($cfg, $volid);
 	    print "Removed volume '$volid'\n";
-	    if ($vtype eq 'backup'
-		&& $path =~ /(.*\/vzdump-\w+-\d+-\d{4}_\d{2}_\d{2}-\d{2}_\d{2}_\d{2})[^\/]+$/) {
-		# Remove log file #318 and notes file #3972 if they still exist
-		PVE::Storage::archive_auxiliaries_remove($path);
-	    }
 	};
 
 	my $id = (defined $ownervm ? "$ownervm@" : '') . $storeid;
@@ -487,26 +494,43 @@ __PACKAGE__->register_method ({
     name => 'copy',
     path => '{volume}',
     method => 'POST',
-    description => "Copy a volume. This is experimental code - do not use.",
+    description => "Copy a volume.",
+    permissions => {
+	description => "If the --delete option is used, the 'Datastore.Allocate' privilege is"
+	    ." required on the source storage."
+	    ." Without --delete, 'Datastore.AllocateSpace' is required on the target storage.",
+	user => 'all',
+    },
     protected => 1,
     proxyto => 'node',
     parameters => {
-    	additionalProperties => 0,
+	additionalProperties => 0,
 	properties => {
 	    node => get_standard_option('pve-node'),
-	    storage => get_standard_option('pve-storage-id', { optional => 1}),
+	    storage => get_standard_option('pve-storage-id', {
+		optional => 1,
+		completion => \&PVE::Storage::complete_storage_enabled,
+	    }),
 	    volume => {
 		description => "Source volume identifier",
 		type => 'string',
+		completion => \&PVE::Storage::complete_volume,
 	    },
-	    target => {
-		description => "Target volume identifier",
-		type => 'string',
-	    },
-	    target_node => get_standard_option('pve-node',  {
+	    'target-storage' => get_standard_option('pve-storage-id', {
+		description => "Target storage",
+		completion => \&PVE::Storage::complete_storage_enabled,
+	    }),
+	    'target-node' => get_standard_option('pve-node',  {
 		description => "Target node. Default is local node.",
 		optional => 1,
 	    }),
+	    delete => {
+		type => 'boolean',
+		description => "Delete the original volume after a successful copy."
+		    ." By default the original is kept.",
+		optional => 1,
+		default => 0,
+	    },
 	},
     },
     returns => {
@@ -515,43 +539,52 @@ __PACKAGE__->register_method ({
     code => sub {
 	my ($param) = @_;
 
-	my $rpcenv = PVE::RPCEnvironment::get();
-
-	my $user = $rpcenv->get_user();
-
-	my $target_node = $param->{target_node} || PVE::INotify::nodename();
-	# pvesh examples
-	# cd /nodes/localhost/storage/local/content
-	# pve:/> create local:103/vm-103-disk-1.raw -target local:103/vm-103-disk-2.raw
-	# pve:/> create 103/vm-103-disk-1.raw -target 103/vm-103-disk-3.raw
+	my $src_volid = $real_volume_id->($param->{storage}, $param->{volume});
+	my $dst_storeid = $param->{'target-storage'};
+	my ($src_storeid, $volname) = PVE::Storage::parse_volume_id($src_volid);
+	my $src_node = PVE::INotify::nodename();
+	my $dst_node = $param->{'target-node'} || $src_node;
+	my $delete = $param->{delete};
 
-	my $src_volid = &$real_volume_id($param->{storage}, $param->{volume});
-	my $dst_volid = &$real_volume_id($param->{storage}, $param->{target});
-
-	print "DEBUG: COPY $src_volid TO $dst_volid\n";
+	die "source and target cannot be the same\n"
+	    if $src_node eq $dst_node && $src_storeid eq $dst_storeid;
 
 	my $cfg = PVE::Storage::config();
 
-	# do all parameter checks first
-
-	# then do all short running task (to raise errors before we go to background)
+	my ($vtype, undef, $ownervm) = PVE::Storage::parse_volname($cfg, $src_volid);
+	die "use pct move-volume or qm disk move\n" if $vtype eq 'images' || $vtype eq 'rootdir';
+	die "moving volume of type '$vtype' not implemented\n"
+	    if !grep { $vtype eq $_ } qw(import iso snippets vztmpl);
 
-	# then start the worker task
-	my $worker = sub  {
-	    my $upid = shift;
+	my $rpcenv = PVE::RPCEnvironment::get();
+	my $user = $rpcenv->get_user();
 
-	    print "DEBUG: starting worker $upid\n";
+	PVE::Storage::check_volume_access($rpcenv, $user, $cfg, $ownervm, $src_volid);
 
-	    my ($target_sid, $target_volname) = PVE::Storage::parse_volume_id($dst_volid);
-	    #my $target_ip = PVE::Cluster::remote_node_ip($target_node);
+	if ($delete) {
+	    $rpcenv->check($user, "/storage/$src_storeid", ["Datastore.Allocate"]);
+	}
 
-	    # you need to get this working (fails currently, because storage_migrate() uses
-	    # ssh to connect to local host (which is not needed
-	    my $sshinfo = PVE::SSHInfo::get_ssh_info($target_node);
-	    PVE::Storage::storage_migrate($cfg, $src_volid, $sshinfo, $target_sid, {'target_volname' => $target_volname});
+	$rpcenv->check($user, "/storage/$dst_storeid", ["Datastore.AllocateSpace"]);
 
-	    print "DEBUG: end worker $upid\n";
+	my $worker = sub {
+	    PVE::Storage::storage_check_enabled($cfg, $src_storeid, $src_node);
+	    PVE::Storage::storage_check_enabled($cfg, $dst_storeid, $dst_node);
+	    my $sshinfo;
+	    $sshinfo = PVE::SSHInfo::get_ssh_info($dst_node) if $src_node ne $dst_node;
+	    my $opts = { 'target_volname' => $volname };
+	    PVE::Storage::storage_migrate($cfg, $src_volid, $sshinfo, $dst_storeid, $opts);
+
+	    if ($delete) {
+		&$volume_remove($cfg, $src_volid);
+	    }
 
+	    if ($src_node eq $dst_node) {
+		print "Copied volume '$src_volid' to '$dst_storeid'\n";
+	    } else {
+		print "Copied volume '$src_volid' on node '$src_node'"
+		    ." to '$dst_storeid' on node '$dst_node'\n";
+	    }
 	};
 
 	return $rpcenv->fork_worker('imgcopy', undef, $user, $worker);
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel