dskCh = createDiskCharacteristics(volume, template, dc, diskOffering);
}
dskCh.setHyperType(hyperType);
VolumeTO created = null;
int retry = _retry;
while (--retry >= 0) {
created = null;
long podId = pod.getId();
pod = _podDao.findById(podId);
if (pod == null) {
s_logger.warn("Unable to find pod " + podId + " when create volume " + volume.getName());
break;
}
pool = findStoragePool(dskCh, dc, pod, clusterId, vm.getHostId(), vm, avoidPools);
if (pool == null) {
s_logger.warn("Unable to find storage poll when create volume " + volume.getName());
break;
}
avoidPools.add(pool);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Trying to create " + volume + " on " + pool);
}
CreateCommand cmd = null;
VMTemplateStoragePoolVO tmpltStoredOn = null;
for (int i = 0; i < 2; i++) {
if (volume.getVolumeType() == Type.ROOT && Storage.ImageFormat.ISO != template.getFormat()) {
if (pool.getPoolType() == StoragePoolType.CLVM) {
//prepareISOForCreate does what we need, which is to tell us where the template is
VMTemplateHostVO tmpltHostOn = _tmpltMgr.prepareISOForCreate(template, pool);
if (tmpltHostOn == null) {
continue;
}
HostVO secondaryStorageHost = _hostDao.findById(tmpltHostOn.getHostId());
String tmpltHostUrl = secondaryStorageHost.getStorageUrl();
String fullTmpltUrl = tmpltHostUrl + "/" + tmpltHostOn.getInstallPath();
cmd = new CreateCommand(dskCh, fullTmpltUrl, new StorageFilerTO(pool));
} else {
tmpltStoredOn = _tmpltMgr.prepareTemplateForCreate(template, pool);
if (tmpltStoredOn == null) {
continue;
}
cmd = new CreateCommand(dskCh, tmpltStoredOn.getLocalDownloadPath(), new StorageFilerTO(pool));
}
} else {
if (volume.getVolumeType() == Type.ROOT && Storage.ImageFormat.ISO == template.getFormat()) {
VMTemplateHostVO tmpltHostOn = _tmpltMgr.prepareISOForCreate(template, pool);
if (tmpltHostOn == null) {
throw new CloudRuntimeException("Did not find ISO in secondry storage in zone " + pool.getDataCenterId());
}
}
cmd = new CreateCommand(dskCh, new StorageFilerTO(pool));
}
try {
Answer answer = sendToPool(pool, cmd);
if (answer != null && answer.getResult()) {
created = ((CreateAnswer) answer).getVolume();
break;
}
if (tmpltStoredOn != null && answer != null && (answer instanceof CreateAnswer) && ((CreateAnswer) answer).templateReloadRequested()) {
if (!_tmpltMgr.resetTemplateDownloadStateOnPool(tmpltStoredOn.getId())) {
break; // break out of template-redeploy retry loop
}
} else {
break;
}
} catch (StorageUnavailableException e) {
s_logger.debug("Storage unavailable for " + pool.getId());
break; // break out of template-redeploy retry loop
}
}
if (created != null) {
break;
}
s_logger.debug("Retrying the create because it failed on pool " + pool);
}
if (created == null) {
try {
stateTransitTo(volume, Volume.Event.OperationFailed);
} catch (NoTransitionException e) {
s_logger.debug("Unable to update volume state: " + e.toString());
}
return null;
} else {
volume.setFolder(pool.getPath());
volume.setPath(created.getPath());
volume.setSize(created.getSize());
volume.setPoolType(pool.getPoolType());
volume.setPoolId(pool.getId());
volume.setPodId(pod.getId());
try {
stateTransitTo(volume, Volume.Event.OperationSucceeded);