Search in sources :

Example 1 with VmwareManager

use of com.cloud.hypervisor.vmware.manager.VmwareManager in project cloudstack by apache.

the class VmwareResource method execute.

protected Answer execute(CreatePrivateTemplateFromVolumeCommand cmd) {
    try {
        VmwareContext context = getServiceContext();
        VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
        return mgr.getStorageManager().execute(this, cmd);
    } catch (Throwable e) {
        return new CreatePrivateTemplateAnswer(cmd, false, createLogMessageException(e, cmd));
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VmwareManager(com.cloud.hypervisor.vmware.manager.VmwareManager) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer)

Example 2 with VmwareManager

use of com.cloud.hypervisor.vmware.manager.VmwareManager in project cloudstack by apache.

the class VmwareResource method relocateVirtualMachine.

/*
     * Method to relocate a virtual machine. This migrates VM and its volumes to given host, datastores.
     * It is used for MigrateVolumeCommand (detached volume case), MigrateVmToPoolCommand and MigrateVmWithStorageCommand.
     */
private List<VolumeObjectTO> relocateVirtualMachine(final VmwareHypervisorHost hypervisorHost, final String name, final VirtualMachineTO vmTo, final String targetHost, final VmwareHypervisorHost hostInTargetCluster, final String poolUuid, final List<Pair<VolumeTO, StorageFilerTO>> volToFiler) throws Exception {
    String vmName = name;
    if (vmName == null && vmTo != null) {
        vmName = vmTo.getName();
    }
    VmwareHypervisorHost sourceHyperHost = hypervisorHost;
    VmwareHypervisorHost targetHyperHost = hostInTargetCluster;
    VirtualMachineMO vmMo = null;
    ManagedObjectReference morSourceHostDc = null;
    VirtualMachineRelocateSpec relocateSpec = new VirtualMachineRelocateSpec();
    List<VirtualMachineRelocateSpecDiskLocator> diskLocators = new ArrayList<VirtualMachineRelocateSpecDiskLocator>();
    Set<String> mountedDatastoresAtSource = new HashSet<String>();
    List<VolumeObjectTO> volumeToList = new ArrayList<>();
    Map<Long, Integer> volumeDeviceKey = new HashMap<Long, Integer>();
    try {
        if (sourceHyperHost == null) {
            sourceHyperHost = getHyperHost(getServiceContext());
        }
        if (targetHyperHost == null && StringUtils.isNotBlank(targetHost)) {
            targetHyperHost = VmwareHelper.getHostMOFromHostName(getServiceContext(), targetHost);
        }
        morSourceHostDc = sourceHyperHost.getHyperHostDatacenter();
        DatacenterMO dcMo = new DatacenterMO(sourceHyperHost.getContext(), morSourceHostDc);
        if (targetHyperHost != null) {
            ManagedObjectReference morTargetHostDc = targetHyperHost.getHyperHostDatacenter();
            if (!morSourceHostDc.getValue().equalsIgnoreCase(morTargetHostDc.getValue())) {
                String msg = String.format("VM: %s cannot be migrated between different datacenter", vmName);
                throw new CloudRuntimeException(msg);
            }
        }
        // find VM through source host (VM is not at the target host yet)
        vmMo = sourceHyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null) {
            String msg = String.format("VM: %s does not exist on host: %s", vmName, sourceHyperHost.getHyperHostName());
            s_logger.warn(msg);
            // find VM through source host (VM is not at the target host yet)
            vmMo = dcMo.findVm(vmName);
            if (vmMo == null) {
                msg = String.format("VM: %s does not exist on datacenter: %s", vmName, dcMo.getName());
                s_logger.error(msg);
                throw new Exception(msg);
            }
            // VM host has changed
            sourceHyperHost = vmMo.getRunningHost();
        }
        vmName = vmMo.getName();
        String srcHostApiVersion = ((HostMO) sourceHyperHost).getHostAboutInfo().getApiVersion();
        if (StringUtils.isNotBlank(poolUuid)) {
            VmwareHypervisorHost dsHost = targetHyperHost == null ? sourceHyperHost : targetHyperHost;
            ManagedObjectReference morDatastore = null;
            morDatastore = getTargetDatastoreMOReference(poolUuid, dsHost);
            if (morDatastore == null) {
                String msg = String.format("Unable to find the target datastore: %s on host: %s to execute migration", poolUuid, dsHost.getHyperHostName());
                s_logger.error(msg);
                throw new CloudRuntimeException(msg);
            }
            relocateSpec.setDatastore(morDatastore);
        } else if (CollectionUtils.isNotEmpty(volToFiler)) {
            // Specify destination datastore location for each volume
            VmwareHypervisorHost dsHost = targetHyperHost == null ? sourceHyperHost : targetHyperHost;
            for (Pair<VolumeTO, StorageFilerTO> entry : volToFiler) {
                VolumeTO volume = entry.first();
                StorageFilerTO filerTo = entry.second();
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug(String.format("Preparing spec for volume: %s to migrate it to datastore: %s", volume.getName(), filerTo.getUuid()));
                }
                ManagedObjectReference morVolumeDatastore = getTargetDatastoreMOReference(filerTo.getUuid(), dsHost);
                if (morVolumeDatastore == null) {
                    String msg = String.format("Unable to find the target datastore: %s in datacenter: %s to execute migration", filerTo.getUuid(), dcMo.getName());
                    s_logger.error(msg);
                    throw new CloudRuntimeException(msg);
                }
                String mountedDs = getMountedDatastoreName(sourceHyperHost, srcHostApiVersion, filerTo);
                if (mountedDs != null) {
                    mountedDatastoresAtSource.add(mountedDs);
                }
                if (volume.getType() == Volume.Type.ROOT) {
                    relocateSpec.setDatastore(morVolumeDatastore);
                }
                VirtualMachineRelocateSpecDiskLocator diskLocator = new VirtualMachineRelocateSpecDiskLocator();
                diskLocator.setDatastore(morVolumeDatastore);
                Pair<VirtualDisk, String> diskInfo = getVirtualDiskInfo(vmMo, volume.getPath() + VMDK_EXTENSION);
                String vmdkAbsFile = getAbsoluteVmdkFile(diskInfo.first());
                if (vmdkAbsFile != null && !vmdkAbsFile.isEmpty()) {
                    vmMo.updateAdapterTypeIfRequired(vmdkAbsFile);
                }
                int diskId = diskInfo.first().getKey();
                diskLocator.setDiskId(diskId);
                diskLocators.add(diskLocator);
                volumeDeviceKey.put(volume.getId(), diskId);
            }
            // If a target datastore is provided for the VM, then by default all volumes associated with the VM will be migrated to that target datastore.
            // Hence set the existing datastore as target datastore for volumes that are not to be migrated.
            List<Pair<Integer, ManagedObjectReference>> diskDatastores = vmMo.getAllDiskDatastores();
            for (Pair<Integer, ManagedObjectReference> diskDatastore : diskDatastores) {
                if (!volumeDeviceKey.containsValue(diskDatastore.first().intValue())) {
                    VirtualMachineRelocateSpecDiskLocator diskLocator = new VirtualMachineRelocateSpecDiskLocator();
                    diskLocator.setDiskId(diskDatastore.first().intValue());
                    diskLocator.setDatastore(diskDatastore.second());
                    diskLocators.add(diskLocator);
                }
            }
            relocateSpec.getDisk().addAll(diskLocators);
        }
        // Specific section for MigrateVmWithStorageCommand
        if (vmTo != null) {
            // Prepare network at target before migration
            NicTO[] nics = vmTo.getNics();
            for (NicTO nic : nics) {
                // prepare network on the host
                prepareNetworkFromNicInfo((HostMO) targetHyperHost, nic, false, vmTo.getType());
            }
            // Ensure secondary storage mounted on target host
            VmwareManager mgr = targetHyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
            Pair<String, Long> secStoreUrlAndId = mgr.getSecondaryStorageStoreUrlAndId(Long.parseLong(_dcId));
            String secStoreUrl = secStoreUrlAndId.first();
            Long secStoreId = secStoreUrlAndId.second();
            if (secStoreUrl == null) {
                String msg = "secondary storage for dc " + _dcId + " is not ready yet?";
                throw new Exception(msg);
            }
            mgr.prepareSecondaryStorageStore(secStoreUrl, secStoreId);
            ManagedObjectReference morSecDs = prepareSecondaryDatastoreOnSpecificHost(secStoreUrl, targetHyperHost);
            if (morSecDs == null) {
                throw new Exception(String.format("Failed to prepare secondary storage on host, secondary store url: %s", secStoreUrl));
            }
        }
        if (srcHostApiVersion.compareTo("5.1") < 0) {
            // Migrate VM's volumes to target datastore(s).
            if (!vmMo.changeDatastore(relocateSpec)) {
                throw new Exception("Change datastore operation failed during storage migration");
            } else {
                s_logger.debug(String.format("Successfully migrated storage of VM: %s to target datastore(s)", vmName));
            }
            // Migrate VM to target host.
            if (targetHyperHost != null) {
                ManagedObjectReference morPool = targetHyperHost.getHyperHostOwnerResourcePool();
                if (!vmMo.migrate(morPool, targetHyperHost.getMor())) {
                    throw new Exception("VM migration to target host failed during storage migration");
                } else {
                    s_logger.debug(String.format("Successfully migrated VM: %s from host %s to %s", vmName, sourceHyperHost.getHyperHostName(), targetHyperHost.getHyperHostName()));
                }
            }
        } else {
            // Add target host to relocate spec
            if (targetHyperHost != null) {
                relocateSpec.setHost(targetHyperHost.getMor());
                relocateSpec.setPool(targetHyperHost.getHyperHostOwnerResourcePool());
            }
            if (!vmMo.changeDatastore(relocateSpec)) {
                throw new Exception("Change datastore operation failed during storage migration");
            } else {
                String msg = String.format("Successfully migrated VM: %s with its storage to target datastore(s)", vmName);
                if (targetHyperHost != null) {
                    msg = String.format("%s from host %s to %s", msg, sourceHyperHost.getHyperHostName(), targetHyperHost.getHyperHostName());
                }
                s_logger.debug(msg);
            }
        }
        // In case of a linked clone VM, if VM's disks are not consolidated, further VM operations such as volume snapshot, VM snapshot etc. will result in DB inconsistencies.
        if (!vmMo.consolidateVmDisks()) {
            s_logger.warn("VM disk consolidation failed after storage migration. Yet proceeding with VM migration.");
        } else {
            s_logger.debug(String.format("Successfully consolidated disks of VM: %s", vmName));
        }
        if (MapUtils.isNotEmpty(volumeDeviceKey)) {
            // Update and return volume path and chain info for every disk because that could have changed after migration
            VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
            for (Pair<VolumeTO, StorageFilerTO> entry : volToFiler) {
                final VolumeTO volume = entry.first();
                final long volumeId = volume.getId();
                VirtualDisk[] disks = vmMo.getAllDiskDevice();
                for (VirtualDisk disk : disks) {
                    if (volumeDeviceKey.get(volumeId) == disk.getKey()) {
                        VolumeObjectTO newVol = new VolumeObjectTO();
                        newVol.setDataStoreUuid(entry.second().getUuid());
                        String newPath = vmMo.getVmdkFileBaseName(disk);
                        ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(targetHyperHost != null ? targetHyperHost : sourceHyperHost, entry.second().getUuid());
                        DatastoreMO dsMo = new DatastoreMO(getServiceContext(), morDs);
                        VirtualMachineDiskInfo diskInfo = diskInfoBuilder.getDiskInfoByBackingFileBaseName(newPath, dsMo.getName());
                        newVol.setId(volumeId);
                        newVol.setPath(newPath);
                        newVol.setChainInfo(_gson.toJson(diskInfo));
                        volumeToList.add(newVol);
                        break;
                    }
                }
            }
        }
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encountered remote exception at vCenter, invalidating VMware session context");
            invalidateServiceContext();
        }
        throw e;
    } finally {
        // Cleanup datastores mounted on source host
        for (String mountedDatastore : mountedDatastoresAtSource) {
            s_logger.debug("Attempting to unmount datastore " + mountedDatastore + " at " + sourceHyperHost.getHyperHostName());
            try {
                sourceHyperHost.unmountDatastore(mountedDatastore);
            } catch (Exception unmountEx) {
                s_logger.warn("Failed to unmount datastore " + mountedDatastore + " at " + sourceHyperHost.getHyperHostName() + ". Seems the datastore is still being used by " + sourceHyperHost.getHyperHostName() + ". Please unmount manually to cleanup.");
            }
            s_logger.debug("Successfully unmounted datastore " + mountedDatastore + " at " + sourceHyperHost.getHyperHostName());
        }
    }
    // Only when volToFiler is not empty a filled list of VolumeObjectTO is returned else it will be empty
    return volumeToList;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) VolumeTO(com.cloud.agent.api.to.VolumeTO) VirtualMachineRelocateSpec(com.vmware.vim25.VirtualMachineRelocateSpec) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualMachineDiskInfo(org.apache.cloudstack.utils.volume.VirtualMachineDiskInfo) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Pair(com.cloud.utils.Pair) NicTO(com.cloud.agent.api.to.NicTO) VmwareManager(com.cloud.hypervisor.vmware.manager.VmwareManager) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VirtualMachineDiskInfoBuilder(com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) VirtualDisk(com.vmware.vim25.VirtualDisk) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) RemoteException(java.rmi.RemoteException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) VirtualMachineRelocateSpecDiskLocator(com.vmware.vim25.VirtualMachineRelocateSpecDiskLocator) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Example 3 with VmwareManager

use of com.cloud.hypervisor.vmware.manager.VmwareManager in project cloudstack by apache.

the class VmwareResource method execute.

protected Answer execute(GetVncPortCommand cmd) {
    try {
        VmwareContext context = getServiceContext();
        VmwareHypervisorHost hyperHost = getHyperHost(context);
        assert (hyperHost instanceof HostMO);
        VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getName());
        if (vmMo == null) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Unable to find the owner VM for GetVncPortCommand on host " + hyperHost.getHyperHostName() + ", try within datacenter");
            }
            vmMo = hyperHost.findVmOnPeerHyperHost(cmd.getName());
            if (vmMo == null) {
                throw new Exception("Unable to find VM in vSphere, vm: " + cmd.getName());
            }
        }
        Pair<String, Integer> portInfo = vmMo.getVncPort(mgr.getManagementPortGroupByHost((HostMO) hyperHost));
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("Found vnc port info. vm: " + cmd.getName() + " host: " + portInfo.first() + ", vnc port: " + portInfo.second());
        }
        return new GetVncPortAnswer(cmd, portInfo.first(), portInfo.second());
    } catch (Throwable e) {
        return new GetVncPortAnswer(cmd, createLogMessageException(e, cmd));
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) GetVncPortAnswer(com.cloud.agent.api.GetVncPortAnswer) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VmwareManager(com.cloud.hypervisor.vmware.manager.VmwareManager) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException)

Example 4 with VmwareManager

use of com.cloud.hypervisor.vmware.manager.VmwareManager in project cloudstack by apache.

the class VmwareResource method execute.

protected Answer execute(RevertToVMSnapshotCommand cmd) {
    try {
        VmwareContext context = getServiceContext();
        VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
        return mgr.getStorageManager().execute(this, cmd);
    } catch (Exception e) {
        createLogMessageException(e, cmd);
        return new RevertToVMSnapshotAnswer(cmd, false, "");
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) VmwareManager(com.cloud.hypervisor.vmware.manager.VmwareManager) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException)

Example 5 with VmwareManager

use of com.cloud.hypervisor.vmware.manager.VmwareManager in project cloudstack by apache.

the class VmwareResource method execute.

protected Answer execute(DeleteVMSnapshotCommand cmd) {
    try {
        VmwareContext context = getServiceContext();
        VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
        return mgr.getStorageManager().execute(this, cmd);
    } catch (Exception e) {
        createLogMessageException(e, cmd);
        return new DeleteVMSnapshotAnswer(cmd, false, "");
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VmwareManager(com.cloud.hypervisor.vmware.manager.VmwareManager) DeleteVMSnapshotAnswer(com.cloud.agent.api.DeleteVMSnapshotAnswer) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException)

Aggregations

VmwareManager (com.cloud.hypervisor.vmware.manager.VmwareManager)24 RemoteException (java.rmi.RemoteException)16 CloudException (com.cloud.exception.CloudException)14 InternalErrorException (com.cloud.exception.InternalErrorException)14 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)14 IOException (java.io.IOException)14 UnsupportedEncodingException (java.io.UnsupportedEncodingException)14 ConnectException (java.net.ConnectException)14 ConfigurationException (javax.naming.ConfigurationException)14 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)8 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)8 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)7 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)6 Pair (com.cloud.utils.Pair)5 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)4 ClusterMO (com.cloud.hypervisor.vmware.mo.ClusterMO)3 BackupSnapshotAnswer (com.cloud.agent.api.BackupSnapshotAnswer)2 CreateVMSnapshotAnswer (com.cloud.agent.api.CreateVMSnapshotAnswer)2 CreateVolumeFromSnapshotAnswer (com.cloud.agent.api.CreateVolumeFromSnapshotAnswer)2