com.vmware.vim25.VirtualDevice Java Examples

The following examples show how to use com.vmware.vim25.VirtualDevice. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VMwareUtil.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public static VirtualMachineDiskInfoBuilder getDiskInfoBuilder(List<VirtualDevice> devices) throws Exception {
    VirtualMachineDiskInfoBuilder builder = new VirtualMachineDiskInfoBuilder();

    if (devices != null) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualDisk) {
                VirtualDisk virtualDisk = (VirtualDisk)device;
                VirtualDeviceBackingInfo backingInfo = virtualDisk.getBacking();

                if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
                    VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo;

                    String deviceBusName = VMwareUtil.getDeviceBusName(devices, virtualDisk);

                    while (diskBackingInfo != null) {
                        builder.addDisk(deviceBusName, diskBackingInfo.getFileName());

                        diskBackingInfo = diskBackingInfo.getParent();
                    }
                }
            }
        }
    }

    return builder;
}
 
Example #2
Source File: VM.java    From development with Apache License 2.0 6 votes vote down vote up
private int getDataDiskKey() throws Exception {
    List<VirtualDevice> devices = configSpec.getHardware().getDevice();
    int countDisks = 0;
    int key = -1;
    for (VirtualDevice vdInfo : devices) {
        if (vdInfo instanceof VirtualDisk) {
            countDisks++;
            if (countDisks == 2) {
                key = ((VirtualDisk) vdInfo).getKey();
                break;
            }
        }
    }

    return key;
}
 
Example #3
Source File: VmwareProcessClient.java    From primecloud-controller with GNU General Public License v2.0 6 votes vote down vote up
protected VirtualSCSIController getSCSIController(VirtualMachine machine) {
    // 仮想マシンにあるSCSIコントローラのうち、BusNumberが0のものを取得する
    VirtualSCSIController scsiController = null;
    for (VirtualDevice device : machine.getConfig().getHardware().getDevice()) {
        if (device instanceof VirtualSCSIController) {
            VirtualSCSIController scsiController2 = VirtualSCSIController.class.cast(device);
            if (scsiController2.getBusNumber() == 0) {
                scsiController = scsiController2;
                break;
            }
        }
    }

    if (scsiController == null) {
        // SCSIコントローラが見つからない場合
        // TODO: SCSIコントローラを作る?
        throw new AutoException("EPROCESS-000517", 0);
    }

    return scsiController;
}
 
Example #4
Source File: VmUtils.java    From cs-actions with Apache License 2.0 6 votes vote down vote up
VirtualDeviceConfigSpec getNicSpecs(String fileName, List<VirtualDevice> virtualDevicesList,
                                    VirtualDeviceConfigSpecOperation operation, String addressType,
                                    Integer key, String parameter, VmInputs vmInputs) {
    VirtualDeviceConfigSpec nicSpecs = new VirtualDeviceConfigSpec();
    VirtualEthernetCard nic;
    if (Operation.ADD.toString().equalsIgnoreCase(parameter)) {
        nic = getEth(fileName, addressType, key);
        return getNicOpSpec(nicSpecs, operation, nic);
    } else {
        nic = findVirtualDevice(VirtualEthernetCard.class, virtualDevicesList, vmInputs);
        if (nic != null) {
            return getNicOpSpec(nicSpecs, operation, nic);
        }
    }
    throw new RuntimeException("No nic named: [" + vmInputs.getUpdateValue() + "] can be found.");
}
 
Example #5
Source File: VmwareProcessClient.java    From primecloud-controller with GNU General Public License v2.0 6 votes vote down vote up
protected VirtualDisk getVirtualDisk(VirtualMachine machine, Integer scsiId) {
    // SCSIコントローラを取得
    VirtualSCSIController scsiController = getSCSIController(machine);

    // SCSIコントローラとSCSI IDが一致するディスクを取得
    VirtualDisk disk = null;
    for (VirtualDevice device : machine.getConfig().getHardware().getDevice()) {
        if (device instanceof VirtualDisk) {
            VirtualDisk tmpDisk = VirtualDisk.class.cast(device);
            if (tmpDisk.getControllerKey() != null && tmpDisk.getControllerKey().equals(scsiController.getKey())) {
                if (tmpDisk.getUnitNumber() != null && tmpDisk.getUnitNumber().equals(scsiId)) {
                    disk = tmpDisk;
                    break;
                }
            }
        }
    }

    if (disk == null) {
        // VirtualDiskが見つからない場合
        throw new AutoException("EPROCESS-000518", scsiId);
    }

    return disk;
}
 
Example #6
Source File: VmwareCustomizeProcess.java    From primecloud-controller with GNU General Public License v2.0 6 votes vote down vote up
protected List<CustomizationAdapterMapping> createCustomizationAdapterMappings(
        VmwareProcessClient vmwareProcessClient, VirtualMachine machine) {

    List<CustomizationAdapterMapping> nicSettingMap = new ArrayList<CustomizationAdapterMapping>();

    // VirtualEthernetCardを取得
    VirtualMachineConfigInfo configInfo = machine.getConfig();
    for (VirtualDevice device : configInfo.getHardware().getDevice()) {
        if (device instanceof VirtualEthernetCard) {
            VirtualEthernetCard virtualEthernetCard = VirtualEthernetCard.class.cast(device);
            CustomizationAdapterMapping mapping = new CustomizationAdapterMapping();
            CustomizationIPSettings settings = new CustomizationIPSettings();

            // すべてのNICをDHCPにする
            CustomizationDhcpIpGenerator dhcpIp = new CustomizationDhcpIpGenerator();
            settings.setIp(dhcpIp);

            mapping.setMacAddress(virtualEthernetCard.getMacAddress());
            mapping.setAdapter(settings);
            nicSettingMap.add(mapping);
        }
    }

    return nicSettingMap;
}
 
Example #7
Source File: DiskManager.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Reconfigures VMware system disks and data disks.
 */
public void reconfigureDisks(VirtualMachineConfigSpec vmConfigSpec,
        ManagedObjectReference vmwInstance) throws Exception {

    logger.debug("");

    long systemDiskMB = (long) paramHandler.getConfigDiskSpaceMB();
    VirtualMachineConfigInfo configSpec = (VirtualMachineConfigInfo) vmw
            .getServiceUtil().getDynamicProperty(vmwInstance, "config");
    List<VirtualDevice> devices = configSpec.getHardware().getDevice();
    VirtualDisk vdSystemDisk = getVMSystemDisk(devices,
            configSpec.getName());

    configureSystemDisk(vmConfigSpec, systemDiskMB, vdSystemDisk);
    configureDataDisks(vmConfigSpec, devices, vdSystemDisk);
}
 
Example #8
Source File: VM.java    From development with Apache License 2.0 6 votes vote down vote up
private String getDiskSizeInGB(int disk) throws Exception {
    String size = "";
    List<VirtualDevice> devices = configSpec.getHardware().getDevice();
    int countDisks = 0;
    for (VirtualDevice vdInfo : devices) {
        if (vdInfo instanceof VirtualDisk) {
            countDisks++;
            if (countDisks == disk) {
                long gigabyte = ((VirtualDisk) vdInfo).getCapacityInKB()
                        / 1024 / 1024;
                size = Long.toString(gigabyte);
                break;
            }
        }
    }

    return size;
}
 
Example #9
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public void tearDownDevices(Class<?>[] deviceClasses) throws Exception {
    VirtualDevice[] devices = getMatchedDevices(deviceClasses);

    if (devices.length > 0) {
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[devices.length];

        for (int i = 0; i < devices.length; i++) {
            deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec();
            deviceConfigSpecArray[i].setDevice(devices[i]);
            deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
            vmConfigSpec.getDeviceChange().add(deviceConfigSpecArray[i]);
        }

        if (!configureVm(vmConfigSpec)) {
            throw new Exception("Failed to detach devices");
        }
    }
}
 
Example #10
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public int getScsiDiskControllerKey(String diskController) throws Exception {
    List<VirtualDevice> devices = (List<VirtualDevice>)_context.getVimClient().
            getDynamicProperty(_mor, "config.hardware.device");

    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if ((DiskControllerType.getType(diskController) == DiskControllerType.lsilogic || DiskControllerType.getType(diskController) == DiskControllerType.scsi)
                    && device instanceof VirtualLsiLogicController) {
                return ((VirtualLsiLogicController)device).getKey();
            } else if ((DiskControllerType.getType(diskController) == DiskControllerType.lsisas1068 || DiskControllerType.getType(diskController) == DiskControllerType.scsi)
                    && device instanceof VirtualLsiLogicSASController) {
                return ((VirtualLsiLogicSASController)device).getKey();
            } else if ((DiskControllerType.getType(diskController) == DiskControllerType.pvscsi || DiskControllerType.getType(diskController) == DiskControllerType.scsi)
                    && device instanceof ParaVirtualSCSIController) {
                return ((ParaVirtualSCSIController)device).getKey();
            } else if ((DiskControllerType.getType(diskController) == DiskControllerType.buslogic || DiskControllerType.getType(diskController) == DiskControllerType.scsi)
                    && device instanceof VirtualBusLogicController) {
                return ((VirtualBusLogicController)device).getKey();
            }
        }
    }

    assert (false);
    throw new IllegalStateException("Scsi disk controller of type " + diskController + " not found among configured devices.");
}
 
Example #11
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public int getScsiDiskControllerKeyNoException(String diskController) throws Exception {
    List<VirtualDevice> devices = (List<VirtualDevice>)_context.getVimClient().
            getDynamicProperty(_mor, "config.hardware.device");

    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if ((DiskControllerType.getType(diskController) == DiskControllerType.lsilogic || DiskControllerType.getType(diskController) == DiskControllerType.scsi)
                    && device instanceof VirtualLsiLogicController) {
                return ((VirtualLsiLogicController)device).getKey();
            } else if ((DiskControllerType.getType(diskController) == DiskControllerType.lsisas1068 || DiskControllerType.getType(diskController) == DiskControllerType.scsi)
                    && device instanceof VirtualLsiLogicSASController) {
                return ((VirtualLsiLogicSASController)device).getKey();
            } else if ((DiskControllerType.getType(diskController) == DiskControllerType.pvscsi || DiskControllerType.getType(diskController) == DiskControllerType.scsi)
                    && device instanceof ParaVirtualSCSIController) {
                return ((ParaVirtualSCSIController)device).getKey();
            } else if ((DiskControllerType.getType(diskController) == DiskControllerType.buslogic || DiskControllerType.getType(diskController) == DiskControllerType.scsi)
                    && device instanceof VirtualBusLogicController) {
                return ((VirtualBusLogicController)device).getKey();
            }
        }
    }
    return -1;
}
 
Example #12
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public VirtualMachineDiskInfoBuilder getDiskInfoBuilder() throws Exception {
    VirtualMachineDiskInfoBuilder builder = new VirtualMachineDiskInfoBuilder();

    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");

    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualDisk) {
                VirtualDeviceBackingInfo backingInfo = ((VirtualDisk)device).getBacking();
                if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
                    VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo;

                    while (diskBackingInfo != null) {
                        String deviceBusName = getDeviceBusName(devices, device);
                        builder.addDisk(deviceBusName, diskBackingInfo.getFileName());
                        diskBackingInfo = diskBackingInfo.getParent();
                    }
                }
            }
        }
    }

    return builder;
}
 
Example #13
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public List<Pair<Integer, ManagedObjectReference>> getAllDiskDatastores() throws Exception {
    List<Pair<Integer, ManagedObjectReference>> disks = new ArrayList<Pair<Integer, ManagedObjectReference>>();

    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualDisk) {
                VirtualDeviceBackingInfo backingInfo = ((VirtualDisk)device).getBacking();
                if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
                    VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo;
                    disks.add(new Pair<Integer, ManagedObjectReference>(new Integer(device.getKey()), diskBackingInfo.getDatastore()));
                }
            }
        }
    }

    return disks;
}
 
Example #14
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public int getFreeUnitNumberOnIDEController(int controllerKey) throws Exception {
    int freeUnitNumber = 0;
    List<VirtualDevice> devices = (List<VirtualDevice>)_context.getVimClient().
            getDynamicProperty(_mor, "config.hardware.device");

    int deviceCount = 0;
    int ideDeviceUnitNumber = -1;
    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualDisk && (controllerKey == device.getControllerKey())) {
                deviceCount++;
                ideDeviceUnitNumber = device.getUnitNumber();
            }
        }
    }
    if (deviceCount == 1) {
        if (ideDeviceUnitNumber == 0) {
            freeUnitNumber = 1;
        } // else freeUnitNumber is already initialized to 0
    } else if (deviceCount == 2) {
        throw new Exception("IDE controller with key [" + controllerKey + "] already has 2 device attached. Cannot attach more than the limit of 2.");
    }
    return freeUnitNumber;
}
 
Example #15
Source File: NetworkManager.java    From development with Apache License 2.0 6 votes vote down vote up
private static void replaceNetworkAdapter(
        VirtualMachineConfigSpec vmConfigSpec, VirtualDevice oldNIC,
        ManagedObjectReference newNetworkRef, String newNetworkName)
        throws Exception {
    logger.debug("new network: " + newNetworkName);
    VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo();
    nicBacking.setDeviceName(newNetworkName);
    nicBacking.setNetwork(newNetworkRef);
    nicBacking.setUseAutoDetect(true);
    oldNIC.setBacking(nicBacking);

    VirtualDeviceConnectInfo info = new VirtualDeviceConnectInfo();
    info.setConnected(true);
    info.setStartConnected(true);
    info.setAllowGuestControl(true);
    oldNIC.setConnectable(info);
    // oldNIC.getConnectable().setConnected(true);
    // oldNIC.getConnectable().setStartConnected(true);
    VirtualDeviceConfigSpec vmDeviceSpec = new VirtualDeviceConfigSpec();
    vmDeviceSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
    vmDeviceSpec.setDevice(oldNIC);
    vmConfigSpec.getDeviceChange().add(vmDeviceSpec);
}
 
Example #16
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
@Override public VirtualMachine importVirtualMachineFromBackup(long zoneId, long domainId, long accountId, long userId, String vmInternalName, Backup backup) throws Exception {
    DatacenterMO dcMo = getDatacenterMO(zoneId);
    VirtualMachineMO vmToImport = dcMo.findVm(vmInternalName);
    if (vmToImport == null) {
        throw new CloudRuntimeException("Error finding VM: " + vmInternalName);
    }
    VirtualMachineConfigSummary configSummary = vmToImport.getConfigSummary();
    VirtualMachineRuntimeInfo runtimeInfo = vmToImport.getRuntimeInfo();
    List<VirtualDisk> virtualDisks = vmToImport.getVirtualDisks();
    String[] vmNetworkNames = vmToImport.getNetworks();
    VirtualDevice[] nicDevices = vmToImport.getNicDevices();

    Map<VirtualDisk, VolumeVO> disksMapping = getDisksMapping(backup, virtualDisks);
    Map<String, NetworkVO> networksMapping = getNetworksMapping(vmNetworkNames, accountId, zoneId, domainId);

    long guestOsId = getImportingVMGuestOs(configSummary);
    long serviceOfferingId = getImportingVMServiceOffering(configSummary, runtimeInfo);
    long templateId = getImportingVMTemplate(virtualDisks, dcMo, vmInternalName, guestOsId, accountId, disksMapping, backup);

    VMInstanceVO vm = getVM(vmInternalName, templateId, guestOsId, serviceOfferingId, zoneId, accountId, userId, domainId);
    syncVMVolumes(vm, virtualDisks, disksMapping, vmToImport, backup);
    syncVMNics(nicDevices, dcMo, networksMapping, vm);

    return vm;
}
 
Example #17
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
private void syncVMNics(VirtualDevice[] nicDevices, DatacenterMO dcMo, Map<String, NetworkVO> networksMapping, VMInstanceVO vm) throws Exception {
    VmwareContext context = dcMo.getContext();
    List<NicVO> allNics = _nicDao.listByVmId(vm.getId());
    for (VirtualDevice nicDevice : nicDevices) {
        Pair<String, String> pair = getNicMacAddressAndNetworkName(nicDevice, context);
        String macAddress = pair.first();
        String networkName = pair.second();
        NetworkVO networkVO = networksMapping.get(networkName);
        NicVO nicVO = _nicDao.findByNetworkIdAndMacAddress(networkVO.getId(), macAddress);
        if (nicVO != null) {
            allNics.remove(nicVO);
        }
    }
    for (final NicVO unMappedNic : allNics) {
        vmManager.removeNicFromVm(vm, unMappedNic);
    }
}
 
Example #18
Source File: VirtualMachineMOTest.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
private List<VirtualDevice> getVirtualScSiDeviceList(Class<?> cls) {

        List<VirtualDevice> deviceList = new ArrayList<>();
        try {

            VirtualSCSIController scsiController = (VirtualSCSIController)cls.newInstance();
            scsiController.setSharedBus(VirtualSCSISharing.NO_SHARING);
            scsiController.setBusNumber(0);
            scsiController.setKey(1);
            deviceList.add(scsiController);
        }
        catch (Exception ex) {

        }
        return deviceList;
    }
 
Example #19
Source File: VmwareHelper.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public static VirtualDevice prepareDvNicDevice(VirtualMachineMO vmMo, ManagedObjectReference morNetwork, VirtualEthernetCardType deviceType, String dvPortGroupName,
        String dvSwitchUuid, String macAddress, int contextNumber, boolean connected, boolean connectOnStart) throws Exception {

    VirtualEthernetCard nic = createVirtualEthernetCard(deviceType);

    final VirtualEthernetCardDistributedVirtualPortBackingInfo dvPortBacking = new VirtualEthernetCardDistributedVirtualPortBackingInfo();
    final DistributedVirtualSwitchPortConnection dvPortConnection = new DistributedVirtualSwitchPortConnection();

    dvPortConnection.setSwitchUuid(dvSwitchUuid);
    dvPortConnection.setPortgroupKey(morNetwork.getValue());
    dvPortBacking.setPort(dvPortConnection);
    nic.setBacking(dvPortBacking);

    nic.setAddressType("Manual");
    nic.setConnectable(getVirtualDeviceConnectInfo(connected, connectOnStart));
    nic.setMacAddress(macAddress);
    nic.setKey(-contextNumber);
    return nic;
}
 
Example #20
Source File: VmwareHelper.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public static VirtualDevice prepareNicDevice(VirtualMachineMO vmMo, ManagedObjectReference morNetwork, VirtualEthernetCardType deviceType, String portGroupName,
        String macAddress, int contextNumber, boolean connected, boolean connectOnStart) throws Exception {

    VirtualEthernetCard nic = createVirtualEthernetCard(deviceType);

    VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo();
    nicBacking.setDeviceName(portGroupName);
    nicBacking.setNetwork(morNetwork);
    nic.setBacking(nicBacking);

    nic.setAddressType("Manual");
    nic.setConnectable(getVirtualDeviceConnectInfo(connected, connectOnStart));
    nic.setMacAddress(macAddress);
    nic.setKey(-contextNumber);
    return nic;
}
 
Example #21
Source File: VmwareHelper.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public static VirtualDevice prepareNicOpaque(VirtualMachineMO vmMo, VirtualEthernetCardType deviceType, String portGroupName,
        String macAddress, int contextNumber, boolean connected, boolean connectOnStart) throws Exception {

    assert(vmMo.getRunningHost().hasOpaqueNSXNetwork());

    VirtualEthernetCard nic = createVirtualEthernetCard(deviceType);

    VirtualEthernetCardOpaqueNetworkBackingInfo nicBacking = new VirtualEthernetCardOpaqueNetworkBackingInfo();
    nicBacking.setOpaqueNetworkId("br-int");
    nicBacking.setOpaqueNetworkType("nsx.network");

    nic.setBacking(nicBacking);

    nic.setAddressType("Manual");
    nic.setConnectable(getVirtualDeviceConnectInfo(connected, connectOnStart));
    nic.setMacAddress(macAddress);
    nic.setKey(-contextNumber);
    return nic;
}
 
Example #22
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public VirtualDevice[] getMatchedDevices(Class<?>[] deviceClasses) throws Exception {
    assert (deviceClasses != null);

    List<VirtualDevice> returnList = new ArrayList<VirtualDevice>();

    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");

    if (devices != null) {
        for (VirtualDevice device : devices) {
            for (Class<?> clz : deviceClasses) {
                if (clz.isInstance(device)) {
                    returnList.add(device);
                    break;
                }
            }
        }
    }

    return returnList.toArray(new VirtualDevice[0]);
}
 
Example #23
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public Pair<Integer, VirtualDevice> getNicDeviceIndex(String networkNamePrefix) throws Exception {
    List<VirtualDevice> nics = getNicDevices(true);

    int index = 0;
    String attachedNetworkSummary;
    String dvPortGroupName;
    for (VirtualDevice nic : nics) {
        attachedNetworkSummary = ((VirtualEthernetCard)nic).getDeviceInfo().getSummary();
        if (attachedNetworkSummary.startsWith(networkNamePrefix)) {
            return new Pair<Integer, VirtualDevice>(new Integer(index), nic);
        } else if (attachedNetworkSummary.endsWith("DistributedVirtualPortBackingInfo.summary") || attachedNetworkSummary.startsWith("DVSwitch")) {
            dvPortGroupName = getDvPortGroupName((VirtualEthernetCard)nic);
            if (dvPortGroupName != null && dvPortGroupName.startsWith(networkNamePrefix)) {
                s_logger.debug("Found a dvPortGroup already associated with public NIC.");
                return new Pair<Integer, VirtualDevice>(new Integer(index), nic);
            }
        }
        index++;
    }
    return new Pair<Integer, VirtualDevice>(new Integer(-1), null);
}
 
Example #24
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public int getNextDeviceNumber(int controllerKey) throws Exception {
    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");

    List<Integer> existingUnitNumbers = new ArrayList<Integer>();
    int deviceNumber = 0;
    int scsiControllerKey = getGenericScsiDeviceControllerKeyNoException();
    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if (device.getControllerKey() != null && device.getControllerKey().intValue() == controllerKey) {
                existingUnitNumbers.add(device.getUnitNumber());
            }
        }
    }
    while (true) {
        // Next device number should be the lowest device number on the key that is not in use and is not reserved.
        if (!existingUnitNumbers.contains(Integer.valueOf(deviceNumber))) {
            if (controllerKey != scsiControllerKey || !VmwareHelper.isReservedScsiDeviceNumber(deviceNumber))
                break;
        }
        ++deviceNumber;
    }
    return deviceNumber;
}
 
Example #25
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public VirtualDevice getIsoDevice(String filename) throws Exception {
    List<VirtualDevice> devices = (List<VirtualDevice>)_context.getVimClient().
            getDynamicProperty(_mor, "config.hardware.device");
    if(devices != null && devices.size() > 0) {
        long isoDevices = devices.stream()
                .filter(x -> x instanceof VirtualCdrom && x.getBacking() instanceof VirtualCdromIsoBackingInfo)
                .count();
        for(VirtualDevice device : devices) {
            if(device instanceof VirtualCdrom && device.getBacking() instanceof VirtualCdromIsoBackingInfo) {
                if (((VirtualCdromIsoBackingInfo)device.getBacking()).getFileName().equals(filename)) {
                    return device;
                } else if (isoDevices == 1L){
                    s_logger.warn(String.format("VM ISO filename %s differs from the expected filename %s",
                            ((VirtualCdromIsoBackingInfo)device.getBacking()).getFileName(), filename));
                    return device;
                }
            }
        }
    }
    return null;
}
 
Example #26
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public int getIDEControllerKey(int ideUnitNumber) throws Exception {
    List<VirtualDevice> devices = (List<VirtualDevice>)_context.getVimClient().
        getDynamicProperty(_mor, "config.hardware.device");

    int requiredIdeController = ideUnitNumber / VmwareHelper.MAX_IDE_CONTROLLER_COUNT;

    int ideControllerCount = 0;
    if(devices != null && devices.size() > 0) {
        for(VirtualDevice device : devices) {
            if(device instanceof VirtualIDEController) {
                if (ideControllerCount == requiredIdeController) {
                    return ((VirtualIDEController)device).getKey();
                }
                ideControllerCount++;
            }
        }
    }

    assert(false);
    throw new Exception("IDE Controller Not Found");
}
 
Example #27
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public VirtualDevice getNicDeviceByIndex(int index) throws Exception {
    List<VirtualDevice> nics = getNicDevices(true);
    try {
        return nics.get(index);
    } catch (IndexOutOfBoundsException e) {
        // Not found
        return null;
    }
}
 
Example #28
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public VirtualDevice getIsoDevice() throws Exception {
    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualCdrom) {
                return device;
            }
        }
    }
    return null;
}
 
Example #29
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
private List<VirtualDevice> getNicDevices(boolean sorted) throws Exception {
    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");

    List<VirtualDevice> nics = new ArrayList<VirtualDevice>();
    if (devices != null) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualEthernetCard) {
                nics.add(device);
            }
        }
    }

    if (sorted) {
        Collections.sort(nics, new Comparator<VirtualDevice>() {
            @Override
            public int compare(VirtualDevice arg0, VirtualDevice arg1) {
                int unitNumber0 = arg0.getUnitNumber() != null ? arg0.getUnitNumber().intValue() : -1;
                int unitNumber1 = arg1.getUnitNumber() != null ? arg1.getUnitNumber().intValue() : -1;
                if (unitNumber0 < unitNumber1)
                    return -1;
                else if (unitNumber0 > unitNumber1)
                    return 1;
                return 0;
            }
        });
    }

    return nics;
}
 
Example #30
Source File: VmUtils.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
VirtualDeviceConfigSpec getPopulatedCDSpecs(String fileName, ManagedObjectReference dataStoreRef,
                                            List<VirtualDevice> virtualDevicesList,
                                            VirtualDeviceConfigSpecOperation operation,
                                            Integer controllerKey, Integer unitNumber, Integer key,
                                            String parameter, VmInputs vmInputs) {

    VirtualDeviceConfigSpec cdSpecs = new VirtualDeviceConfigSpec();
    if (Operation.ADD.toString().equalsIgnoreCase(parameter)) {
        VirtualCdromIsoBackingInfo cdIsoBacking = new VirtualCdromIsoBackingInfo();
        cdIsoBacking.setDatastore(dataStoreRef);
        cdIsoBacking.setFileName(fileName + TEST_CD_ISO);

        VirtualCdrom cdRom = new VirtualCdrom();
        cdRom.setBacking(cdIsoBacking);
        cdRom.setControllerKey(controllerKey);
        cdRom.setUnitNumber(unitNumber);
        cdRom.setKey(key);

        return getCDSpec(cdSpecs, operation, cdRom);
    } else {
        VirtualCdrom cdRemove = findVirtualDevice(VirtualCdrom.class, virtualDevicesList, vmInputs);
        if (cdRemove != null) {
            return getCDSpec(cdSpecs, operation, cdRemove);
        }
    }
    throw new RuntimeException("No optical device named: [" + vmInputs.getUpdateValue() + "] can be found.");
}