use of com.vmware.vim25.mo.Datastore in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(GetStorageStatsCommand cmd) {
if (s_logger.isTraceEnabled()) {
s_logger.trace("Executing resource GetStorageStatsCommand: " + _gson.toJson(cmd));
}
try {
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, cmd.getStorageId());
if (morDs != null) {
DatastoreMO datastoreMo = new DatastoreMO(context, morDs);
DatastoreSummary summary = datastoreMo.getSummary();
assert (summary != null);
long capacity = summary.getCapacity();
long free = summary.getFreeSpace();
long used = capacity - free;
if (s_logger.isDebugEnabled()) {
s_logger.debug("Datastore summary info, storageId: " + cmd.getStorageId() + ", localPath: " + cmd.getLocalPath() + ", poolType: " + cmd.getPooltype() + ", capacity: " + capacity + ", free: " + free + ", used: " + used);
}
if (summary.getCapacity() <= 0) {
s_logger.warn("Something is wrong with vSphere NFS datastore, rebooting ESX(ESXi) host should help");
}
return new GetStorageStatsAnswer(cmd, capacity, used);
} else {
String msg = "Could not find datastore for GetStorageStatsCommand storageId : " + cmd.getStorageId() + ", localPath: " + cmd.getLocalPath() + ", poolType: " + cmd.getPooltype();
s_logger.error(msg);
return new GetStorageStatsAnswer(cmd, msg);
}
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
invalidateServiceContext();
}
String msg = "Unable to execute GetStorageStatsCommand(storageId : " + cmd.getStorageId() + ", localPath: " + cmd.getLocalPath() + ", poolType: " + cmd.getPooltype() + ") due to " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg, e);
return new GetStorageStatsAnswer(cmd, msg);
}
}
use of com.vmware.vim25.mo.Datastore in project cloudstack by apache.
the class VmwareStorageManagerImpl method execute.
@Override
public Answer execute(VmwareHostService hostService, CreateVolumeFromSnapshotCommand cmd) {
String primaryStorageNameLabel = cmd.getPrimaryStoragePoolNameLabel();
Long accountId = cmd.getAccountId();
Long volumeId = cmd.getVolumeId();
String secondaryStorageUrl = cmd.getSecondaryStorageUrl();
String backedUpSnapshotUuid = cmd.getSnapshotUuid();
String details = null;
boolean success = false;
String newVolumeName = UUID.randomUUID().toString().replaceAll("-", "");
VmwareContext context = hostService.getServiceContext(cmd);
try {
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
ManagedObjectReference morPrimaryDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, primaryStorageNameLabel);
if (morPrimaryDs == null) {
String msg = "Unable to find datastore: " + primaryStorageNameLabel;
s_logger.error(msg);
throw new Exception(msg);
}
DatastoreMO primaryDsMo = new DatastoreMO(hyperHost.getContext(), morPrimaryDs);
details = createVolumeFromSnapshot(hyperHost, primaryDsMo, newVolumeName, accountId, volumeId, secondaryStorageUrl, backedUpSnapshotUuid, cmd.getNfsVersion());
if (details == null) {
success = true;
}
} catch (Throwable e) {
if (e instanceof RemoteException) {
hostService.invalidateServiceContext(context);
}
s_logger.error("Unexpecpted exception ", e);
details = "CreateVolumeFromSnapshotCommand exception: " + StringUtils.getExceptionStackInfo(e);
}
return new CreateVolumeFromSnapshotAnswer(cmd, success, details, newVolumeName);
}
use of com.vmware.vim25.mo.Datastore in project cloudstack by apache.
the class VmwareHelper method prepareDiskDevice.
// vmdkDatastorePath: [datastore name] vmdkFilePath, create delta disk based on disk from template
public static VirtualDevice prepareDiskDevice(VirtualMachineMO vmMo, int controllerKey, String vmdkDatastorePath, int sizeInMb, ManagedObjectReference morDs, VirtualDisk templateDisk, int deviceNumber, int contextNumber) throws Exception {
assert (templateDisk != null);
VirtualDeviceBackingInfo parentBacking = templateDisk.getBacking();
assert (parentBacking != null);
// TODO Not sure if we need to check if the disk in template and the new disk needs to share the
// same datastore
VirtualDisk disk = new VirtualDisk();
if (parentBacking instanceof VirtualDiskFlatVer1BackingInfo) {
VirtualDiskFlatVer1BackingInfo backingInfo = new VirtualDiskFlatVer1BackingInfo();
backingInfo.setDiskMode(((VirtualDiskFlatVer1BackingInfo) parentBacking).getDiskMode());
backingInfo.setDatastore(morDs);
backingInfo.setFileName(vmdkDatastorePath);
backingInfo.setParent((VirtualDiskFlatVer1BackingInfo) parentBacking);
disk.setBacking(backingInfo);
} else if (parentBacking instanceof VirtualDiskFlatVer2BackingInfo) {
VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo();
backingInfo.setDiskMode(((VirtualDiskFlatVer2BackingInfo) parentBacking).getDiskMode());
backingInfo.setDatastore(morDs);
backingInfo.setFileName(vmdkDatastorePath);
backingInfo.setParent((VirtualDiskFlatVer2BackingInfo) parentBacking);
disk.setBacking(backingInfo);
} else if (parentBacking instanceof VirtualDiskRawDiskMappingVer1BackingInfo) {
VirtualDiskRawDiskMappingVer1BackingInfo backingInfo = new VirtualDiskRawDiskMappingVer1BackingInfo();
backingInfo.setDiskMode(((VirtualDiskRawDiskMappingVer1BackingInfo) parentBacking).getDiskMode());
backingInfo.setDatastore(morDs);
backingInfo.setFileName(vmdkDatastorePath);
backingInfo.setParent((VirtualDiskRawDiskMappingVer1BackingInfo) parentBacking);
disk.setBacking(backingInfo);
} else if (parentBacking instanceof VirtualDiskSparseVer1BackingInfo) {
VirtualDiskSparseVer1BackingInfo backingInfo = new VirtualDiskSparseVer1BackingInfo();
backingInfo.setDiskMode(((VirtualDiskSparseVer1BackingInfo) parentBacking).getDiskMode());
backingInfo.setDatastore(morDs);
backingInfo.setFileName(vmdkDatastorePath);
backingInfo.setParent((VirtualDiskSparseVer1BackingInfo) parentBacking);
disk.setBacking(backingInfo);
} else if (parentBacking instanceof VirtualDiskSparseVer2BackingInfo) {
VirtualDiskSparseVer2BackingInfo backingInfo = new VirtualDiskSparseVer2BackingInfo();
backingInfo.setDiskMode(((VirtualDiskSparseVer2BackingInfo) parentBacking).getDiskMode());
backingInfo.setDatastore(morDs);
backingInfo.setFileName(vmdkDatastorePath);
backingInfo.setParent((VirtualDiskSparseVer2BackingInfo) parentBacking);
disk.setBacking(backingInfo);
} else {
throw new Exception("Unsupported disk backing: " + parentBacking.getClass().getCanonicalName());
}
int ideControllerKey = vmMo.getIDEDeviceControllerKey();
if (controllerKey < 0)
controllerKey = ideControllerKey;
disk.setControllerKey(controllerKey);
if (deviceNumber < 0) {
deviceNumber = vmMo.getNextDeviceNumber(controllerKey);
}
disk.setKey(-contextNumber);
disk.setUnitNumber(deviceNumber);
disk.setCapacityInKB(sizeInMb * 1024);
VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo();
connectInfo.setConnected(true);
connectInfo.setStartConnected(true);
disk.setConnectable(connectInfo);
return disk;
}
use of com.vmware.vim25.mo.Datastore in project cloudstack by apache.
the class VmwareStorageProcessor method getVmfsDatastore.
private ManagedObjectReference getVmfsDatastore(VmwareContext context, VmwareHypervisorHost hyperHost, String datastoreName, String storageIpAddress, int storagePortNumber, String iqn, String chapName, String chapSecret, String mutualChapName, String mutualChapSecret) throws Exception {
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
ClusterMO cluster = new ClusterMO(context, morCluster);
List<Pair<ManagedObjectReference, String>> lstHosts = cluster.getClusterHosts();
HostInternetScsiHbaStaticTarget target = new HostInternetScsiHbaStaticTarget();
target.setAddress(storageIpAddress);
target.setPort(storagePortNumber);
target.setIScsiName(iqn);
if (StringUtils.isNotBlank(chapName) && StringUtils.isNotBlank(chapSecret)) {
HostInternetScsiHbaAuthenticationProperties auth = new HostInternetScsiHbaAuthenticationProperties();
String strAuthType = "chapRequired";
auth.setChapAuthEnabled(true);
auth.setChapInherited(false);
auth.setChapAuthenticationType(strAuthType);
auth.setChapName(chapName);
auth.setChapSecret(chapSecret);
if (StringUtils.isNotBlank(mutualChapName) && StringUtils.isNotBlank(mutualChapSecret)) {
auth.setMutualChapInherited(false);
auth.setMutualChapAuthenticationType(strAuthType);
auth.setMutualChapName(mutualChapName);
auth.setMutualChapSecret(mutualChapSecret);
}
target.setAuthenticationProperties(auth);
}
final List<HostInternetScsiHbaStaticTarget> lstTargets = new ArrayList<HostInternetScsiHbaStaticTarget>();
lstTargets.add(target);
addRemoveInternetScsiTargetsToAllHosts(context, true, lstTargets, lstHosts);
rescanAllHosts(context, lstHosts);
HostMO host = new HostMO(context, lstHosts.get(0).first());
HostDatastoreSystemMO hostDatastoreSystem = host.getHostDatastoreSystemMO();
ManagedObjectReference morDs = hostDatastoreSystem.findDatastoreByName(datastoreName);
if (morDs != null) {
return morDs;
}
rescanAllHosts(context, lstHosts);
HostStorageSystemMO hostStorageSystem = host.getHostStorageSystemMO();
List<HostScsiDisk> lstHostScsiDisks = hostDatastoreSystem.queryAvailableDisksForVmfs();
HostScsiDisk hostScsiDisk = getHostScsiDisk(hostStorageSystem.getStorageDeviceInfo().getScsiTopology(), lstHostScsiDisks, iqn);
if (hostScsiDisk == null) {
// check to see if the datastore actually does exist already
morDs = hostDatastoreSystem.findDatastoreByName(datastoreName);
if (morDs != null) {
return morDs;
}
throw new Exception("A relevant SCSI disk could not be located to use to create a datastore.");
}
morDs = hostDatastoreSystem.createVmfsDatastore(datastoreName, hostScsiDisk);
if (morDs != null) {
rescanAllHosts(context, lstHosts);
return morDs;
}
throw new Exception("Unable to create a datastore");
}
use of com.vmware.vim25.mo.Datastore in project cloudstack by apache.
the class VmwareStorageProcessor method cloneVolumeFromBaseTemplate.
@Override
public Answer cloneVolumeFromBaseTemplate(CopyCommand cmd) {
DataTO srcData = cmd.getSrcTO();
TemplateObjectTO template = (TemplateObjectTO) srcData;
DataTO destData = cmd.getDestTO();
VolumeObjectTO volume = (VolumeObjectTO) destData;
DataStoreTO primaryStore = volume.getDataStore();
DataStoreTO srcStore = template.getDataStore();
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
VirtualMachineMO vmMo = null;
ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, primaryStore.getUuid());
if (morDatastore == null) {
throw new Exception("Unable to find datastore in vSphere");
}
DatastoreMO dsMo = new DatastoreMO(context, morDatastore);
String vmdkName = volume.getName();
String vmdkFileBaseName = null;
if (srcStore == null) {
// create a root volume for blank VM (created from ISO)
String dummyVmName = hostService.getWorkerName(context, cmd, 0);
try {
vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, dummyVmName);
if (vmMo == null) {
throw new Exception("Unable to create a dummy VM for volume creation");
}
vmdkFileBaseName = vmMo.getVmdkFileBaseNames().get(0);
// we only use the first file in the pair, linked or not will not matter
String[] vmdkFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, null, vmdkFileBaseName, VmwareStorageLayoutType.CLOUDSTACK_LEGACY, true);
String volumeDatastorePath = vmdkFilePair[0];
synchronized (this) {
s_logger.info("Delete file if exists in datastore to clear the way for creating the volume. file: " + volumeDatastorePath);
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vmdkName, dcMo);
vmMo.createDisk(volumeDatastorePath, (int) (volume.getSize() / (1024L * 1024L)), morDatastore, -1);
vmMo.detachDisk(volumeDatastorePath, false);
}
} finally {
s_logger.info("Destroy dummy VM after volume creation");
if (vmMo != null) {
s_logger.warn("Unable to destroy a null VM ManagedObjectReference");
vmMo.detachAllDisks();
vmMo.destroy();
}
}
} else {
String templatePath = template.getPath();
VirtualMachineMO vmTemplate = VmwareHelper.pickOneVmOnRunningHost(dcMo.findVmByNameAndLabel(templatePath), true);
if (vmTemplate == null) {
s_logger.warn("Template host in vSphere is not in connected state, request template reload");
return new CopyCmdAnswer("Template host in vSphere is not in connected state, request template reload");
}
ManagedObjectReference morPool = hyperHost.getHyperHostOwnerResourcePool();
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
if (!_fullCloneFlag) {
createVMLinkedClone(vmTemplate, dcMo, dsMo, vmdkName, morDatastore, morPool);
} else {
createVMFullClone(vmTemplate, dcMo, dsMo, vmdkName, morDatastore, morPool);
}
vmMo = new ClusterMO(context, morCluster).findVmOnHyperHost(vmdkName);
assert (vmMo != null);
// TO-DO: Support for base template containing multiple disks
vmdkFileBaseName = vmMo.getVmdkFileBaseNames().get(0);
s_logger.info("Move volume out of volume-wrapper VM ");
String[] vmwareLayoutFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, vmdkName, vmdkFileBaseName, VmwareStorageLayoutType.VMWARE, !_fullCloneFlag);
String[] legacyCloudStackLayoutFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, vmdkName, vmdkFileBaseName, VmwareStorageLayoutType.CLOUDSTACK_LEGACY, !_fullCloneFlag);
dsMo.moveDatastoreFile(vmwareLayoutFilePair[0], dcMo.getMor(), dsMo.getMor(), legacyCloudStackLayoutFilePair[0], dcMo.getMor(), true);
dsMo.moveDatastoreFile(vmwareLayoutFilePair[1], dcMo.getMor(), dsMo.getMor(), legacyCloudStackLayoutFilePair[1], dcMo.getMor(), true);
s_logger.info("detach disks from volume-wrapper VM " + vmdkName);
vmMo.detachAllDisks();
s_logger.info("destroy volume-wrapper VM " + vmdkName);
vmMo.destroy();
String srcFile = dsMo.getDatastorePath(vmdkName, true);
dsMo.deleteFile(srcFile, dcMo.getMor(), true);
}
// restoreVM - move the new ROOT disk into corresponding VM folder
VirtualMachineMO restoreVmMo = dcMo.findVm(volume.getVmName());
if (restoreVmMo != null) {
// VM folder name in datastore will be VM's name in vCenter.
String vmNameInVcenter = restoreVmMo.getName();
if (dsMo.folderExists(String.format("[%s]", dsMo.getName()), vmNameInVcenter)) {
VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dcMo, vmNameInVcenter, dsMo, vmdkFileBaseName);
}
}
VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(vmdkFileBaseName);
newVol.setSize(volume.getSize());
return new CopyCmdAnswer(newVol);
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
hostService.invalidateServiceContext(null);
}
String msg = "clone volume from base image failed due to " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg, e);
return new CopyCmdAnswer(e.toString());
}
}
Aggregations