com.vmware.vim25.VirtualMachineConfigSpec Java Examples

The following examples show how to use com.vmware.vim25.VirtualMachineConfigSpec. 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: 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 #2
Source File: HostMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
@Override
public boolean createVm(VirtualMachineConfigSpec vmSpec) throws Exception {
    assert (vmSpec != null);
    DatacenterMO dcMo = new DatacenterMO(_context, getHyperHostDatacenter());
    ManagedObjectReference morPool = getHyperHostOwnerResourcePool();

    ManagedObjectReference morTask = _context.getService().createVMTask(dcMo.getVmFolder(), vmSpec, morPool, _mor);
    boolean result = _context.getVimClient().waitForTask(morTask);

    if (result) {
        _context.waitForTaskProgressDone(morTask);
        return true;
    } else {
        s_logger.error("VMware createVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    return false;
}
 
Example #3
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 #4
Source File: VmService.java    From cs-actions with Apache License 2.0 6 votes vote down vote up
/**
 * Method used to connect to specified data center and create a virtual machine using the inputs provided.
 *
 * @param httpInputs Object that has all the inputs necessary to made a connection to data center
 * @param vmInputs   Object that has all the specific inputs necessary to create a new virtual machine
 * @return Map with String as key and value that contains returnCode of the operation, success message with task id
 *         of the execution or failure message and the exception if there is one
 * @throws Exception
 */
public Map<String, String> createVM(HttpInputs httpInputs, VmInputs vmInputs) throws Exception {
    ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs);
    try {
        VmUtils utils = new VmUtils();
        ManagedObjectReference vmFolderMor = utils.getMorFolder(vmInputs.getFolderName(), connectionResources);
        ManagedObjectReference resourcePoolMor = utils.getMorResourcePool(vmInputs.getResourcePool(), connectionResources);
        ManagedObjectReference hostMor = utils.getMorHost(vmInputs.getHostname(), connectionResources, null);
        VirtualMachineConfigSpec vmConfigSpec = new VmConfigSpecs().getVmConfigSpec(vmInputs, connectionResources);

        ManagedObjectReference task = connectionResources.getVimPortType()
                .createVMTask(vmFolderMor, vmConfigSpec, resourcePoolMor, hostMor);

        return new ResponseHelper(connectionResources, task).getResultsMap("Success: Created [" +
                        vmInputs.getVirtualMachineName() + "] VM. The taskId is: " + task.getValue(),
                "Failure: Could not create [" + vmInputs.getVirtualMachineName() + "] VM");
    } catch (Exception ex) {
        return ResponseUtils.getResultsMap(ex.toString(), Outputs.RETURN_CODE_FAILURE);
    } finally {
        if (httpInputs.isCloseSession()) {
            connectionResources.getConnection().disconnect();
            clearConnectionFromContext(httpInputs.getGlobalSessionObject());
        }
    }

}
 
Example #5
Source File: VmUtils.java    From cs-actions with Apache License 2.0 6 votes vote down vote up
VirtualMachineConfigSpec getPopulatedVmConfigSpec(VirtualMachineConfigSpec vmConfigSpec, VmInputs vmInputs, String name) {
    vmConfigSpec.setName(name);
    vmConfigSpec.setNumCPUs(vmInputs.getIntNumCPUs());
    vmConfigSpec.setMemoryMB(vmInputs.getLongVmMemorySize());
    vmConfigSpec.setAnnotation(vmInputs.getDescription());

    if (vmInputs.getCoresPerSocket() != null) {
        vmConfigSpec.setNumCoresPerSocket(InputUtils.getIntInput(vmInputs.getCoresPerSocket(), DEFAULT_CORES_PER_SOCKET));
    }

    if (vmInputs.getGuestOsId() != null) {
        vmConfigSpec.setGuestId(vmInputs.getGuestOsId());
    }

    return vmConfigSpec;
}
 
Example #6
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public void ensurePvScsiDeviceController(int requiredNumScsiControllers, int availableBusNum) throws Exception {
    VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();

    int busNum = availableBusNum;
    while (busNum < requiredNumScsiControllers) {
        ParaVirtualSCSIController scsiController = new ParaVirtualSCSIController();

        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)) {
        throw new Exception("Unable to add Scsi controllers to the VM " + getName());
    } else {
        s_logger.info("Successfully added " + requiredNumScsiControllers + " SCSI controllers.");
    }
}
 
Example #7
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public void ensureLsiLogicDeviceControllers(int count, int availableBusNum) throws Exception {
    int scsiControllerKey = getLsiLogicDeviceControllerKeyNoException();
    if (scsiControllerKey < 0) {
        VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();

        int busNum = availableBusNum;
        while (busNum < count) {
            VirtualLsiLogicController scsiController = new VirtualLsiLogicController();
            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)) {
            throw new Exception("Unable to add Lsi Logic controllers to the VM " + getName());
        } else {
            s_logger.info("Successfully added " + count + " LsiLogic Parallel SCSI controllers.");
        }
    }
}
 
Example #8
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public void ensureScsiDeviceController() throws Exception {
    int scsiControllerKey = getScsiDeviceControllerKeyNoException();
    if (scsiControllerKey < 0) {
        VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();

        // Scsi controller
        VirtualLsiLogicController scsiController = new VirtualLsiLogicController();
        scsiController.setSharedBus(VirtualSCSISharing.NO_SHARING);
        scsiController.setBusNumber(0);
        scsiController.setKey(1);
        VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
        scsiControllerSpec.setDevice(scsiController);
        scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);

        vmConfig.getDeviceChange().add(scsiControllerSpec);
        if (configureVm(vmConfig)) {
            throw new Exception("Unable to add Scsi controller");
        }
    }
}
 
Example #9
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public void ensureScsiDeviceControllers(int count, int availableBusNum) throws Exception {
    int scsiControllerKey = getScsiDeviceControllerKeyNoException();
    if (scsiControllerKey < 0) {
        VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();

        int busNum = availableBusNum;
        while (busNum < count) {
        VirtualLsiLogicController scsiController = new VirtualLsiLogicController();
        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)) {
            throw new Exception("Unable to add Scsi controllers to the VM " + getName());
        } else {
            s_logger.info("Successfully added " + count + " SCSI controllers.");
        }
    }
}
 
Example #10
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public void ensureLsiLogicSasDeviceControllers(int count, int availableBusNum) throws Exception {
    int scsiControllerKey = getLsiLogicSasDeviceControllerKeyNoException();
    if (scsiControllerKey < 0) {
        VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();

        int busNum = availableBusNum;
        while (busNum < count) {
            VirtualLsiLogicSASController scsiController = new VirtualLsiLogicSASController();
            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)) {
            throw new Exception("Unable to add Scsi controller of type LsiLogic SAS.");
        }
    }

}
 
Example #11
Source File: ClusterMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
@Override
public boolean createVm(VirtualMachineConfigSpec vmSpec) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - createVM_Task(). target MOR: " + _mor.getValue() + ", VirtualMachineConfigSpec: " + new Gson().toJson(vmSpec));

    assert (vmSpec != null);
    DatacenterMO dcMo = new DatacenterMO(_context, getHyperHostDatacenter());
    ManagedObjectReference morPool = getHyperHostOwnerResourcePool();

    ManagedObjectReference morTask = _context.getService().createVMTask(dcMo.getVmFolder(), vmSpec, morPool, null);
    boolean result = _context.getVimClient().waitForTask(morTask);

    if (result) {
        _context.waitForTaskProgressDone(morTask);

        if (s_logger.isTraceEnabled())
            s_logger.trace("vCenter API trace - createVM_Task() done(successfully)");
        return true;
    } else {
        s_logger.error("VMware createVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }

    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - createVM_Task() done(failed)");
    return false;
}
 
Example #12
Source File: VmwareHelper.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public static void setVmScaleUpConfig(VirtualMachineConfigSpec vmConfig, int cpuCount, int cpuSpeedMHz, int cpuReservedMhz, int memoryMB, int memoryReserveMB,
        boolean limitCpuUse) {

    // VM config for scaling up
    vmConfig.setMemoryMB((long)memoryMB);
    vmConfig.setNumCPUs(cpuCount);

    ResourceAllocationInfo cpuInfo = new ResourceAllocationInfo();
    if (limitCpuUse) {
        cpuInfo.setLimit((long)(cpuSpeedMHz * cpuCount));
    } else {
        cpuInfo.setLimit(-1L);
    }

    cpuInfo.setReservation((long)cpuReservedMhz);
    vmConfig.setCpuAllocation(cpuInfo);

    ResourceAllocationInfo memInfo = new ResourceAllocationInfo();
    memInfo.setLimit((long)memoryMB);
    memInfo.setReservation((long)memoryReserveMB);
    vmConfig.setMemoryAllocation(memInfo);

}
 
Example #13
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 #14
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public void ensureBusLogicDeviceControllers(int count, int availableBusNum) throws Exception {
    int scsiControllerKey = getBusLogicDeviceControllerKeyNoException();
    if (scsiControllerKey < 0) {
        VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();

        int busNum = availableBusNum;
        while (busNum < count) {
            VirtualBusLogicController scsiController = new VirtualBusLogicController();

            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)) {
            throw new Exception("Unable to add Scsi BusLogic controllers to the VM " + getName());
        } else {
            s_logger.info("Successfully added " + count + " SCSI BusLogic controllers.");
        }
    }
}
 
Example #15
Source File: VmwareHelper.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public static void setBasicVmConfig(VirtualMachineConfigSpec vmConfig, int cpuCount, int cpuSpeedMHz, int cpuReservedMhz, int memoryMB, int memoryReserveMB,
        String guestOsIdentifier, boolean limitCpuUse) {

    // VM config basics
    vmConfig.setMemoryMB((long)memoryMB);
    vmConfig.setNumCPUs(cpuCount);

    ResourceAllocationInfo cpuInfo = new ResourceAllocationInfo();
    if (limitCpuUse) {
        cpuInfo.setLimit(((long)cpuSpeedMHz * cpuCount));
    } else {
        cpuInfo.setLimit(-1L);
    }

    cpuInfo.setReservation((long)cpuReservedMhz);
    vmConfig.setCpuAllocation(cpuInfo);
    if (cpuSpeedMHz != cpuReservedMhz) {
        vmConfig.setCpuHotAddEnabled(true);
    }
    if (memoryMB != memoryReserveMB) {
        vmConfig.setMemoryHotAddEnabled(true);
    }
    ResourceAllocationInfo memInfo = new ResourceAllocationInfo();
    memInfo.setLimit((long)memoryMB);
    memInfo.setReservation((long)memoryReserveMB);
    vmConfig.setMemoryAllocation(memInfo);

    vmConfig.setGuestId(guestOsIdentifier);
}
 
Example #16
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public boolean configureVm(Ternary<VirtualDevice, VirtualDeviceConfigSpecOperation, VirtualDeviceConfigSpecFileOperation>[] devices) throws Exception {

        assert (devices != null);

        VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec();
        VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[devices.length];
        int i = 0;
        for (Ternary<VirtualDevice, VirtualDeviceConfigSpecOperation, VirtualDeviceConfigSpecFileOperation> deviceTernary : devices) {
            VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
            deviceConfigSpec.setDevice(deviceTernary.first());
            deviceConfigSpec.setOperation(deviceTernary.second());
            deviceConfigSpec.setFileOperation(deviceTernary.third());
            deviceConfigSpecArray[i++] = deviceConfigSpec;
        }
        configSpec.getDeviceChange().addAll(Arrays.asList(deviceConfigSpecArray));

        ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, configSpec);

        boolean result = _context.getVimClient().waitForTask(morTask);
        if (result) {
            _context.waitForTaskProgressDone(morTask);
            return true;
        } else {
            s_logger.error("VMware reconfigVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
        }
        return false;
    }
 
Example #17
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public void tearDownDevice(VirtualDevice device) throws Exception {
    VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
    VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
    deviceConfigSpec.setDevice(device);
    deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);

    vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
    if (!configureVm(vmConfigSpec)) {
        throw new Exception("Failed to detach devices");
    }
}
 
Example #18
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public void plugDevice(VirtualDevice device) throws Exception {
    VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
    VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
    deviceConfigSpec.setDevice(device);
    deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);

    vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
    if (!configureVm(vmConfigSpec)) {
        throw new Exception("Failed to add devices");
    }
}
 
Example #19
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public VirtualMachineMO cloneFromDiskChain(String clonedVmName, int cpuSpeedMHz, int memoryMb, String[] disks, ManagedObjectReference morDs) throws Exception {
    assert (disks != null);
    assert (disks.length >= 1);

    HostMO hostMo = getRunningHost();

    VirtualMachineMO clonedVmMo = HypervisorHostHelper.createWorkerVM(hostMo, new DatastoreMO(hostMo.getContext(), morDs), clonedVmName);
    if (clonedVmMo == null)
        throw new Exception("Unable to find just-created blank VM");

    boolean bSuccess = false;
    try {
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();

        VirtualDevice device = VmwareHelper.prepareDiskDevice(clonedVmMo, null, -1, disks, morDs, -1, 1);

        deviceConfigSpec.setDevice(device);
        deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
        vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
        clonedVmMo.configureVm(vmConfigSpec);
        bSuccess = true;
        return clonedVmMo;
    } finally {
        if (!bSuccess) {
            clonedVmMo.detachAllDisks();
            clonedVmMo.destroy();
        }
    }
}
 
Example #20
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public void detachAllDisks() throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - detachAllDisk(). target MOR: " + _mor.getValue());

    VirtualDisk[] disks = getAllDiskDevice();
    if (disks.length > 0) {
        VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
        VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[disks.length];

        for (int i = 0; i < disks.length; i++) {
            deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec();
            deviceConfigSpecArray[i].setDevice(disks[i]);
            deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
        }
        reConfigSpec.getDeviceChange().addAll(Arrays.asList(deviceConfigSpecArray));

        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 - detachAllDisk() done(failed)");
            throw new Exception("Failed to detach disk due to " + TaskMO.getTaskFailureInfo(_context, morTask));
        }

        _context.waitForTaskProgressDone(morTask);
    }

    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - detachAllDisk() done(successfully)");
}
 
Example #21
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public void attachDisk(Pair<String, ManagedObjectReference>[] vmdkDatastorePathChain, int controllerKey) throws Exception {

        if (s_logger.isTraceEnabled())
            s_logger.trace("vCenter API trace - attachDisk(). target MOR: " + _mor.getValue() + ", vmdkDatastorePath: " + new Gson().toJson(vmdkDatastorePathChain));

        synchronized (_mor.getValue().intern()) {
            VirtualDevice newDisk = VmwareHelper.prepareDiskDevice(this, controllerKey, vmdkDatastorePathChain, -1, 1);
            VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
            VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();

            deviceConfigSpec.setDevice(newDisk);
            deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);

            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)");
    }
 
Example #22
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public void attachDisk(String[] vmdkDatastorePathChain, ManagedObjectReference morDs) throws Exception {

        if (s_logger.isTraceEnabled())
            s_logger.trace("vCenter API trace - attachDisk(). target MOR: " + _mor.getValue() + ", vmdkDatastorePath: " + new Gson().toJson(vmdkDatastorePathChain) +
                    ", datastore: " + morDs.getValue());

        synchronized (_mor.getValue().intern()) {
            VirtualDevice newDisk = VmwareHelper.prepareDiskDevice(this, null, getScsiDeviceControllerKey(), vmdkDatastorePathChain, morDs, -1, 1);
            VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
            VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();

            deviceConfigSpec.setDevice(newDisk);
            deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);

            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)");
    }
 
Example #23
Source File: NetworkManager.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces the NICs in the given VM.
 *
 * @param vmw
 *            connected VMware client entity
 * @param paramHandler
 *            entity which holds all properties of the instance
 * @param vmwInstance
 *            the virtual machine that gets reconfigured
 */
public static void configureNetworkAdapter(VMwareClient vmw,
        VirtualMachineConfigSpec vmConfigSpec,
        VMPropertyHandler paramHandler, ManagedObjectReference vmwInstance)
        throws Exception {
    logger.debug("");

    VirtualMachineConfigInfo configInfo = (VirtualMachineConfigInfo) vmw
            .getServiceUtil().getDynamicProperty(vmwInstance, "config");
    List<VirtualEthernetCard> vmNics = getNetworkAdapter(configInfo);

    int numberOfNICs = Integer.parseInt(paramHandler
            .getServiceSetting(VMPropertyHandler.TS_NUMBER_OF_NICS));

    if (numberOfNICs != vmNics.size()) {
        throw new Exception(
                "the number of NICs in virtual machine does not match the service parameter. VM: "
                        + configInfo.getName() + " NICs: " + vmNics.size()
                        + " " + VMPropertyHandler.TS_NUMBER_OF_NICS + ": "
                        + numberOfNICs);
    }

    for (int i = 1; i <= numberOfNICs; i++) {
        String newNetworkName = paramHandler.getNetworkAdapter(i);
        VirtualEthernetCard vmNic = vmNics.get(i - 1);
        String vmNetworkName = getNetworkName(vmw, vmwInstance, i);
        if (newNetworkName != null && newNetworkName.length() > 0
                && !newNetworkName.equals(vmNetworkName)) {
            ManagedObjectReference newNetworkRef = getNetworkFromHost(vmw,
                    vmwInstance, newNetworkName);

            replaceNetworkAdapter(vmConfigSpec, vmNic, newNetworkRef,
                    newNetworkName);
        } else {
            connectNIC(vmConfigSpec, vmNic, vmNetworkName);
        }
    }
}
 
Example #24
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public boolean configureVm(VirtualMachineConfigSpec vmConfigSpec) throws Exception {
    ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, vmConfigSpec);

    boolean result = _context.getVimClient().waitForTask(morTask);
    if (result) {
        _context.waitForTaskProgressDone(morTask);
        return true;
    } else {
        s_logger.error("VMware reconfigVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    return false;
}
 
Example #25
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public boolean setVncConfigInfo(boolean enableVnc, String vncPassword, int vncPort, String keyboard) throws Exception {
    VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
    OptionValue[] vncOptions = VmwareHelper.composeVncOptions(null, enableVnc, vncPassword, vncPort, keyboard);
    vmConfigSpec.getExtraConfig().addAll(Arrays.asList(vncOptions));
    ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, vmConfigSpec);

    boolean result = _context.getVimClient().waitForTask(morTask);
    if (result) {
        _context.waitForTaskProgressDone(morTask);
        return true;
    } else {
        s_logger.error("VMware reconfigVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    return false;
}
 
Example #26
Source File: NetworkManager.java    From development with Apache License 2.0 5 votes vote down vote up
private static void connectNIC(VirtualMachineConfigSpec vmConfigSpec,
        VirtualDevice oldNIC, String vmNetworkName) throws Exception {

    logger.debug("networkName: " + vmNetworkName);

    VirtualDeviceConnectInfo info = new VirtualDeviceConnectInfo();
    info.setConnected(true);
    info.setStartConnected(true);
    info.setAllowGuestControl(true);
    oldNIC.setConnectable(info);
    VirtualDeviceConfigSpec vmDeviceSpec = new VirtualDeviceConfigSpec();
    vmDeviceSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
    vmDeviceSpec.setDevice(oldNIC);
    vmConfigSpec.getDeviceChange().add(vmDeviceSpec);
}
 
Example #27
Source File: HypervisorHostHelper.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public static String getOVFParamValue(VirtualMachineConfigSpec config) {
    String paramVal = "";
    List<OptionValue> options = config.getExtraConfig();
    for (OptionValue option : options) {
        if (OVA_OPTION_KEY_BOOTDISK.equalsIgnoreCase(option.getKey())) {
            paramVal = (String)option.getValue();
            break;
        }
    }
    return paramVal;
}
 
Example #28
Source File: DiskManager.java    From development with Apache License 2.0 5 votes vote down vote up
private void updateDiskConfiguration(VirtualMachineConfigSpec vmConfigSpec,
        List<VirtualDevice> devices, int vdKey, long newDiskSpace)
        throws Exception {
    VirtualDisk vdDataDisk = findDataDisk(devices, vdKey);
    if (vdDataDisk != null && newDiskSpace > vdDataDisk.getCapacityInKB()) {

        logger.info("reconfigureDisks() extend data disk #" + vdKey
                + " space to " + newDiskSpace + " ("
                + vdDataDisk.getDeviceInfo().getLabel() + ")");

        if (newDiskSpace < vdDataDisk.getCapacityInKB()) {
            logger.error("Cannot reduce size of data disk "
                    + vdDataDisk.getDeviceInfo().getLabel());
            logger.error("Current disk space: "
                    + vdDataDisk.getCapacityInKB() + " new disk space: "
                    + newDiskSpace);
            throw new Exception(Messages
                    .getAll("error_invalid_diskspacereduction").get(0)
                    .getText());
        } else if (newDiskSpace > vdDataDisk.getCapacityInKB()) {
            vdDataDisk.setCapacityInKB(newDiskSpace);
            VirtualDeviceConfigSpec vmDeviceSpec = new VirtualDeviceConfigSpec();
            vmDeviceSpec
                    .setOperation(VirtualDeviceConfigSpecOperation.EDIT);
            vmDeviceSpec.setDevice(vdDataDisk);

            vmConfigSpec.getDeviceChange().add(vmDeviceSpec);
        } else {
            logger.debug("Data disk size has not been changed. "
                    + newDiskSpace + " KB");
        }
    }
}
 
Example #29
Source File: DiskManager.java    From development with Apache License 2.0 5 votes vote down vote up
private void configureSystemDisk(VirtualMachineConfigSpec vmConfigSpec,
        long systemDiskMB, VirtualDisk vdSystemDisk) throws Exception {
    if (systemDiskMB > 0) {
        long newDiskSpace = systemDiskMB * 1024;
        if (newDiskSpace < vdSystemDisk.getCapacityInKB()) {
            logger.error("Cannot reduce size of system disk \""
                    + vdSystemDisk.getDeviceInfo().getLabel() + "\"");
            logger.error("Current disk size: "
                    + vdSystemDisk.getCapacityInKB() + " new disk space: "
                    + newDiskSpace);

            throw new Exception(Messages
                    .getAll("error_invalid_diskspacereduction").get(0)
                    .getText());
        } else if (newDiskSpace > vdSystemDisk.getCapacityInKB()) {
            logger.info("reconfigureDisks() extend system disk space to "
                    + newDiskSpace + " ("
                    + vdSystemDisk.getDeviceInfo().getLabel() + ")");
            vdSystemDisk.setCapacityInKB(newDiskSpace);
            VirtualDeviceConfigSpec vmDeviceSpec = new VirtualDeviceConfigSpec();
            vmDeviceSpec
                    .setOperation(VirtualDeviceConfigSpecOperation.EDIT);
            vmDeviceSpec.setDevice(vdSystemDisk);
            vmConfigSpec.getDeviceChange().add(vmDeviceSpec);
        } else {
            logger.debug("System disk size has not been changed. "
                    + newDiskSpace + " KB");
        }
    } else {
        logger.error("Reconfiguration of system disk not possible because system disk size is not defined.");
    }
}
 
Example #30
Source File: VM.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * Reconfigures VMware instance. Memory, CPU, disk space and network
 * adapter. The VM has been created and must be stopped to reconfigure the
 * hardware.
 */
public TaskInfo reconfigureVirtualMachine(VMPropertyHandler paramHandler)
        throws Exception {
    LOG.debug("instanceName: " + instanceName);

    VimPortType service = vmw.getConnection().getService();
    VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();

    vmConfigSpec
            .setMemoryMB(Long.valueOf(paramHandler.getConfigMemoryMB()));
    vmConfigSpec.setNumCPUs(Integer.valueOf(paramHandler.getConfigCPUs()));

    String reqUser = paramHandler
            .getServiceSetting(VMPropertyHandler.REQUESTING_USER);

    String comment = Messages.get(paramHandler.getLocale(), "vm_comment",
            new Object[] { paramHandler.getSettings().getOrganizationName(),
                    paramHandler.getSettings().getSubscriptionId(),
                    reqUser });
    String annotation = vmConfigSpec.getAnnotation();
    comment = updateComment(comment, annotation);
    vmConfigSpec.setAnnotation(comment);

    DiskManager diskManager = new DiskManager(vmw, paramHandler);
    diskManager.reconfigureDisks(vmConfigSpec, vmInstance);

    NetworkManager.configureNetworkAdapter(vmw, vmConfigSpec, paramHandler,
            vmInstance);

    LOG.debug("Call vSphere API: reconfigVMTask()");
    ManagedObjectReference reconfigureTask = service
            .reconfigVMTask(vmInstance, vmConfigSpec);

    return (TaskInfo) vmw.getServiceUtil()
            .getDynamicProperty(reconfigureTask, "info");
}