use of com.cloud.hypervisor.vmware.mo.DatastoreMO in project cloudstack by apache.
the class VmwareResource method initializeLocalStorage.
private List<StartupStorageCommand> initializeLocalStorage() {
List<StartupStorageCommand> storageCmds = new ArrayList<StartupStorageCommand>();
VmwareContext context = getServiceContext();
try {
VmwareHypervisorHost hyperHost = getHyperHost(context);
if (hyperHost instanceof HostMO) {
HostMO hostMo = (HostMO) hyperHost;
List<Pair<ManagedObjectReference, String>> dsList = hostMo.getLocalDatastoreOnHost();
for (Pair<ManagedObjectReference, String> dsPair : dsList) {
DatastoreMO dsMo = new DatastoreMO(context, dsPair.first());
String poolUuid = dsMo.getCustomFieldValue(CustomFieldConstants.CLOUD_UUID);
if (poolUuid == null || poolUuid.isEmpty()) {
poolUuid = UUID.randomUUID().toString();
dsMo.setCustomFieldValue(CustomFieldConstants.CLOUD_UUID, poolUuid);
}
DatastoreSummary dsSummary = dsMo.getSummary();
String address = hostMo.getHostName();
StoragePoolInfo pInfo = new StoragePoolInfo(poolUuid, address, dsMo.getMor().getValue(), "", StoragePoolType.VMFS, dsSummary.getCapacity(), dsSummary.getFreeSpace());
StartupStorageCommand cmd = new StartupStorageCommand();
cmd.setName(poolUuid);
cmd.setPoolInfo(pInfo);
// give storage host the same UUID as the local storage pool itself
cmd.setGuid(poolUuid);
cmd.setResourceType(Storage.StorageResourceType.STORAGE_POOL);
cmd.setDataCenter(_dcId);
cmd.setPod(_pod);
cmd.setCluster(_cluster);
s_logger.info("Add local storage startup command: " + _gson.toJson(cmd));
storageCmds.add(cmd);
}
} else {
s_logger.info("Cluster host does not support local storage, skip it");
}
} catch (Exception e) {
String msg = "initializing local storage failed due to : " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg);
invalidateServiceContext();
throw new CloudRuntimeException(msg);
}
return storageCmds;
}
use of com.cloud.hypervisor.vmware.mo.DatastoreMO in project cloudstack by apache.
the class VmwareResource method inferDatastoreDetailsFromDiskInfo.
private HashMap<String, Pair<ManagedObjectReference, DatastoreMO>> inferDatastoreDetailsFromDiskInfo(VmwareHypervisorHost hyperHost, VmwareContext context, DiskTO[] disks, Command cmd) throws Exception {
HashMap<String, Pair<ManagedObjectReference, DatastoreMO>> mapIdToMors = new HashMap<String, Pair<ManagedObjectReference, DatastoreMO>>();
assert (hyperHost != null) && (context != null);
for (DiskTO vol : disks) {
if (vol.getType() != Volume.Type.ISO) {
VolumeObjectTO volumeTO = (VolumeObjectTO) vol.getData();
DataStoreTO primaryStore = volumeTO.getDataStore();
String poolUuid = primaryStore.getUuid();
if (mapIdToMors.get(poolUuid) == null) {
boolean isManaged = false;
Map<String, String> details = vol.getDetails();
if (details != null) {
isManaged = Boolean.parseBoolean(details.get(DiskTO.MANAGED));
}
if (isManaged) {
// details should not be null for managed storage (it may or may not be null for non-managed storage)
String iScsiName = details.get(DiskTO.IQN);
String datastoreName = VmwareResource.getDatastoreName(iScsiName);
ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, datastoreName);
// create the datastore, and create a VMDK file in the datastore
if (morDatastore == null) {
morDatastore = _storageProcessor.prepareManagedStorage(context, hyperHost, null, iScsiName, details.get(DiskTO.STORAGE_HOST), Integer.parseInt(details.get(DiskTO.STORAGE_PORT)), volumeTO.getVolumeType() == Volume.Type.ROOT ? volumeTO.getName() : 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), Long.parseLong(details.get(DiskTO.VOLUME_SIZE)), cmd);
DatastoreMO dsMo = new DatastoreMO(getServiceContext(), morDatastore);
String datastoreVolumePath = dsMo.getDatastorePath((volumeTO.getVolumeType() == Volume.Type.ROOT ? volumeTO.getName() : dsMo.getName()) + ".vmdk");
volumeTO.setPath(datastoreVolumePath);
vol.setPath(datastoreVolumePath);
}
mapIdToMors.put(datastoreName, new Pair<ManagedObjectReference, DatastoreMO>(morDatastore, new DatastoreMO(context, morDatastore)));
} else {
ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, poolUuid);
if (morDatastore == null) {
String msg = "Failed to get the mounted datastore for the volume's pool " + poolUuid;
s_logger.error(msg);
throw new Exception(msg);
}
mapIdToMors.put(poolUuid, new Pair<ManagedObjectReference, DatastoreMO>(morDatastore, new DatastoreMO(context, morDatastore)));
}
}
}
}
return mapIdToMors;
}
use of com.cloud.hypervisor.vmware.mo.DatastoreMO in project cloudstack by apache.
the class VmwareResource method getDatastoreThatRootDiskIsOn.
private DatastoreMO getDatastoreThatRootDiskIsOn(HashMap<String, Pair<ManagedObjectReference, DatastoreMO>> dataStoresDetails, DiskTO[] disks) {
Pair<ManagedObjectReference, DatastoreMO> rootDiskDataStoreDetails = null;
for (DiskTO vol : disks) {
if (vol.getType() == Volume.Type.ROOT) {
Map<String, String> details = vol.getDetails();
boolean managed = false;
if (details != null) {
managed = Boolean.parseBoolean(details.get(DiskTO.MANAGED));
}
if (managed) {
String datastoreName = VmwareResource.getDatastoreName(details.get(DiskTO.IQN));
rootDiskDataStoreDetails = dataStoresDetails.get(datastoreName);
break;
} else {
DataStoreTO primaryStore = vol.getData().getDataStore();
rootDiskDataStoreDetails = dataStoresDetails.get(primaryStore.getUuid());
break;
}
}
}
if (rootDiskDataStoreDetails != null) {
return rootDiskDataStoreDetails.second();
}
return null;
}
use of com.cloud.hypervisor.vmware.mo.DatastoreMO 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() / 1024;
long oldSize = cmd.getCurrentSize() / 1024;
boolean useWorkerVm = false;
VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
String poolId = cmd.getPoolUuid();
VirtualMachineMO vmMo = null;
DatastoreMO dsMo = null;
ManagedObjectReference morDS = null;
String vmdkDataStorePath = null;
try {
if (newSize < oldSize) {
throw new Exception("VMware doesn't support shrinking volume from larger size: " + oldSize / (1024 * 1024) + " GB to a smaller size: " + newSize / (1024 * 1024) + " GB");
} else if (newSize == oldSize) {
return new ResizeVolumeAnswer(cmd, true, "success", newSize * 1024);
}
if (vmName.equalsIgnoreCase("none")) {
// we need to spawn a worker VM to attach the volume to and
// resize the volume.
useWorkerVm = true;
vmName = getWorkerName(getServiceContext(), cmd, 0);
morDS = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, poolId);
dsMo = new DatastoreMO(hyperHost.getContext(), morDS);
s_logger.info("Create worker VM " + vmName);
vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, vmName);
if (vmMo == null) {
throw new Exception("Unable to create a worker VM for volume resize");
}
synchronized (this) {
vmdkDataStorePath = VmwareStorageLayoutHelper.getLegacyDatastorePathFromVmdkFileName(dsMo, path + ".vmdk");
vmMo.attachDisk(new String[] { vmdkDataStorePath }, morDS);
}
}
// find VM through datacenter (VM is not at the target host yet)
vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
if (vmMo == null) {
String msg = "VM " + vmName + " does not exist in VMware datacenter";
s_logger.error(msg);
throw new Exception(msg);
}
Pair<VirtualDisk, String> vdisk = vmMo.getDiskDevice(path);
if (vdisk == null) {
if (s_logger.isTraceEnabled())
s_logger.trace("resize volume done (failed)");
throw new Exception("No such disk device: " + path);
}
// IDE virtual disk cannot be re-sized if VM is running
if (vdisk.second() != null && vdisk.second().contains("ide")) {
throw new Exception("Re-sizing a virtual disk over IDE controller is not supported in VMware hypervisor. " + "Please re-try when virtual disk is attached to a VM using SCSI controller.");
}
if (vdisk.second() != null && !vdisk.second().toLowerCase().startsWith("scsi")) {
s_logger.error("Unsupported disk device bus " + vdisk.second());
throw new Exception("Unsupported disk device bus " + vdisk.second());
}
VirtualDisk disk = vdisk.first();
if ((VirtualDiskFlatVer2BackingInfo) disk.getBacking() != null && ((VirtualDiskFlatVer2BackingInfo) disk.getBacking()).getParent() != null) {
s_logger.error("Resize is not supported because Disk device has Parent " + ((VirtualDiskFlatVer2BackingInfo) disk.getBacking()).getParent().getUuid());
throw new Exception("Resize is not supported because Disk device has Parent " + ((VirtualDiskFlatVer2BackingInfo) disk.getBacking()).getParent().getUuid());
}
String vmdkAbsFile = getAbsoluteVmdkFile(disk);
if (vmdkAbsFile != null && !vmdkAbsFile.isEmpty()) {
vmMo.updateAdapterTypeIfRequired(vmdkAbsFile);
}
disk.setCapacityInKB(newSize);
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(disk);
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
if (!vmMo.configureVm(vmConfigSpec)) {
throw new Exception("Failed to configure VM to resize disk. vmName: " + vmName);
}
return new ResizeVolumeAnswer(cmd, true, "success", newSize * 1024);
} catch (Exception e) {
s_logger.error("Unable to resize volume", e);
String error = "Failed to resize volume: " + e.getMessage();
return new ResizeVolumeAnswer(cmd, false, error);
} finally {
try {
if (useWorkerVm == true) {
s_logger.info("Destroy worker VM after volume resize");
vmMo.detachDisk(vmdkDataStorePath, false);
vmMo.destroy();
}
} catch (Throwable e) {
s_logger.info("Failed to destroy worker VM: " + vmName);
}
}
}
use of com.cloud.hypervisor.vmware.mo.DatastoreMO in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(AttachIsoCommand cmd) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Executing resource AttachIsoCommand: " + _gson.toJson(cmd));
}
try {
VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
if (vmMo == null) {
String msg = "Unable to find VM in vSphere to execute AttachIsoCommand, vmName: " + cmd.getVmName();
s_logger.error(msg);
throw new Exception(msg);
}
String storeUrl = cmd.getStoreUrl();
if (storeUrl == null) {
if (!cmd.getIsoPath().equalsIgnoreCase("vmware-tools.iso")) {
String msg = "ISO store root url is not found in AttachIsoCommand";
s_logger.error(msg);
throw new Exception(msg);
} else {
if (cmd.isAttach()) {
vmMo.mountToolsInstaller();
} else {
try {
if (!vmMo.unmountToolsInstaller()) {
return new Answer(cmd, false, "Failed to unmount vmware-tools installer ISO as the corresponding CDROM device is locked by VM. Please unmount the CDROM device inside the VM and ret-try.");
}
} catch (Throwable e) {
vmMo.detachIso(null);
}
}
return new Answer(cmd);
}
}
ManagedObjectReference morSecondaryDs = prepareSecondaryDatastoreOnHost(storeUrl);
String isoPath = cmd.getIsoPath();
if (!isoPath.startsWith(storeUrl)) {
assert (false);
String msg = "ISO path does not start with the secondary storage root";
s_logger.error(msg);
throw new Exception(msg);
}
int isoNameStartPos = isoPath.lastIndexOf('/');
String isoFileName = isoPath.substring(isoNameStartPos + 1);
String isoStorePathFromRoot = isoPath.substring(storeUrl.length(), isoNameStartPos);
// TODO, check if iso is already attached, or if there is a previous
// attachment
DatastoreMO secondaryDsMo = new DatastoreMO(getServiceContext(), morSecondaryDs);
String storeName = secondaryDsMo.getName();
String isoDatastorePath = String.format("[%s] %s%s", storeName, isoStorePathFromRoot, isoFileName);
if (cmd.isAttach()) {
vmMo.attachIso(isoDatastorePath, morSecondaryDs, true, false);
} else {
vmMo.detachIso(isoDatastorePath);
}
return new Answer(cmd);
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
invalidateServiceContext();
}
if (cmd.isAttach()) {
String msg = "AttachIsoCommand(attach) failed due to " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg, e);
return new Answer(cmd, false, msg);
} else {
String msg = "AttachIsoCommand(detach) failed due to " + VmwareHelper.getExceptionMessage(e);
s_logger.warn(msg, e);
return new Answer(cmd, false, msg);
}
}
}
Aggregations