Search in sources :

Example 31 with Datastore

use of com.vmware.vim25.mo.Datastore in project cloudstack by apache.

the class HostMO method getHostLocalDatastore.

public ManagedObjectReference[] getHostLocalDatastore() throws Exception {
    List<ManagedObjectReference> datastores = _context.getVimClient().getDynamicProperty(_mor, "datastore");
    List<ManagedObjectReference> l = new ArrayList<ManagedObjectReference>();
    if (datastores != null) {
        for (ManagedObjectReference mor : datastores) {
            DatastoreSummary summary = (DatastoreSummary) _context.getVimClient().getDynamicProperty(mor, "summary");
            if (summary.getType().equalsIgnoreCase("VMFS") && !summary.isMultipleHostAccess())
                l.add(mor);
        }
    }
    return l.toArray(new ManagedObjectReference[1]);
}
Also used : DatastoreSummary(com.vmware.vim25.DatastoreSummary) ArrayList(java.util.ArrayList) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 32 with Datastore

use of com.vmware.vim25.mo.Datastore in project cloudstack by apache.

the class HostMO method getDatastorePropertiesOnHyperHost.

@Override
public ObjectContent[] getDatastorePropertiesOnHyperHost(String[] propertyPaths) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - retrieveProperties() on Datastore properties. target MOR: " + _mor.getValue() + ", properties: " + new Gson().toJson(propertyPaths));
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("Datastore");
    pSpec.getPathSet().addAll(Arrays.asList(propertyPaths));
    TraversalSpec host2DatastoreTraversal = new TraversalSpec();
    host2DatastoreTraversal.setType("HostSystem");
    host2DatastoreTraversal.setPath("datastore");
    host2DatastoreTraversal.setName("host2DatastoreTraversal");
    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(host2DatastoreTraversal);
    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.getPropSet().add(pSpec);
    pfSpec.getObjectSet().add(oSpec);
    List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
    pfSpecArr.add(pfSpec);
    List<ObjectContent> properties = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - retrieveProperties() done");
    return properties.toArray(new ObjectContent[properties.size()]);
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) ObjectContent(com.vmware.vim25.ObjectContent) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) TraversalSpec(com.vmware.vim25.TraversalSpec) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson)

Example 33 with Datastore

use of com.vmware.vim25.mo.Datastore 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 34 with Datastore

use of com.vmware.vim25.mo.Datastore in project cloudstack by apache.

the class VirtualMachineMO method detachDisk.

// vmdkDatastorePath: [datastore name] vmdkFilePath
public List<Pair<String, ManagedObjectReference>> detachDisk(String vmdkDatastorePath, boolean deleteBackingFile) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - detachDisk(). target MOR: " + _mor.getValue() + ", vmdkDatastorePath: " + vmdkDatastorePath + ", deleteBacking: " + deleteBackingFile);
    // Note: if VM has been taken snapshot, original backing file will be renamed, therefore, when we try to find the matching
    // VirtualDisk, we only perform prefix matching
    Pair<VirtualDisk, String> deviceInfo = getDiskDevice(vmdkDatastorePath);
    if (deviceInfo == null) {
        s_logger.warn("vCenter API trace - detachDisk() done (failed)");
        throw new Exception("No such disk device: " + vmdkDatastorePath);
    }
    // IDE virtual disk cannot be detached if VM is running
    if (deviceInfo.second() != null && deviceInfo.second().contains("ide")) {
        if (getPowerState() == VirtualMachinePowerState.POWERED_ON) {
            throw new Exception("Removing a virtual disk over IDE controller is not supported while VM is running in VMware hypervisor. " + "Please re-try when VM is not running.");
        }
    }
    List<Pair<String, ManagedObjectReference>> chain = getDiskDatastorePathChain(deviceInfo.first(), true);
    VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
    VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
    deviceConfigSpec.setDevice(deviceInfo.first());
    if (deleteBackingFile) {
        deviceConfigSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.DESTROY);
    }
    deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
    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 - detachDisk() done (failed)");
        throw new Exception("Failed to detach disk due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    _context.waitForTaskProgressDone(morTask);
    // VMware does not update snapshot references to the detached disk, we have to work around it
    SnapshotDescriptor snapshotDescriptor = null;
    try {
        snapshotDescriptor = getSnapshotDescriptor();
    } catch (Exception e) {
        s_logger.info("Unable to retrieve snapshot descriptor, will skip updating snapshot reference");
    }
    if (snapshotDescriptor != null) {
        for (Pair<String, ManagedObjectReference> pair : chain) {
            DatastoreFile dsFile = new DatastoreFile(pair.first());
            snapshotDescriptor.removeDiskReferenceFromSnapshot(dsFile.getFileName());
        }
        Pair<DatacenterMO, String> dcPair = getOwnerDatacenter();
        String dsPath = getSnapshotDescriptorDatastorePath();
        assert (dsPath != null);
        String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), dsPath);
        getContext().uploadResourceContent(url, snapshotDescriptor.getVmsdContent());
    }
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - detachDisk() done (successfully)");
    return chain;
}
Also used : VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDisk(com.vmware.vim25.VirtualDisk) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) Pair(com.cloud.utils.Pair) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 35 with Datastore

use of com.vmware.vim25.mo.Datastore in project cloudstack by apache.

the class VirtualMachineMO method getHttpAccessPathInfo.

/**
     * Retrieve path info to access VM files via vSphere web interface
     * @return [0] vm-name, [1] data-center-name, [2] datastore-name
     * @throws Exception
     */
public String[] getHttpAccessPathInfo() throws Exception {
    String[] pathInfo = new String[3];
    Pair<DatacenterMO, String> dcInfo = getOwnerDatacenter();
    VirtualMachineFileInfo fileInfo = getFileInfo();
    String vmxFilePath = fileInfo.getVmPathName();
    String[] vmxPathTokens = vmxFilePath.split("\\[|\\]|/");
    assert (vmxPathTokens.length == 4);
    // vSphere vm name
    pathInfo[1] = vmxPathTokens[1].trim();
    // vSphere datacenter name
    pathInfo[2] = dcInfo.second();
    // vSphere datastore name
    pathInfo[3] = vmxPathTokens[0].trim();
    return pathInfo;
}
Also used : VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)39 RemoteException (java.rmi.RemoteException)15 ArrayList (java.util.ArrayList)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)14 VirtualDisk (com.vmware.vim25.VirtualDisk)14 TraversalSpec (com.vmware.vim25.TraversalSpec)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)12 ObjectContent (com.vmware.vim25.ObjectContent)12 ObjectSpec (com.vmware.vim25.ObjectSpec)12 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)12 PropertySpec (com.vmware.vim25.PropertySpec)12 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)11 Pair (com.cloud.utils.Pair)10 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)10 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)10 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)10 VirtualDiskFlatVer2BackingInfo (com.vmware.vim25.VirtualDiskFlatVer2BackingInfo)9 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)9 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)8