package org.zstack.image;

import org.zstack.core.db.Q;
import org.zstack.header.cluster.ClusterVO;
import org.zstack.header.cluster.ClusterVO_;
import org.zstack.header.vm.VmInstanceVO;
import org.zstack.header.vm.VmInstanceVO_;
import org.zstack.header.volume.VolumeVO;
import org.zstack.header.volume.VolumeVO_;

public class ImageUtils {
    public static String getArchitectureFromRootVolume(String rootVolumeUuid, String architecture) {
        if (architecture == null) {
            String vmUuid = Q.New(VolumeVO.class).select(VolumeVO_.vmInstanceUuid).eq(VolumeVO_.uuid, rootVolumeUuid).findValue();
            String clusterUuid = Q.New(VmInstanceVO.class).select(VmInstanceVO_.clusterUuid).eq(VmInstanceVO_.uuid, vmUuid).findValue();
            return Q.New(ClusterVO.class).select(ClusterVO_.architecture).eq(ClusterVO_.uuid, clusterUuid).findValue();
        }
        return architecture;
    }
}

@@ -134,24 +134,101 @@
 ClusterVO_;%0A
+import org.zstack.header.host.HostVO;%0Aimport org.zstack.header.host.HostVO_;%0A
 import org.z
@@ -538,17 +538,17 @@
 tecture 
-=
+!
 = null) 
@@ -549,24 +549,64 @@
 null) %7B%0A    
+        return architecture;%0A        %7D%0A%0A
         Stri
@@ -732,49 +732,212 @@
     
-    String clusterUuid = Q.New(VmInstance
+VmInstanceVO vm = Q.New(VmInstanceVO.class).eq(VmInstanceVO_.uuid, vmUuid).find();%0A        String clusterUuid = vm.getClusterUuid();%0A        if (clusterUuid == null) %7B%0A            clusterUuid = Q.New(Host
 VO.c
@@ -949,26 +949,20 @@
 .select(
-VmInstance
+Host
 VO_.clus
@@ -969,34 +969,28 @@
 terUuid).eq(
-VmInstance
+Host
 VO_.uuid, vm
@@ -985,28 +985,42 @@
 VO_.uuid, vm
+.getLastHost
 Uuid
+()
 ).findValue(
@@ -1022,24 +1022,30 @@
 alue();%0A    
+    %7D%0A
         retu
@@ -1152,47 +1152,8 @@
 ();%0A
-        %7D%0A        return architecture;%0A
     

fix getArchitectureFromRootVolume if VM stopped