This is an automated email from the ASF dual-hosted git repository.

rohit pushed a commit to branch 4.14
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/4.14 by this push:
     new 4527424  vm: Reset deviceId to fix missing nic with vm (#4000)
4527424 is described below

commit 4527424fce0510cbeab6412faa23ee21838b07f1
Author: Wei Zhou <[email protected]>
AuthorDate: Wed Aug 5 10:19:03 2020 +0200

    vm: Reset deviceId to fix missing nic with vm (#4000)
    
    Co-authored-by: Wei Zhou <[email protected]>
---
 .../com/cloud/vm/VirtualMachineManagerImpl.java     | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git 
a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java
 
b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java
index 8d7e4a5..f312752 100755
--- 
a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java
+++ 
b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java
@@ -1124,6 +1124,7 @@ public class VirtualMachineManagerImpl extends 
ManagerBase implements VirtualMac
                 }
 
                 try {
+                    resetVmNicsDeviceId(vm.getId());
                     _networkMgr.prepare(vmProfile, new 
DeployDestination(dest.getDataCenter(), dest.getPod(), null, null, 
dest.getStorageForDisks()), ctx);
                     if (vm.getHypervisorType() != HypervisorType.BareMetal) {
                         volumeMgr.prepare(vmProfile, dest);
@@ -1317,6 +1318,26 @@ public class VirtualMachineManagerImpl extends 
ManagerBase implements VirtualMac
         }
     }
 
+    private void resetVmNicsDeviceId(Long vmId) {
+        final List<NicVO> nics = _nicsDao.listByVmId(vmId);
+        Collections.sort(nics, new Comparator<NicVO>() {
+            @Override
+            public int compare(NicVO nic1, NicVO nic2) {
+                Long nicDevId1 = Long.valueOf(nic1.getDeviceId());
+                Long nicDevId2 = Long.valueOf(nic2.getDeviceId());
+                return nicDevId1.compareTo(nicDevId2);
+            }
+        });
+        int deviceId = 0;
+        for (final NicVO nic : nics) {
+            if (nic.getDeviceId() != deviceId) {
+                nic.setDeviceId(deviceId);
+                _nicsDao.update(nic.getId(),nic);
+            }
+            deviceId ++;
+        }
+    }
+
     // Add extra config data to the vmTO as a Map
     private void addExtraConfig(VirtualMachineTO vmTO) {
         Map<String, String> details = vmTO.getDetails();

Reply via email to