use of com.vmware.vim25.mo.HostDatastoreSystem in project coprhd-controller by CoprHD.
the class VcenterApiClient method createDatastore.
public String createDatastore(String datacenterName, String clusterNameOrMoRef, String hostname, String volumeUuid, String datastoreName) throws VcenterSystemException, VcenterObjectNotFoundException, VcenterObjectConnectionException {
try {
_log.info("Request to create datastore on volume " + volumeUuid + " host " + hostname + " to datacenter " + datacenterName + " cluster " + clusterNameOrMoRef);
HostSystem hostSystem = (HostSystem) createManagedEntityMap(datacenterName, clusterNameOrMoRef, hostname, true).get("HostSystem");
if (volumeUuid == null || volumeUuid.trim().equals("")) {
_log.error("Volume UUID not specified");
throw new VcenterSystemException("Volume UUID not specified");
}
Datastore[] datastores = hostSystem.getDatastores();
if (datastores != null && datastores.length > 0) {
_log.info("Check host " + hostname + " for existing datastore on volume " + volumeUuid);
String specifiedVolumeDevicePath = null;
HostStorageSystem hostStorageSystem = hostSystem.getHostStorageSystem();
HostStorageDeviceInfo hostStorageDeviceInfo = hostStorageSystem.getStorageDeviceInfo();
ScsiLun[] hostScsiLuns = hostStorageDeviceInfo.getScsiLun();
for (ScsiLun scsiLun : hostScsiLuns) {
if (scsiLun instanceof HostScsiDisk) {
HostScsiDisk hostScsiDisk = (HostScsiDisk) scsiLun;
if (hostScsiDisk.getUuid().toLowerCase().contains(volumeUuid.toLowerCase())) {
_log.info("Found disk " + hostScsiDisk.getUuid() + " on " + hostname + " for volume UUID " + volumeUuid);
specifiedVolumeDevicePath = hostScsiDisk.getDevicePath();
break;
}
}
}
// datastore already exists.
if (specifiedVolumeDevicePath != null) {
for (Datastore datastore : datastores) {
if (datastore.getInfo() instanceof VmfsDatastoreInfo) {
VmfsDatastoreInfo vmfsDatastoreInfo = (VmfsDatastoreInfo) datastore.getInfo();
_log.info("Found datastore " + vmfsDatastoreInfo.getName() + " " + vmfsDatastoreInfo.getVmfs().getUuid());
String diskName = vmfsDatastoreInfo.getVmfs().getExtent()[0].getDiskName();
_log.info("Found datastore " + vmfsDatastoreInfo.getName() + " on disk " + diskName);
String devicePath = "/vmfs/devices/disks/" + diskName;
if (devicePath.equalsIgnoreCase(specifiedVolumeDevicePath)) {
_log.info("Datastore " + vmfsDatastoreInfo.getName() + " " + devicePath + " " + datastore.getMOR().getVal() + " already present");
return datastore.getMOR().getVal();
}
}
}
}
}
_log.info("Search for candidate disk via host " + hostname);
HostDatastoreSystem hostDatastoreSystem = hostSystem.getHostDatastoreSystem();
HostScsiDisk[] hostScsiDisks = hostDatastoreSystem.queryAvailableDisksForVmfs(null);
HostScsiDisk candidateHostScsiDisk = null;
for (HostScsiDisk hostScsiDisk : hostScsiDisks) {
_log.info("Found disk " + hostScsiDisk.getDevicePath() + " " + hostScsiDisk.getUuid());
if (hostScsiDisk.getUuid().toLowerCase().contains(volumeUuid.toLowerCase())) {
candidateHostScsiDisk = hostScsiDisk;
break;
}
}
if (candidateHostScsiDisk == null) {
_log.error("Disk " + volumeUuid + " not found - Ensure underlying storage properly configured and disk accessible to host");
throw new VcenterSystemException("Disk " + volumeUuid + " not found - Ensure underlying storage properly configured and disk accessible to host");
}
String devicePath = candidateHostScsiDisk.getDevicePath();
_log.info("Create datastore via host " + hostname + " on disk " + devicePath);
VmfsDatastoreOption[] vmfsDatastoreOption = hostDatastoreSystem.queryVmfsDatastoreCreateOptions(devicePath);
VmfsDatastoreCreateSpec vmfsDatastoreCreateSpec = (VmfsDatastoreCreateSpec) vmfsDatastoreOption[0].getSpec();
vmfsDatastoreCreateSpec.getVmfs().setVolumeName(datastoreName);
// TODO externalize
vmfsDatastoreCreateSpec.getVmfs().setBlockSizeMb(1);
Datastore datastore = null;
try {
datastore = hostDatastoreSystem.createVmfsDatastore(vmfsDatastoreCreateSpec);
} catch (HostConfigFault hcf) {
_log.info("HostConfigFault creating datastore on disk " + devicePath + " thus retry");
if (hcf.getFaultMessage() != null && hcf.getFaultMessage().length > 0 && hcf.getFaultMessage()[0] != null) {
String errorMessage = hcf.getFaultMessage()[0].toString();
_log.error("HostConfigFault details are " + errorMessage);
}
datastore = hostDatastoreSystem.createVmfsDatastore(vmfsDatastoreCreateSpec);
}
if (datastore == null) {
// Should not happen
_log.error("Datastore null after create");
throw new VcenterSystemException("Error creating datastore");
}
return datastore.getMOR().getVal();
} catch (VcenterSystemException | VcenterObjectNotFoundException | VcenterObjectConnectionException e) {
throw e;
} catch (Exception e) {
_log.error("Exception creating datastore: " + e);
throw new VcenterSystemException(e.getLocalizedMessage());
}
}
use of com.vmware.vim25.mo.HostDatastoreSystem in project cloudstack by apache.
the class VmwareResource method execute.
private Answer execute(ResizeVolumeCommand cmd) {
String path = cmd.getPath();
String vmName = cmd.getInstanceName();
long newSize = cmd.getNewSize() / ResourceType.bytesToKiB;
long oldSize = cmd.getCurrentSize() / ResourceType.bytesToKiB;
boolean managed = cmd.isManaged();
String poolUUID = cmd.getPoolUuid();
String chainInfo = cmd.getChainInfo();
boolean useWorkerVm = false;
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
VirtualMachineMO vmMo = null;
String vmdkDataStorePath = null;
try {
if (newSize < oldSize) {
String errorMsg = String.format("VMware doesn't support shrinking volume from larger size [%s] GB to a smaller size [%s] GB. Can't resize volume of VM [name: %s].", oldSize / Float.valueOf(ResourceType.bytesToMiB), newSize / Float.valueOf(ResourceType.bytesToMiB), vmName);
s_logger.error(errorMsg);
throw new Exception(errorMsg);
} else if (newSize == oldSize) {
return new ResizeVolumeAnswer(cmd, true, "success", newSize * ResourceType.bytesToKiB);
}
if (vmName.equalsIgnoreCase("none")) {
// OfflineVmwareMigration: we need to refactor the worker vm creation out for use in migration methods as well as here
// OfflineVmwareMigration: this method is 100 lines and needs refactorring anyway
// we need to spawn a worker VM to attach the volume to and resize the volume.
useWorkerVm = true;
String poolId = cmd.getPoolUuid();
// OfflineVmwareMigration: refactor for re-use
// OfflineVmwareMigration: 1. find data(store)
ManagedObjectReference morDS = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, poolId);
DatastoreMO dsMo = new DatastoreMO(hyperHost.getContext(), morDS);
vmName = getWorkerName(getServiceContext(), cmd, 0, dsMo);
s_logger.info("Create worker VM " + vmName);
// OfflineVmwareMigration: 2. create the worker with access to the data(store)
vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, vmName, null);
if (vmMo == null) {
// OfflineVmwareMigration: don't throw a general Exception but think of a specific one
throw new Exception("Unable to create a worker VM for volume resize");
}
synchronized (this) {
// OfflineVmwareMigration: 3. attach the disk to the worker
vmdkDataStorePath = VmwareStorageLayoutHelper.getLegacyDatastorePathFromVmdkFileName(dsMo, path + VMDK_EXTENSION);
vmMo.attachDisk(new String[] { vmdkDataStorePath }, morDS);
}
}
// OfflineVmwareMigration: 4. find the (worker-) VM
// find VM through datacenter (VM is not at the target host yet)
vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
if (vmMo == null) {
String errorMsg = String.format("VM [name: %s] does not exist in VMware datacenter.", vmName);
s_logger.error(errorMsg);
throw new Exception(errorMsg);
}
if (managed) {
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
ClusterMO clusterMO = new ClusterMO(context, morCluster);
List<Pair<ManagedObjectReference, String>> lstHosts = clusterMO.getClusterHosts();
Collections.shuffle(lstHosts, RANDOM);
Pair<ManagedObjectReference, String> host = lstHosts.get(0);
HostMO hostMO = new HostMO(context, host.first());
HostDatastoreSystemMO hostDatastoreSystem = hostMO.getHostDatastoreSystemMO();
String iScsiName = cmd.get_iScsiName();
ManagedObjectReference morDS = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, VmwareResource.getDatastoreName(iScsiName));
DatastoreMO dsMo = new DatastoreMO(hyperHost.getContext(), morDS);
_storageProcessor.expandDatastore(hostDatastoreSystem, dsMo);
}
boolean volumePathChangeObserved = false;
boolean datastoreChangeObserved = false;
Pair<String, String> pathAndChainInfo = getNewPathAndChainInfoInDatastoreCluster(vmMo, path, chainInfo, managed, cmd.get_iScsiName(), poolUUID, cmd.getContextParam(DiskTO.PROTOCOL_TYPE));
Pair<String, String> poolUUIDandChainInfo = getNewPoolUUIDAndChainInfoInDatastoreCluster(vmMo, path, chainInfo, managed, cmd.get_iScsiName(), poolUUID, cmd.getContextParam(DiskTO.PROTOCOL_TYPE));
if (pathAndChainInfo != null) {
volumePathChangeObserved = true;
path = pathAndChainInfo.first();
chainInfo = pathAndChainInfo.second();
}
if (poolUUIDandChainInfo != null) {
datastoreChangeObserved = true;
poolUUID = poolUUIDandChainInfo.first();
chainInfo = poolUUIDandChainInfo.second();
}
// OfflineVmwareMigration: 5. ignore/replace the rest of the try-block; It is the functional bit
VirtualDisk disk = getDiskAfterResizeDiskValidations(vmMo, path);
String vmdkAbsFile = getAbsoluteVmdkFile(disk);
if (vmdkAbsFile != null && !vmdkAbsFile.isEmpty()) {
vmMo.updateAdapterTypeIfRequired(vmdkAbsFile);
}
disk.setCapacityInKB(newSize);
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(disk);
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
if (!vmMo.configureVm(vmConfigSpec)) {
throw new Exception(String.format("Failed to configure VM [name: %s] to resize disk.", vmName));
}
ResizeVolumeAnswer answer = new ResizeVolumeAnswer(cmd, true, "success", newSize * 1024);
if (datastoreChangeObserved) {
answer.setContextParam("datastoreUUID", poolUUID);
answer.setContextParam("chainInfo", chainInfo);
}
if (volumePathChangeObserved) {
answer.setContextParam("volumePath", path);
answer.setContextParam("chainInfo", chainInfo);
}
return answer;
} catch (Exception e) {
String errorMsg = String.format("Failed to resize volume of VM [name: %s] due to: [%s].", vmName, e.getMessage());
s_logger.error(errorMsg, e);
return new ResizeVolumeAnswer(cmd, false, errorMsg);
} finally {
// OfflineVmwareMigration: 6. check if a worker was used and destroy it if needed
try {
if (useWorkerVm) {
s_logger.info("Destroy worker VM after volume resize");
vmMo.detachDisk(vmdkDataStorePath, false);
vmMo.destroy();
}
} catch (Throwable e) {
s_logger.error(String.format("Failed to destroy worker VM [name: %s] due to: [%s].", vmName, e.getMessage()), e);
}
}
}
use of com.vmware.vim25.mo.HostDatastoreSystem in project cloudstack by apache.
the class HostDatastoreSystemMO method getDatastorePropertiesOnHostDatastoreSystem.
public List<ObjectContent> getDatastorePropertiesOnHostDatastoreSystem(String[] propertyPaths) throws Exception {
PropertySpec pSpec = new PropertySpec();
pSpec.setType("Datastore");
pSpec.getPathSet().addAll(Arrays.asList(propertyPaths));
TraversalSpec hostDsSys2DatastoreTraversal = new TraversalSpec();
hostDsSys2DatastoreTraversal.setType("HostDatastoreSystem");
hostDsSys2DatastoreTraversal.setPath("datastore");
hostDsSys2DatastoreTraversal.setName("hostDsSys2DatastoreTraversal");
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(_mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(hostDsSys2DatastoreTraversal);
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.getPropSet().add(pSpec);
pfSpec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
pfSpecArr.add(pfSpec);
return _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
}
use of com.vmware.vim25.mo.HostDatastoreSystem in project CloudStack-archive by CloudStack-extras.
the class HostDatastoreSystemMO method getDatastorePropertiesOnHostDatastoreSystem.
public ObjectContent[] getDatastorePropertiesOnHostDatastoreSystem(String[] propertyPaths) throws Exception {
PropertySpec pSpec = new PropertySpec();
pSpec.setType("Datastore");
pSpec.setPathSet(propertyPaths);
TraversalSpec hostDsSys2DatastoreTraversal = new TraversalSpec();
hostDsSys2DatastoreTraversal.setType("HostDatastoreSystem");
hostDsSys2DatastoreTraversal.setPath("datastore");
hostDsSys2DatastoreTraversal.setName("hostDsSys2DatastoreTraversal");
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(_mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.setSelectSet(new SelectionSpec[] { hostDsSys2DatastoreTraversal });
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.setPropSet(new PropertySpec[] { pSpec });
pfSpec.setObjectSet(new ObjectSpec[] { oSpec });
return _context.getService().retrieveProperties(_context.getServiceContent().getPropertyCollector(), new PropertyFilterSpec[] { pfSpec });
}
use of com.vmware.vim25.mo.HostDatastoreSystem 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());
}
}
Aggregations