use of com.vmware.vim25.mo.Datastore 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();
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(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 = null;
String diskUuid = volumeTO.getUuid().replace("-", "");
if (isAttach && isManaged) {
Map<String, String> details = disk.getDetails();
morDs = prepareManagedStorage(context, hyperHost, diskUuid, iScsiName, storageHost, storagePort, null, 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;
if (isAttach) {
if (isManaged) {
datastoreVolumePath = dsMo.getDatastorePath(dsMo.getName() + ".vmdk");
} else {
datastoreVolumePath = VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dsMo.getOwnerDatacenter().first(), vmName, dsMo, volumeTO.getPath());
}
} else {
if (isManaged) {
datastoreVolumePath = dsMo.getDatastorePath(dsMo.getName() + ".vmdk");
} else {
datastoreVolumePath = VmwareStorageLayoutHelper.getLegacyDatastorePathFromVmdkFileName(dsMo, volumeTO.getPath() + ".vmdk");
if (!dsMo.fileExists(datastoreVolumePath)) {
datastoreVolumePath = VmwareStorageLayoutHelper.getVmwareDatastorePathFromVmdkFileName(dsMo, vmName, volumeTO.getPath() + ".vmdk");
}
}
}
disk.setPath(datastoreVolumePath);
AttachAnswer answer = new AttachAnswer(disk);
if (isAttach) {
String diskController = getLegacyVmDataDiskController();
if (controllerInfo != null && !Strings.isNullOrEmpty(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);
} else {
vmMo.removeAllSnapshots();
vmMo.detachDisk(datastoreVolumePath, false);
if (isManaged) {
handleDatastoreAndVmdkDetachManaged(diskUuid, iScsiName, storageHost, storagePort);
} else {
VmwareStorageLayoutHelper.syncVolumeToRootFolder(dsMo.getOwnerDatacenter().first(), dsMo, volumeTO.getPath(), vmName);
}
}
return answer;
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
hostService.invalidateServiceContext(null);
}
String msg = "";
if (isAttach)
msg += "Failed to attach volume: " + e.getMessage();
else
msg += "Failed to detach volume: " + e.getMessage();
s_logger.error(msg, e);
return new AttachAnswer(msg);
}
}
use of com.vmware.vim25.mo.Datastore in project cloudstack by apache.
the class VmwareStorageProcessor method deleteVolume.
@Override
public Answer deleteVolume(DeleteCommand cmd) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Executing resource DeleteCommand: " + _gson.toJson(cmd));
}
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
VolumeObjectTO vol = (VolumeObjectTO) cmd.getData();
DataStoreTO store = vol.getDataStore();
PrimaryDataStoreTO primaryDataStoreTO = (PrimaryDataStoreTO) store;
Map<String, String> details = primaryDataStoreTO.getDetails();
boolean isManaged = false;
String managedDatastoreName = null;
if (details != null) {
isManaged = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));
if (isManaged) {
managedDatastoreName = getManagedDatastoreNameFromPath(vol.getPath());
}
}
ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, isManaged ? managedDatastoreName : store.getUuid());
if (morDs == null) {
String msg = "Unable to find datastore based on volume mount point " + store.getUuid();
s_logger.error(msg);
throw new Exception(msg);
}
DatastoreMO dsMo = new DatastoreMO(context, morDs);
ManagedObjectReference morDc = hyperHost.getHyperHostDatacenter();
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
ClusterMO clusterMo = new ClusterMO(context, morCluster);
if (vol.getVolumeType() == Volume.Type.ROOT) {
String vmName = vol.getVmName();
if (vmName != null) {
VirtualMachineMO vmMo = clusterMo.findVmOnHyperHost(vmName);
if (vmMo == null) {
// Volume might be on a zone-wide storage pool, look for VM in datacenter
DatacenterMO dcMo = new DatacenterMO(context, morDc);
vmMo = dcMo.findVm(vmName);
}
if (vmMo != null) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Destroy root volume and VM itself. vmName " + vmName);
}
VirtualMachineDiskInfo diskInfo = null;
if (vol.getChainInfo() != null)
diskInfo = _gson.fromJson(vol.getChainInfo(), VirtualMachineDiskInfo.class);
HostMO hostMo = vmMo.getRunningHost();
List<NetworkDetails> networks = vmMo.getNetworksWithDetails();
// tear down all devices first before we destroy the VM to avoid accidently delete disk backing files
if (VmwareResource.getVmState(vmMo) != PowerState.PowerOff) {
vmMo.safePowerOff(_shutdownWaitMs);
}
// call this before calling detachAllDisksExcept
// when expunging a VM, we need to see if any of its disks are serviced by managed storage
// if there is one or more disk serviced by managed storage, remove the iSCSI connection(s)
// don't remove the iSCSI connection(s) until the supported disk(s) is/are removed from the VM
// (removeManagedTargetsFromCluster should be called after detachAllDisksExcept and vm.destroy)
List<VirtualDisk> virtualDisks = vmMo.getVirtualDisks();
List<String> managedIqns = getManagedIqnsFromVirtualDisks(virtualDisks);
List<String> detachedDisks = vmMo.detachAllDisksExcept(vol.getPath(), diskInfo != null ? diskInfo.getDiskDeviceBusName() : null);
VmwareStorageLayoutHelper.moveVolumeToRootFolder(new DatacenterMO(context, morDc), detachedDisks);
if (isManaged) {
vmMo.unregisterVm();
} else {
vmMo.destroy();
}
// this.hostService.handleDatastoreAndVmdkDetach(iScsiName, storageHost, storagePort);
if (managedIqns != null && !managedIqns.isEmpty()) {
removeManagedTargetsFromCluster(managedIqns);
}
for (NetworkDetails netDetails : networks) {
if (netDetails.getGCTag() != null && netDetails.getGCTag().equalsIgnoreCase("true")) {
if (netDetails.getVMMorsOnNetwork() == null || netDetails.getVMMorsOnNetwork().length == 1) {
resource.cleanupNetwork(hostMo, netDetails);
}
}
}
}
/*
if (s_logger.isInfoEnabled()) {
s_logger.info("Destroy volume by original name: " + vol.getPath() + ".vmdk");
}
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vol.getPath(), new DatacenterMO(context, morDc));
*/
return new Answer(cmd, true, "Success");
}
if (s_logger.isInfoEnabled()) {
s_logger.info("Destroy root volume directly from datastore");
}
}
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vol.getPath(), new DatacenterMO(context, morDc));
return new Answer(cmd, true, "Success");
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
hostService.invalidateServiceContext(null);
}
String msg = "delete volume failed due to " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg, e);
return new Answer(cmd, false, msg);
}
}
use of com.vmware.vim25.mo.Datastore in project cloudstack by apache.
the class VmwareStorageProcessor method createVolume.
@Override
public Answer createVolume(CreateObjectCommand cmd) {
VolumeObjectTO volume = (VolumeObjectTO) cmd.getData();
DataStoreTO primaryStore = volume.getDataStore();
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
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);
// create data volume
VirtualMachineMO vmMo = null;
String volumeUuid = UUID.randomUUID().toString().replace("-", "");
String volumeDatastorePath = dsMo.getDatastorePath(volumeUuid + ".vmdk");
String dummyVmName = hostService.getWorkerName(context, cmd, 0);
try {
s_logger.info("Create worker VM " + dummyVmName);
vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, dummyVmName);
if (vmMo == null) {
throw new Exception("Unable to create a dummy VM for volume creation");
}
synchronized (this) {
try {
vmMo.createDisk(volumeDatastorePath, (int) (volume.getSize() / (1024L * 1024L)), morDatastore, vmMo.getScsiDeviceControllerKey());
vmMo.detachDisk(volumeDatastorePath, false);
} catch (Exception e) {
s_logger.error("Deleting file " + volumeDatastorePath + " due to error: " + e.getMessage());
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, volumeUuid.toString(), dcMo);
throw new CloudRuntimeException("Unable to create volume due to: " + e.getMessage());
}
}
VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(volumeUuid);
newVol.setSize(volume.getSize());
return new CreateObjectAnswer(newVol);
} finally {
s_logger.info("Destroy dummy VM after volume creation");
if (vmMo != null) {
vmMo.detachAllDisks();
vmMo.destroy();
}
}
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
hostService.invalidateServiceContext(null);
}
String msg = "create volume failed due to " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg, e);
return new CreateObjectAnswer(e.toString());
}
}
use of com.vmware.vim25.mo.Datastore in project cloudstack by apache.
the class HostMO method mountDatastore.
@Override
public ManagedObjectReference mountDatastore(boolean vmfsDatastore, String poolHostAddress, int poolHostPort, String poolPath, String poolUuid) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - mountDatastore(). target MOR: " + _mor.getValue() + ", vmfs: " + vmfsDatastore + ", poolHost: " + poolHostAddress + ", poolHostPort: " + poolHostPort + ", poolPath: " + poolPath + ", poolUuid: " + poolUuid);
HostDatastoreSystemMO hostDatastoreSystemMo = getHostDatastoreSystemMO();
ManagedObjectReference morDatastore = hostDatastoreSystemMo.findDatastore(poolUuid);
if (morDatastore == null) {
if (!vmfsDatastore) {
try {
morDatastore = hostDatastoreSystemMo.createNfsDatastore(poolHostAddress, poolHostPort, poolPath, poolUuid);
} catch (AlreadyExistsFaultMsg e) {
s_logger.info("Creation of NFS datastore on vCenter failed since datastore already exists." + " Details: vCenter API trace - mountDatastore(). target MOR: " + _mor.getValue() + ", vmfs: " + vmfsDatastore + ", poolHost: " + poolHostAddress + ", poolHostPort: " + poolHostPort + ", poolPath: " + poolPath + ", poolUuid: " + poolUuid);
// Retrieve the morDatastore and return it.
return (getExistingDataStoreOnHost(vmfsDatastore, poolHostAddress, poolHostPort, poolPath, poolUuid, hostDatastoreSystemMo));
} catch (Exception e) {
s_logger.info("Creation of NFS datastore on vCenter failed. " + " Details: vCenter API trace - mountDatastore(). target MOR: " + _mor.getValue() + ", vmfs: " + vmfsDatastore + ", poolHost: " + poolHostAddress + ", poolHostPort: " + poolHostPort + ", poolPath: " + poolPath + ", poolUuid: " + poolUuid + ". Exception mesg: " + e.getMessage());
throw new Exception("Creation of NFS datastore on vCenter failed.");
}
if (morDatastore == null) {
String msg = "Unable to create NFS datastore. host: " + poolHostAddress + ", port: " + poolHostPort + ", path: " + poolPath + ", uuid: " + poolUuid;
s_logger.error(msg);
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - mountDatastore() done(failed)");
throw new Exception(msg);
}
} else {
morDatastore = _context.getDatastoreMorByPath(poolPath);
if (morDatastore == null) {
String msg = "Unable to create VMFS datastore. host: " + poolHostAddress + ", port: " + poolHostPort + ", path: " + poolPath + ", uuid: " + poolUuid;
s_logger.error(msg);
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - mountDatastore() done(failed)");
throw new Exception(msg);
}
DatastoreMO dsMo = new DatastoreMO(_context, morDatastore);
dsMo.setCustomFieldValue(CustomFieldConstants.CLOUD_UUID, poolUuid);
}
}
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - mountDatastore() done(successfully)");
return morDatastore;
}
use of com.vmware.vim25.mo.Datastore in project cloudstack by apache.
the class DatastoreMO method folderExists.
public boolean folderExists(String folderParentDatastorePath, String folderName) throws Exception {
HostDatastoreBrowserMO browserMo = getHostDatastoreBrowserMO();
HostDatastoreBrowserSearchResults results = browserMo.searchDatastore(folderParentDatastorePath, folderName, true);
if (results != null) {
List<FileInfo> info = results.getFile();
if (info != null && info.size() > 0) {
s_logger.info("Folder " + folderName + " exists on datastore");
return true;
}
}
s_logger.info("Folder " + folderName + " does not exist on datastore");
return false;
}
Aggregations