Search in sources :

Example 16 with CreateObjectAnswer

use of org.apache.cloudstack.storage.command.CreateObjectAnswer in project cloudstack by apache.

the class KVMStorageProcessor method createVolume.

@Override
public Answer createVolume(final CreateObjectCommand cmd) {
    final VolumeObjectTO volume = (VolumeObjectTO) cmd.getData();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
    KVMStoragePool primaryPool = null;
    KVMPhysicalDisk vol = null;
    long disksize;
    try {
        primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        disksize = volume.getSize();
        PhysicalDiskFormat format;
        if (volume.getFormat() == null) {
            format = primaryPool.getDefaultFormat();
        } else {
            format = PhysicalDiskFormat.valueOf(volume.getFormat().toString().toUpperCase());
        }
        vol = primaryPool.createPhysicalDisk(volume.getUuid(), format, volume.getProvisioningType(), disksize);
        final VolumeObjectTO newVol = new VolumeObjectTO();
        if (vol != null) {
            newVol.setPath(vol.getName());
        }
        newVol.setSize(volume.getSize());
        newVol.setFormat(ImageFormat.valueOf(format.toString().toUpperCase()));
        return new CreateObjectAnswer(newVol);
    } catch (final Exception e) {
        s_logger.debug("Failed to create volume: ", e);
        return new CreateObjectAnswer(e.toString());
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) RbdException(com.ceph.rbd.RbdException) QemuImgException(org.apache.cloudstack.utils.qemu.QemuImgException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RadosException(com.ceph.rados.exceptions.RadosException) IOException(java.io.IOException) PhysicalDiskFormat(org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat)

Example 17 with CreateObjectAnswer

use of org.apache.cloudstack.storage.command.CreateObjectAnswer in project cloudstack by apache.

the class VmwareStorageProcessor method createVolume.

@Override
public Answer createVolume(CreateObjectCommand cmd) {
    VolumeObjectTO volume = (VolumeObjectTO) cmd.getData();
    DataStoreTO primaryStore = volume.getDataStore();
    try {
        VmwareContext context = hostService.getServiceContext(null);
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
        DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
        ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, primaryStore.getUuid());
        if (morDatastore == null) {
            throw new Exception("Unable to find datastore in vSphere");
        }
        DatastoreMO dsMo = new DatastoreMO(context, morDatastore);
        // create data volume
        VirtualMachineMO vmMo = null;
        String volumeUuid = UUID.randomUUID().toString().replace("-", "");
        String volumeDatastorePath = dsMo.getDatastorePath(volumeUuid + ".vmdk");
        String dummyVmName = hostService.getWorkerName(context, cmd, 0);
        try {
            s_logger.info("Create worker VM " + dummyVmName);
            vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, dummyVmName);
            if (vmMo == null) {
                throw new Exception("Unable to create a dummy VM for volume creation");
            }
            synchronized (this) {
                try {
                    vmMo.createDisk(volumeDatastorePath, (int) (volume.getSize() / (1024L * 1024L)), morDatastore, vmMo.getScsiDeviceControllerKey());
                    vmMo.detachDisk(volumeDatastorePath, false);
                } catch (Exception e) {
                    s_logger.error("Deleting file " + volumeDatastorePath + " due to error: " + e.getMessage());
                    VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, volumeUuid.toString(), dcMo);
                    throw new CloudRuntimeException("Unable to create volume due to: " + e.getMessage());
                }
            }
            VolumeObjectTO newVol = new VolumeObjectTO();
            newVol.setPath(volumeUuid);
            newVol.setSize(volume.getSize());
            return new CreateObjectAnswer(newVol);
        } finally {
            s_logger.info("Destroy dummy VM after volume creation");
            if (vmMo != null) {
                vmMo.detachAllDisks();
                vmMo.destroy();
            }
        }
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            hostService.invalidateServiceContext(null);
        }
        String msg = "create volume failed due to " + VmwareHelper.getExceptionMessage(e);
        s_logger.error(msg, e);
        return new CreateObjectAnswer(e.toString());
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) 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) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) RemoteException(java.rmi.RemoteException) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 18 with CreateObjectAnswer

use of org.apache.cloudstack.storage.command.CreateObjectAnswer in project cloudstack by apache.

the class Ovm3StorageProcessor method createSnapshot.

/**
     * Creates a snapshot from a volume, but only if the VM is stopped.
     * This due qemu not being able to snap raw volumes.
     *
     * if stopped yes, if running ... no, unless we have ocfs2 when
     * using raw partitions (file:) if using tap:aio we cloud...
     * The "ancient" way:
     * We do however follow the "two stage" approach, of "snap"
     * on primary first, with the create object... and then
     * backup the snapshot with the copycmd....
     * (should transfer to createSnapshot, backupSnapshot)
     */
@Override
public Answer createSnapshot(CreateObjectCommand cmd) {
    LOGGER.debug("execute createSnapshot: " + cmd.getClass());
    DataTO data = cmd.getData();
    Xen xen = new Xen(c);
    SnapshotObjectTO snap = (SnapshotObjectTO) data;
    VolumeObjectTO vol = snap.getVolume();
    try {
        Xen.Vm vm = xen.getVmConfig(snap.getVmName());
        if (vm != null) {
            return new CreateObjectAnswer("Snapshot object creation not supported for running VMs." + snap.getVmName());
        }
        Linux host = new Linux(c);
        String uuid = host.newUuid();
        /* for root volumes this works... */
        String src = vol.getPath() + "/" + vol.getUuid() + ".raw";
        String dest = vol.getPath() + "/" + uuid + ".raw";
        /* seems that sometimes the path is already contains a file
             * in case, we just replace it.... (Seems to happen if not ROOT)
             */
        if (vol.getPath().contains(vol.getUuid())) {
            src = getVirtualDiskPath(vol.getUuid(), data.getDataStore().getUuid());
            dest = src.replace(vol.getUuid(), uuid);
        }
        LOGGER.debug("Snapshot " + src + " to " + dest);
        host.copyFile(src, dest);
        SnapshotObjectTO nsnap = new SnapshotObjectTO();
        // nsnap.setPath(dest);
        // move to something that looks the same as xenserver.
        nsnap.setPath(uuid);
        return new CreateObjectAnswer(nsnap);
    } catch (Ovm3ResourceException e) {
        return new CreateObjectAnswer("Snapshot object creation failed. " + e.getMessage());
    }
}
Also used : Xen(com.cloud.hypervisor.ovm3.objects.Xen) SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) DataTO(com.cloud.agent.api.to.DataTO) Linux(com.cloud.hypervisor.ovm3.objects.Linux) Ovm3ResourceException(com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO)

Example 19 with CreateObjectAnswer

use of org.apache.cloudstack.storage.command.CreateObjectAnswer in project cloudstack by apache.

the class XenServerStorageProcessor method createVolume.

@Override
public Answer createVolume(final CreateObjectCommand cmd) {
    final DataTO data = cmd.getData();
    final VolumeObjectTO volume = (VolumeObjectTO) data;
    try {
        final Connection conn = hypervisorResource.getConnection();
        final SR poolSr = hypervisorResource.getStorageRepository(conn, data.getDataStore().getUuid());
        VDI.Record vdir = new VDI.Record();
        vdir.nameLabel = volume.getName();
        vdir.SR = poolSr;
        vdir.type = Types.VdiType.USER;
        vdir.virtualSize = volume.getSize();
        VDI vdi;
        vdi = VDI.create(conn, vdir);
        vdir = vdi.getRecord(conn);
        final VolumeObjectTO newVol = new VolumeObjectTO();
        newVol.setName(vdir.nameLabel);
        newVol.setSize(vdir.virtualSize);
        newVol.setPath(vdir.uuid);
        return new CreateObjectAnswer(newVol);
    } catch (final Exception e) {
        s_logger.debug("create volume failed: " + e.toString());
        return new CreateObjectAnswer(e.toString());
    }
}
Also used : DataTO(com.cloud.agent.api.to.DataTO) Connection(com.xensource.xenapi.Connection) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SR(com.xensource.xenapi.SR)

Example 20 with CreateObjectAnswer

use of org.apache.cloudstack.storage.command.CreateObjectAnswer in project cloudstack by apache.

the class Ovm3StorageProcessor method execute.

public Answer execute(CreateObjectCommand cmd) {
    LOGGER.debug("execute: " + cmd.getClass());
    DataTO data = cmd.getData();
    if (data.getObjectType() == DataObjectType.VOLUME) {
        return createVolume(cmd);
    } else if (data.getObjectType() == DataObjectType.SNAPSHOT) {
        return createSnapshot(cmd);
    } else if (data.getObjectType() == DataObjectType.TEMPLATE) {
        LOGGER.debug("Template object creation not supported.");
    }
    return new CreateObjectAnswer(data.getObjectType() + " object creation not supported");
}
Also used : DataTO(com.cloud.agent.api.to.DataTO) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer)

Aggregations

CreateObjectAnswer (org.apache.cloudstack.storage.command.CreateObjectAnswer)20 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)11 SnapshotObjectTO (org.apache.cloudstack.storage.to.SnapshotObjectTO)11 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)10 DataTO (com.cloud.agent.api.to.DataTO)6 VolumeVO (com.cloud.storage.VolumeVO)5 InternalErrorException (com.cloud.exception.InternalErrorException)4 Ovm3ResourceException (com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException)4 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)4 Answer (com.cloud.agent.api.Answer)3 StoragePlugin (com.cloud.hypervisor.ovm3.objects.StoragePlugin)3 URISyntaxException (java.net.URISyntaxException)3 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)3 RadosException (com.ceph.rados.exceptions.RadosException)2 RbdException (com.ceph.rbd.RbdException)2 CopyVolumeAnswer (com.cloud.agent.api.storage.CopyVolumeAnswer)2 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)2 CreatePrivateTemplateAnswer (com.cloud.agent.api.storage.CreatePrivateTemplateAnswer)2 DownloadAnswer (com.cloud.agent.api.storage.DownloadAnswer)2 Connection (com.xensource.xenapi.Connection)2