[pve-devel] [PATCH storage 16/26] common: add volume_type_from_name convenience helper

Wolfgang Bumiller
2025-07-29T11:15:16Z

For plugins which follow our new naming scheme.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
---
src/PVE/Storage/Common.pm | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)

diff --git a/src/PVE/Storage/Common.pm b/src/PVE/Storage/Common.pm
index 7086e1c..813fba6 100644
--- a/src/PVE/Storage/Common.pm
+++ b/src/PVE/Storage/Common.pm
@@ -313,6 +313,35 @@ sub should_list_images : prototype($$) ($expected_content_type, $volume_type) {
return $expected_content_type eq $volume_type;
}

+=head3 volume_type_from_name
+
+ volume_type_from_name($name)
+
+This is available for storages which follow our most common naming scheme relating to volume types:
+A "vol-vm-" prefix for the C<vm-vol> vtype, and a "vol-ct-" prefix for the C<ct-vol> vtype.
+
+This function allows (and ignores) an optional "base-" prefix, such that
+"base-vol-ct-100-disk-0.raw" would return C<ct-vol>.
+
+In any other case it returns C<images>.
+
+=cut
+
+sub volume_type_from_name : prototype($) {
+ my ($name) = @_;
+
+ if ($name =~ /^(?:base-)?(?:(?:sub)?vol-(vm|ct))-/) {
+ return "$1-vol";
+ }
+
+ # Note: Despite the legacy `subvol-*` and `basevol-*` dirs always being containers, giving
+ # them the vtype `ct-vol` would break `path_to_volume_id` which would expect their volids to
+ # be in the `cts/` subdir instead of in `images/`...
+ # return 'ct-vol' if $name =~ /^(?:subvol|basevol)-/;
+
+ return 'images';
+}
+
=head3 is_volume_type

is_volume_type($vtype)