use of com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder in project cloudstack by apache.
the class VmwareResource method createAnswerForCmd.
Answer createAnswerForCmd(VirtualMachineMO vmMo, List<VolumeObjectTO> volumeObjectToList, Command cmd, Map<Integer, Long> volumeDeviceKey) throws Exception {
List<VolumeObjectTO> volumeToList = new ArrayList<>();
VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
VirtualDisk[] disks = vmMo.getAllDiskDevice();
Answer answer;
if (s_logger.isTraceEnabled()) {
s_logger.trace(String.format("creating answer for %s", cmd.getClass().getSimpleName()));
}
if (cmd instanceof MigrateVolumeCommand) {
if (disks.length == 1) {
String volumePath = vmMo.getVmdkFileBaseName(disks[0]);
return new MigrateVolumeAnswer(cmd, true, null, volumePath);
}
throw new CloudRuntimeException("not expecting more then one disk after migrate volume command");
} else if (cmd instanceof MigrateVmToPoolCommand) {
volumeToList = volumeObjectToList;
return new MigrateVmToPoolAnswer((MigrateVmToPoolCommand) cmd, volumeToList);
}
return new Answer(cmd, false, null);
}
use of com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder in project cloudstack by apache.
the class VmwareStorageProcessor method getMatchingExistingDisk.
private VirtualMachineDiskInfo getMatchingExistingDisk(VmwareHypervisorHost hyperHost, VmwareContext context, VirtualMachineMO vmMo, DiskTO vol) throws Exception {
VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
if (diskInfoBuilder != null) {
VolumeObjectTO volume = (VolumeObjectTO) vol.getData();
ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, volume.getDataStore().getUuid());
DatastoreMO dsMo = new DatastoreMO(context, morDs);
String dsName = dsMo.getName();
String diskBackingFileBaseName = volume.getPath();
VirtualMachineDiskInfo diskInfo = diskInfoBuilder.getDiskInfoByBackingFileBaseName(diskBackingFileBaseName, dsName);
if (diskInfo != null) {
s_logger.info("Found existing disk info from volume path: " + volume.getPath());
return diskInfo;
} else {
String chainInfo = volume.getChainInfo();
if (chainInfo != null) {
VirtualMachineDiskInfo infoInChain = _gson.fromJson(chainInfo, VirtualMachineDiskInfo.class);
if (infoInChain != null) {
String[] disks = infoInChain.getDiskChain();
if (disks.length > 0) {
for (String diskPath : disks) {
DatastoreFile file = new DatastoreFile(diskPath);
diskInfo = diskInfoBuilder.getDiskInfoByBackingFileBaseName(file.getFileBaseName(), dsName);
if (diskInfo != null) {
s_logger.info("Found existing disk from chain info: " + diskPath);
return diskInfo;
}
}
}
if (diskInfo == null) {
diskInfo = diskInfoBuilder.getDiskInfoByDeviceBusName(infoInChain.getDiskDeviceBusName());
if (diskInfo != null) {
s_logger.info("Found existing disk from from chain device bus information: " + infoInChain.getDiskDeviceBusName());
return diskInfo;
}
}
}
}
}
}
return null;
}
use of com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder in project cloudstack by apache.
the class VmwareStorageProcessor method syncVolumePath.
@Override
public Answer syncVolumePath(SyncVolumePathCommand cmd) {
DiskTO disk = cmd.getDisk();
VolumeObjectTO volumeTO = (VolumeObjectTO) disk.getData();
DataStoreTO primaryStore = volumeTO.getDataStore();
String volumePath = volumeTO.getPath();
String vmName = volumeTO.getVmName();
boolean datastoreChangeObserved = false;
boolean volumePathChangeObserved = false;
String chainInfo = null;
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo == null) {
vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
if (vmMo == null) {
String msg = "Unable to find the VM to execute SyncVolumePathCommand, vmName: " + vmName;
s_logger.error(msg);
throw new Exception(msg);
}
}
String datastoreUUID = primaryStore.getUuid();
if (disk.getDetails().get(DiskTO.PROTOCOL_TYPE) != null && disk.getDetails().get(DiskTO.PROTOCOL_TYPE).equalsIgnoreCase("DatastoreCluster")) {
VirtualMachineDiskInfo matchingExistingDisk = getMatchingExistingDisk(hyperHost, context, vmMo, disk);
VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
if (diskInfoBuilder != null && matchingExistingDisk != null) {
String[] diskChain = matchingExistingDisk.getDiskChain();
assert (diskChain.length > 0);
DatastoreFile file = new DatastoreFile(diskChain[0]);
if (!file.getFileBaseName().equalsIgnoreCase(volumePath)) {
if (s_logger.isInfoEnabled())
s_logger.info("Detected disk-chain top file change on volume: " + volumeTO.getId() + " " + volumePath + " -> " + file.getFileBaseName());
volumePathChangeObserved = true;
volumePath = file.getFileBaseName();
volumeTO.setPath(volumePath);
chainInfo = _gson.toJson(matchingExistingDisk);
}
DatastoreMO diskDatastoreMofromVM = getDiskDatastoreMofromVM(hyperHost, context, vmMo, disk, diskInfoBuilder);
if (diskDatastoreMofromVM != null) {
String actualPoolUuid = diskDatastoreMofromVM.getCustomFieldValue(CustomFieldConstants.CLOUD_UUID);
if (!actualPoolUuid.equalsIgnoreCase(primaryStore.getUuid())) {
s_logger.warn(String.format("Volume %s found to be in a different storage pool %s", volumePath, actualPoolUuid));
datastoreChangeObserved = true;
datastoreUUID = actualPoolUuid;
chainInfo = _gson.toJson(matchingExistingDisk);
}
}
}
}
SyncVolumePathAnswer answer = new SyncVolumePathAnswer(disk);
if (datastoreChangeObserved) {
answer.setContextParam("datastoreName", datastoreUUID);
}
if (volumePathChangeObserved) {
answer.setContextParam("volumePath", volumePath);
}
if (chainInfo != null && !chainInfo.isEmpty()) {
answer.setContextParam("chainInfo", chainInfo);
}
return answer;
} catch (Throwable e) {
return new SyncVolumePathAnswer(hostService.createLogMessageException(e, cmd));
}
}
use of com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder in project cloudstack by apache.
the class ResultWrapper method getVolumeFromVirtualDisk.
private VolumeVO getVolumeFromVirtualDisk(VMInstanceVO vmInstance, long storagePoolId, List<VirtualDevice> allDevices, VirtualDisk disk) throws Exception {
List<VolumeVO> volumes = volumeDao.findByInstance(vmInstance.getId());
if (volumes == null || volumes.size() == 0) {
String errMsg = "Error: The VMware virtual disk '" + disk + "' could not be mapped to a CloudStack volume. " + "There were no volumes for the VM with the following ID: " + vmInstance.getId() + ".";
throw new Exception(errMsg);
}
VirtualMachineDiskInfoBuilder diskInfoBuilder = VMwareUtil.getDiskInfoBuilder(allDevices);
for (VolumeVO volume : volumes) {
Long poolId = volume.getPoolId();
if (poolId != null && poolId == storagePoolId) {
StoragePoolVO storagePool = storagePoolDao.findById(poolId);
String path = storagePool.getPath();
String charToSearchFor = "/";
int index = path.lastIndexOf(charToSearchFor) + charToSearchFor.length();
String datastoreName = path.substring(index);
VirtualMachineDiskInfo diskInfo = diskInfoBuilder.getDiskInfoByBackingFileBaseName(volume.getPath(), datastoreName);
if (diskInfo != null) {
String deviceBusName = VMwareUtil.getDeviceBusName(allDevices, disk);
if (deviceBusName.equals(diskInfo.getDiskDeviceBusName())) {
return volume;
}
}
}
}
return null;
}
Aggregations