Search in sources :

Example 16 with DatastoreMO

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

the class VmwareResource method getIsoDatastoreInfo.

// isoUrl sample content :
// nfs://192.168.10.231/export/home/kelven/vmware-test/secondary/template/tmpl/2/200//200-2-80f7ee58-6eff-3a2d-bcb0-59663edf6d26.iso
private Pair<String, ManagedObjectReference> getIsoDatastoreInfo(VmwareHypervisorHost hyperHost, String isoUrl) throws Exception {
    assert (isoUrl != null);
    int isoFileNameStartPos = isoUrl.lastIndexOf("/");
    if (isoFileNameStartPos < 0) {
        throw new Exception("Invalid ISO path info");
    }
    String isoFileName = isoUrl.substring(isoFileNameStartPos);
    int templateRootPos = isoUrl.indexOf("template/tmpl");
    if (templateRootPos < 0) {
        throw new Exception("Invalid ISO path info");
    }
    String storeUrl = isoUrl.substring(0, templateRootPos - 1);
    String isoPath = isoUrl.substring(templateRootPos, isoFileNameStartPos);
    ManagedObjectReference morDs = prepareSecondaryDatastoreOnHost(storeUrl);
    DatastoreMO dsMo = new DatastoreMO(getServiceContext(), morDs);
    return new Pair<String, ManagedObjectReference>(String.format("[%s] %s%s", dsMo.getName(), isoPath, isoFileName), morDs);
}
Also used : 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) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Example 17 with DatastoreMO

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

the class VmwareStorageProcessor method attachIso.

private Answer attachIso(DiskTO disk, boolean isAttach, String vmName) {
    try {
        VmwareContext context = hostService.getServiceContext(null);
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null) {
            String msg = "Unable to find VM in vSphere to execute AttachIsoCommand, vmName: " + vmName;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
        NfsTO nfsImageStore = (NfsTO) iso.getDataStore();
        String storeUrl = null;
        if (nfsImageStore != null) {
            storeUrl = nfsImageStore.getUrl();
        }
        if (storeUrl == null) {
            if (!iso.getName().equalsIgnoreCase("vmware-tools.iso")) {
                String msg = "ISO store root url is not found in AttachIsoCommand";
                s_logger.error(msg);
                throw new Exception(msg);
            } else {
                if (isAttach) {
                    vmMo.mountToolsInstaller();
                } else {
                    try {
                        if (!vmMo.unmountToolsInstaller()) {
                            return new AttachAnswer("Failed to unmount vmware-tools installer ISO as the corresponding CDROM device is locked by VM. Please unmount the CDROM device inside the VM and ret-try.");
                        }
                    } catch (Throwable e) {
                        vmMo.detachIso(null);
                    }
                }
                return new AttachAnswer(disk);
            }
        }
        ManagedObjectReference morSecondaryDs = prepareSecondaryDatastoreOnHost(storeUrl);
        String isoPath = nfsImageStore.getUrl() + File.separator + iso.getPath();
        if (!isoPath.startsWith(storeUrl)) {
            assert (false);
            String msg = "ISO path does not start with the secondary storage root";
            s_logger.error(msg);
            throw new Exception(msg);
        }
        int isoNameStartPos = isoPath.lastIndexOf('/');
        String isoFileName = isoPath.substring(isoNameStartPos + 1);
        String isoStorePathFromRoot = isoPath.substring(storeUrl.length(), isoNameStartPos);
        // TODO, check if iso is already attached, or if there is a previous
        // attachment
        DatastoreMO secondaryDsMo = new DatastoreMO(context, morSecondaryDs);
        String storeName = secondaryDsMo.getName();
        String isoDatastorePath = String.format("[%s] %s/%s", storeName, isoStorePathFromRoot, isoFileName);
        if (isAttach) {
            vmMo.attachIso(isoDatastorePath, morSecondaryDs, true, false);
        } else {
            vmMo.detachIso(isoDatastorePath);
        }
        return new AttachAnswer(disk);
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            hostService.invalidateServiceContext(null);
        }
        if (isAttach) {
            String msg = "AttachIsoCommand(attach) failed due to " + VmwareHelper.getExceptionMessage(e);
            msg = msg + " Also check if your guest os is a supported version";
            s_logger.error(msg, e);
            return new AttachAnswer(msg);
        } else {
            String msg = "AttachIsoCommand(detach) failed due to " + VmwareHelper.getExceptionMessage(e);
            msg = msg + " Also check if your guest os is a supported version";
            s_logger.warn(msg, e);
            return new AttachAnswer(msg);
        }
    }
}
Also used : VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) NfsTO(com.cloud.agent.api.to.NfsTO) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) RemoteException(java.rmi.RemoteException) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 18 with DatastoreMO

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

the class VmwareStorageLayoutHelper method moveVolumeToRootFolder.

public static void moveVolumeToRootFolder(DatacenterMO dcMo, List<String> detachedDisks) throws Exception {
    if (detachedDisks.size() > 0) {
        for (String fileFullDsPath : detachedDisks) {
            DatastoreFile file = new DatastoreFile(fileFullDsPath);
            s_logger.info("Check if we need to move " + fileFullDsPath + " to its root location");
            DatastoreMO dsMo = new DatastoreMO(dcMo.getContext(), dcMo.findDatastore(file.getDatastoreName()));
            if (dsMo.getMor() != null) {
                DatastoreFile targetFile = new DatastoreFile(file.getDatastoreName(), file.getFileName());
                if (!targetFile.getPath().equalsIgnoreCase(file.getPath())) {
                    s_logger.info("Move " + file.getPath() + " -> " + targetFile.getPath());
                    dsMo.moveDatastoreFile(file.getPath(), dcMo.getMor(), dsMo.getMor(), targetFile.getPath(), dcMo.getMor(), true);
                    String pairSrcFilePath = file.getCompanionPath(file.getFileBaseName() + "-flat.vmdk");
                    String pairTargetFilePath = targetFile.getCompanionPath(file.getFileBaseName() + "-flat.vmdk");
                    if (dsMo.fileExists(pairSrcFilePath)) {
                        s_logger.info("Move " + pairSrcFilePath + " -> " + pairTargetFilePath);
                        dsMo.moveDatastoreFile(pairSrcFilePath, dcMo.getMor(), dsMo.getMor(), pairTargetFilePath, dcMo.getMor(), true);
                    }
                    pairSrcFilePath = file.getCompanionPath(file.getFileBaseName() + "-delta.vmdk");
                    pairTargetFilePath = targetFile.getCompanionPath(file.getFileBaseName() + "-delta.vmdk");
                    if (dsMo.fileExists(pairSrcFilePath)) {
                        s_logger.info("Move " + pairSrcFilePath + " -> " + pairTargetFilePath);
                        dsMo.moveDatastoreFile(pairSrcFilePath, dcMo.getMor(), dsMo.getMor(), pairTargetFilePath, dcMo.getMor(), true);
                    }
                }
            } else {
                s_logger.warn("Datastore for " + fileFullDsPath + " no longer exists, we have to skip");
            }
        }
    }
}
Also used : DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO)

Example 19 with DatastoreMO

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

the class VmwareStorageProcessor method backupSnapshot.

@Override
public Answer backupSnapshot(CopyCommand cmd) {
    SnapshotObjectTO srcSnapshot = (SnapshotObjectTO) cmd.getSrcTO();
    DataStoreTO primaryStore = srcSnapshot.getDataStore();
    SnapshotObjectTO destSnapshot = (SnapshotObjectTO) cmd.getDestTO();
    DataStoreTO destStore = destSnapshot.getDataStore();
    if (!(destStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    NfsTO destNfsStore = (NfsTO) destStore;
    String secondaryStorageUrl = destNfsStore.getUrl();
    String snapshotUuid = srcSnapshot.getPath();
    String prevSnapshotUuid = srcSnapshot.getParentSnapshotPath();
    String prevBackupUuid = destSnapshot.getParentSnapshotPath();
    VirtualMachineMO workerVm = null;
    String workerVMName = null;
    String volumePath = srcSnapshot.getVolume().getPath();
    ManagedObjectReference morDs = null;
    DatastoreMO dsMo = null;
    // By default assume failure
    String details = null;
    boolean success = false;
    String snapshotBackupUuid = null;
    boolean hasOwnerVm = false;
    Ternary<String, String, String[]> backupResult = null;
    VmwareContext context = hostService.getServiceContext(cmd);
    VirtualMachineMO vmMo = null;
    String vmName = srcSnapshot.getVmName();
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, primaryStore.getUuid());
        CopyCmdAnswer answer = null;
        try {
            if (vmName != null) {
                vmMo = hyperHost.findVmOnHyperHost(vmName);
                if (vmMo == null) {
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Unable to find owner VM for BackupSnapshotCommand on host " + hyperHost.getHyperHostName() + ", will try within datacenter");
                    }
                    vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
                }
            }
            if (vmMo == null) {
                dsMo = new DatastoreMO(hyperHost.getContext(), morDs);
                workerVMName = hostService.getWorkerName(context, cmd, 0);
                vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVMName);
                if (vmMo == null) {
                    throw new Exception("Failed to find the newly create or relocated VM. vmName: " + workerVMName);
                }
                workerVm = vmMo;
                // attach volume to worker VM
                String datastoreVolumePath = dsMo.getDatastorePath(volumePath + ".vmdk");
                vmMo.attachDisk(new String[] { datastoreVolumePath }, morDs);
            } else {
                s_logger.info("Using owner VM " + vmName + " for snapshot operation");
                hasOwnerVm = true;
            }
            if (!vmMo.createSnapshot(snapshotUuid, "Snapshot taken for " + srcSnapshot.getName(), false, false)) {
                throw new Exception("Failed to take snapshot " + srcSnapshot.getName() + " on vm: " + vmName);
            }
            backupResult = backupSnapshotToSecondaryStorage(vmMo, destSnapshot.getPath(), srcSnapshot.getVolume().getPath(), snapshotUuid, secondaryStorageUrl, prevSnapshotUuid, prevBackupUuid, hostService.getWorkerName(context, cmd, 1), _nfsVersion);
            snapshotBackupUuid = backupResult.first();
            success = (snapshotBackupUuid != null);
            if (!success) {
                details = "Failed to backUp the snapshot with uuid: " + snapshotUuid + " to secondary storage.";
                answer = new CopyCmdAnswer(details);
            } else {
                details = "Successfully backedUp the snapshot with Uuid: " + snapshotUuid + " to secondary storage.";
                // Get snapshot physical size
                long physicalSize = 0l;
                String secondaryMountPoint = mountService.getMountPoint(secondaryStorageUrl, _nfsVersion);
                String snapshotDir = destSnapshot.getPath() + "/" + snapshotBackupUuid;
                File[] files = new File(secondaryMountPoint + "/" + snapshotDir).listFiles();
                if (files != null) {
                    for (File file : files) {
                        String fileName = file.getName();
                        if (fileName.toLowerCase().startsWith(snapshotBackupUuid) && fileName.toLowerCase().endsWith(".vmdk")) {
                            physicalSize = new File(secondaryMountPoint + "/" + snapshotDir + "/" + fileName).length();
                            break;
                        }
                    }
                }
                SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
                newSnapshot.setPath(snapshotDir + "/" + snapshotBackupUuid);
                newSnapshot.setPhysicalSize(physicalSize);
                answer = new CopyCmdAnswer(newSnapshot);
            }
        } finally {
            if (vmMo != null) {
                ManagedObjectReference snapshotMor = vmMo.getSnapshotMor(snapshotUuid);
                if (snapshotMor != null) {
                    vmMo.removeSnapshot(snapshotUuid, false);
                    // in the middle
                    if (backupResult != null && hasOwnerVm) {
                        s_logger.info("Check if we have disk consolidation after snapshot operation");
                        boolean chainConsolidated = false;
                        for (String vmdkDsFilePath : backupResult.third()) {
                            s_logger.info("Validate disk chain file:" + vmdkDsFilePath);
                            if (vmMo.getDiskDevice(vmdkDsFilePath) == null) {
                                s_logger.info("" + vmdkDsFilePath + " no longer exists, consolidation detected");
                                chainConsolidated = true;
                                break;
                            } else {
                                s_logger.info("" + vmdkDsFilePath + " is found still in chain");
                            }
                        }
                        if (chainConsolidated) {
                            String topVmdkFilePath = null;
                            try {
                                topVmdkFilePath = vmMo.getDiskCurrentTopBackingFileInChain(backupResult.second());
                            } catch (Exception e) {
                                s_logger.error("Unexpected exception", e);
                            }
                            s_logger.info("Disk has been consolidated, top VMDK is now: " + topVmdkFilePath);
                            if (topVmdkFilePath != null) {
                                DatastoreFile file = new DatastoreFile(topVmdkFilePath);
                                SnapshotObjectTO snapshotInfo = (SnapshotObjectTO) answer.getNewData();
                                VolumeObjectTO vol = new VolumeObjectTO();
                                vol.setUuid(srcSnapshot.getVolume().getUuid());
                                vol.setPath(file.getFileBaseName());
                                snapshotInfo.setVolume(vol);
                            } else {
                                s_logger.error("Disk has been consolidated, but top VMDK is not found ?!");
                            }
                        }
                    }
                } else {
                    s_logger.error("Can not find the snapshot we just used ?!");
                }
            }
            try {
                if (workerVm != null) {
                    // detach volume and destroy worker vm
                    workerVm.detachAllDisks();
                    workerVm.destroy();
                }
            } catch (Throwable e) {
                s_logger.warn("Failed to destroy worker VM: " + workerVMName);
            }
        }
        return answer;
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            hostService.invalidateServiceContext(context);
        }
        s_logger.error("Unexpecpted exception ", e);
        details = "backup snapshot exception: " + VmwareHelper.getExceptionMessage(e);
        return new CopyCmdAnswer(details);
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) NfsTO(com.cloud.agent.api.to.NfsTO) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) RemoteException(java.rmi.RemoteException) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) File(java.io.File) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 20 with DatastoreMO

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

the class VmwareStorageProcessor method copyVolumeToSecStorage.

private Pair<String, String> copyVolumeToSecStorage(VmwareHostService hostService, VmwareHypervisorHost hyperHost, CopyCommand cmd, String vmName, String poolId, String volumePath, String destVolumePath, String secStorageUrl, String workerVmName) throws Exception {
    VirtualMachineMO workerVm = null;
    VirtualMachineMO vmMo = null;
    String exportName = UUID.randomUUID().toString().replace("-", "");
    try {
        ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, poolId);
        if (morDs == null) {
            String msg = "Unable to find volumes's storage pool for copy volume operation";
            s_logger.error(msg);
            throw new Exception(msg);
        }
        vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null || VmwareResource.getVmState(vmMo) == PowerState.PowerOff) {
            // create a dummy worker vm for attaching the volume
            DatastoreMO dsMo = new DatastoreMO(hyperHost.getContext(), morDs);
            workerVm = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVmName);
            if (workerVm == null) {
                String msg = "Unable to create worker VM to execute CopyVolumeCommand";
                s_logger.error(msg);
                throw new Exception(msg);
            }
            // attach volume to worker VM
            String datastoreVolumePath = getVolumePathInDatastore(dsMo, volumePath + ".vmdk");
            workerVm.attachDisk(new String[] { datastoreVolumePath }, morDs);
            vmMo = workerVm;
        }
        vmMo.createSnapshot(exportName, "Temporary snapshot for copy-volume command", false, false);
        exportVolumeToSecondaryStroage(vmMo, volumePath, secStorageUrl, destVolumePath, exportName, hostService.getWorkerName(hyperHost.getContext(), cmd, 1), _nfsVersion);
        return new Pair<String, String>(destVolumePath, exportName);
    } finally {
        vmMo.removeSnapshot(exportName, false);
        if (workerVm != null) {
            //detach volume and destroy worker vm
            workerVm.detachAllDisks();
            workerVm.destroy();
        }
    }
}
Also used : VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Aggregations

DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)32 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)29 RemoteException (java.rmi.RemoteException)27 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)25 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)21 UnsupportedEncodingException (java.io.UnsupportedEncodingException)21 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)16 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)16 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)12 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)12 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)12 CloudException (com.cloud.exception.CloudException)11 InternalErrorException (com.cloud.exception.InternalErrorException)11 IOException (java.io.IOException)11 ConnectException (java.net.ConnectException)11 ConfigurationException (javax.naming.ConfigurationException)11 Pair (com.cloud.utils.Pair)8 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)7 NfsTO (com.cloud.agent.api.to.NfsTO)6 DatastoreFile (com.cloud.hypervisor.vmware.mo.DatastoreFile)6