use of com.vmware.vim25.VirtualMachineDefinedProfileSpec in project photon-model by vmware.
the class ClientUtils method attachDiskToVM.
public static String attachDiskToVM(ArrayOfVirtualDevice devices, ManagedObjectReference vm, DiskService.DiskStateExpanded diskState, ManagedObjectReference diskDatastore, Connection connection, VimPortType vimPort) throws Exception {
String diskPath = VimUtils.uriToDatastorePath(diskState.sourceImageReference);
String diskFullPath = CustomProperties.of(diskState).getString(DISK_FULL_PATH, null);
Boolean insertCdRom = CustomProperties.of(diskState).getBoolean(INSERT_CDROM, false);
VirtualDeviceConfigSpec deviceConfigSpec = null;
if (diskState.type == DiskService.DiskType.HDD) {
VirtualSCSIController scsiController = getFirstScsiController(devices);
// Get available free unit numbers for the given scsi controller.
Integer[] scsiUnits = findFreeScsiUnit(scsiController, devices.getVirtualDevice());
List<VirtualMachineDefinedProfileSpec> pbmSpec = getPbmProfileSpec(diskState);
deviceConfigSpec = createHdd(scsiController.getKey(), scsiUnits[0], diskState, diskFullPath, diskDatastore, pbmSpec, false);
} else if (diskState.type == DiskService.DiskType.CDROM) {
if (insertCdRom) {
if (diskPath == null) {
throw new IllegalStateException(String.format("Cannot insert empty iso file into CD-ROM"));
}
// Find first available CD ROM to insert the iso file
VirtualCdrom cdrom = devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualCdrom).map(d -> (VirtualCdrom) d).findFirst().orElse(null);
if (cdrom == null) {
throw new IllegalStateException(String.format("Could not find Virtual CD ROM to insert %s.", diskPath));
}
insertCdrom(cdrom, diskPath);
deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(cdrom);
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
} else {
VirtualDevice ideController = getFirstIdeController(devices);
int ideUnit = findFreeUnit(ideController, devices.getVirtualDevice());
int availableUnitNumber = nextUnitNumber(ideUnit);
deviceConfigSpec = createCdrom(ideController, availableUnitNumber);
fillInControllerUnitNumber(diskState, availableUnitNumber);
if (diskPath != null) {
// mount iso image
insertCdrom((VirtualCdrom) deviceConfigSpec.getDevice(), diskPath);
}
// Live add of cd-rom is not possible. Hence it needs to be powered off
// Power off is needed to ADD cd-rom
powerOffVm(connection, vimPort, vm);
}
} else if (diskState.type == DiskService.DiskType.FLOPPY) {
VirtualDevice sioController = getFirstSioController(devices);
int sioUnit = findFreeUnit(sioController, devices.getVirtualDevice());
int availableUnitNumber = nextUnitNumber(sioUnit);
deviceConfigSpec = createFloppy(sioController, availableUnitNumber);
fillInControllerUnitNumber(diskState, availableUnitNumber);
if (diskPath != null) {
insertFloppy((VirtualFloppy) deviceConfigSpec.getDevice(), diskPath);
}
// Power off is needed to ADD floppy
powerOffVm(connection, vimPort, vm);
}
VirtualMachineConfigSpec spec = new VirtualMachineConfigSpec();
spec.getDeviceChange().add(deviceConfigSpec);
ManagedObjectReference reconfigureTask = vimPort.reconfigVMTask(vm, spec);
TaskInfo info = VimUtils.waitTaskEnd(connection, reconfigureTask);
if (info.getState() == TaskInfoState.ERROR) {
VimUtils.rethrow(info.getError());
}
if (!insertCdRom && diskState.type != DiskService.DiskType.HDD) {
// This means it is CDROM or Floppy. Hence power on the VM as it is powered off to
// perform the operation
powerOnVM(connection, vimPort, vm);
}
return diskFullPath;
}
use of com.vmware.vim25.VirtualMachineDefinedProfileSpec in project photon-model by vmware.
the class ClientUtils method getDatastoreFromStoragePolicy.
/**
* Get one of the datastore compatible with storage policy
*/
public static ManagedObjectReference getDatastoreFromStoragePolicy(final Connection connection, List<VirtualMachineDefinedProfileSpec> pbmSpec) throws FinderException, InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
if (pbmSpec != null) {
for (VirtualMachineDefinedProfileSpec sp : pbmSpec) {
try {
PbmProfileId pbmProfileId = new PbmProfileId();
pbmProfileId.setUniqueId(sp.getProfileId());
List<String> datastoreNames = ClientUtils.getDatastores(connection, pbmProfileId);
String dsName = datastoreNames.stream().findFirst().orElse(null);
if (dsName != null) {
ManagedObjectReference dsFromSp = new ManagedObjectReference();
dsFromSp.setType(VimNames.TYPE_DATASTORE);
dsFromSp.setValue(dsName);
return dsFromSp;
}
} catch (Exception runtimeFaultFaultMsg) {
// Just ignore. No need to log, as there are alternative paths.
}
}
}
return null;
}
use of com.vmware.vim25.VirtualMachineDefinedProfileSpec in project photon-model by vmware.
the class InstanceClient method setProvisioningType.
private VirtualMachineRelocateSpecDiskLocator setProvisioningType(VirtualDisk vDisk, ManagedObjectReference datastore, List<VirtualMachineDefinedProfileSpec> pbmSpec) throws InvalidPropertyFaultMsg, FinderException, RuntimeFaultFaultMsg {
if (vDisk == null) {
return null;
}
// datastore from that.
if (datastore == null) {
ManagedObjectReference dsFromSp = getDatastoreFromStoragePolicy(this.connection, pbmSpec);
datastore = dsFromSp == null ? getDatastore() : dsFromSp;
}
VirtualDiskFlatVer2BackingInfo flatBacking = (VirtualDiskFlatVer2BackingInfo) vDisk.getBacking();
VirtualDiskType provisioningType = getDiskProvisioningType(this.bootDisk);
boolean wasThinProvision = flatBacking.isThinProvisioned();
Boolean wasEagerScrubbed = flatBacking.isEagerlyScrub() != null ? flatBacking.isEagerlyScrub() : false;
if (provisioningType != null) {
flatBacking.setThinProvisioned(provisioningType == VirtualDiskType.THIN);
flatBacking.setEagerlyScrub(provisioningType == VirtualDiskType.EAGER_ZEROED_THICK);
}
VirtualMachineRelocateSpecDiskLocator diskLocator = new VirtualMachineRelocateSpecDiskLocator();
diskLocator.setDiskId(vDisk.getKey());
diskLocator.setDiskBackingInfo(flatBacking);
diskLocator.setDatastore(datastore);
Boolean isEagerScrub = flatBacking.isEagerlyScrub() != null ? flatBacking.isEagerlyScrub() : false;
// to MOVE_ALL_DISK_BACKINGS_AND_DISALLOW_SHARING
if (wasThinProvision != flatBacking.isThinProvisioned() || !wasEagerScrubbed.equals(isEagerScrub)) {
diskLocator.setDiskMoveType(VirtualMachineRelocateDiskMoveOptions.MOVE_ALL_DISK_BACKINGS_AND_DISALLOW_SHARING.value());
}
return diskLocator;
}
use of com.vmware.vim25.VirtualMachineDefinedProfileSpec in project cloudstack by apache.
the class PbmProfileManagerMO method getProfileSpec.
public VirtualMachineDefinedProfileSpec getProfileSpec(String profileId) throws Exception {
VirtualMachineDefinedProfileSpec profileSpec = new VirtualMachineDefinedProfileSpec();
PbmProfile profile = getStorageProfile(profileId);
profileSpec.setProfileId(profile.getProfileId().getUniqueId());
return profileSpec;
}
use of com.vmware.vim25.VirtualMachineDefinedProfileSpec in project cloudstack by apache.
the class VirtualMachineMO method attachDisk.
public void attachDisk(String[] vmdkDatastorePathChain, ManagedObjectReference morDs, String diskController, String vSphereStoragePolicyId) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - attachDisk(). target MOR: " + _mor.getValue() + ", vmdkDatastorePath: " + GSON.toJson(vmdkDatastorePathChain) + ", datastore: " + morDs.getValue());
int controllerKey = 0;
int unitNumber = 0;
if (DiskControllerType.getType(diskController) == DiskControllerType.ide) {
// IDE virtual disk cannot be added if VM is running
if (getPowerState() == VirtualMachinePowerState.POWERED_ON) {
throw new Exception("Adding a virtual disk over IDE controller is not supported while VM is running in VMware hypervisor. Please re-try when VM is not running.");
}
// Get next available unit number and controller key
int ideDeviceCount = getNumberOfIDEDevices();
if (ideDeviceCount >= VmwareHelper.MAX_IDE_CONTROLLER_COUNT * VmwareHelper.MAX_ALLOWED_DEVICES_IDE_CONTROLLER) {
throw new Exception("Maximum limit of devices supported on IDE controllers [" + VmwareHelper.MAX_IDE_CONTROLLER_COUNT * VmwareHelper.MAX_ALLOWED_DEVICES_IDE_CONTROLLER + "] is reached.");
}
controllerKey = getIDEControllerKey(ideDeviceCount);
unitNumber = getFreeUnitNumberOnIDEController(controllerKey);
} else {
if (StringUtils.isNotBlank(diskController)) {
controllerKey = getScsiDiskControllerKey(diskController);
} else {
controllerKey = getScsiDeviceControllerKey();
}
unitNumber = -1;
}
synchronized (_mor.getValue().intern()) {
VirtualDevice newDisk = VmwareHelper.prepareDiskDevice(this, null, controllerKey, vmdkDatastorePathChain, morDs, unitNumber, 1);
if (StringUtils.isNotBlank(diskController)) {
String vmdkFileName = vmdkDatastorePathChain[0];
updateVmdkAdapter(vmdkFileName, diskController);
}
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(newDisk);
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
if (StringUtils.isNotEmpty(vSphereStoragePolicyId)) {
PbmProfileManagerMO profMgrMo = new PbmProfileManagerMO(getContext());
VirtualMachineDefinedProfileSpec diskProfileSpec = profMgrMo.getProfileSpec(vSphereStoragePolicyId);
deviceConfigSpec.getProfile().add(diskProfileSpec);
if (s_logger.isDebugEnabled()) {
s_logger.debug(String.format("Adding vSphere storage profile: %s to volume [%s]", vSphereStoragePolicyId, vmdkDatastorePathChain[0]));
}
}
reConfigSpec.getDeviceChange().add(deviceConfigSpec);
ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec);
boolean result = _context.getVimClient().waitForTask(morTask);
if (!result) {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - attachDisk() done(failed)");
throw new Exception("Failed to attach disk due to " + TaskMO.getTaskFailureInfo(_context, morTask));
}
_context.waitForTaskProgressDone(morTask);
}
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - attachDisk() done(successfully)");
}
Aggregations