Skip to content

Commit 2075918

Browse files
Fix local storage deletion cases (#10231)
* Delete local storage properties in agent.properties during delete pool * Fix stale entry when add local storage failed * Smaller methods * Comment added
1 parent 1e59f5c commit 2075918

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
431431

432432
protected static final String LOCAL_STORAGE_PATH = "local.storage.path";
433433
protected static final String LOCAL_STORAGE_UUID = "local.storage.uuid";
434-
protected static final String DEFAULT_LOCAL_STORAGE_PATH = "/var/lib/libvirt/images/";
434+
public static final String DEFAULT_LOCAL_STORAGE_PATH = "/var/lib/libvirt/images";
435435

436436
protected List<String> localStoragePaths = new ArrayList<>();
437437
protected List<String> localStorageUUIDs = new ArrayList<>();

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtDeleteStoragePoolCommandWrapper.java

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,78 @@
2222
import com.cloud.agent.api.Answer;
2323
import com.cloud.agent.api.DeleteStoragePoolCommand;
2424
import com.cloud.agent.api.to.StorageFilerTO;
25+
import com.cloud.agent.dao.impl.PropertiesStorage;
26+
import com.cloud.agent.properties.AgentProperties;
27+
import com.cloud.agent.properties.AgentPropertiesFileHandler;
2528
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
2629
import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager;
2730
import com.cloud.resource.CommandWrapper;
2831
import com.cloud.resource.ResourceWrapper;
32+
import com.cloud.storage.Storage;
2933
import com.cloud.utils.exception.CloudRuntimeException;
3034

35+
import java.util.Arrays;
36+
import java.util.HashMap;
37+
import java.util.stream.Collectors;
38+
3139
@ResourceWrapper(handles = DeleteStoragePoolCommand.class)
3240
public final class LibvirtDeleteStoragePoolCommandWrapper extends CommandWrapper<DeleteStoragePoolCommand, Answer, LibvirtComputingResource> {
3341
@Override
3442
public Answer execute(final DeleteStoragePoolCommand command, final LibvirtComputingResource libvirtComputingResource) {
3543
try {
3644
// if getRemoveDatastore() is true, then we are dealing with managed storage and can skip the delete logic here
3745
if (!command.getRemoveDatastore()) {
38-
final StorageFilerTO pool = command.getPool();
39-
final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
40-
41-
storagePoolMgr.deleteStoragePool(pool.getType(), pool.getUuid());
46+
handleStoragePoolDeletion(command, libvirtComputingResource);
4247
}
43-
4448
return new Answer(command);
4549
} catch (final CloudRuntimeException e) {
4650
return new Answer(command, false, e.toString());
4751
}
4852
}
53+
54+
private void handleStoragePoolDeletion(final DeleteStoragePoolCommand command, final LibvirtComputingResource libvirtComputingResource) {
55+
final StorageFilerTO pool = command.getPool();
56+
final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
57+
storagePoolMgr.deleteStoragePool(pool.getType(), pool.getUuid());
58+
59+
if (isLocalStorageAndNotHavingDefaultPath(pool, libvirtComputingResource)) {
60+
updateLocalStorageProperties(pool);
61+
}
62+
}
63+
64+
private boolean isLocalStorageAndNotHavingDefaultPath(final StorageFilerTO pool, final LibvirtComputingResource libvirtComputingResource) {
65+
return Storage.StoragePoolType.Filesystem.equals(pool.getType())
66+
&& !libvirtComputingResource.DEFAULT_LOCAL_STORAGE_PATH.equals(pool.getPath());
67+
}
68+
69+
private void updateLocalStorageProperties(final StorageFilerTO pool) {
70+
String localStoragePath = AgentPropertiesFileHandler.getPropertyValue(AgentProperties.LOCAL_STORAGE_PATH);
71+
String localStorageUuid = AgentPropertiesFileHandler.getPropertyValue(AgentProperties.LOCAL_STORAGE_UUID);
72+
73+
String uuidToRemove = pool.getUuid();
74+
String pathToRemove = pool.getPath();
75+
76+
if (localStorageUuid != null && uuidToRemove != null) {
77+
localStorageUuid = Arrays.stream(localStorageUuid.split(","))
78+
.filter(uuid -> !uuid.equals(uuidToRemove))
79+
.collect(Collectors.joining(","));
80+
}
81+
82+
if (localStoragePath != null && pathToRemove != null) {
83+
localStoragePath = Arrays.stream(localStoragePath.split(","))
84+
.filter(path -> !path.equals(pathToRemove))
85+
.collect(Collectors.joining(","));
86+
}
87+
88+
PropertiesStorage agentProperties = new PropertiesStorage();
89+
agentProperties.configure("AgentProperties", new HashMap<String, Object>());
90+
91+
if (localStorageUuid != null) {
92+
agentProperties.persist(AgentProperties.LOCAL_STORAGE_UUID.getName(), localStorageUuid);
93+
}
94+
95+
if (localStoragePath != null) {
96+
agentProperties.persist(AgentProperties.LOCAL_STORAGE_PATH.getName(), localStoragePath);
97+
}
98+
}
4999
}

server/src/main/java/com/cloud/storage/StorageManagerImpl.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,9 @@ public DataStore createLocalStorage(Host host, StoragePoolInfo pInfo) throws Con
803803
if (!(dc.isLocalStorageEnabled() || useLocalStorageForSystemVM)) {
804804
return null;
805805
}
806-
DataStore store;
806+
DataStore store = null;
807+
DataStoreProvider provider = _dataStoreProviderMgr.getDefaultPrimaryDataStoreProvider();
808+
DataStoreLifeCycle lifeCycle = provider.getDataStoreLifeCycle();
807809
try {
808810
String hostAddress = pInfo.getHost();
809811
if (host.getHypervisorType() == Hypervisor.HypervisorType.VMware) {
@@ -829,8 +831,6 @@ public DataStore createLocalStorage(Host host, StoragePoolInfo pInfo) throws Con
829831
}
830832
}
831833

832-
DataStoreProvider provider = _dataStoreProviderMgr.getDefaultPrimaryDataStoreProvider();
833-
DataStoreLifeCycle lifeCycle = provider.getDataStoreLifeCycle();
834834
if (pool == null) {
835835
Map<String, Object> params = new HashMap<String, Object>();
836836
String name = pInfo.getName() != null ? pInfo.getName() : createLocalStoragePoolName(host, pInfo);
@@ -860,6 +860,14 @@ public DataStore createLocalStorage(Host host, StoragePoolInfo pInfo) throws Con
860860

861861
} catch (Exception e) {
862862
s_logger.warn("Unable to setup the local storage pool for " + host, e);
863+
try {
864+
if (store != null) {
865+
s_logger.debug(String.format("Trying to delete storage pool entry if exists %s", store));
866+
lifeCycle.deleteDataStore(store);
867+
}
868+
} catch (Exception ex) {
869+
s_logger.debug(String.format("Failed to clean up local storage pool: %s", ex.getMessage()));
870+
}
863871
throw new ConnectionException(true, "Unable to setup the local storage pool for " + host, e);
864872
}
865873

0 commit comments

Comments
 (0)