@@ -9480,16 +9480,31 @@ public UserVm restoreVMFromBackup(CreateVMFromBackupCmd cmd) throws ResourceUnav
94809480 long vmId = cmd.getEntityId();
94819481 Map<Long, DiskOffering> diskOfferingMap = cmd.getDataDiskTemplateToDiskOfferingMap();
94829482 Map<VirtualMachineProfile.Param, Object> additonalParams = new HashMap<>();
9483- UserVm vm = startVirtualMachine(vmId, null, null, null, diskOfferingMap, additonalParams, null) ;
9483+ UserVm vm;
94849484
9485- boolean status = false;
9486- status = stopVirtualMachine(CallContext.current().getCallingUserId(), vm.getId()) ;
9487- if (!status) {
9488- expungeVm(vm.getId());
9489- throw new CloudRuntimeException("Unable to stop the instance before restore ");
9490- }
9485+ try {
9486+ vm = startVirtualMachine(vmId, null, null, null, diskOfferingMap, additonalParams, null);
9487+
9488+ boolean status = stopVirtualMachine(CallContext.current().getCallingUserId(), vm.getId()) ;
9489+ if (!status) {
9490+ UserVmVO vmVO = _vmDao.findById(vmId);
9491+ expunge(vmVO);
9492+ logger.debug("Successfully cleaned up VM {} after create instance from backup failed", vmId);
9493+ throw new CloudRuntimeException("Unable to stop the instance before restore");
9494+ }
9495+
9496+ backupManager.restoreBackupToVM(cmd.getBackupId(), vmId);
94919497
9492- backupManager.restoreBackupToVM(cmd.getBackupId(), vmId);
9498+ } catch (Exception e) {
9499+ UserVmVO vmVO = _vmDao.findById(vmId);
9500+ try {
9501+ expunge(vmVO);
9502+ logger.debug("Successfully cleaned up VM {} after create instance from backup failed", vmId);
9503+ } catch (Exception cleanupException) {
9504+ logger.debug("Failed to cleanup VM {} after create instance from backup failed", vmId, cleanupException);
9505+ }
9506+ throw e;
9507+ }
94939508
94949509 Account owner = _accountService.getActiveAccountById(cmd.getEntityOwnerId());
94959510 UserVmVO userVm = _vmDao.findById(vmId);
0 commit comments