Search in sources :

Example 1 with VirtualMachineMO

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

the class VmwareStorageManagerImpl method createTemplateFromVolume.

private Ternary<String, Long, Long> createTemplateFromVolume(VirtualMachineMO vmMo, long accountId, long templateId, String templateUniqueName, String secStorageUrl, String volumePath, String workerVmName, Integer nfsVersion) throws Exception {
    String secondaryMountPoint = _mountService.getMountPoint(secStorageUrl, nfsVersion);
    String installPath = getTemplateRelativeDirInSecStorage(accountId, templateId);
    String installFullPath = secondaryMountPoint + "/" + installPath;
    synchronized (installPath.intern()) {
        Script command = new Script(false, "mkdir", _timeout, s_logger);
        command.add("-p");
        command.add(installFullPath);
        String result = command.execute();
        if (result != null) {
            String msg = "unable to prepare template directory: " + installPath + ", storage: " + secStorageUrl + ", error msg: " + result;
            s_logger.error(msg);
            throw new Exception(msg);
        }
    }
    VirtualMachineMO clonedVm = null;
    try {
        Pair<VirtualDisk, String> volumeDeviceInfo = vmMo.getDiskDevice(volumePath);
        if (volumeDeviceInfo == null) {
            String msg = "Unable to find related disk device for volume. volume path: " + volumePath;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        if (!vmMo.createSnapshot(templateUniqueName, "Temporary snapshot for template creation", false, false)) {
            String msg = "Unable to take snapshot for creating template from volume. volume path: " + volumePath;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        // 4 MB is the minimum requirement for VM memory in VMware
        vmMo.cloneFromCurrentSnapshot(workerVmName, 0, 4, volumeDeviceInfo.second(), VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()));
        clonedVm = vmMo.getRunningHost().findVmOnHyperHost(workerVmName);
        if (clonedVm == null) {
            String msg = "Unable to create dummy VM to export volume. volume path: " + volumePath;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        clonedVm.exportVm(secondaryMountPoint + "/" + installPath, templateUniqueName, true, false);
        long physicalSize = new File(installFullPath + "/" + templateUniqueName + ".ova").length();
        OVAProcessor processor = new OVAProcessor();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(StorageLayer.InstanceConfigKey, _storage);
        processor.configure("OVA Processor", params);
        long virtualSize = processor.getTemplateVirtualSize(installFullPath, templateUniqueName);
        postCreatePrivateTemplate(installFullPath, templateId, templateUniqueName, physicalSize, virtualSize);
        return new Ternary<String, Long, Long>(installPath + "/" + templateUniqueName + ".ova", physicalSize, virtualSize);
    } finally {
        if (clonedVm != null) {
            clonedVm.detachAllDisks();
            clonedVm.destroy();
        }
        vmMo.removeSnapshot(templateUniqueName, false);
    }
}
Also used : Script(com.cloud.utils.script.Script) OVAProcessor(com.cloud.storage.template.OVAProcessor) HashMap(java.util.HashMap) Ternary(com.cloud.utils.Ternary) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) RemoteException(java.rmi.RemoteException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualDisk(com.vmware.vim25.VirtualDisk) File(java.io.File)

Example 2 with VirtualMachineMO

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

the class VmwareStorageManagerImpl method copyTemplateFromSecondaryToPrimary.

// templateName: name in secondary storage
// templateUuid: will be used at hypervisor layer
private void copyTemplateFromSecondaryToPrimary(VmwareHypervisorHost hyperHost, DatastoreMO datastoreMo, String secondaryStorageUrl, String templatePathAtSecondaryStorage, String templateName, String templateUuid, Integer nfsVersion) throws Exception {
    s_logger.info("Executing copyTemplateFromSecondaryToPrimary. secondaryStorage: " + secondaryStorageUrl + ", templatePathAtSecondaryStorage: " + templatePathAtSecondaryStorage + ", templateName: " + templateName);
    String secondaryMountPoint = _mountService.getMountPoint(secondaryStorageUrl, nfsVersion);
    s_logger.info("Secondary storage mount point: " + secondaryMountPoint);
    String srcOVAFileName = secondaryMountPoint + "/" + templatePathAtSecondaryStorage + templateName + "." + ImageFormat.OVA.getFileExtension();
    String srcFileName = getOVFFilePath(srcOVAFileName);
    if (srcFileName == null) {
        Script command = new Script("tar", 0, s_logger);
        command.add("--no-same-owner");
        command.add("-xf", srcOVAFileName);
        command.setWorkDir(secondaryMountPoint + "/" + templatePathAtSecondaryStorage);
        s_logger.info("Executing command: " + command.toString());
        String result = command.execute();
        if (result != null) {
            String msg = "Unable to unpack snapshot OVA file at: " + srcOVAFileName;
            s_logger.error(msg);
            throw new Exception(msg);
        }
    }
    srcFileName = getOVFFilePath(srcOVAFileName);
    if (srcFileName == null) {
        String msg = "Unable to locate OVF file in template package directory: " + srcOVAFileName;
        s_logger.error(msg);
        throw new Exception(msg);
    }
    String vmName = templateUuid;
    hyperHost.importVmFromOVF(srcFileName, vmName, datastoreMo, "thin");
    VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
    if (vmMo == null) {
        String msg = "Failed to import OVA template. secondaryStorage: " + secondaryStorageUrl + ", templatePathAtSecondaryStorage: " + templatePathAtSecondaryStorage + ", templateName: " + templateName + ", templateUuid: " + templateUuid;
        s_logger.error(msg);
        throw new Exception(msg);
    }
    if (vmMo.createSnapshot("cloud.template.base", "Base snapshot", false, false)) {
        vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_UUID, templateUuid);
        vmMo.markAsTemplate();
    } else {
        vmMo.destroy();
        String msg = "Unable to create base snapshot for template, templateName: " + templateName + ", templateUuid: " + templateUuid;
        s_logger.error(msg);
        throw new Exception(msg);
    }
}
Also used : Script(com.cloud.utils.script.Script) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) RemoteException(java.rmi.RemoteException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 3 with VirtualMachineMO

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

the class VmwareStorageManagerImpl method exportVolumeToSecondaryStroage.

private void exportVolumeToSecondaryStroage(VirtualMachineMO vmMo, String volumePath, String secStorageUrl, String secStorageDir, String exportName, String workerVmName, Integer nfsVersion) throws Exception {
    String secondaryMountPoint = _mountService.getMountPoint(secStorageUrl, nfsVersion);
    String exportPath = secondaryMountPoint + "/" + secStorageDir + "/" + exportName;
    synchronized (exportPath.intern()) {
        if (!new File(exportPath).exists()) {
            Script command = new Script(false, "mkdir", _timeout, s_logger);
            command.add("-p");
            command.add(exportPath);
            if (command.execute() != null) {
                throw new Exception("unable to prepare snapshot backup directory");
            }
        }
    }
    VirtualMachineMO clonedVm = null;
    try {
        Pair<VirtualDisk, String> volumeDeviceInfo = vmMo.getDiskDevice(volumePath);
        if (volumeDeviceInfo == null) {
            String msg = "Unable to find related disk device for volume. volume path: " + volumePath;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        // 4 MB is the minimum requirement for VM memory in VMware
        vmMo.cloneFromCurrentSnapshot(workerVmName, 0, 4, volumeDeviceInfo.second(), VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()));
        clonedVm = vmMo.getRunningHost().findVmOnHyperHost(workerVmName);
        if (clonedVm == null) {
            String msg = "Unable to create dummy VM to export volume. volume path: " + volumePath;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        //Note: volss: not to create ova.
        clonedVm.exportVm(exportPath, exportName, false, false);
    } finally {
        if (clonedVm != null) {
            clonedVm.detachAllDisks();
            clonedVm.destroy();
        }
    }
}
Also used : Script(com.cloud.utils.script.Script) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) File(java.io.File) RemoteException(java.rmi.RemoteException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualDisk(com.vmware.vim25.VirtualDisk)

Example 4 with VirtualMachineMO

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

the class VmwareStorageProcessor method copyTemplateFromSecondaryToPrimary.

private Pair<VirtualMachineMO, Long> copyTemplateFromSecondaryToPrimary(VmwareHypervisorHost hyperHost, DatastoreMO datastoreMo, String secondaryStorageUrl, String templatePathAtSecondaryStorage, String templateName, String templateUuid, boolean createSnapshot, Integer nfsVersion) throws Exception {
    s_logger.info("Executing copyTemplateFromSecondaryToPrimary. secondaryStorage: " + secondaryStorageUrl + ", templatePathAtSecondaryStorage: " + templatePathAtSecondaryStorage + ", templateName: " + templateName);
    String secondaryMountPoint = mountService.getMountPoint(secondaryStorageUrl, nfsVersion);
    s_logger.info("Secondary storage mount point: " + secondaryMountPoint);
    String srcOVAFileName = VmwareStorageLayoutHelper.getTemplateOnSecStorageFilePath(secondaryMountPoint, templatePathAtSecondaryStorage, templateName, ImageFormat.OVA.getFileExtension());
    String srcFileName = getOVFFilePath(srcOVAFileName);
    if (srcFileName == null) {
        Script command = new Script("tar", 0, s_logger);
        command.add("--no-same-owner");
        command.add("-xf", srcOVAFileName);
        command.setWorkDir(secondaryMountPoint + "/" + templatePathAtSecondaryStorage);
        s_logger.info("Executing command: " + command.toString());
        String result = command.execute();
        if (result != null) {
            String msg = "Unable to unpack snapshot OVA file at: " + srcOVAFileName;
            s_logger.error(msg);
            throw new Exception(msg);
        }
    }
    srcFileName = getOVFFilePath(srcOVAFileName);
    if (srcFileName == null) {
        String msg = "Unable to locate OVF file in template package directory: " + srcOVAFileName;
        s_logger.error(msg);
        throw new Exception(msg);
    }
    String vmName = templateUuid;
    hyperHost.importVmFromOVF(srcFileName, vmName, datastoreMo, "thin");
    VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
    if (vmMo == null) {
        String msg = "Failed to import OVA template. secondaryStorage: " + secondaryStorageUrl + ", templatePathAtSecondaryStorage: " + templatePathAtSecondaryStorage + ", templateName: " + templateName + ", templateUuid: " + templateUuid;
        s_logger.error(msg);
        throw new Exception(msg);
    }
    OVAProcessor processor = new OVAProcessor();
    Map<String, Object> params = new HashMap<String, Object>();
    params.put(StorageLayer.InstanceConfigKey, _storage);
    processor.configure("OVA Processor", params);
    long virtualSize = processor.getTemplateVirtualSize(secondaryMountPoint + "/" + templatePathAtSecondaryStorage, templateName);
    if (createSnapshot) {
        if (vmMo.createSnapshot("cloud.template.base", "Base snapshot", false, false)) {
            // the same template may be deployed with multiple copies at per-datastore per-host basis,
            // save the original template name from CloudStack DB as the UUID to associate them.
            vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_UUID, templateName);
            vmMo.markAsTemplate();
        } else {
            vmMo.destroy();
            String msg = "Unable to create base snapshot for template, templateName: " + templateName + ", templateUuid: " + templateUuid;
            s_logger.error(msg);
            throw new Exception(msg);
        }
    }
    return new Pair<VirtualMachineMO, Long>(vmMo, new Long(virtualSize));
}
Also used : Script(com.cloud.utils.script.Script) OVAProcessor(com.cloud.storage.template.OVAProcessor) HashMap(java.util.HashMap) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Pair(com.cloud.utils.Pair)

Example 5 with VirtualMachineMO

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

the class VmwareResource method cleanupNetworkElementCommand.

private ExecutionResult cleanupNetworkElementCommand(IpAssocCommand cmd) {
    VmwareContext context = getServiceContext();
    try {
        VmwareHypervisorHost hyperHost = getHyperHost(context);
        IpAddressTO[] ips = cmd.getIpAddresses();
        String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(routerName);
        // the check and will try to find it within datacenter
        if (vmMo == null) {
            if (hyperHost instanceof HostMO) {
                final DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
                vmMo = dcMo.findVm(routerName);
            }
        }
        if (vmMo == null) {
            String msg = String.format("Router %s no longer exists to execute IPAssoc command ", routerName);
            s_logger.error(msg);
            throw new Exception(msg);
        }
        final String lastIp = cmd.getAccessDetail(NetworkElementCommand.NETWORK_PUB_LAST_IP);
        for (IpAddressTO ip : ips) {
            if (ip.isAdd() || lastIp.equalsIgnoreCase("false")) {
                continue;
            }
            Pair<VirtualDevice, Integer> nicInfo = getVirtualDevice(vmMo, ip);
            if (nicInfo.second() == 2) {
                return new ExecutionResult(true, "Not removing eth2 in network VR because it is the public NIC of source NAT");
            }
            if (nicInfo.first() == null) {
                return new ExecutionResult(false, "Couldn't find NIC");
            }
            configureNicDevice(vmMo, nicInfo.first(), VirtualDeviceConfigSpecOperation.REMOVE, "unplugNicCommand");
        }
    } catch (Throwable e) {
        s_logger.error("Unexpected exception: " + e.toString() + " will shortcut rest of IPAssoc commands", e);
        return new ExecutionResult(false, e.toString());
    }
    return new ExecutionResult(true, null);
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VirtualDevice(com.vmware.vim25.VirtualDevice) ExecutionResult(com.cloud.utils.ExecutionResult) 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) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Aggregations

VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)78 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)68 RemoteException (java.rmi.RemoteException)52 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)47 UnsupportedEncodingException (java.io.UnsupportedEncodingException)46 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)40 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)38 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)28 CloudException (com.cloud.exception.CloudException)26 InternalErrorException (com.cloud.exception.InternalErrorException)26 IOException (java.io.IOException)26 ConnectException (java.net.ConnectException)26 ConfigurationException (javax.naming.ConfigurationException)26 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)24 VirtualDisk (com.vmware.vim25.VirtualDisk)20 DatastoreFile (com.cloud.hypervisor.vmware.mo.DatastoreFile)17 Script (com.cloud.utils.script.Script)17 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)16 File (java.io.File)14 Pair (com.cloud.utils.Pair)13