Search in sources :

Example 46 with VirtualDisk

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

the class VmwareHelper method prepareDiskDevice.

@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;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDeviceConnectInfo(com.vmware.vim25.VirtualDeviceConnectInfo) VirtualDisk(com.vmware.vim25.VirtualDisk) Pair(com.cloud.utils.Pair)

Example 47 with VirtualDisk

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

the class VirtualMachineMO method getVmdkFileBaseName.

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

Example 48 with VirtualDisk

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

the class VirtualMachineMO method getVirtualDisks.

public List<VirtualDisk> getVirtualDisks() throws Exception {
    List<VirtualDisk> virtualDisks = new ArrayList<VirtualDisk>();
    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
    for (VirtualDevice device : devices) {
        if (device instanceof VirtualDisk) {
            virtualDisks.add((VirtualDisk) device);
        }
    }
    return virtualDisks;
}
Also used : ArrayList(java.util.ArrayList) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualDisk(com.vmware.vim25.VirtualDisk)

Example 49 with VirtualDisk

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

the class VmwareStorageProcessor method deleteVolume.

@Override
public Answer deleteVolume(DeleteCommand cmd) {
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource DeleteCommand: " + _gson.toJson(cmd));
    }
    try {
        VmwareContext context = hostService.getServiceContext(null);
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
        VolumeObjectTO vol = (VolumeObjectTO) cmd.getData();
        DataStoreTO store = vol.getDataStore();
        PrimaryDataStoreTO primaryDataStoreTO = (PrimaryDataStoreTO) store;
        Map<String, String> details = primaryDataStoreTO.getDetails();
        boolean isManaged = false;
        String managedDatastoreName = null;
        if (details != null) {
            isManaged = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));
            if (isManaged) {
                managedDatastoreName = getManagedDatastoreNameFromPath(vol.getPath());
            }
        }
        ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, isManaged ? managedDatastoreName : store.getUuid());
        if (morDs == null) {
            String msg = "Unable to find datastore based on volume mount point " + store.getUuid();
            s_logger.error(msg);
            throw new Exception(msg);
        }
        DatastoreMO dsMo = new DatastoreMO(context, morDs);
        ManagedObjectReference morDc = hyperHost.getHyperHostDatacenter();
        ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
        ClusterMO clusterMo = new ClusterMO(context, morCluster);
        if (vol.getVolumeType() == Volume.Type.ROOT) {
            String vmName = vol.getVmName();
            if (vmName != null) {
                VirtualMachineMO vmMo = clusterMo.findVmOnHyperHost(vmName);
                if (vmMo == null) {
                    // Volume might be on a zone-wide storage pool, look for VM in datacenter
                    DatacenterMO dcMo = new DatacenterMO(context, morDc);
                    vmMo = dcMo.findVm(vmName);
                }
                if (vmMo != null) {
                    if (s_logger.isInfoEnabled()) {
                        s_logger.info("Destroy root volume and VM itself. vmName " + vmName);
                    }
                    VirtualMachineDiskInfo diskInfo = null;
                    if (vol.getChainInfo() != null)
                        diskInfo = _gson.fromJson(vol.getChainInfo(), VirtualMachineDiskInfo.class);
                    HostMO hostMo = vmMo.getRunningHost();
                    List<NetworkDetails> networks = vmMo.getNetworksWithDetails();
                    // tear down all devices first before we destroy the VM to avoid accidently delete disk backing files
                    if (VmwareResource.getVmState(vmMo) != PowerState.PowerOff) {
                        vmMo.safePowerOff(_shutdownWaitMs);
                    }
                    // call this before calling detachAllDisksExcept
                    // when expunging a VM, we need to see if any of its disks are serviced by managed storage
                    // if there is one or more disk serviced by managed storage, remove the iSCSI connection(s)
                    // don't remove the iSCSI connection(s) until the supported disk(s) is/are removed from the VM
                    // (removeManagedTargetsFromCluster should be called after detachAllDisksExcept and vm.destroy)
                    List<VirtualDisk> virtualDisks = vmMo.getVirtualDisks();
                    List<String> managedIqns = getManagedIqnsFromVirtualDisks(virtualDisks);
                    List<String> detachedDisks = vmMo.detachAllDisksExcept(vol.getPath(), diskInfo != null ? diskInfo.getDiskDeviceBusName() : null);
                    VmwareStorageLayoutHelper.moveVolumeToRootFolder(new DatacenterMO(context, morDc), detachedDisks);
                    if (isManaged) {
                        vmMo.unregisterVm();
                    } else {
                        vmMo.destroy();
                    }
                    // this.hostService.handleDatastoreAndVmdkDetach(iScsiName, storageHost, storagePort);
                    if (managedIqns != null && !managedIqns.isEmpty()) {
                        removeManagedTargetsFromCluster(managedIqns);
                    }
                    for (NetworkDetails netDetails : networks) {
                        if (netDetails.getGCTag() != null && netDetails.getGCTag().equalsIgnoreCase("true")) {
                            if (netDetails.getVMMorsOnNetwork() == null || netDetails.getVMMorsOnNetwork().length == 1) {
                                resource.cleanupNetwork(hostMo, netDetails);
                            }
                        }
                    }
                }
                /*
                    if (s_logger.isInfoEnabled()) {
                        s_logger.info("Destroy volume by original name: " + vol.getPath() + ".vmdk");
                    }

                    VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vol.getPath(), new DatacenterMO(context, morDc));
                     */
                return new Answer(cmd, true, "Success");
            }
            if (s_logger.isInfoEnabled()) {
                s_logger.info("Destroy root volume directly from datastore");
            }
        }
        VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vol.getPath(), new DatacenterMO(context, morDc));
        return new Answer(cmd, true, "Success");
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            hostService.invalidateServiceContext(null);
        }
        String msg = "delete volume failed due to " + VmwareHelper.getExceptionMessage(e);
        s_logger.error(msg, e);
        return new Answer(cmd, false, msg);
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) NetworkDetails(com.cloud.hypervisor.vmware.mo.NetworkDetails) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) ClusterMO(com.cloud.hypervisor.vmware.mo.ClusterMO) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) VirtualDisk(com.vmware.vim25.VirtualDisk) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) ResignatureAnswer(org.apache.cloudstack.storage.command.ResignatureAnswer) Answer(com.cloud.agent.api.Answer) SnapshotAndCopyAnswer(org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) VirtualMachineDiskInfo(org.apache.cloudstack.utils.volume.VirtualMachineDiskInfo) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) RemoteException(java.rmi.RemoteException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Aggregations

VirtualDisk (com.vmware.vim25.VirtualDisk)41 VirtualDiskFlatVer2BackingInfo (com.vmware.vim25.VirtualDiskFlatVer2BackingInfo)24 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)20 VirtualDevice (com.vmware.vim25.VirtualDevice)17 ArrayList (java.util.ArrayList)17 Pair (com.cloud.utils.Pair)16 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)16 VirtualDeviceBackingInfo (com.vmware.vim25.VirtualDeviceBackingInfo)11 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)9 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)9 RemoteException (java.rmi.RemoteException)9 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)8 VirtualDeviceConnectInfo (com.vmware.vim25.VirtualDeviceConnectInfo)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 VirtualDiskRawDiskMappingVer1BackingInfo (com.vmware.vim25.VirtualDiskRawDiskMappingVer1BackingInfo)6 IOException (java.io.IOException)6 CloudException (com.cloud.exception.CloudException)4 InternalErrorException (com.cloud.exception.InternalErrorException)4 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)4