Search in sources :

Example 1 with VirtualMachineDiskInfoBuilder

use of com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder in project cloudstack by apache.

the class VmwareResource method postDiskConfigBeforeStart.

private void postDiskConfigBeforeStart(VirtualMachineMO vmMo, VirtualMachineTO vmSpec, DiskTO[] sortedDisks, int ideControllerKey, int scsiControllerKey, Map<String, String> iqnToPath, VmwareHypervisorHost hyperHost, VmwareContext context) throws Exception {
    VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
    for (DiskTO vol : sortedDisks) {
        if (vol.getType() == Volume.Type.ISO)
            continue;
        VolumeObjectTO volumeTO = (VolumeObjectTO) vol.getData();
        VirtualMachineDiskInfo diskInfo = getMatchingExistingDisk(diskInfoBuilder, vol, hyperHost, context);
        assert (diskInfo != null);
        String[] diskChain = diskInfo.getDiskChain();
        assert (diskChain.length > 0);
        Map<String, String> details = vol.getDetails();
        boolean managed = false;
        if (details != null) {
            managed = Boolean.parseBoolean(details.get(DiskTO.MANAGED));
        }
        DatastoreFile file = new DatastoreFile(diskChain[0]);
        if (managed) {
            DatastoreFile originalFile = new DatastoreFile(volumeTO.getPath());
            if (!file.getFileBaseName().equalsIgnoreCase(originalFile.getFileBaseName())) {
                if (s_logger.isInfoEnabled())
                    s_logger.info("Detected disk-chain top file change on volume: " + volumeTO.getId() + " " + volumeTO.getPath() + " -> " + diskChain[0]);
            }
        } else {
            if (!file.getFileBaseName().equalsIgnoreCase(volumeTO.getPath())) {
                if (s_logger.isInfoEnabled())
                    s_logger.info("Detected disk-chain top file change on volume: " + volumeTO.getId() + " " + volumeTO.getPath() + " -> " + file.getFileBaseName());
            }
        }
        VolumeObjectTO volInSpec = getVolumeInSpec(vmSpec, volumeTO);
        if (volInSpec != null) {
            if (managed) {
                String datastoreVolumePath = diskChain[0];
                iqnToPath.put(details.get(DiskTO.IQN), datastoreVolumePath);
                vol.setPath(datastoreVolumePath);
                volumeTO.setPath(datastoreVolumePath);
                volInSpec.setPath(datastoreVolumePath);
            } else {
                volInSpec.setPath(file.getFileBaseName());
            }
            volInSpec.setChainInfo(_gson.toJson(diskInfo));
        }
    }
}
Also used : DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) VirtualMachineDiskInfo(org.apache.cloudstack.utils.volume.VirtualMachineDiskInfo) VirtualMachineDiskInfoBuilder(com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 2 with VirtualMachineDiskInfoBuilder

use of com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder in project cloudstack by apache.

the class VmwareResource method getNewPathAndChainInfoInDatastoreCluster.

private Pair<String, String> getNewPathAndChainInfoInDatastoreCluster(VirtualMachineMO vmMo, String path, String chainInfo, boolean managed, String iscsiName, String poolUUID, String poolType) throws Exception {
    VmwareContext context = getServiceContext();
    VmwareHypervisorHost hyperHost = getHyperHost(context);
    if (poolType != null && poolType.equalsIgnoreCase(Storage.StoragePoolType.DatastoreCluster.toString())) {
        VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
        VirtualMachineDiskInfo matchingExistingDisk = getMatchingExistingDiskWithVolumeDetails(diskInfoBuilder, path, chainInfo, managed, iscsiName, poolUUID, hyperHost, context);
        if (diskInfoBuilder != null && matchingExistingDisk != null) {
            String[] diskChain = matchingExistingDisk.getDiskChain();
            DatastoreFile file = new DatastoreFile(diskChain[0]);
            if (!file.getFileBaseName().equalsIgnoreCase(path)) {
                if (s_logger.isInfoEnabled())
                    s_logger.info("Detected disk-chain top file change on volume: " + path + " -> " + file.getFileBaseName());
                path = file.getFileBaseName();
                chainInfo = _gson.toJson(matchingExistingDisk);
                return new Pair<>(path, chainInfo);
            }
        }
    }
    return null;
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) VirtualMachineDiskInfo(org.apache.cloudstack.utils.volume.VirtualMachineDiskInfo) VirtualMachineDiskInfoBuilder(com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) Pair(com.cloud.utils.Pair)

Example 3 with VirtualMachineDiskInfoBuilder

use of com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder 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 4 with VirtualMachineDiskInfoBuilder

use of com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder in project cloudstack by apache.

the class VMwareGuru method getDiskInfo.

private VirtualMachineDiskInfo getDiskInfo(VirtualMachineMO vmMo, Long poolId, String volumeName) throws Exception {
    VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
    String poolName = _storagePoolDao.findById(poolId).getUuid().replace("-", "");
    return diskInfoBuilder.getDiskInfoByBackingFileBaseName(volumeName, poolName);
}
Also used : VirtualMachineDiskInfoBuilder(com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder)

Example 5 with VirtualMachineDiskInfoBuilder

use of com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder in project cloudstack by apache.

the class VMwareUtil method getDiskInfoBuilder.

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

Aggregations

VirtualMachineDiskInfoBuilder (com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder)14 VirtualMachineDiskInfo (org.apache.cloudstack.utils.volume.VirtualMachineDiskInfo)11 DatastoreFile (com.cloud.hypervisor.vmware.mo.DatastoreFile)9 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)9 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)7 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)7 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)6 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)5 Pair (com.cloud.utils.Pair)5 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 RemoteException (java.rmi.RemoteException)5 DiskTO (com.cloud.agent.api.to.DiskTO)4 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)4 VirtualDisk (com.vmware.vim25.VirtualDisk)4 ArrayList (java.util.ArrayList)4 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)3 CloudException (com.cloud.exception.CloudException)3 InternalErrorException (com.cloud.exception.InternalErrorException)3