From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <a.lauterer@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 779FC7BB93
 for <pve-devel@lists.proxmox.com>; Tue,  2 Nov 2021 16:03:37 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 6B275A846
 for <pve-devel@lists.proxmox.com>; Tue,  2 Nov 2021 16:03:37 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 50324A828
 for <pve-devel@lists.proxmox.com>; Tue,  2 Nov 2021 16:03:36 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 1B726469D5
 for <pve-devel@lists.proxmox.com>; Tue,  2 Nov 2021 16:03:36 +0100 (CET)
From: Aaron Lauterer <a.lauterer@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue,  2 Nov 2021 16:03:27 +0100
Message-Id: <20211102150335.2371545-2-a.lauterer@proxmox.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20211102150335.2371545-1-a.lauterer@proxmox.com>
References: <20211102150335.2371545-1-a.lauterer@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.128 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 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 v4 storage 1/9] storage: add new
 find_free_volname
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>
X-List-Received-Date: Tue, 02 Nov 2021 15:03:37 -0000

This new method exposes the functionality to request a new, not yet
used, volname for a storage.

The default implementation will return the result from
'find_free_diskname' prefixed with "<VMID>/" if $scfg->{path} exists.
Otherwise it will only return the result from 'find_free_diskname'.

If the format suffix is added also depends on the existence of
$scfg->{path}.

$scfg->{path} will be present for directory based storage types.

Should a storage need to return different volname, it needs to override
the 'find_free_volname' method.

Signed-off-by: Aaron Lauterer <a.lauterer@proxmox.com>
---

v3 -> v4:
* fix style nit

v2 -> v3:
* dropped exists() check
* fixed base image handling
* fixed smaller code style issues

v1 -> v2:
* many small fixes and improvements
* rename_volume now accepts $source_volname instead of $source_volid
    helps us to avoid to parse the volid a second time

rfc -> v1:
* reduced number of parameters to minimum needed, plugins infer needed
  information themselves
* added storage locking and checking if volume already exists
* parse target_volname prior to renaming to check if valid

old dedicated API endpoint -> rfc:
only do rename now but the rename function handles templates and returns
the new volid as this can be differently handled on some storages.
 
 PVE/Storage.pm        | 9 +++++++++
 PVE/Storage/Plugin.pm | 8 ++++++++
 2 files changed, 17 insertions(+)

diff --git a/PVE/Storage.pm b/PVE/Storage.pm
index 71d6ad7..4fbeaea 100755
--- a/PVE/Storage.pm
+++ b/PVE/Storage.pm
@@ -203,6 +203,15 @@ sub storage_can_replicate {
     return $plugin->storage_can_replicate($scfg, $storeid, $format);
 }
 
+sub find_free_volname {
+    my ($cfg, $storeid, $vmid, $fmt) = @_;
+
+    my $scfg = storage_config($cfg, $storeid);
+    my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+
+    return $plugin->find_free_volname($storeid, $scfg, $vmid, $fmt);
+}
+
 sub storage_ids {
     my ($cfg) = @_;
 
diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm
index aeb4fff..d21c874 100644
--- a/PVE/Storage/Plugin.pm
+++ b/PVE/Storage/Plugin.pm
@@ -732,6 +732,14 @@ sub find_free_diskname {
     return get_next_vm_diskname($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix);
 }
 
+sub find_free_volname {
+    my ($class, $storeid, $scfg, $vmid, $fmt) = @_;
+
+    my $diskname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt, $scfg->{path});
+
+    return $scfg->{path} ? "${vmid}/$diskname" : $diskname;
+}
+
 sub clone_image {
     my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
 
-- 
2.30.2