[pve-devel] [PATCH container] api: autocomplete rootdir storages for create, move-volume and clone

Daniel Kral d.kral at proxmox.com
Tue Sep 10 13:23:42 CEST 2024


Adds a helper subroutine for enumerating storages that are enabled and
have the content type `rootdir` set, therefore supporting container
directories.

The autocompletion is added to the clone command and changed for the
create and move-volume commands, which previously suggested any storage
device. This is misleading as these commands will fail for any storage
that is not configured to store container directories.

Signed-off-by: Daniel Kral <d.kral at proxmox.com>
---
I have already noticed that the create command has the default value
'local' for the storage parameter, which could fail on any installation
where the local storage is not configured to store rootdirs. This should
be fixed in a separate patch in my opinion.

 src/PVE/API2/LXC.pm |  5 +++--
 src/PVE/LXC.pm      | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index d9f1c0a..918e719 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -148,9 +148,9 @@ __PACKAGE__->register_method({
 	    },
 	    storage => get_standard_option('pve-storage-id', {
 		description => "Default Storage.",
+		completion => \&PVE::LXC::complete_storage,
 		default => 'local',
 		optional => 1,
-		completion => \&PVE::Storage::complete_storage_enabled,
 	    }),
 	    force => {
 		optional => 1,
@@ -1582,6 +1582,7 @@ __PACKAGE__->register_method({
             }),
 	    storage => get_standard_option('pve-storage-id', {
 		description => "Target storage for full clone.",
+		completion => \&PVE::LXC::complete_storage,
 		optional => 1,
 	    }),
 	    full => {
@@ -2084,7 +2085,7 @@ __PACKAGE__->register_method({
 	    },
 	    storage => get_standard_option('pve-storage-id', {
 		description => "Target Storage.",
-		completion => \&PVE::Storage::complete_storage_enabled,
+		completion => \&PVE::LXC::complete_storage,
 		optional => 1,
 	    }),
 	    delete => {
diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 65d0fa8..269614c 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -2382,6 +2382,20 @@ sub complete_ctid_running {
     return &$complete_ctid_full(1);
 }
 
+sub complete_storage {
+    my $cfg = PVE::Storage::config();
+    my $ids = $cfg->{ids};
+
+    my $res = [];
+    foreach my $sid (keys %$ids) {
+	next if !PVE::Storage::storage_check_enabled($cfg, $sid, undef, 1);
+	next if !$ids->{$sid}->{content}->{rootdir};
+	push @$res, $sid;
+    }
+
+    return $res;
+}
+
 sub parse_id_maps {
     my ($conf) = @_;
 
-- 
2.39.2





More information about the pve-devel mailing list