Search in sources :

Example 11 with VirtualDiskFlatVer2BackingInfo

use of com.vmware.vim25.VirtualDiskFlatVer2BackingInfo in project cloudstack by apache.

the class VirtualMachineMO method getDiskDatastorePathChain.

@Deprecated
public List<Pair<String, ManagedObjectReference>> getDiskDatastorePathChain(VirtualDisk disk, boolean followChain) throws Exception {
    VirtualDeviceBackingInfo backingInfo = disk.getBacking();
    if (!(backingInfo instanceof VirtualDiskFlatVer2BackingInfo)) {
        throw new Exception("Unsupported VirtualDeviceBackingInfo");
    }
    List<Pair<String, ManagedObjectReference>> pathList = new ArrayList<Pair<String, ManagedObjectReference>>();
    VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo) backingInfo;
    if (!followChain) {
        pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
        return pathList;
    }
    Pair<DatacenterMO, String> dcPair = getOwnerDatacenter();
    VirtualMachineFileInfo vmFilesInfo = getFileInfo();
    DatastoreFile snapshotDirFile = new DatastoreFile(vmFilesInfo.getSnapshotDirectory());
    DatastoreFile vmxDirFile = new DatastoreFile(vmFilesInfo.getVmPathName());
    do {
        if (diskBackingInfo.getParent() != null) {
            pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
            diskBackingInfo = diskBackingInfo.getParent();
        } else {
            // try getting parent info from VMDK file itself
            byte[] content = null;
            try {
                String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), diskBackingInfo.getFileName());
                content = getContext().getResourceContent(url);
                if (content == null || content.length == 0) {
                    break;
                }
                pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
            } catch (Exception e) {
                // if snapshot directory has been changed to place other than default. VMware has a bug
                // that its corresponding disk backing info is not updated correctly. therefore, we will try search
                // in snapshot directory one more time
                DatastoreFile currentFile = new DatastoreFile(diskBackingInfo.getFileName());
                String vmdkFullDsPath = snapshotDirFile.getCompanionPath(currentFile.getFileName());
                String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), vmdkFullDsPath);
                content = getContext().getResourceContent(url);
                if (content == null || content.length == 0) {
                    break;
                }
                pathList.add(new Pair<String, ManagedObjectReference>(vmdkFullDsPath, diskBackingInfo.getDatastore()));
            }
            VmdkFileDescriptor descriptor = new VmdkFileDescriptor();
            descriptor.parse(content);
            if (descriptor.getParentFileName() != null && !descriptor.getParentFileName().isEmpty()) {
                // create a fake one
                VirtualDiskFlatVer2BackingInfo parentDiskBackingInfo = new VirtualDiskFlatVer2BackingInfo();
                parentDiskBackingInfo.setDatastore(diskBackingInfo.getDatastore());
                String parentFileName = descriptor.getParentFileName();
                if (parentFileName.startsWith("/")) {
                    int fileNameStartPos = parentFileName.lastIndexOf("/");
                    parentFileName = parentFileName.substring(fileNameStartPos + 1);
                    parentDiskBackingInfo.setFileName(vmxDirFile.getCompanionPath(parentFileName));
                } else {
                    parentDiskBackingInfo.setFileName(snapshotDirFile.getCompanionPath(parentFileName));
                }
                diskBackingInfo = parentDiskBackingInfo;
            } else {
                break;
            }
        }
    } while (diskBackingInfo != null);
    return pathList;
}
Also used : ArrayList(java.util.ArrayList) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Example 12 with VirtualDiskFlatVer2BackingInfo

use of com.vmware.vim25.VirtualDiskFlatVer2BackingInfo in project cloudstack by apache.

the class VirtualMachineMO method attachDisk.

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)");
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDevice(com.vmware.vim25.VirtualDevice) Gson(com.google.gson.Gson) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 13 with VirtualDiskFlatVer2BackingInfo

use of com.vmware.vim25.VirtualDiskFlatVer2BackingInfo in project cloudstack by apache.

the class VirtualMachineMO method getDiskCurrentTopBackingFileInChain.

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;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) VirtualDisk(com.vmware.vim25.VirtualDisk)

Example 14 with VirtualDiskFlatVer2BackingInfo

use of com.vmware.vim25.VirtualDiskFlatVer2BackingInfo in project cloudstack by apache.

the class VirtualMachineMO method getAllDiskDatastores.

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;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) ArrayList(java.util.ArrayList) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) VirtualDisk(com.vmware.vim25.VirtualDisk) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Example 15 with VirtualDiskFlatVer2BackingInfo

use of com.vmware.vim25.VirtualDiskFlatVer2BackingInfo in project cloudstack by apache.

the class VirtualMachineMO method getAllIndependentDiskDevice.

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]);
}
Also used : VirtualDiskSparseVer2BackingInfo(com.vmware.vim25.VirtualDiskSparseVer2BackingInfo) VirtualDiskFlatVer1BackingInfo(com.vmware.vim25.VirtualDiskFlatVer1BackingInfo) VirtualDiskSparseVer1BackingInfo(com.vmware.vim25.VirtualDiskSparseVer1BackingInfo) VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) ArrayList(java.util.ArrayList) VirtualDiskRawDiskMappingVer1BackingInfo(com.vmware.vim25.VirtualDiskRawDiskMappingVer1BackingInfo) VirtualDisk(com.vmware.vim25.VirtualDisk)

Aggregations

VirtualDiskFlatVer2BackingInfo (com.vmware.vim25.VirtualDiskFlatVer2BackingInfo)29 VirtualDisk (com.vmware.vim25.VirtualDisk)20 VirtualDeviceBackingInfo (com.vmware.vim25.VirtualDeviceBackingInfo)11 Pair (com.cloud.utils.Pair)9 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)8 VirtualDeviceConnectInfo (com.vmware.vim25.VirtualDeviceConnectInfo)8 ArrayList (java.util.ArrayList)8 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)7 VirtualDevice (com.vmware.vim25.VirtualDevice)7 VirtualDiskRawDiskMappingVer1BackingInfo (com.vmware.vim25.VirtualDiskRawDiskMappingVer1BackingInfo)6 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)5 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)5 VirtualDiskFlatVer1BackingInfo (com.vmware.vim25.VirtualDiskFlatVer1BackingInfo)4 VirtualDiskSparseVer1BackingInfo (com.vmware.vim25.VirtualDiskSparseVer1BackingInfo)4 VirtualDiskSparseVer2BackingInfo (com.vmware.vim25.VirtualDiskSparseVer2BackingInfo)4 IOException (java.io.IOException)3 VirtualMachineFileInfo (com.vmware.vim25.VirtualMachineFileInfo)2 ResizeVolumeAnswer (com.cloud.agent.api.storage.ResizeVolumeAnswer)1 CloudException (com.cloud.exception.CloudException)1 InternalErrorException (com.cloud.exception.InternalErrorException)1