@@ -220,6 +220,9 @@ private String restoreVolume(KVMStoragePoolManager storagePoolMgr, String backup
220220 * volume's identity themselves (StorPool) nobody else could find and delete it.
221221 */
222222 private void cleanupCreatedVolume (KVMStoragePoolManager storagePoolMgr , PrimaryDataStoreTO volumePool , String volumePath ) {
223+ if (volumePath == null ) {
224+ return ;
225+ }
223226 try {
224227 if (!storagePoolMgr .cleanupCreatedPhysicalDisk (volumePool .getPoolType (), volumePool .getUuid (), volumePath )) {
225228 logger .warn ("Volume {} created for the failed restore was not cleaned up by the {} storage adaptor; it may need to be deleted manually" ,
@@ -366,6 +369,9 @@ private Pair<Boolean, String> replaceBlockDeviceWithBackup(KVMStoragePoolManager
366369 KVMStoragePool volumeStoragePool = storagePoolMgr .getStoragePool (volumePool .getPoolType (), volumePool .getUuid ());
367370 Storage .StoragePoolType poolType = volumePool .getPoolType ();
368371 QemuImg qemu ;
372+ // Path of a volume created here whose identity the storage assigned itself, so a failure
373+ // before the copy starts can release it (see cleanupCreatedVolume).
374+ String createdVolumePath = null ;
369375 try {
370376 String volumeUuid = getVolumeUuidFromPath (volumePath , volumePool );
371377 KVMPhysicalDisk disk = null ;
@@ -385,6 +391,7 @@ private Pair<Boolean, String> replaceBlockDeviceWithBackup(KVMStoragePoolManager
385391 }
386392 if (Storage .StoragePoolType .StorPool .equals (poolType )) {
387393 volumePath = disk .getPath ();
394+ createdVolumePath = volumePath ;
388395 }
389396 targetReadsAsZero = volumeStoragePool .newVolumeReadsAsZero (volumeUuid );
390397 } else {
@@ -408,7 +415,11 @@ private Pair<Boolean, String> replaceBlockDeviceWithBackup(KVMStoragePoolManager
408415 logger .debug ("Restoring volume: {}" , disk .toString ());
409416 }
410417 } catch (LibvirtException ex ) {
418+ cleanupCreatedVolume (storagePoolMgr , volumePool , createdVolumePath );
411419 throw new CloudRuntimeException (String .format ("Failed to create qemu-img command to restore %s volume with backup" , poolType ), ex );
420+ } catch (RuntimeException ex ) {
421+ cleanupCreatedVolume (storagePoolMgr , volumePool , createdVolumePath );
422+ throw ex ;
412423 }
413424
414425 QemuImgFile srcBackupFile = null ;
0 commit comments