Search in sources :

Example 1 with KVMPhysicalDisk

use of com.cloud.agent.storage.KVMPhysicalDisk in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

protected CreatePrivateTemplateAnswer execute(CreatePrivateTemplateFromVolumeCommand cmd) {
    String secondaryStorageURL = cmd.getSecondaryStorageUrl();
    KVMStoragePool secondaryStorage = null;
    try {
        Connect conn = LibvirtConnection.getConnection();
        String templateFolder = cmd.getAccountId() + File.separator + cmd.getTemplateId() + File.separator;
        String templateInstallFolder = "/template/tmpl/" + templateFolder;
        secondaryStorage = _storagePoolMgr.getStoragePoolByURI(secondaryStorageURL);
        KVMStoragePool primary = _storagePoolMgr.getStoragePool(cmd.getPrimaryStoragePoolNameLabel());
        KVMPhysicalDisk disk = primary.getPhysicalDisk(cmd.getVolumePath());
        String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateInstallFolder;
        _storage.mkdirs(tmpltPath);
        Script command = new Script(_createTmplPath, _cmdsTimeout, s_logger);
        command.add("-f", disk.getPath());
        command.add("-t", tmpltPath);
        command.add("-n", cmd.getUniqueName() + ".qcow2");
        String result = command.execute();
        if (result != null) {
            s_logger.debug("failed to create template: " + result);
            return new CreatePrivateTemplateAnswer(cmd, false, result);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(StorageLayer.InstanceConfigKey, _storage);
        Processor qcow2Processor = new QCOW2Processor();
        qcow2Processor.configure("QCOW2 Processor", params);
        FormatInfo info = qcow2Processor.process(tmpltPath, null, cmd.getUniqueName());
        TemplateLocation loc = new TemplateLocation(_storage, tmpltPath);
        loc.create(1, true, cmd.getUniqueName());
        loc.addFormat(info);
        loc.save();
        return new CreatePrivateTemplateAnswer(cmd, true, null, templateInstallFolder + cmd.getUniqueName() + ".qcow2", info.virtualSize, info.size, cmd.getUniqueName(), ImageFormat.QCOW2);
    } catch (LibvirtException e) {
        s_logger.debug("Failed to get secondary storage pool: " + e.toString());
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (InternalErrorException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (IOException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (ConfigurationException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (CloudRuntimeException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } finally {
        if (secondaryStorage != null) {
            secondaryStorage.delete();
        }
    }
}
Also used : Script(com.cloud.utils.script.Script) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) Processor(com.cloud.storage.template.Processor) LibvirtException(org.libvirt.LibvirtException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) ConfigurationException(javax.naming.ConfigurationException) TemplateLocation(com.cloud.storage.template.TemplateLocation) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) FormatInfo(com.cloud.storage.template.Processor.FormatInfo)

Example 2 with KVMPhysicalDisk

use of com.cloud.agent.storage.KVMPhysicalDisk in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

protected Answer execute(CreateCommand cmd) {
    StorageFilerTO pool = cmd.getPool();
    DiskProfile dskch = cmd.getDiskCharacteristics();
    KVMPhysicalDisk BaseVol = null;
    KVMStoragePool primaryPool = null;
    KVMPhysicalDisk vol = null;
    long disksize;
    try {
        primaryPool = _storagePoolMgr.getStoragePool(pool.getUuid());
        if (cmd.getTemplateUrl() != null) {
            BaseVol = primaryPool.getPhysicalDisk(cmd.getTemplateUrl());
            vol = _storagePoolMgr.createDiskFromTemplate(BaseVol, UUID.randomUUID().toString(), primaryPool);
            if (vol == null) {
                return new Answer(cmd, false, " Can't create storage volume on storage pool");
            }
            disksize = vol.getSize();
        } else {
            disksize = dskch.getSize();
            vol = primaryPool.createPhysicalDisk(UUID.randomUUID().toString(), dskch.getSize());
        }
        VolumeTO volume = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), pool.getPath(), vol.getName(), vol.getName(), disksize, null);
        return new CreateAnswer(cmd, volume);
    } catch (CloudRuntimeException e) {
        s_logger.debug("Failed to create volume: " + e.toString());
        return new CreateAnswer(cmd, e);
    }
}
Also used : CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) FenceAnswer(com.cloud.agent.api.FenceAnswer) ConsoleProxyLoadAnswer(com.cloud.agent.api.proxy.ConsoleProxyLoadAnswer) DeleteSnapshotBackupAnswer(com.cloud.agent.api.DeleteSnapshotBackupAnswer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) GetHostStatsAnswer(com.cloud.agent.api.GetHostStatsAnswer) CheckSshAnswer(com.cloud.agent.api.check.CheckSshAnswer) GetVncPortAnswer(com.cloud.agent.api.GetVncPortAnswer) RebootAnswer(com.cloud.agent.api.RebootAnswer) ManageSnapshotAnswer(com.cloud.agent.api.ManageSnapshotAnswer) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) AttachVolumeAnswer(com.cloud.agent.api.AttachVolumeAnswer) ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) StartAnswer(com.cloud.agent.api.StartAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) MigrateAnswer(com.cloud.agent.api.MigrateAnswer) CreateVolumeFromSnapshotAnswer(com.cloud.agent.api.CreateVolumeFromSnapshotAnswer) CheckNetworkAnswer(com.cloud.agent.api.CheckNetworkAnswer) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) StopAnswer(com.cloud.agent.api.StopAnswer) NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer) Answer(com.cloud.agent.api.Answer) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) CheckHealthAnswer(com.cloud.agent.api.CheckHealthAnswer) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) PrepareForMigrationAnswer(com.cloud.agent.api.PrepareForMigrationAnswer) SecurityGroupRuleAnswer(com.cloud.agent.api.SecurityGroupRuleAnswer) VolumeTO(com.cloud.agent.api.to.VolumeTO) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) DiskProfile(com.cloud.vm.DiskProfile)

Example 3 with KVMPhysicalDisk

use of com.cloud.agent.storage.KVMPhysicalDisk in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method attachOrDetachISO.

protected synchronized String attachOrDetachISO(Connect conn, String vmName, String isoPath, boolean isAttach) throws LibvirtException, URISyntaxException, InternalErrorException {
    String isoXml = null;
    if (isoPath != null && isAttach) {
        int index = isoPath.lastIndexOf("/");
        String path = isoPath.substring(0, index);
        String name = isoPath.substring(index + 1);
        KVMStoragePool secondaryPool = _storagePoolMgr.getStoragePoolByURI(path);
        KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
        isoPath = isoVol.getPath();
        DiskDef iso = new DiskDef();
        iso.defISODisk(isoPath);
        isoXml = iso.toString();
    } else {
        DiskDef iso = new DiskDef();
        iso.defISODisk(null);
        isoXml = iso.toString();
    }
    List<DiskDef> disks = getDisks(conn, vmName);
    String result = attachOrDetachDevice(conn, true, vmName, isoXml);
    if (result == null && !isAttach) {
        for (DiskDef disk : disks) {
            if (disk.getDeviceType() == DiskDef.deviceType.CDROM) {
                cleanupDisk(conn, disk);
            }
        }
    }
    return result;
}
Also used : DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk)

Example 4 with KVMPhysicalDisk

use of com.cloud.agent.storage.KVMPhysicalDisk in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

private AttachVolumeAnswer execute(AttachVolumeCommand cmd) {
    try {
        Connect conn = LibvirtConnection.getConnection();
        KVMStoragePool primary = _storagePoolMgr.getStoragePool(cmd.getPoolUuid());
        KVMPhysicalDisk disk = primary.getPhysicalDisk(cmd.getVolumePath());
        attachOrDetachDisk(conn, cmd.getAttach(), cmd.getVmName(), disk, cmd.getDeviceId().intValue());
    } catch (LibvirtException e) {
        return new AttachVolumeAnswer(cmd, e.toString());
    } catch (InternalErrorException e) {
        return new AttachVolumeAnswer(cmd, e.toString());
    }
    return new AttachVolumeAnswer(cmd, cmd.getDeviceId());
}
Also used : AttachVolumeAnswer(com.cloud.agent.api.AttachVolumeAnswer) LibvirtException(org.libvirt.LibvirtException) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.exception.InternalErrorException)

Example 5 with KVMPhysicalDisk

use of com.cloud.agent.storage.KVMPhysicalDisk in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method getVolumePath.

private String getVolumePath(Connect conn, VolumeTO volume) throws LibvirtException, URISyntaxException {
    if (volume.getType() == Volume.Type.ISO && volume.getPath() != null) {
        String isoPath = volume.getPath();
        int index = isoPath.lastIndexOf("/");
        String path = isoPath.substring(0, index);
        String name = isoPath.substring(index + 1);
        KVMStoragePool secondaryPool = _storagePoolMgr.getStoragePoolByURI(path);
        KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
        return isoVol.getPath();
    } else {
        return volume.getPath();
    }
}
Also used : KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk)

Aggregations

KVMPhysicalDisk (com.cloud.agent.storage.KVMPhysicalDisk)13 KVMStoragePool (com.cloud.agent.storage.KVMStoragePool)13 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)7 InternalErrorException (com.cloud.exception.InternalErrorException)4 Script (com.cloud.utils.script.Script)4 Connect (org.libvirt.Connect)4 LibvirtException (org.libvirt.LibvirtException)4 CreatePrivateTemplateAnswer (com.cloud.agent.api.storage.CreatePrivateTemplateAnswer)3 VolumeTO (com.cloud.agent.api.to.VolumeTO)3 DiskDef (com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef)3 AttachVolumeAnswer (com.cloud.agent.api.AttachVolumeAnswer)2 BackupSnapshotAnswer (com.cloud.agent.api.BackupSnapshotAnswer)2 CreateVolumeFromSnapshotAnswer (com.cloud.agent.api.CreateVolumeFromSnapshotAnswer)2 ManageSnapshotAnswer (com.cloud.agent.api.ManageSnapshotAnswer)2 CopyVolumeAnswer (com.cloud.agent.api.storage.CopyVolumeAnswer)2 PrimaryStorageDownloadAnswer (com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer)2 StorageFilerTO (com.cloud.agent.api.to.StorageFilerTO)2 Domain (org.libvirt.Domain)2 DomainInfo (org.libvirt.DomainInfo)2 DomainSnapshot (org.libvirt.DomainSnapshot)2