Java Code Examples for com.vmware.vim25.VirtualDisk#setCapacityInKB()

The following examples show how to use com.vmware.vim25.VirtualDisk#setCapacityInKB() . 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: VmUtils.java    From cs-actions with Apache License 2.0 6 votes vote down vote up
private VirtualDisk createVirtualDisk(String volumeName, int controllerKey, int unitNumber, int key, VmInputs vmInputs,
                                      String parameter) throws Exception {
    VirtualDiskFlatVer2BackingInfo diskFileBacking = new VirtualDiskFlatVer2BackingInfo();
    diskFileBacking.setFileName(volumeName);
    if (Operation.CREATE.toString().equalsIgnoreCase(parameter)) {
        diskFileBacking.setDiskMode(DiskMode.PERSISTENT.toString());
    } else {
        diskFileBacking.setDiskMode(DiskMode.getValue(vmInputs.getVmDiskMode()));
    }

    VirtualDisk disk = new VirtualDisk();
    disk.setBacking(diskFileBacking);
    disk.setControllerKey(controllerKey);
    disk.setUnitNumber(unitNumber);
    disk.setKey(key);
    disk.setCapacityInKB(vmInputs.getLongVmDiskSize() * DISK_AMOUNT_MULTIPLIER);

    return disk;
}
 
Example 2
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 3
Source File: DiskManager.java    From development with Apache License 2.0 5 votes vote down vote up
private VirtualDeviceConfigSpec createNewDataDisk(VirtualDisk vdSystemDisk,
        long newDiskSpace, int deviceKey, int unitNumber) throws Exception {

    logger.info("reconfigureDisks() create data disk space with "
            + newDiskSpace + " KB");

    ManagedObjectReference vmDatastore = ((VirtualDeviceFileBackingInfo) vdSystemDisk
            .getBacking()).getDatastore();
    String vmDatastoreName = (String) vmw.getServiceUtil()
            .getDynamicProperty(vmDatastore, "summary.name");

    VirtualDisk vdDataDisk = new VirtualDisk();
    VirtualDiskFlatVer2BackingInfo diskfileBacking = new VirtualDiskFlatVer2BackingInfo();
    diskfileBacking.setFileName("[" + vmDatastoreName + "]");
    diskfileBacking.setDiskMode("persistent");

    vdDataDisk.setKey(deviceKey);
    vdDataDisk.setControllerKey(vdSystemDisk.getControllerKey());
    vdDataDisk.setUnitNumber(new Integer(unitNumber));
    vdDataDisk.setBacking(diskfileBacking);
    vdDataDisk.setCapacityInKB(newDiskSpace);

    VirtualDeviceConfigSpec vmDeviceSpec = new VirtualDeviceConfigSpec();
    vmDeviceSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
    vmDeviceSpec
            .setFileOperation(VirtualDeviceConfigSpecFileOperation.CREATE);
    vmDeviceSpec.setDevice(vdDataDisk);

    return vmDeviceSpec;
}
 
Example 4
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 5
Source File: VmwareProcessClient.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
protected VirtualDisk newDisk(VirtualMachine machine, Integer scsiId, Integer size) {
    // SCSIコントローラを取得
    VirtualSCSIController scsiController = getSCSIController(machine);

    // 仮想マシン自体のディスクを取得
    VirtualDisk machineDisk = getVirtualDisk(machine, 0);
    VirtualDiskFlatVer2BackingInfo machieBackingInfo = VirtualDiskFlatVer2BackingInfo.class
            .cast(machineDisk.getBacking());

    // VirtualDisk
    VirtualDisk disk = new VirtualDisk();
    disk.setUnitNumber(scsiId);
    disk.setCapacityInKB(size * 1024L * 1024L);
    disk.setControllerKey(scsiController.getKey());

    // VirtualDiskFlatVer2BackingInfo
    VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo();
    backingInfo.setDatastore(null);
    backingInfo.setFileName("");
    backingInfo.setDiskMode("persistent");
    backingInfo.setSplit(false);
    backingInfo.setEagerlyScrub(false);
    backingInfo.setThinProvisioned(machieBackingInfo.getThinProvisioned());
    backingInfo.setWriteThrough(false);
    disk.setBacking(backingInfo);

    // VirtualDeviceConnectInfo
    VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo();
    connectInfo.setAllowGuestControl(false);
    connectInfo.setStartConnected(true);
    connectInfo.setConnected(true);
    disk.setConnectable(connectInfo);

    return disk;
}
 
Example 6
Source File: VmwareHelper.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public static VirtualDevice prepareDiskDevice(VirtualMachineMO vmMo, int controllerKey, String vmdkDatastorePath, int sizeInMb, ManagedObjectReference morDs,
        int deviceNumber, int contextNumber) throws Exception {

    VirtualDisk disk = new VirtualDisk();

    VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo();
    backingInfo.setDiskMode(VirtualDiskMode.PERSISTENT.value());
    backingInfo.setThinProvisioned(true);
    backingInfo.setEagerlyScrub(false);
    backingInfo.setDatastore(morDs);
    backingInfo.setFileName(vmdkDatastorePath);
    disk.setBacking(backingInfo);

    int ideControllerKey = vmMo.getIDEDeviceControllerKey();
    if (controllerKey < 0)
        controllerKey = ideControllerKey;
    if (deviceNumber < 0) {
        deviceNumber = vmMo.getNextDeviceNumber(controllerKey);
    }
    disk.setControllerKey(controllerKey);

    disk.setKey(-contextNumber);
    disk.setUnitNumber(deviceNumber);
    disk.setCapacityInKB(sizeInMb * 1024);

    VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo();
    connectInfo.setConnected(true);
    connectInfo.setStartConnected(true);
    disk.setConnectable(connectInfo);

    return disk;
}
 
Example 7
Source File: VmwareStorageProcessor.java    From cloudstack with Apache License 2.0 3 votes vote down vote up
private boolean expandVirtualDisk(VirtualMachineMO vmMo, String datastoreVolumePath, long currentSizeInBytes) throws Exception {
    long currentSizeInKB = currentSizeInBytes / 1024;

    Pair<VirtualDisk, String> vDiskPair = vmMo.getDiskDevice(datastoreVolumePath);

    VirtualDisk vDisk = vDiskPair.first();

    if (vDisk.getCapacityInKB() < currentSizeInKB) {
        // IDE virtual disk cannot be re-sized if VM is running
        if (vDiskPair.second() != null && vDiskPair.second().contains("ide")) {
            throw new Exception("Re-sizing a virtual disk over an IDE controller is not supported in VMware hypervisor. " +
                    "Please re-try when virtual disk is attached to a VM using a SCSI controller.");
        }

        String vmdkAbsFile = resource.getAbsoluteVmdkFile(vDisk);

        if (vmdkAbsFile != null && !vmdkAbsFile.isEmpty()) {
            vmMo.updateAdapterTypeIfRequired(vmdkAbsFile);
        }

        vDisk.setCapacityInKB(currentSizeInKB);

        VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();

        deviceConfigSpec.setDevice(vDisk);
        deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);

        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();

        vmConfigSpec.getDeviceChange().add(deviceConfigSpec);

        if (!vmMo.configureVm(vmConfigSpec)) {
            throw new Exception("Failed to configure VM to resize disk. vmName: " + vmMo.getName());
        }

        return true;
    }

    return false;
}