Search in sources :

Example 11 with OVAProcessor

use of com.cloud.storage.template.OVAProcessor in project cloudstack by apache.

the class VmwareStorageProcessor method handleMetadataCreateTemplateFromSnapshot.

private Size handleMetadataCreateTemplateFromSnapshot(String installFullPath, String templateVMDKName, long templateId, String templateUniqueName, String ovfFilename) throws Exception {
    long physicalSize = new File(installFullPath + "/" + templateVMDKName).length();
    OVAProcessor processor = new OVAProcessor();
    Map<String, Object> params = new HashMap<>();
    params.put(StorageLayer.InstanceConfigKey, _storage);
    processor.configure("OVA Processor", params);
    long virtualSize = processor.getTemplateVirtualSize(installFullPath, templateUniqueName);
    postCreatePrivateTemplate(installFullPath, templateId, templateUniqueName, physicalSize, virtualSize);
    writeMetaOvaForTemplate(installFullPath, ovfFilename + ".ovf", templateVMDKName, templateUniqueName, physicalSize);
    return new Size(physicalSize, virtualSize);
}
Also used : OVAProcessor(com.cloud.storage.template.OVAProcessor) HashMap(java.util.HashMap) VStorageObject(com.vmware.vim25.VStorageObject) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) File(java.io.File)

Example 12 with OVAProcessor

use of com.cloud.storage.template.OVAProcessor in project cloudstack by apache.

the class DownloadManagerImpl method gatherVolumeInfo.

@Override
public Map<Long, TemplateProp> gatherVolumeInfo(String rootDir) {
    Map<Long, TemplateProp> result = new HashMap<Long, TemplateProp>();
    String volumeDir = rootDir + File.separator + _volumeDir;
    if (!_storage.exists(volumeDir)) {
        _storage.mkdirs(volumeDir);
    }
    List<String> vols = listVolumes(volumeDir);
    for (String vol : vols) {
        String path = vol.substring(0, vol.lastIndexOf(File.separator));
        TemplateLocation loc = new TemplateLocation(_storage, path);
        try {
            if (!loc.load()) {
                LOGGER.warn("Post download installation was not completed for " + path);
                // loc.purge();
                _storage.cleanup(path, volumeDir);
                continue;
            }
        } catch (IOException e) {
            LOGGER.warn("Unable to load volume location " + path, e);
            continue;
        }
        TemplateProp vInfo = loc.getTemplateInfo();
        if ((vInfo.getSize() == vInfo.getPhysicalSize()) && (vInfo.getInstallPath().endsWith(ImageFormat.OVA.getFileExtension()))) {
            try {
                Processor processor = _processors.get("OVA Processor");
                OVAProcessor vmdkProcessor = (OVAProcessor) processor;
                long vSize = vmdkProcessor.getTemplateVirtualSize(path, vInfo.getInstallPath().substring(vInfo.getInstallPath().lastIndexOf(File.separator) + 1));
                vInfo.setSize(vSize);
                loc.updateVirtualSize(vSize);
                loc.save();
            } catch (Exception e) {
                LOGGER.error("Unable to get the virtual size of the volume: " + vInfo.getInstallPath() + " due to " + e.getMessage());
            }
        }
        result.put(vInfo.getId(), vInfo);
        LOGGER.debug("Added volume name: " + vInfo.getTemplateName() + ", path: " + vol);
    }
    return result;
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) VhdProcessor(com.cloud.storage.template.VhdProcessor) OVAProcessor(com.cloud.storage.template.OVAProcessor) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) TARProcessor(com.cloud.storage.template.TARProcessor) IsoProcessor(com.cloud.storage.template.IsoProcessor) Processor(com.cloud.storage.template.Processor) VmdkProcessor(com.cloud.storage.template.VmdkProcessor) RawImageProcessor(com.cloud.storage.template.RawImageProcessor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) OVAProcessor(com.cloud.storage.template.OVAProcessor) TemplateLocation(com.cloud.storage.template.TemplateLocation) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException)

Example 13 with OVAProcessor

use of com.cloud.storage.template.OVAProcessor 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, String 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
        String vmxFormattedVirtualHardwareVersion = VirtualMachineMO.getVmxFormattedVirtualHardwareVersion(vmMo.getVirtualHardwareVersion());
        vmMo.cloneFromCurrentSnapshot(workerVmName, 0, 4, volumeDeviceInfo.second(), VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()), vmxFormattedVirtualHardwareVersion);
        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.detachAllDisksAndDestroy();
        }
        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) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualDisk(com.vmware.vim25.VirtualDisk) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) File(java.io.File)

Example 14 with OVAProcessor

use of com.cloud.storage.template.OVAProcessor in project cloudstack by apache.

the class VmwareStorageProcessor method createTemplateFromVolume.

private Ternary<String, Long, Long> createTemplateFromVolume(VmwareContext context, VirtualMachineMO vmMo, VmwareHypervisorHost hyperHost, String installPath, long templateId, String templateUniqueName, String secStorageUrl, String volumePath, String workerVmName, String nfsVersion) throws Exception {
    String secondaryMountPoint = mountService.getMountPoint(secStorageUrl, nfsVersion);
    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);
        }
        DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
        ManagedObjectReference morPool = hyperHost.getHyperHostOwnerResourcePool();
        VirtualDisk requiredDisk = volumeDeviceInfo.first();
        clonedVm = vmMo.createFullCloneWithSpecificDisk(templateUniqueName, dcMo.getVmFolder(), morPool, requiredDisk);
        if (clonedVm == null) {
            throw new Exception(String.format("Failed to clone VM with name %s during create template from volume operation", templateUniqueName));
        }
        clonedVm.exportVm(secondaryMountPoint + "/" + installPath, templateUniqueName, false, false);
        // Get VMDK filename
        String templateVMDKName = "";
        File[] files = new File(installFullPath).listFiles();
        if (files != null) {
            for (File file : files) {
                String fileName = file.getName();
                if (fileName.toLowerCase().startsWith(templateUniqueName) && fileName.toLowerCase().endsWith(".vmdk")) {
                    templateVMDKName += fileName;
                    break;
                }
            }
        }
        long physicalSize = new File(installFullPath + "/" + templateVMDKName).length();
        OVAProcessor processor = new OVAProcessor();
        Map<String, Object> params = new HashMap<>();
        params.put(StorageLayer.InstanceConfigKey, _storage);
        processor.configure("OVA Processor", params);
        long virtualSize = processor.getTemplateVirtualSize(installFullPath, templateUniqueName);
        postCreatePrivateTemplate(installFullPath, templateId, templateUniqueName, physicalSize, virtualSize);
        writeMetaOvaForTemplate(installFullPath, templateUniqueName + ".ovf", templateVMDKName, templateUniqueName, physicalSize);
        return new Ternary<String, Long, Long>(installPath + "/" + templateUniqueName + ".ova", physicalSize, virtualSize);
    } finally {
        if (clonedVm != null) {
            s_logger.debug(String.format("Destroying cloned VM: %s with its disks", clonedVm.getName()));
            clonedVm.destroy();
        }
    }
}
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) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) VirtualDisk(com.vmware.vim25.VirtualDisk) VStorageObject(com.vmware.vim25.VStorageObject) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) File(java.io.File) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

OVAProcessor (com.cloud.storage.template.OVAProcessor)14 HashMap (java.util.HashMap)13 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 Script (com.cloud.utils.script.Script)9 File (java.io.File)8 Ternary (com.cloud.utils.Ternary)7 RemoteException (java.rmi.RemoteException)7 DatastoreFile (com.cloud.hypervisor.vmware.mo.DatastoreFile)6 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Processor (com.cloud.storage.template.Processor)4 QCOW2Processor (com.cloud.storage.template.QCOW2Processor)4 RawImageProcessor (com.cloud.storage.template.RawImageProcessor)4 TARProcessor (com.cloud.storage.template.TARProcessor)4 VhdProcessor (com.cloud.storage.template.VhdProcessor)4 VmdkProcessor (com.cloud.storage.template.VmdkProcessor)4 VirtualDisk (com.vmware.vim25.VirtualDisk)4 ConfigurationException (javax.naming.ConfigurationException)4 InternalErrorException (com.cloud.exception.InternalErrorException)3 IsoProcessor (com.cloud.storage.template.IsoProcessor)3