fermosan commented on code in PR #6550:
URL: https://github.com/apache/cloudstack/pull/6550#discussion_r967636017


##########
server/src/main/java/com/cloud/hypervisor/KVMGuru.java:
##########
@@ -297,4 +343,64 @@ public Map<String, String> getClusterSettings(long vmId) {
         return null;
     }
 
-}
+    @Override
+    public VirtualMachine importVirtualMachineFromBackup(long zoneId, long 
domainId, long accountId, long userId, String vmInternalName, Backup backup)  {
+        s_logger.debug(String.format("Trying to import VM [vmInternalName: %s] 
from Backup [%s].", vmInternalName,
+                
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(backup, "id", "uuid", 
"vmId", "externalId", "backupType")));
+
+        VMInstanceVO vm = 
_instanceDao.findVMByInstanceNameIncludingRemoved(vmInternalName);
+
+        if (vm == null) {
+            throw new CloudRuntimeException("Cannot find VM: " + 
vmInternalName);
+        }
+        try {
+            if (vm.getRemoved() == null) {
+                vm.setState(VirtualMachine.State.Stopped);
+                vm.setPowerState(VirtualMachine.PowerState.PowerOff);
+                _instanceDao.update(vm.getId(), vm);
+            }
+           for ( Backup.VolumeInfo VMVolToRestore : vm.getBackupVolumeList()) {
+               VolumeVO volume = 
_volumeDao.findByUuidIncludingRemoved(VMVolToRestore.getUuid());
+               volume.setState(Volume.State.Ready);
+               _volumeDao.update(volume.getId(), volume);
+               if (VMVolToRestore.getType() == Volume.Type.ROOT) {
+                   _volumeDao.update(volume.getId(), volume);
+                   _volumeDao.attachVolume(volume.getId(), vm.getId(), 0L);
+               }
+               else if ( VMVolToRestore.getType() == Volume.Type.DATADISK) {
+                   List<VolumeVO> vmVolumes = 
_volumeDao.findByInstance(vm.getId());
+                   _volumeDao.update(volume.getId(), volume);
+                   _volumeDao.attachVolume(volume.getId(), vm.getId(), 
getNextAvailableDeviceId(vmVolumes));
+               }
+           }
+        } catch (Exception e) {
+            throw new RuntimeException("Could not restore VM " + vm.getName() 
+ " due to : " + e.getMessage());
+        }
+    return vm;
+    }
+
+

Review Comment:
   fixed in latest commit



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to