use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost in project cloudstack by apache.
the class VmwareStorageProcessor method removeManagedTargetsFromCluster.
private void removeManagedTargetsFromCluster(List<String> managedDatastoreNames) throws Exception {
List<HostInternetScsiHbaStaticTarget> lstManagedTargets = new ArrayList<>();
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
ClusterMO cluster = new ClusterMO(context, morCluster);
List<Pair<ManagedObjectReference, String>> lstHosts = cluster.getClusterHosts();
HostMO hostMO = new HostMO(context, lstHosts.get(0).first());
HostStorageSystemMO hostStorageSystem = hostMO.getHostStorageSystemMO();
for (String managedDatastoreName : managedDatastoreNames) {
unmountVmfsDatastore(context, hyperHost, managedDatastoreName, lstHosts);
}
for (HostHostBusAdapter hba : hostStorageSystem.getStorageDeviceInfo().getHostBusAdapter()) {
if (hba instanceof HostInternetScsiHba && ((HostInternetScsiHba) hba).isIsSoftwareBased()) {
List<HostInternetScsiHbaStaticTarget> lstTargets = ((HostInternetScsiHba) hba).getConfiguredStaticTarget();
if (lstTargets != null) {
for (HostInternetScsiHbaStaticTarget target : lstTargets) {
if (managedDatastoreNames.contains(VmwareResource.createDatastoreNameFromIqn(target.getIScsiName()))) {
lstManagedTargets.add(target);
}
}
}
}
}
ExecutorService executorService = Executors.newFixedThreadPool(lstHosts.size());
for (Pair<ManagedObjectReference, String> host : lstHosts) {
List<Pair<ManagedObjectReference, String>> hosts = new ArrayList<>();
hosts.add(host);
List<HostInternetScsiHbaStaticTarget> staticTargetsForHost = new ArrayList<>();
for (HostInternetScsiHbaStaticTarget iScsiManagedTarget : lstManagedTargets) {
String storageAddress = iScsiManagedTarget.getAddress();
HostDiscoveryMethod hostDiscoveryMethod = getHostDiscoveryMethod(context, storageAddress, hosts);
List<HostMO> hostsUsingStaticDiscovery = hostDiscoveryMethod.getHostsUsingStaticDiscovery();
if (hostsUsingStaticDiscovery != null && hostsUsingStaticDiscovery.size() > 0) {
staticTargetsForHost.add(iScsiManagedTarget);
}
}
if (staticTargetsForHost.size() > 0) {
executorService.submit(new Thread(() -> {
try {
addRemoveInternetScsiTargetsToAllHosts(context, false, staticTargetsForHost, hosts);
rescanAllHosts(context, hosts, true, false);
} catch (Exception ex) {
s_logger.warn(ex.getMessage());
}
}));
}
}
executorService.shutdown();
if (!executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES)) {
throw new Exception("The system timed out before completing the task 'removeManagedTargetsFromCluster'.");
}
}
use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost in project cloudstack by apache.
the class VmwareStorageProcessor method attachVolume.
private Answer attachVolume(Command cmd, DiskTO disk, boolean isAttach, boolean isManaged, String vmName, String iScsiName, String storageHost, int storagePort, Map<String, String> controllerInfo) {
VolumeObjectTO volumeTO = (VolumeObjectTO) disk.getData();
DataStoreTO primaryStore = volumeTO.getDataStore();
String volumePath = volumeTO.getPath();
String storagePolicyId = volumeTO.getvSphereStoragePolicyId();
String vmdkPath = isManaged ? resource.getVmdkPath(volumePath) : 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 AttachCommand, vmName: " + vmName;
s_logger.error(msg);
throw new Exception(msg);
}
}
vmName = vmMo.getName();
ManagedObjectReference morDs;
String diskUuid = volumeTO.getUuid().replace("-", "");
if (isAttach && isManaged) {
Map<String, String> details = disk.getDetails();
morDs = prepareManagedStorage(context, hyperHost, diskUuid, iScsiName, storageHost, storagePort, vmdkPath, details.get(DiskTO.CHAP_INITIATOR_USERNAME), details.get(DiskTO.CHAP_INITIATOR_SECRET), details.get(DiskTO.CHAP_TARGET_USERNAME), details.get(DiskTO.CHAP_TARGET_SECRET), volumeTO.getSize(), cmd);
} else {
if (storagePort == DEFAULT_NFS_PORT) {
morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, isManaged ? VmwareResource.getDatastoreName(diskUuid) : primaryStore.getUuid());
} else {
morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, isManaged ? VmwareResource.getDatastoreName(iScsiName) : primaryStore.getUuid());
}
}
if (morDs == null) {
String msg = "Unable to find the mounted datastore to execute AttachCommand, vmName: " + vmName;
s_logger.error(msg);
throw new Exception(msg);
}
DatastoreMO dsMo = new DatastoreMO(context, morDs);
String datastoreVolumePath;
boolean datastoreChangeObserved = false;
boolean volumePathChangeObserved = false;
String chainInfo = null;
if (isAttach) {
if (isManaged) {
datastoreVolumePath = dsMo.getDatastorePath((vmdkPath != null ? vmdkPath : dsMo.getName()) + ".vmdk");
} else {
if (dsMo.getDatastoreType().equalsIgnoreCase("VVOL")) {
datastoreVolumePath = VmwareStorageLayoutHelper.getLegacyDatastorePathFromVmdkFileName(dsMo, volumePath + ".vmdk");
if (!dsMo.fileExists(datastoreVolumePath)) {
datastoreVolumePath = VmwareStorageLayoutHelper.getVmwareDatastorePathFromVmdkFileName(dsMo, vmName, volumePath + ".vmdk");
}
if (!dsMo.folderExists(String.format("[%s]", dsMo.getName()), vmName) || !dsMo.fileExists(datastoreVolumePath)) {
datastoreVolumePath = VmwareStorageLayoutHelper.getVmwareDatastorePathFromVmdkFileName(dsMo, volumePath, volumePath + ".vmdk");
}
if (!dsMo.folderExists(String.format("[%s]", dsMo.getName()), volumePath) || !dsMo.fileExists(datastoreVolumePath)) {
datastoreVolumePath = dsMo.searchFileInSubFolders(volumePath + ".vmdk", true, null);
}
} else {
datastoreVolumePath = VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dsMo.getOwnerDatacenter().first(), vmName, dsMo, volumePath, VmwareManager.s_vmwareSearchExcludeFolder.value());
}
}
} else {
if (isManaged) {
datastoreVolumePath = dsMo.getDatastorePath((vmdkPath != null ? vmdkPath : dsMo.getName()) + ".vmdk");
} else {
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);
}
}
}
}
if (storagePort == DEFAULT_NFS_PORT) {
morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, isManaged ? VmwareResource.getDatastoreName(diskUuid) : datastoreUUID);
} else {
morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, isManaged ? VmwareResource.getDatastoreName(iScsiName) : datastoreUUID);
}
dsMo = new DatastoreMO(context, morDs);
datastoreVolumePath = VmwareStorageLayoutHelper.getLegacyDatastorePathFromVmdkFileName(dsMo, volumePath + ".vmdk");
if (!dsMo.fileExists(datastoreVolumePath)) {
datastoreVolumePath = VmwareStorageLayoutHelper.getVmwareDatastorePathFromVmdkFileName(dsMo, vmName, volumePath + ".vmdk");
}
if (!dsMo.folderExists(String.format("[%s]", dsMo.getName()), vmName) || !dsMo.fileExists(datastoreVolumePath)) {
datastoreVolumePath = VmwareStorageLayoutHelper.getVmwareDatastorePathFromVmdkFileName(dsMo, volumePath, volumePath + ".vmdk");
}
if (!dsMo.folderExists(String.format("[%s]", dsMo.getName()), volumePath) || !dsMo.fileExists(datastoreVolumePath)) {
datastoreVolumePath = dsMo.searchFileInSubFolders(volumePath + ".vmdk", true, null);
}
}
}
disk.setPath(datastoreVolumePath);
AttachAnswer answer = new AttachAnswer(disk);
if (isAttach) {
String diskController = getLegacyVmDataDiskController();
if (controllerInfo != null && StringUtils.isNotEmpty(controllerInfo.get(VmDetailConstants.DATA_DISK_CONTROLLER))) {
diskController = controllerInfo.get(VmDetailConstants.DATA_DISK_CONTROLLER);
}
if (DiskControllerType.getType(diskController) == DiskControllerType.osdefault) {
diskController = vmMo.getRecommendedDiskController(null);
}
vmMo.attachDisk(new String[] { datastoreVolumePath }, morDs, diskController, storagePolicyId);
VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
VirtualMachineDiskInfo diskInfo = diskInfoBuilder.getDiskInfoByBackingFileBaseName(volumePath, dsMo.getName());
chainInfo = _gson.toJson(diskInfo);
if (isManaged) {
expandVirtualDisk(vmMo, datastoreVolumePath, volumeTO.getSize());
}
} else {
vmMo.removeAllSnapshots();
vmMo.detachDisk(datastoreVolumePath, false);
if (isManaged) {
handleDatastoreAndVmdkDetachManaged(cmd, diskUuid, iScsiName, storageHost, storagePort);
} else {
if (!dsMo.getDatastoreType().equalsIgnoreCase("VVOL")) {
VmwareStorageLayoutHelper.syncVolumeToRootFolder(dsMo.getOwnerDatacenter().first(), dsMo, volumePath, vmName, VmwareManager.s_vmwareSearchExcludeFolder.value());
}
}
if (datastoreChangeObserved) {
answer.setContextParam("datastoreName", dsMo.getCustomFieldValue(CustomFieldConstants.CLOUD_UUID));
}
if (volumePathChangeObserved) {
answer.setContextParam("volumePath", volumePath);
}
}
if (chainInfo != null && !chainInfo.isEmpty())
answer.setContextParam("chainInfo", chainInfo);
return answer;
} catch (Throwable e) {
String msg = String.format("Failed to %s volume!", isAttach ? "attach" : "detach");
s_logger.error(msg, e);
hostService.createLogMessageException(e, cmd);
// Sending empty error message - too many duplicate errors in UI
return new AttachAnswer("");
}
}
use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost in project cloudstack by apache.
the class VmwareStorageProcessor method resignature.
@Override
public ResignatureAnswer resignature(ResignatureCommand cmd) {
final Map<String, String> details = cmd.getDetails();
String scsiNaaDeviceId = details.get(DiskTO.SCSI_NAA_DEVICE_ID);
if (scsiNaaDeviceId == null || scsiNaaDeviceId.trim().length() == 0) {
throw new CloudRuntimeException("The 'scsiNaaDeviceId' needs to be specified when resignaturing a VMware datastore.");
}
final String iScsiName = details.get(DiskTO.IQN);
final String datastoreName = getMaximumDatastoreName(VmwareResource.getDatastoreName(iScsiName));
String vmdk = null;
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
ClusterMO clusterMO = new ClusterMO(context, morCluster);
List<Pair<ManagedObjectReference, String>> lstHosts = clusterMO.getClusterHosts();
// add iSCSI connection to host
final String storageHost = details.get(DiskTO.STORAGE_HOST);
final int storagePortNumber = Integer.parseInt(details.get(DiskTO.STORAGE_PORT));
final String chapInitiatorUsername = details.get(DiskTO.CHAP_INITIATOR_USERNAME);
final String chapInitiatorSecret = details.get(DiskTO.CHAP_INITIATOR_SECRET);
final String chapTargetUsername = details.get(DiskTO.CHAP_TARGET_USERNAME);
final String chapTargetSecret = details.get(DiskTO.CHAP_TARGET_SECRET);
HostDiscoveryMethod hostDiscoveryMethod = getHostDiscoveryMethod(context, storageHost, lstHosts);
List<HostMO> hostsUsingStaticDiscovery = hostDiscoveryMethod.getHostsUsingStaticDiscovery();
if (hostsUsingStaticDiscovery != null && hostsUsingStaticDiscovery.size() > 0) {
List<HostInternetScsiHbaStaticTarget> lstTargets = getTargets(storageHost, storagePortNumber, trimIqn(iScsiName), chapInitiatorUsername, chapInitiatorSecret, chapTargetUsername, chapTargetSecret);
addRemoveInternetScsiTargetsToAllHosts(true, lstTargets, hostsUsingStaticDiscovery);
}
rescanAllHosts(context, lstHosts, true, true);
// perform resignature operation
HostMO hostMO = new HostMO(context, lstHosts.get(0).first());
HostDatastoreSystemMO hostDatastoreSystem = hostMO.getHostDatastoreSystemMO();
List<HostUnresolvedVmfsVolume> hostUnresolvedVmfsVolumes = hostDatastoreSystem.queryUnresolvedVmfsVolumes();
if (hostUnresolvedVmfsVolumes == null || hostUnresolvedVmfsVolumes.size() == 0) {
throw new CloudRuntimeException("Unable to locate any snapshot datastores");
}
boolean foundExtent = false;
for (HostUnresolvedVmfsVolume hostUnresolvedVmfsVolume : hostUnresolvedVmfsVolumes) {
List<HostUnresolvedVmfsExtent> extents = hostUnresolvedVmfsVolume.getExtent();
List<HostUnresolvedVmfsExtent> matchingExtents = getExtentsMatching(extents, scsiNaaDeviceId);
if (matchingExtents.size() >= 1) {
String extentDevicePath = matchingExtents.get(0).getDevicePath();
HostResignatureRescanResult hostResignatureRescanResult = resignatureDatastore(hostDatastoreSystem, extentDevicePath);
if (hostResignatureRescanResult == null) {
throw new CloudRuntimeException("'hostResignatureRescanResult' should not be 'null'.");
}
ManagedObjectReference morDs = hostResignatureRescanResult.getResult();
if (morDs == null) {
throw new CloudRuntimeException("'morDs' should not be 'null'.");
}
DatastoreMO datastoreMO = new DatastoreMO(context, morDs);
boolean isOnlyForTemplate = Boolean.parseBoolean(details.get(DiskTO.TEMPLATE_RESIGN));
// then rename the datastore.
if (isOnlyForTemplate) {
vmdk = details.get(DiskTO.VMDK);
} else {
vmdk = cleanUpDatastore(cmd, hostDatastoreSystem, datastoreMO, details);
}
if (renameDatastore(context, morDs, datastoreName, lstHosts)) {
foundExtent = true;
break;
}
}
}
removeVmfsDatastore(cmd, hyperHost, datastoreName, storageHost, storagePortNumber, trimIqn(iScsiName), lstHosts);
if (!foundExtent) {
throw new CloudRuntimeException("Unable to locate the applicable extent");
}
final ResignatureAnswer answer = new ResignatureAnswer();
final long volumeSize = Long.parseLong(details.get(DiskTO.VOLUME_SIZE));
answer.setSize(volumeSize);
answer.setPath("[" + datastoreName + "] " + vmdk);
answer.setFormat(ImageFormat.OVA);
return answer;
} catch (Exception ex) {
s_logger.error(String.format("Command %s failed due to: [%s].", cmd.getClass().getSimpleName(), ex.getMessage()), ex);
throw new CloudRuntimeException(ex.getMessage());
}
}
use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost in project cloudstack by apache.
the class VmwareStorageProcessor method cleanUpDatastore.
/**
* 1) Possibly expand the datastore.
* 2) Possibly consolidate all relevant VMDK files into one VMDK file.
* 3) Possibly move the VMDK file to the root folder (may already be there).
* 4) If the VMDK file wasn't already in the root folder, then delete the folder the VMDK file was in.
* 5) Possibly rename the VMDK file (this will lead to there being a delta file with the new name and the
* original file with the original name).
*
* Note: If the underlying VMDK file was for a root disk, the 'vmdk' parameter's value might look, for example,
* like "i-2-32-VM/ROOT-32.vmdk".
*
* Note: If the underlying VMDK file was for a data disk, the 'vmdk' parameter's value might look, for example,
* like "-iqn.2010-01.com.solidfire:4nhe.data-32.79-0.vmdk".
*
* Returns the (potentially new) name of the VMDK file.
*/
private String cleanUpDatastore(Command cmd, HostDatastoreSystemMO hostDatastoreSystem, DatastoreMO dsMo, Map<String, String> details) throws Exception {
s_logger.debug(String.format("Executing clean up in DataStore: [%s].", dsMo.getName()));
boolean expandDatastore = Boolean.parseBoolean(details.get(DiskTO.EXPAND_DATASTORE));
// reserve value in use is set to the minimum for the cloned volume), but that's fine.
if (expandDatastore) {
expandDatastore(hostDatastoreSystem, dsMo);
}
String vmdk = details.get(DiskTO.VMDK);
String fullVmdkPath = new DatastoreFile(dsMo.getName(), vmdk).getPath();
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
String vmName = getVmName(vmdk);
// If vmName is not null, then move all VMDK files out of this folder to the root folder and then delete the folder named vmName.
if (vmName != null) {
String workerVmName = hostService.getWorkerName(context, cmd, 0, dsMo);
VirtualMachineMO vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVmName, null);
if (vmMo == null) {
throw new Exception("Unable to create a worker VM for volume creation");
}
vmMo.attachDisk(new String[] { fullVmdkPath }, dsMo.getMor());
List<String> backingFiles = new ArrayList<>(1);
List<VirtualDisk> virtualDisks = vmMo.getVirtualDisks();
VirtualDisk virtualDisk = virtualDisks.get(0);
VirtualDeviceBackingInfo virtualDeviceBackingInfo = virtualDisk.getBacking();
while (virtualDeviceBackingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
VirtualDiskFlatVer2BackingInfo backingInfo = (VirtualDiskFlatVer2BackingInfo) virtualDeviceBackingInfo;
backingFiles.add(backingInfo.getFileName());
virtualDeviceBackingInfo = backingInfo.getParent();
}
vmMo.detachAllDisksAndDestroy();
VmwareStorageLayoutHelper.moveVolumeToRootFolder(dcMo, backingFiles);
vmdk = new DatastoreFile(vmdk).getFileName();
// Delete the folder the VMDK file was in.
DatastoreFile folderToDelete = new DatastoreFile(dsMo.getName(), vmName);
dsMo.deleteFolder(folderToDelete.getPath(), dcMo.getMor());
}
return vmdk;
}
use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost in project cloudstack by apache.
the class VmwareStorageProcessor method copyVolumeFromPrimaryToSecondary.
@Override
public Answer copyVolumeFromPrimaryToSecondary(CopyCommand cmd) {
VolumeObjectTO srcVolume = (VolumeObjectTO) cmd.getSrcTO();
VolumeObjectTO destVolume = (VolumeObjectTO) cmd.getDestTO();
String vmName = srcVolume.getVmName();
VmwareContext context = hostService.getServiceContext(cmd);
try {
DataStoreTO primaryStorage = srcVolume.getDataStore();
NfsTO destStore = (NfsTO) destVolume.getDataStore();
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
Pair<String, String> result;
result = copyVolumeToSecStorage(hostService, hyperHost, cmd, vmName, primaryStorage.getUuid(), srcVolume.getPath(), destVolume.getPath(), destStore.getUrl(), hostService.getWorkerName(context, cmd, 0, null));
VolumeObjectTO newVolume = new VolumeObjectTO();
newVolume.setPath(result.first() + File.separator + result.second());
return new CopyCmdAnswer(newVolume);
} catch (Throwable e) {
return new CopyCmdAnswer(hostService.createLogMessageException(e, cmd));
}
}
Aggregations