com.vmware.vim25.VirtualDiskFlatVer2BackingInfo Java Examples

The following examples show how to use com.vmware.vim25.VirtualDiskFlatVer2BackingInfo. 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: VmwareHelper.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
private static void setParentBackingInfo(VirtualDiskFlatVer2BackingInfo backingInfo, ManagedObjectReference morDs, String[] parentDatastorePathList) {

        VirtualDiskFlatVer2BackingInfo parentBacking = new VirtualDiskFlatVer2BackingInfo();
        parentBacking.setDatastore(morDs);
        parentBacking.setDiskMode(VirtualDiskMode.PERSISTENT.value());

        if (parentDatastorePathList.length > 1) {
            String[] nextDatastorePathList = new String[parentDatastorePathList.length - 1];
            for (int i = 0; i < parentDatastorePathList.length - 1; i++)
                nextDatastorePathList[i] = parentDatastorePathList[i + 1];
            setParentBackingInfo(parentBacking, morDs, nextDatastorePathList);
        }
        parentBacking.setFileName(parentDatastorePathList[0]);

        backingInfo.setParent(parentBacking);
    }
 
Example #2
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 #3
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
private VolumeVO createVolume(VirtualDisk disk, VirtualMachineMO vmToImport, long domainId, long zoneId, long accountId, long instanceId, Long poolId, long templateId, Backup backup, boolean isImport) throws Exception {
    VMInstanceVO vm = _vmDao.findByIdIncludingRemoved(backup.getVmId());
    if (vm == null) {
        throw new CloudRuntimeException("Failed to find the backup volume information from the VM backup");
    }
    List<Backup.VolumeInfo> backedUpVolumes = vm.getBackupVolumeList();
    Volume.Type type = Volume.Type.DATADISK;
    Long size = disk.getCapacityInBytes();
    if (isImport) {
        for (Backup.VolumeInfo volumeInfo : backedUpVolumes) {
            if (volumeInfo.getSize().equals(disk.getCapacityInBytes())) {
                type = volumeInfo.getType();
            }
        }
    }
    VirtualDeviceBackingInfo backing = disk.getBacking();
    checkBackingInfo(backing);
    VirtualDiskFlatVer2BackingInfo info = (VirtualDiskFlatVer2BackingInfo)backing;
    String volumeName = getVolumeName(disk, vmToImport);
    Storage.ProvisioningType provisioningType = getProvisioningType(info);
    long diskOfferingId = getDiskOfferingId(size, provisioningType);
    Integer unitNumber = disk.getUnitNumber();
    VirtualMachineDiskInfo diskInfo = getDiskInfo(vmToImport, poolId, volumeName);
    return createVolumeRecord(type, volumeName, zoneId, domainId, accountId, diskOfferingId, provisioningType, size, instanceId, poolId, templateId, unitNumber, diskInfo);
}
 
Example #4
Source File: VmwareHelper.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static void setParentBackingInfo(VirtualDiskFlatVer2BackingInfo backingInfo, Pair<String, ManagedObjectReference>[] parentDatastorePathList) {

    VirtualDiskFlatVer2BackingInfo parentBacking = new VirtualDiskFlatVer2BackingInfo();
    parentBacking.setDatastore(parentDatastorePathList[0].second());
    parentBacking.setDiskMode(VirtualDiskMode.PERSISTENT.value());

    if (parentDatastorePathList.length > 1) {
        Pair<String, ManagedObjectReference>[] nextDatastorePathList = new Pair[parentDatastorePathList.length - 1];
        for (int i = 0; i < parentDatastorePathList.length - 1; i++)
            nextDatastorePathList[i] = parentDatastorePathList[i + 1];
        setParentBackingInfo(parentBacking, nextDatastorePathList);
    }
    parentBacking.setFileName(parentDatastorePathList[0].first());

    backingInfo.setParent(parentBacking);
}
 
Example #5
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public VirtualDisk[] getAllIndependentDiskDevice() throws Exception {
    List<VirtualDisk> independentDisks = new ArrayList<VirtualDisk>();
    VirtualDisk[] allDisks = getAllDiskDevice();
    if (allDisks.length > 0) {
        for (VirtualDisk disk : allDisks) {
            String diskMode = "";
            if (disk.getBacking() instanceof VirtualDiskFlatVer1BackingInfo) {
                diskMode = ((VirtualDiskFlatVer1BackingInfo)disk.getBacking()).getDiskMode();
            } else if (disk.getBacking() instanceof VirtualDiskFlatVer2BackingInfo) {
                diskMode = ((VirtualDiskFlatVer2BackingInfo)disk.getBacking()).getDiskMode();
            } else if (disk.getBacking() instanceof VirtualDiskRawDiskMappingVer1BackingInfo) {
                diskMode = ((VirtualDiskRawDiskMappingVer1BackingInfo)disk.getBacking()).getDiskMode();
            } else if (disk.getBacking() instanceof VirtualDiskSparseVer1BackingInfo) {
                diskMode = ((VirtualDiskSparseVer1BackingInfo)disk.getBacking()).getDiskMode();
            } else if (disk.getBacking() instanceof VirtualDiskSparseVer2BackingInfo) {
                diskMode = ((VirtualDiskSparseVer2BackingInfo)disk.getBacking()).getDiskMode();
            }

            if (diskMode.indexOf("independent") != -1) {
                independentDisks.add(disk);
            }
        }
    }

    return independentDisks.toArray(new VirtualDisk[0]);
}
 
Example #6
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 #7
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 #8
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public String getDiskCurrentTopBackingFileInChain(String deviceBusName) 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 VirtualDisk) {
                s_logger.info("Test against disk device, controller key: " + device.getControllerKey() + ", unit number: " + device.getUnitNumber());

                VirtualDeviceBackingInfo backingInfo = ((VirtualDisk)device).getBacking();
                if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
                    VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo;

                    String deviceNumbering = getDeviceBusName(devices, device);
                    if (deviceNumbering.equals(deviceBusName))
                        return diskBackingInfo.getFileName();
                }
            }
        }
    }

    return null;
}
 
Example #9
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 #10
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public String getVmdkFileBaseName(VirtualDisk disk) throws Exception {
    String vmdkFileBaseName = null;
    VirtualDeviceBackingInfo backingInfo = disk.getBacking();
    if(backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
        VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo;
        DatastoreFile dsBackingFile = new DatastoreFile(diskBackingInfo.getFileName());
        vmdkFileBaseName = dsBackingFile.getFileBaseName();
    }
    return vmdkFileBaseName;
}
 
Example #11
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 #12
Source File: VmwareStorageProcessor.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
private List<String> getManagedDatastoreNamesFromVirtualDisks(List<VirtualDisk> virtualDisks) {
    List<String> managedDatastoreNames = new ArrayList<>();

    if (virtualDisks != null) {
        for (VirtualDisk virtualDisk : virtualDisks) {
            if (virtualDisk.getBacking() instanceof VirtualDiskFlatVer2BackingInfo) {
                VirtualDiskFlatVer2BackingInfo backingInfo = (VirtualDiskFlatVer2BackingInfo)virtualDisk.getBacking();
                String path = backingInfo.getFileName();

                String search = "[-";
                int index = path.indexOf(search);

                if (index > -1) {
                    path = path.substring(index + search.length());

                    String search2 = "-0]";

                    index = path.lastIndexOf(search2);

                    if (index > -1) {
                        path = path.substring(0, index);

                        if (path.startsWith("iqn.")) {
                            managedDatastoreNames.add("-" + path + "-0");
                        }
                    }
                }
            }
        }
    }

    return managedDatastoreNames;
}
 
Example #13
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
/**
 * Get dest datastore mor
 */
private ManagedObjectReference getDestStoreMor(VirtualMachineMO vmMo) throws Exception {
    VirtualDisk vmDisk = vmMo.getVirtualDisks().get(0);
    VirtualDeviceBackingInfo backing = vmDisk.getBacking();
    checkBackingInfo(backing);
    VirtualDiskFlatVer2BackingInfo info = (VirtualDiskFlatVer2BackingInfo)backing;
    return info.getDatastore();
}
 
Example #14
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
/**
 * Get volume full path
 */
private String getVolumeFullPath(VirtualDisk disk) {
    VirtualDeviceBackingInfo backing = disk.getBacking();
    checkBackingInfo(backing);
    VirtualDiskFlatVer2BackingInfo info = (VirtualDiskFlatVer2BackingInfo)backing;
    return info.getFileName();
}
 
Example #15
Source File: VmwareProcessClient.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
protected VirtualDisk reuseDisk(VirtualMachine machine, Integer scsiId, String fileName) {
    // 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.setControllerKey(scsiController.getKey());

    // VirtualDiskFlatVer2BackingInfo
    VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo();
    backingInfo.setFileName(fileName);
    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 #16
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
/**
 * Get provisioning type for VM disk info
 */
private Storage.ProvisioningType getProvisioningType(VirtualDiskFlatVer2BackingInfo backing) {
    Boolean thinProvisioned = backing.isThinProvisioned();
    if (BooleanUtils.isTrue(thinProvisioned)) {
        return Storage.ProvisioningType.THIN;
    }
    return Storage.ProvisioningType.SPARSE;
}
 
Example #17
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
/**
 * Get root disk template path
 */
private String getRootDiskTemplatePath(VirtualDisk rootDisk) {
    VirtualDeviceBackingInfo backing = rootDisk.getBacking();
    checkBackingInfo(backing);
    VirtualDiskFlatVer2BackingInfo info = (VirtualDiskFlatVer2BackingInfo)backing;
    VirtualDiskFlatVer2BackingInfo parent = info.getParent();
    return (parent != null) ? getVolumeNameFromFileName(parent.getFileName()) : getVolumeNameFromFileName(info.getFileName());
}
 
Example #18
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
/**
 * Get pool ID for disk
 */
private Long getPoolId(VirtualDisk disk) {
    VirtualDeviceBackingInfo backing = disk.getBacking();
    checkBackingInfo(backing);
    VirtualDiskFlatVer2BackingInfo info = (VirtualDiskFlatVer2BackingInfo)backing;
    String[] fileNameParts = info.getFileName().split(" ");
    String datastoreUuid = StringUtils.substringBetween(fileNameParts[0], "[", "]");
    return getPoolIdFromDatastoreUuid(datastoreUuid);
}
 
Example #19
Source File: VmwareHelper.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public static String getDiskDeviceFileName(VirtualDisk diskDevice) {
    VirtualDeviceBackingInfo backingInfo = diskDevice.getBacking();
    if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
        final String vmdkName = ((VirtualDiskFlatVer2BackingInfo)backingInfo).getFileName().replace(".vmdk", "");
        if (vmdkName.contains("/")) {
            return vmdkName.split("/", 2)[1];
        }
        return vmdkName;
    }
    return null;
}
 
Example #20
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 #21
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 #22
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
/**
 * Check backing info
 */
private void checkBackingInfo(VirtualDeviceBackingInfo backingInfo) {
    if (!(backingInfo instanceof VirtualDiskFlatVer2BackingInfo)) {
        throw new CloudRuntimeException("Unsopported backing, expected " + VirtualDiskFlatVer2BackingInfo.class.getSimpleName());
    }
}
 
Example #23
Source File: VmwareHelper.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
public static ManagedObjectReference getDiskDeviceDatastore(VirtualDisk diskDevice) throws Exception {
    VirtualDeviceBackingInfo backingInfo = diskDevice.getBacking();
    assert (backingInfo instanceof VirtualDiskFlatVer2BackingInfo);
    return ((VirtualDiskFlatVer2BackingInfo)backingInfo).getDatastore();
}
 
Example #24
Source File: VmwareHelper.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static VirtualDevice prepareDiskDevice(VirtualMachineMO vmMo, int controllerKey, Pair<String, ManagedObjectReference>[] vmdkDatastorePathChain,
        int deviceNumber, int contextNumber) throws Exception {

    assert (vmdkDatastorePathChain != null);
    assert (vmdkDatastorePathChain.length >= 1);

    VirtualDisk disk = new VirtualDisk();

    VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo();
    backingInfo.setDatastore(vmdkDatastorePathChain[0].second());
    backingInfo.setFileName(vmdkDatastorePathChain[0].first());
    backingInfo.setDiskMode(VirtualDiskMode.PERSISTENT.value());
    if (vmdkDatastorePathChain.length > 1) {
        Pair<String, ManagedObjectReference>[] parentDisks = new Pair[vmdkDatastorePathChain.length - 1];
        for (int i = 0; i < vmdkDatastorePathChain.length - 1; i++)
            parentDisks[i] = vmdkDatastorePathChain[i + 1];

        setParentBackingInfo(backingInfo, parentDisks);
    }

    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);

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

    return disk;
}
 
Example #25
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
public void attachDisk(String[] vmdkDatastorePathChain, ManagedObjectReference morDs, String diskController) 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());
        int controllerKey = 0;
        int unitNumber = 0;

        if (DiskControllerType.getType(diskController) == DiskControllerType.ide) {
            // IDE virtual disk cannot be added if VM is running
            if (getPowerState() == VirtualMachinePowerState.POWERED_ON) {
                throw new Exception("Adding a virtual disk over IDE controller is not supported while VM is running in VMware hypervisor. Please re-try when VM is not running.");
            }
            // Get next available unit number and controller key
            int ideDeviceCount = getNumberOfIDEDevices();
            if (ideDeviceCount >= VmwareHelper.MAX_IDE_CONTROLLER_COUNT * VmwareHelper.MAX_ALLOWED_DEVICES_IDE_CONTROLLER) {
                throw new Exception("Maximum limit of  devices supported on IDE controllers [" + VmwareHelper.MAX_IDE_CONTROLLER_COUNT
                        * VmwareHelper.MAX_ALLOWED_DEVICES_IDE_CONTROLLER + "] is reached.");
            }
            controllerKey = getIDEControllerKey(ideDeviceCount);
            unitNumber = getFreeUnitNumberOnIDEController(controllerKey);
        } else {
            controllerKey = getScsiDiskControllerKey(diskController);
            unitNumber = -1;
        }
        synchronized (_mor.getValue().intern()) {
            VirtualDevice newDisk = VmwareHelper.prepareDiskDevice(this, null, controllerKey, vmdkDatastorePathChain, morDs, unitNumber, 1);
            controllerKey = newDisk.getControllerKey();
            unitNumber = newDisk.getUnitNumber();
            VirtualDiskFlatVer2BackingInfo backingInfo = (VirtualDiskFlatVer2BackingInfo)newDisk.getBacking();
            String vmdkFileName = backingInfo.getFileName();
            DiskControllerType diskControllerType = DiskControllerType.getType(diskController);
            VmdkAdapterType vmdkAdapterType = VmdkAdapterType.getAdapterType(diskControllerType);
            if (vmdkAdapterType == VmdkAdapterType.none) {
                String message = "Failed to attach disk due to invalid vmdk adapter type for vmdk file [" +
                    vmdkFileName + "] with controller : " + diskControllerType;
                s_logger.debug(message);
                throw new Exception(message);
            }
            updateVmdkAdapter(vmdkFileName, vmdkAdapterType.toString());
            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 #26
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
public List<String> detachAllDisksExcept(String vmdkBaseName, String deviceBusName) throws Exception {
    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");

    VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
    List<String> detachedDiskFiles = new ArrayList<String>();

    for (VirtualDevice device : devices) {
        if (device instanceof VirtualDisk) {
            VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();

            VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)device.getBacking();

            DatastoreFile dsBackingFile = new DatastoreFile(diskBackingInfo.getFileName());
            String backingBaseName = dsBackingFile.getFileBaseName();
            String deviceNumbering = getDeviceBusName(devices, device);
            if (backingBaseName.equalsIgnoreCase(vmdkBaseName) || (deviceBusName != null && deviceBusName.equals(deviceNumbering))) {
                continue;
            } else {
                s_logger.info("Detach " + diskBackingInfo.getFileName() + " from " + getName());

                detachedDiskFiles.add(diskBackingInfo.getFileName());

                deviceConfigSpec.setDevice(device);
                deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);

                reConfigSpec.getDeviceChange().add(deviceConfigSpec);
            }
        }
    }

    if (detachedDiskFiles.size() > 0) {
        ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec);
        boolean result = _context.getVimClient().waitForTask(morTask);
        if (result) {
            _context.waitForTaskProgressDone(morTask);
        } else {
            s_logger.warn("Unable to reconfigure the VM to detach disks");
            throw new Exception("Unable to reconfigure the VM to detach disks");
        }
    }

    return detachedDiskFiles;
}
 
Example #27
Source File: VmwareStorageProcessor.java    From cloudstack with Apache License 2.0 2 votes vote down vote up
/**
 * 1) Possibly expand the datastore.
 * 2) Possibly consolidate all relevant VMDK files into one VMDK file.
 * 3) Possibly move the VMDK file to the root folder (may already be there).
 * 4) If the VMDK file wasn't already in the root folder, then delete the folder the VMDK file was in.
 * 5) Possibly rename the VMDK file (this will lead to there being a delta file with the new name and the
 *    original file with the original name).
 *
 * Note: If the underlying VMDK file was for a root disk, the 'vmdk' parameter's value might look, for example,
 *  like "i-2-32-VM/ROOT-32.vmdk".
 *
 * Note: If the underlying VMDK file was for a data disk, the 'vmdk' parameter's value might look, for example,
 *  like "-iqn.2010-01.com.solidfire:4nhe.data-32.79-0.vmdk".
 *
 * Returns the (potentially new) name of the VMDK file.
 */
private String cleanUpDatastore(Command cmd, HostDatastoreSystemMO hostDatastoreSystem, DatastoreMO dsMo, Map<String, String> details) throws Exception {
    boolean expandDatastore = Boolean.parseBoolean(details.get(DiskTO.EXPAND_DATASTORE));

    // A volume on the storage system holding a template uses a minimum hypervisor snapshot reserve value.
    // When this volume is cloned to a new volume, the new volume can be expanded (to take a new hypervisor snapshot reserve value
    // into consideration). If expandDatastore is true, we want to expand the datastore in the new volume to the size of the cloned volume.
    // It's possible that expandDatastore might be true and there isn't any extra space in the cloned volume (if the hypervisor snapshot
    // reserve value in use is set to the minimum for the cloned volume), but that's fine.
    if (expandDatastore) {
        expandDatastore(hostDatastoreSystem, dsMo);
    }

    String vmdk = details.get(DiskTO.VMDK);
    String fullVmdkPath = new DatastoreFile(dsMo.getName(), vmdk).getPath();

    VmwareContext context = hostService.getServiceContext(null);
    VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);

    DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());

    String vmName = getVmName(vmdk);

    // If vmName is not null, then move all VMDK files out of this folder to the root folder and then delete the folder named vmName.
    if (vmName != null) {
        String workerVmName = hostService.getWorkerName(context, cmd, 0);

        VirtualMachineMO vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVmName);

        if (vmMo == null) {
            throw new Exception("Unable to create a worker VM for volume creation");
        }

        vmMo.attachDisk(new String[] { fullVmdkPath }, dsMo.getMor());

        List<String> backingFiles = new ArrayList<>(1);

        List<VirtualDisk> virtualDisks = vmMo.getVirtualDisks();

        VirtualDisk virtualDisk = virtualDisks.get(0);

        VirtualDeviceBackingInfo virtualDeviceBackingInfo = virtualDisk.getBacking();

        while (virtualDeviceBackingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
            VirtualDiskFlatVer2BackingInfo backingInfo = (VirtualDiskFlatVer2BackingInfo)virtualDeviceBackingInfo;

            backingFiles.add(backingInfo.getFileName());

            virtualDeviceBackingInfo = backingInfo.getParent();
        }

        vmMo.detachAllDisks();
        vmMo.destroy();

        VmwareStorageLayoutHelper.moveVolumeToRootFolder(dcMo, backingFiles);

        vmdk = new DatastoreFile(vmdk).getFileName();

        // Delete the folder the VMDK file was in.

        DatastoreFile folderToDelete = new DatastoreFile(dsMo.getName(), vmName);

        dsMo.deleteFolder(folderToDelete.getPath(), dcMo.getMor());
    }

    return vmdk;
}