use of com.vmware.vim25.VirtualSCSIController 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.VirtualSCSIController in project cloudstack by apache.
the class VirtualMachineMO method addScsiDeviceControllers.
public void addScsiDeviceControllers(DiskControllerType type) throws Exception {
VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
int busNum = 0;
while (busNum < VmwareHelper.MAX_SCSI_CONTROLLER_COUNT) {
VirtualSCSIController scsiController = getScsiController(type);
scsiController.setSharedBus(VirtualSCSISharing.NO_SHARING);
scsiController.setBusNumber(busNum);
scsiController.setKey(busNum - VmwareHelper.MAX_SCSI_CONTROLLER_COUNT);
VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
scsiControllerSpec.setDevice(scsiController);
scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
vmConfig.getDeviceChange().add(scsiControllerSpec);
busNum++;
}
if (configureVm(vmConfig)) {
s_logger.info("Successfully added SCSI controllers.");
} else {
throw new Exception("Unable to add Scsi controllers to the VM " + getName());
}
}
use of com.vmware.vim25.VirtualSCSIController in project cloudstack by apache.
the class HypervisorHostHelper method getOsDiskFromOvfConf.
public static int getOsDiskFromOvfConf(VirtualMachineConfigSpec config, String deviceLocation) {
List<VirtualDeviceConfigSpec> deviceConfigList = config.getDeviceChange();
int controllerKey = 0;
int deviceSeqNumber = 0;
int controllerNumber = 0;
int deviceNodeNumber = 0;
int controllerCount = 0;
String[] virtualNodeInfo = deviceLocation.split(":");
if (deviceLocation.startsWith("scsi")) {
// get substring excluding prefix scsi
controllerNumber = Integer.parseInt(virtualNodeInfo[0].substring(4));
deviceNodeNumber = Integer.parseInt(virtualNodeInfo[1]);
for (VirtualDeviceConfigSpec deviceConfig : deviceConfigList) {
VirtualDevice device = deviceConfig.getDevice();
if (device instanceof VirtualSCSIController) {
if (controllerNumber == controllerCount) {
// ((VirtualSCSIController)device).getBusNumber()) {
controllerKey = device.getKey();
break;
}
controllerCount++;
}
}
} else {
// get substring excluding prefix ide
controllerNumber = Integer.parseInt(virtualNodeInfo[0].substring(3));
deviceNodeNumber = Integer.parseInt(virtualNodeInfo[1]);
controllerCount = 0;
for (VirtualDeviceConfigSpec deviceConfig : deviceConfigList) {
VirtualDevice device = deviceConfig.getDevice();
if (device instanceof VirtualIDEController) {
if (controllerNumber == controllerCount) {
// ((VirtualIDEController)device).getBusNumber()) {
// Only 2 IDE controllers supported and they will have bus numbers 0 and 1
controllerKey = device.getKey();
break;
}
controllerCount++;
}
}
}
// Get devices on this controller at specific device node.
for (VirtualDeviceConfigSpec deviceConfig : deviceConfigList) {
VirtualDevice device = deviceConfig.getDevice();
if (device instanceof VirtualDisk) {
if (controllerKey == device.getControllerKey() && deviceNodeNumber == device.getUnitNumber()) {
break;
}
deviceSeqNumber++;
}
}
return deviceSeqNumber;
}
use of com.vmware.vim25.VirtualSCSIController in project photon-model by vmware.
the class InstanceClient method cloneOvfBasedTemplate.
private ManagedObjectReference cloneOvfBasedTemplate(ManagedObjectReference vmTempl, ManagedObjectReference datastore, ManagedObjectReference folder, ManagedObjectReference resourcePool, List<VirtualMachineDefinedProfileSpec> pbmSpec) throws Exception {
String vmName = this.ctx.child.name;
Map<String, Object> props = this.get.entityProps(vmTempl, VimPath.vm_summary_config_numCpu, VimPath.vm_summary_config_memorySizeMB, VimPath.vm_snapshot, VimPath.vm_config_hardware_device, VimPath.vm_config_vAppConfig_property);
VirtualMachineSnapshotInfo snapshot = (VirtualMachineSnapshotInfo) props.get(VimPath.vm_snapshot);
ArrayOfVirtualDevice devices = (ArrayOfVirtualDevice) props.get(VimPath.vm_config_hardware_device);
VirtualDisk vd = devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualDisk).map(d -> (VirtualDisk) d).findFirst().orElse(null);
VirtualSCSIController scsiController = getFirstScsiController(devices);
Integer[] scsiUnit = findFreeScsiUnit(scsiController, devices.getVirtualDevice());
VirtualMachineRelocateDiskMoveOptions diskMoveOption = computeDiskMoveType();
boolean customizeImageDisk = false;
List<VirtualDeviceConfigSpec> newDisks = new ArrayList<>();
VirtualMachineRelocateSpecDiskLocator bootDiskLocator = null;
List<VirtualDisk> vDisks = null;
if (this.bootDisk != null) {
if (vd == null) {
String datastoreName = this.get.entityProp(datastore, VimPath.ds_summary_name);
String path = makePathToVmdkFile("ephemeral_disk", vmName);
String diskName = String.format(VM_PATH_FORMAT, datastoreName, path);
VirtualDeviceConfigSpec hdd = createHdd(scsiController.getKey(), scsiUnit[0], this.bootDisk, diskName, datastore, pbmSpec);
newDisks.add(hdd);
} else {
// strategy
if (this.imageDisks != null && !this.imageDisks.isEmpty()) {
vDisks = devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualDisk).map(d -> (VirtualDisk) d).filter(d -> {
DiskStateExpanded ds = findMatchingImageDiskState(d, this.imageDisks);
return toKb(ds.capacityMBytes) > d.getCapacityInKB() || ds.customProperties != null;
}).collect(Collectors.toList());
if (vDisks.size() > 0) {
diskMoveOption = VirtualMachineRelocateDiskMoveOptions.MOVE_ALL_DISK_BACKINGS_AND_DISALLOW_SHARING;
logger.warn("Changing clone strategy to MOVE_ALL_DISK_BACKINGS_AND_DISALLOW_SHARING, as there is disk resize requested");
customizeImageDisk = true;
bootDiskLocator = setProvisioningType(vDisks.get(0), datastore, pbmSpec);
}
}
}
}
VirtualCdrom vcd = devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualCdrom).map(d -> (VirtualCdrom) d).findFirst().orElse(null);
// add a cdrom so that ovf transport works
if (vcd == null) {
VirtualDevice ideController = getFirstIdeController(devices);
int ideUnit = findFreeUnit(ideController, devices.getVirtualDevice());
VirtualDeviceConfigSpec cdrom = createCdrom(ideController, ideUnit);
newDisks.add(cdrom);
} else {
VirtualDeviceConfigSpec cdrom = reconfigureCdrom(vcd);
newDisks.add(cdrom);
}
VirtualMachineConfigSpec spec = new VirtualMachineConfigSpec();
// even though this is a clone, hw config from the compute resource
// is takes precedence
spec.setNumCPUs((int) this.ctx.child.description.cpuCount);
spec.setMemoryMB(toMemoryMb(this.ctx.child.description.totalMemoryBytes));
String gt = CustomProperties.of(this.ctx.child).getString(CustomProperties.GUEST_ID, null);
if (gt != null) {
spec.setGuestId(gt);
}
// set ovf environment
ArrayOfVAppPropertyInfo infos = (ArrayOfVAppPropertyInfo) props.get(// this.get.entityProp(vmTempl,
VimPath.vm_config_vAppConfig_property);
// VimPath.vm_config_vAppConfig_property);
populateVAppProperties(spec, infos);
populateCloudConfig(spec, infos);
recordTimestamp(spec.getExtraConfig());
// set the maximum snapshot limit if specified
final String snapshotLimit = CustomProperties.of(this.ctx.child).getString(CustomProperties.SNAPSHOT_MAXIMUM_LIMIT);
recordSnapshotLimit(spec.getExtraConfig(), snapshotLimit);
// add disks one at a time
for (VirtualDeviceConfigSpec newDisk : newDisks) {
spec.getDeviceChange().add(newDisk);
}
// configure network
VirtualPCIController pci = getFirstPciController(devices);
for (NetworkInterfaceStateWithDetails nicWithDetails : this.ctx.nics) {
VirtualDevice nic = createNic(nicWithDetails, pci.getControllerKey());
addDeviceToVm(spec, nic);
}
// remove any networks from the template
devices.getVirtualDevice().stream().filter(d -> VirtualEthernetCard.class.isAssignableFrom(d.getClass())).forEach(d -> addRemoveDeviceFromVm(spec, d));
VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();
if (pbmSpec != null) {
pbmSpec.stream().forEach(sp -> {
relocSpec.getProfile().add(sp);
});
}
relocSpec.setDatastore(datastore);
relocSpec.setFolder(folder);
relocSpec.setPool(resourcePool);
relocSpec.setDiskMoveType(diskMoveOption.value());
VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
cloneSpec.setLocation(relocSpec);
cloneSpec.setPowerOn(false);
cloneSpec.setTemplate(false);
if (snapshot != null) {
cloneSpec.setSnapshot(snapshot.getCurrentSnapshot());
}
cloneSpec.setConfig(spec);
if (bootDiskLocator != null) {
cloneSpec.getLocation().getDisk().add(bootDiskLocator);
}
ManagedObjectReference cloneTask = getVimPort().cloneVMTask(vmTempl, folder, vmName, cloneSpec);
TaskInfo info = waitTaskEnd(cloneTask);
if (info.getState() == TaskInfoState.ERROR) {
return VimUtils.rethrow(info.getError());
}
ManagedObjectReference vmMoref = (ManagedObjectReference) info.getResult();
// Apply boot disk customization if any, if done through full clone.
if (customizeImageDisk) {
ArrayOfVirtualDevice virtualDevices = this.get.entityProp(vmMoref, VimPath.vm_config_hardware_device);
reconfigureBootDisk(vmMoref, getCustomizationConfigSpecs(virtualDevices, this.imageDisks));
}
return vmMoref;
}
use of com.vmware.vim25.VirtualSCSIController in project photon-model by vmware.
the class InstanceClient method attachDisks.
/**
* Creates disks and attaches them to the vm created by {@link #createInstance()}. The given
* diskStates are enriched with data from vSphere and can be patched back to xenon.
*/
public void attachDisks(List<DiskStateExpanded> diskStates, boolean isImageDisks) throws Exception {
if (this.vm == null) {
throw new IllegalStateException("Cannot attach diskStates if VM is not created");
}
EnumSet<DiskType> notSupportedTypes = EnumSet.of(DiskType.SSD, DiskType.NETWORK);
List<DiskStateExpanded> unsupportedDisks = diskStates.stream().filter(d -> notSupportedTypes.contains(d.type)).collect(Collectors.toList());
if (!unsupportedDisks.isEmpty()) {
throw new IllegalStateException("Some diskStates cannot be created: " + unsupportedDisks.stream().map(d -> d.documentSelfLink).collect(Collectors.toList()));
}
// the path to folder holding all vm files
String dir = this.get.entityProp(this.vm, VimPath.vm_config_files_vmPathName);
dir = Paths.get(dir).getParent().toString();
ArrayOfVirtualDevice devices = this.get.entityProp(this.vm, VimPath.vm_config_hardware_device);
VirtualSCSIController scsiController = getFirstScsiController(devices);
// Get available free unit numbers for the given scsi controller.
Integer[] scsiUnits = findFreeScsiUnit(scsiController, devices.getVirtualDevice());
VirtualDevice ideController = getFirstIdeController(devices);
int ideUnit = findFreeUnit(ideController, devices.getVirtualDevice());
VirtualDevice sioController = getFirstSioController(devices);
int sioUnit = findFreeUnit(sioController, devices.getVirtualDevice());
List<VirtualDeviceConfigSpec> newDisks = new ArrayList<>();
boolean cdromAdded = false;
List<DiskStateExpanded> disksToBeCustomized = null;
int scsiUnitIndex = 0;
for (DiskStateExpanded ds : diskStates) {
String diskPath = VimUtils.uriToDatastorePath(ds.sourceImageReference);
if (ds.type == DiskType.HDD) {
// Find if there is a storage policy defined for this disk
List<VirtualMachineDefinedProfileSpec> pbmSpec = getPbmProfileSpec(ds);
VirtualDeviceConfigSpec hdd;
if (diskPath != null) {
// create full clone of given disk
hdd = createFullCloneAndAttach(diskPath, ds, dir, scsiController, scsiUnits[scsiUnitIndex], pbmSpec);
newDisks.add(hdd);
// When it is through clone, customize after the clone is complete.
if (disksToBeCustomized == null) {
disksToBeCustomized = new ArrayList<>(diskStates.size());
}
if (isImageDisks) {
disksToBeCustomized.add(ds);
}
} else {
String dsDirForDisk = getDatastorePathForDisk(ds, dir);
String diskName = makePathToVmdkFile(ds.name, dsDirForDisk);
hdd = createHdd(scsiController.getKey(), scsiUnits[scsiUnitIndex], ds, diskName, getDataStoreForDisk(ds, pbmSpec), pbmSpec);
newDisks.add(hdd);
}
scsiUnitIndex++;
}
if (ds.type == DiskType.CDROM) {
VirtualDeviceConfigSpec cdrom = createCdrom(ideController, ideUnit);
fillInControllerUnitNumber(ds, ideUnit);
ideUnit = nextUnitNumber(ideUnit);
if (diskPath != null) {
// mount iso image
insertCdrom((VirtualCdrom) cdrom.getDevice(), diskPath);
}
newDisks.add(cdrom);
cdromAdded = true;
}
if (ds.type == DiskType.FLOPPY) {
VirtualDeviceConfigSpec floppy = createFloppy(sioController, sioUnit);
fillInControllerUnitNumber(ds, sioUnit);
sioUnit = nextUnitNumber(sioUnit);
if (diskPath != null) {
// mount iso image
insertFloppy((VirtualFloppy) floppy.getDevice(), diskPath);
}
newDisks.add(floppy);
}
// mark disk as attached
ds.status = DiskStatus.ATTACHED;
}
// add a cdrom so that ovf transport works
if (!cdromAdded && isImageDisks) {
VirtualDeviceConfigSpec cdrom = createCdrom(ideController, ideUnit);
newDisks.add(cdrom);
}
// add disks one at a time
for (VirtualDeviceConfigSpec newDisk : newDisks) {
VirtualMachineConfigSpec spec = new VirtualMachineConfigSpec();
spec.getDeviceChange().add(newDisk);
ManagedObjectReference reconfigureTask = getVimPort().reconfigVMTask(this.vm, spec);
TaskInfo info = waitTaskEnd(reconfigureTask);
if (info.getState() == TaskInfoState.ERROR) {
VimUtils.rethrow(info.getError());
}
}
// If disks are created through full clone, then reconfigure
if (disksToBeCustomized != null && !disksToBeCustomized.isEmpty()) {
// Get the hardware devices once again as they are reconfigured
devices = this.get.entityProp(this.vm, VimPath.vm_config_hardware_device);
reconfigureBootDisk(this.vm, getCustomizationConfigSpecs(devices, disksToBeCustomized));
}
}
Aggregations