Search in sources :

Example 11 with TemplateObjectTO

use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method attachIso.

@Override
public Answer attachIso(final AttachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final TemplateObjectTO isoTo = (TemplateObjectTO) disk.getData();
    final DataStoreTO store = isoTo.getDataStore();
    if (!(store instanceof NfsTO)) {
        return new AttachAnswer("unsupported protocol");
    }
    final NfsTO nfsStore = (NfsTO) store;
    try {
        final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName());
        attachOrDetachIso(conn, cmd.getVmName(), nfsStore.getUrl() + File.separator + isoTo.getPath(), true);
    } catch (final LibvirtException | URISyntaxException | InternalErrorException e) {
        return new Answer(cmd, false, e.toString());
    }
    return new Answer(cmd);
}
Also used : CreateObjectAnswer(com.cloud.storage.command.CreateObjectAnswer) Answer(com.cloud.agent.api.Answer) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) SnapshotAndCopyAnswer(com.cloud.storage.command.SnapshotAndCopyAnswer) DettachAnswer(com.cloud.storage.command.DettachAnswer) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) AttachAnswer(com.cloud.storage.command.AttachAnswer) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) URISyntaxException(java.net.URISyntaxException) InternalErrorException(com.cloud.exception.InternalErrorException) NfsTO(com.cloud.agent.api.to.NfsTO) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(com.cloud.storage.command.AttachAnswer)

Example 12 with TemplateObjectTO

use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method cloneVolumeFromBaseTemplate.

@Override
public Answer cloneVolumeFromBaseTemplate(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final TemplateObjectTO template = (TemplateObjectTO) srcData;
    final DataStoreTO imageStore = template.getDataStore();
    final VolumeObjectTO volume = (VolumeObjectTO) destData;
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
    KvmPhysicalDisk baseVolume;
    KvmStoragePool primaryPool;
    KvmPhysicalDisk vol;
    try {
        primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        String templatePath = template.getPath();
        if (primaryPool.getType() == StoragePoolType.CLVM) {
            templatePath = imageStore.getUrl() + File.separator + templatePath;
            vol = templateToPrimaryDownload(templatePath, primaryPool, volume.getUuid(), volume.getSize(), cmd.getWaitInMillSeconds());
        } else {
            if (templatePath.contains("/mnt")) {
                // upgrade issue, if the path contains path, need to extract the volume uuid from path
                templatePath = templatePath.substring(templatePath.lastIndexOf(File.separator) + 1);
            }
            baseVolume = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), templatePath);
            vol = storagePoolMgr.createDiskFromTemplate(baseVolume, volume.getUuid(), volume.getProvisioningType(), baseVolume.getPool(), volume.getSize(), cmd.getWaitInMillSeconds());
        }
        if (vol == null) {
            return new CopyCmdAnswer(" Can't create storage volume on storage pool");
        }
        final VolumeObjectTO newVol = new VolumeObjectTO();
        newVol.setPath(vol.getName());
        newVol.setSize(volume.getSize());
        if (vol.getFormat() == PhysicalDiskFormat.RAW) {
            newVol.setFormat(ImageFormat.RAW);
        } else if (vol.getFormat() == PhysicalDiskFormat.QCOW2) {
            newVol.setFormat(ImageFormat.QCOW2);
        } else if (vol.getFormat() == PhysicalDiskFormat.DIR) {
            newVol.setFormat(ImageFormat.DIR);
        }
        return new CopyCmdAnswer(newVol);
    } catch (final CloudRuntimeException e) {
        logger.debug("Failed to create volume: ", e);
        return new CopyCmdAnswer(e.toString());
    }
}
Also used : PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer)

Example 13 with TemplateObjectTO

use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method createTemplateFromVolume.

@Override
public Answer createTemplateFromVolume(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final int wait = cmd.getWaitInMillSeconds();
    final TemplateObjectTO template = (TemplateObjectTO) destData;
    final DataStoreTO imageStore = template.getDataStore();
    final VolumeObjectTO volume = (VolumeObjectTO) srcData;
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
    if (!(imageStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    final NfsTO nfsImageStore = (NfsTO) imageStore;
    KvmStoragePool secondaryStorage = null;
    KvmStoragePool primary;
    try {
        final String templateFolder = template.getPath();
        secondaryStorage = storagePoolMgr.getStoragePoolByUri(nfsImageStore.getUrl());
        primary = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        final KvmPhysicalDisk disk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volume.getPath());
        final String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateFolder;
        storageLayer.mkdirs(tmpltPath);
        final String templateName = UUID.randomUUID().toString();
        if (primary.getType() != StoragePoolType.RBD) {
            final Script command = new Script(createTmplPath, wait, logger);
            command.add("-f", disk.getPath());
            command.add("-t", tmpltPath);
            command.add("-n", templateName + ".qcow2");
            final String result = command.execute();
            if (result != null) {
                logger.debug("failed to create template: " + result);
                return new CopyCmdAnswer(result);
            }
        } else {
            logger.debug("Converting RBD disk " + disk.getPath() + " into template " + templateName);
            final QemuImgFile srcFile = new QemuImgFile(KvmPhysicalDisk.rbdStringBuilder(primary.getSourceHost(), primary.getSourcePort(), primary.getAuthUserName(), primary.getAuthSecret(), disk.getPath()));
            srcFile.setFormat(PhysicalDiskFormat.RAW);
            final QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + templateName + ".qcow2");
            destFile.setFormat(PhysicalDiskFormat.QCOW2);
            final QemuImg q = new QemuImg(cmd.getWaitInMillSeconds());
            try {
                q.convert(srcFile, destFile);
            } catch (final QemuImgException e) {
                final String message = "Failed to create new template while converting " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage();
                throw new QemuImgException(message);
            }
            final File templateProp = new File(tmpltPath + "/template.properties");
            if (!templateProp.exists()) {
                templateProp.createNewFile();
            }
            String templateContent = "filename=" + templateName + ".qcow2" + System.getProperty("line.separator");
            final DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy");
            final Date date = new Date();
            templateContent += "snapshot.name=" + dateFormat.format(date) + System.getProperty("line.separator");
            try (FileOutputStream templFo = new FileOutputStream(templateProp)) {
                templFo.write(templateContent.getBytes());
                templFo.flush();
            } catch (final IOException e) {
                throw e;
            }
        }
        final Map<String, Object> params = new HashMap<>();
        params.put(StorageLayer.InstanceConfigKey, storageLayer);
        final Processor qcow2Processor = new QCOW2Processor();
        qcow2Processor.configure("QCOW2 Processor", params);
        final FormatInfo info = qcow2Processor.process(tmpltPath, null, templateName);
        final TemplateLocation loc = new TemplateLocation(storageLayer, tmpltPath);
        loc.create(1, true, templateName);
        loc.addFormat(info);
        loc.save();
        final TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(templateFolder + File.separator + templateName + ".qcow2");
        newTemplate.setSize(info.virtualSize);
        newTemplate.setPhysicalSize(info.size);
        newTemplate.setFormat(ImageFormat.QCOW2);
        newTemplate.setName(templateName);
        return new CopyCmdAnswer(newTemplate);
    } catch (final QemuImgException e) {
        logger.error(e.getMessage());
        return new CopyCmdAnswer(e.toString());
    } catch (final Exception e) {
        logger.debug("Failed to createTemplateFromVolume: ", e);
        return new CopyCmdAnswer(e.toString());
    } finally {
        if (secondaryStorage != null) {
            secondaryStorage.delete();
        }
    }
}
Also used : QCOW2Processor(com.cloud.storage.template.QCOW2Processor) StorageProcessor(com.cloud.storage.resource.StorageProcessor) Processor(com.cloud.storage.template.Processor) HashMap(java.util.HashMap) DataTO(com.cloud.agent.api.to.DataTO) TemplateLocation(com.cloud.storage.template.TemplateLocation) QemuImgException(com.cloud.utils.qemu.QemuImgException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) Script(com.cloud.utils.script.Script) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) IOException(java.io.IOException) NfsTO(com.cloud.agent.api.to.NfsTO) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) QemuImgException(com.cloud.utils.qemu.QemuImgException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) QemuImg(com.cloud.utils.qemu.QemuImg) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) QemuImgFile(com.cloud.utils.qemu.QemuImgFile) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) FileOutputStream(java.io.FileOutputStream) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) FormatInfo(com.cloud.storage.template.Processor.FormatInfo) QemuImgFile(com.cloud.utils.qemu.QemuImgFile) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer)

Example 14 with TemplateObjectTO

use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.

the class CitrixResourceBase method createVmFromTemplate.

public VM createVmFromTemplate(final Connection conn, final VirtualMachineTO vmSpec, final Host host) throws XenAPIException, XmlRpcException {
    final String guestOsTypeName = getGuestOsType(vmSpec.getOs(), vmSpec.getPlatformEmulator(), vmSpec.getBootloader() == BootloaderType.CD);
    final Set<VM> templates = VM.getByNameLabel(conn, guestOsTypeName);
    if (templates == null || templates.isEmpty()) {
        throw new CloudRuntimeException("Cannot find template " + guestOsTypeName + " on XenServer host");
    }
    assert templates.size() == 1 : "Should only have 1 template but found " + templates.size();
    final VM template = templates.iterator().next();
    final VM.Record vmr = template.getRecord(conn);
    vmr.affinity = host;
    vmr.otherConfig.remove("disks");
    vmr.otherConfig.remove("default_template");
    vmr.otherConfig.remove("mac_seed");
    vmr.isATemplate = false;
    vmr.nameLabel = vmSpec.getName();
    vmr.actionsAfterCrash = Types.OnCrashBehaviour.DESTROY;
    vmr.actionsAfterShutdown = Types.OnNormalExit.DESTROY;
    vmr.otherConfig.put("vm_uuid", vmSpec.getUuid());
    // FIX ME: In case of dynamic
    vmr.VCPUsMax = (long) vmSpec.getCpus();
    if (isDmcEnabled(conn, host) && vmSpec.isEnableDynamicallyScaleVm()) {
        // scaling is allowed
        vmr.memoryStaticMin = getStaticMin(vmSpec.getOs(), vmSpec.getBootloader() == BootloaderType.CD, vmSpec.getMinRam(), vmSpec.getMaxRam());
        vmr.memoryStaticMax = getStaticMax(vmSpec.getOs(), vmSpec.getBootloader() == BootloaderType.CD, vmSpec.getMinRam(), vmSpec.getMaxRam());
        vmr.memoryDynamicMin = vmSpec.getMinRam();
        vmr.memoryDynamicMax = vmSpec.getMaxRam();
        if (guestOsTypeName.toLowerCase().contains("windows")) {
            vmr.VCPUsMax = (long) vmSpec.getCpus();
        } else {
            if (vmSpec.getVcpuMaxLimit() != null) {
                vmr.VCPUsMax = (long) vmSpec.getVcpuMaxLimit();
            }
        }
    } else {
        // scaling disallowed, set static memory target
        if (vmSpec.isEnableDynamicallyScaleVm() && !isDmcEnabled(conn, host)) {
            s_logger.warn("Host " + host.getHostname(conn) + " does not support dynamic scaling, so the vm " + vmSpec.getName() + " is not dynamically scalable");
        }
        vmr.memoryStaticMin = vmSpec.getMinRam();
        vmr.memoryStaticMax = vmSpec.getMaxRam();
        vmr.memoryDynamicMin = vmSpec.getMinRam();
        vmr.memoryDynamicMax = vmSpec.getMaxRam();
        vmr.VCPUsMax = (long) vmSpec.getCpus();
    }
    vmr.VCPUsAtStartup = (long) vmSpec.getCpus();
    vmr.consoles.clear();
    final VM vm = VM.create(conn, vmr);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Created VM " + vm.getUuid(conn) + " for " + vmSpec.getName());
    }
    final String bootArgs = vmSpec.getBootArgs();
    if (bootArgs != null && bootArgs.length() > 0) {
        String pvargs = vm.getPVArgs(conn);
        pvargs = pvargs + vmSpec.getBootArgs().replaceAll(" ", "%");
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("PV args are " + pvargs);
        }
        vm.setPVArgs(conn, pvargs);
    }
    if (!(guestOsTypeName.startsWith("Windows") || guestOsTypeName.startsWith("Citrix") || guestOsTypeName.startsWith("Other"))) {
        if (vmSpec.getBootloader() == BootloaderType.CD) {
            final DiskTO[] disks = vmSpec.getDisks();
            for (final DiskTO disk : disks) {
                if (disk.getType() == Volume.Type.ISO) {
                    final TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
                    final String osType = iso.getGuestOsType();
                    if (osType != null) {
                        final String isoGuestOsName = getGuestOsType(osType, vmSpec.getPlatformEmulator(), vmSpec.getBootloader() == BootloaderType.CD);
                        if (!isoGuestOsName.equals(guestOsTypeName)) {
                            vmSpec.setBootloader(BootloaderType.PyGrub);
                        }
                    }
                }
            }
        }
        if (vmSpec.getBootloader() == BootloaderType.CD) {
            vm.setPVBootloader(conn, "eliloader");
            if (!vm.getOtherConfig(conn).containsKey("install-repository")) {
                vm.addToOtherConfig(conn, "install-repository", "cdrom");
            }
        } else if (vmSpec.getBootloader() == BootloaderType.PyGrub) {
            vm.setPVBootloader(conn, "pygrub");
            vm.setPVBootloaderArgs(conn, CitrixHelper.getPVbootloaderArgs(guestOsTypeName));
        } else {
            vm.destroy(conn);
            throw new CloudRuntimeException("Unable to handle boot loader type: " + vmSpec.getBootloader());
        }
    }
    try {
        finalizeVmMetaData(vm, conn, vmSpec);
    } catch (final Exception e) {
        throw new CloudRuntimeException("Unable to finalize VM MetaData: " + vmSpec);
    }
    return vm;
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 15 with TemplateObjectTO

use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.

the class XenServerStorageProcessor method createTemplateFromVolume.

@Override
public Answer createTemplateFromVolume(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final VolumeObjectTO volume = (VolumeObjectTO) cmd.getSrcTO();
    final TemplateObjectTO template = (TemplateObjectTO) cmd.getDestTO();
    final NfsTO destStore = (NfsTO) cmd.getDestTO().getDataStore();
    final int wait = cmd.getWait();
    final String secondaryStoragePoolURL = destStore.getUrl();
    final String volumeUUID = volume.getPath();
    final String userSpecifiedName = template.getName();
    String details = null;
    SR tmpltSR = null;
    boolean result = false;
    String secondaryStorageMountPath = null;
    String installPath = null;
    try {
        final URI uri = new URI(secondaryStoragePoolURL);
        secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath();
        installPath = template.getPath();
        if (!hypervisorResource.createSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath)) {
            details = " Filed to create folder " + installPath + " in secondary storage";
            s_logger.warn(details);
            return new CopyCmdAnswer(details);
        }
        final VDI vol = getVDIbyUuid(conn, volumeUUID);
        // create template SR
        final URI tmpltURI = new URI(secondaryStoragePoolURL + "/" + installPath);
        tmpltSR = hypervisorResource.createNfsSRbyURI(conn, tmpltURI, false);
        // copy volume to template SR
        final VDI tmpltVDI = hypervisorResource.cloudVDIcopy(conn, vol, tmpltSR, wait);
        // scan makes XenServer pick up VDI physicalSize
        tmpltSR.scan(conn);
        if (userSpecifiedName != null) {
            tmpltVDI.setNameLabel(conn, userSpecifiedName);
        }
        final String tmpltUUID = tmpltVDI.getUuid(conn);
        final String tmpltFilename = tmpltUUID + ".vhd";
        final long virtualSize = tmpltVDI.getVirtualSize(conn);
        final long physicalSize = tmpltVDI.getPhysicalUtilisation(conn);
        // create the template.properties file
        final String templatePath = secondaryStorageMountPath + "/" + installPath;
        result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, tmpltFilename, tmpltUUID, userSpecifiedName, null, physicalSize, virtualSize, template.getId());
        if (!result) {
            throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir: " + tmpltURI);
        }
        installPath = installPath + "/" + tmpltFilename;
        hypervisorResource.removeSR(conn, tmpltSR);
        tmpltSR = null;
        final TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(installPath);
        newTemplate.setFormat(ImageFormat.VHD);
        newTemplate.setSize(virtualSize);
        newTemplate.setPhysicalSize(physicalSize);
        newTemplate.setName(tmpltUUID);
        final CopyCmdAnswer answer = new CopyCmdAnswer(newTemplate);
        return answer;
    } catch (final Exception e) {
        if (tmpltSR != null) {
            hypervisorResource.removeSR(conn, tmpltSR);
        }
        if (secondaryStorageMountPath != null) {
            hypervisorResource.deleteSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath);
        }
        details = "Creating template from volume " + volumeUUID + " failed due to " + e.toString();
        s_logger.error(details, e);
    }
    return new CopyCmdAnswer(details);
}
Also used : Connection(com.xensource.xenapi.Connection) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Aggregations

TemplateObjectTO (com.cloud.storage.to.TemplateObjectTO)24 CopyCmdAnswer (com.cloud.storage.command.CopyCmdAnswer)17 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)17 NfsTO (com.cloud.agent.api.to.NfsTO)15 InternalErrorException (com.cloud.exception.InternalErrorException)14 PrimaryDataStoreTO (com.cloud.storage.to.PrimaryDataStoreTO)12 XenAPIException (com.xensource.xenapi.Types.XenAPIException)11 XmlRpcException (org.apache.xmlrpc.XmlRpcException)11 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)10 DataTO (com.cloud.agent.api.to.DataTO)10 VDI (com.xensource.xenapi.VDI)10 Connection (com.xensource.xenapi.Connection)9 SR (com.xensource.xenapi.SR)9 URI (java.net.URI)8 VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)7 DiskTO (com.cloud.agent.api.to.DiskTO)6 URISyntaxException (java.net.URISyntaxException)6 ConfigurationException (javax.naming.ConfigurationException)6 IOException (java.io.IOException)5 Answer (com.cloud.agent.api.Answer)4