[pve-devel] [storage] Add option check if the image can safely remove

Wolfgang Link w.link at proxmox.com
Tue Nov 13 07:44:19 CET 2018


If an image has a <vmid> encoded in the image name
and the guest does not exist in the cluster
we can safely delete it on the GUI.

Also, if a config exists on another node and the storage is local
we can delete it too.

In all other cases, a rescan must manual performed
to add it back to the guest config.

Note: It is possible when a replication job exists
to removes the target image of the replica.
This will force new initial sync of this image.
---
 PVE/API2/Storage/Content.pm | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/PVE/API2/Storage/Content.pm b/PVE/API2/Storage/Content.pm
index e941cb6..5c75d87 100644
--- a/PVE/API2/Storage/Content.pm
+++ b/PVE/API2/Storage/Content.pm
@@ -285,6 +285,11 @@ __PACKAGE__->register_method ({
 		type => 'string',
 		completion => \&PVE::Storage::complete_volume,
 	    },
+	    check => {
+		description => "Check if the image can remove safely",
+		type => 'boolean',
+		optional => 1,
+	    },
 	},
     },
     returns => { type => 'null' },
@@ -295,8 +300,10 @@ __PACKAGE__->register_method ({
 	my $authuser = $rpcenv->get_user();
 
 	my $cfg = PVE::Storage::config();
+	my $volume = PVE::Tools::extract_param($param, 'volume');
+	my $node = PVE::Tools::extract_param($param, 'node');
 
-	my ($volid, $storeid) = &$real_volume_id($param->{storage}, $param->{volume});
+	my ($volid, $storeid) = &$real_volume_id($param->{storage}, $volume);
 
 	my ($path, $ownervm, $vtype) = PVE::Storage::path($cfg, $volid);
 	if ($vtype eq 'backup' && $ownervm) {
@@ -306,6 +313,21 @@ __PACKAGE__->register_method ({
 	    $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.Allocate']);
 	}
 
+	if ($param->{check}) {
+
+		my $vmid = (PVE::Storage::parse_volname($cfg, $volume))[2];
+		my $vms = PVE::Cluster::get_vmlist();
+
+		my $guest_exist = defined($vms->{ids}->{$vmid});
+		my $guest_on_localhost = $guest_exist && $vms->{ids}->{$vmid}->{node} eq $node;
+
+		my $storage_is_shared = $cfg->{ids}->{$storeid}->{shared};
+		$storage_is_shared = defined($storage_is_shared) ? $storage_is_shared : 0;
+
+		die "Guest: $vmid exist on local node. Remove image from guest Hardware.\n"
+			if $guest_on_localhost || ($guest_exist && $storage_is_shared);
+	}
+
 	PVE::Storage::vdisk_free ($cfg, $volid);
 
 	return undef;
-- 
2.11.0





More information about the pve-devel mailing list