kvm: Fix migrating VM from ISO failures (#2928)

Prevents errors while migrating VM from ISO:

Test 1: Deploy VM from ISO -> Live migrate VM to another host -> ERROR
Test 2: Register ISO using Direct Download on KVM -> Deploy VM from ISO -> Live migrate VM to another host -> ERROR

- Prevent NullPointerException migrating VM from ISO
- Prevent mount secondary storage on ISO direct downloads on KVM
This commit is contained in:
Nicolas Vazquez 2018-10-29 07:44:20 -03:00 committed by Rohit Yadav
parent f0491d5c72
commit dffb430975
3 changed files with 20 additions and 5 deletions

View File

@ -46,6 +46,7 @@ public class TemplateObjectTO implements DataTO {
private Hypervisor.HypervisorType hypervisorType; private Hypervisor.HypervisorType hypervisorType;
private boolean bootable; private boolean bootable;
private String uniqueName; private String uniqueName;
private boolean directDownload;
public TemplateObjectTO() { public TemplateObjectTO() {
@ -235,6 +236,14 @@ public class TemplateObjectTO implements DataTO {
this.uniqueName = uniqueName; this.uniqueName = uniqueName;
} }
public boolean isDirectDownload() {
return directDownload;
}
public void setDirectDownload(boolean directDownload) {
this.directDownload = directDownload;
}
@Override @Override
public String toString() { public String toString() {
return new StringBuilder("TemplateTO[id=").append(id).append("|origUrl=").append(origUrl).append("|name").append(name).append("]").toString(); return new StringBuilder("TemplateTO[id=").append(id).append("|origUrl=").append(origUrl).append("|name").append(name).append("]").toString();

View File

@ -49,6 +49,7 @@ import javax.xml.parsers.ParserConfigurationException;
import com.cloud.resource.RequestWrapper; import com.cloud.resource.RequestWrapper;
import org.apache.cloudstack.storage.to.PrimaryDataStoreTO; import org.apache.cloudstack.storage.to.PrimaryDataStoreTO;
import org.apache.cloudstack.storage.to.TemplateObjectTO;
import org.apache.cloudstack.storage.to.VolumeObjectTO; import org.apache.cloudstack.storage.to.VolumeObjectTO;
import org.apache.cloudstack.utils.hypervisor.HypervisorUtils; import org.apache.cloudstack.utils.hypervisor.HypervisorUtils;
import org.apache.cloudstack.utils.linux.CPUStat; import org.apache.cloudstack.utils.linux.CPUStat;
@ -2209,7 +2210,8 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
final DataTO data = volume.getData(); final DataTO data = volume.getData();
final DataStoreTO store = data.getDataStore(); final DataStoreTO store = data.getDataStore();
if (volume.getType() == Volume.Type.ISO && data.getPath() != null && (store instanceof NfsTO || store instanceof PrimaryDataStoreTO)) { if (volume.getType() == Volume.Type.ISO && data.getPath() != null && (store instanceof NfsTO ||
store instanceof PrimaryDataStoreTO && data instanceof TemplateObjectTO && !((TemplateObjectTO) data).isDirectDownload())) {
final String isoPath = store.getUrl().split("\\?")[0] + File.separator + data.getPath(); final String isoPath = store.getUrl().split("\\?")[0] + File.separator + data.getPath();
final int index = isoPath.lastIndexOf("/"); final int index = isoPath.lastIndexOf("/");
final String path = isoPath.substring(0, index); final String path = isoPath.substring(0, index);

View File

@ -50,6 +50,7 @@ import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
import org.apache.cloudstack.storage.datastore.db.ImageStoreVO; import org.apache.cloudstack.storage.datastore.db.ImageStoreVO;
import org.apache.cloudstack.utils.imagestore.ImageStoreUtil; import org.apache.cloudstack.utils.imagestore.ImageStoreUtil;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.apache.cloudstack.acl.SecurityChecker.AccessType; import org.apache.cloudstack.acl.SecurityChecker.AccessType;
import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiConstants;
@ -557,11 +558,13 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
if (vm.getIsoId() != null) { if (vm.getIsoId() != null) {
Map<Volume, StoragePool> storageForDisks = dest.getStorageForDisks(); Map<Volume, StoragePool> storageForDisks = dest.getStorageForDisks();
Long poolId = null; Long poolId = null;
for (StoragePool storagePool : storageForDisks.values()) { if (MapUtils.isNotEmpty(storageForDisks)) {
if (poolId != null && storagePool.getId() != poolId) { for (StoragePool storagePool : storageForDisks.values()) {
throw new CloudRuntimeException("Cannot determine where to download iso"); if (poolId != null && storagePool.getId() != poolId) {
throw new CloudRuntimeException("Cannot determine where to download iso");
}
poolId = storagePool.getId();
} }
poolId = storagePool.getId();
} }
TemplateInfo template = prepareIso(vm.getIsoId(), vm.getDataCenterId(), dest.getHost().getId(), poolId); TemplateInfo template = prepareIso(vm.getIsoId(), vm.getDataCenterId(), dest.getHost().getId(), poolId);
if (template == null){ if (template == null){
@ -579,6 +582,7 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
} }
TemplateObjectTO iso = (TemplateObjectTO)template.getTO(); TemplateObjectTO iso = (TemplateObjectTO)template.getTO();
iso.setDirectDownload(template.isDirectDownload());
iso.setGuestOsType(displayName); iso.setGuestOsType(displayName);
DiskTO disk = new DiskTO(iso, 3L, null, Volume.Type.ISO); DiskTO disk = new DiskTO(iso, 3L, null, Volume.Type.ISO);
profile.addDisk(disk); profile.addDisk(disk);