Search in sources :

Example 21 with PrimaryDataStoreTO

use of org.apache.cloudstack.storage.to.PrimaryDataStoreTO in project cloudstack by apache.

the class KVMStorageProcessor method copyTemplateToPrimaryStorage.

@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final TemplateObjectTO template = (TemplateObjectTO) srcData;
    final DataStoreTO imageStore = template.getDataStore();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) destData.getDataStore();
    if (!(imageStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    final NfsTO nfsImageStore = (NfsTO) imageStore;
    final String tmplturl = nfsImageStore.getUrl() + File.separator + template.getPath();
    final int index = tmplturl.lastIndexOf("/");
    final String mountpoint = tmplturl.substring(0, index);
    String tmpltname = null;
    if (index < tmplturl.length() - 1) {
        tmpltname = tmplturl.substring(index + 1);
    }
    KVMPhysicalDisk tmplVol = null;
    KVMStoragePool secondaryPool = null;
    try {
        secondaryPool = storagePoolMgr.getStoragePoolByURI(mountpoint);
        /* Get template vol */
        if (tmpltname == null) {
            secondaryPool.refresh();
            final List<KVMPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
            if (disks == null || disks.isEmpty()) {
                return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: " + secondaryPool.getUuid());
            }
            for (final KVMPhysicalDisk disk : disks) {
                if (disk.getName().endsWith("qcow2")) {
                    tmplVol = disk;
                    break;
                }
            }
        } else {
            tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
        }
        if (tmplVol == null) {
            return new PrimaryStorageDownloadAnswer("Failed to get template from pool: " + secondaryPool.getUuid());
        }
        /* Copy volume to primary storage */
        s_logger.debug("Copying template to primary storage, template format is " + tmplVol.getFormat());
        final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        KVMPhysicalDisk primaryVol = null;
        if (destData instanceof VolumeObjectTO) {
            final VolumeObjectTO volume = (VolumeObjectTO) destData;
            // pass along volume's target size if it's bigger than template's size, for storage types that copy template rather than cloning on deploy
            if (volume.getSize() != null && volume.getSize() > tmplVol.getVirtualSize()) {
                s_logger.debug("Using configured size of " + volume.getSize());
                tmplVol.setSize(volume.getSize());
                tmplVol.setVirtualSize(volume.getSize());
            } else {
                s_logger.debug("Using template's size of " + tmplVol.getVirtualSize());
            }
            primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, volume.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
        } else if (destData instanceof TemplateObjectTO) {
            final TemplateObjectTO destTempl = (TemplateObjectTO) destData;
            primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, destTempl.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
        } else {
            primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, cmd.getWaitInMillSeconds());
        }
        DataTO data = null;
        /**
             * Force the ImageFormat for RBD templates to RAW
             *
             */
        if (destData.getObjectType() == DataObjectType.TEMPLATE) {
            final TemplateObjectTO newTemplate = new TemplateObjectTO();
            newTemplate.setPath(primaryVol.getName());
            newTemplate.setSize(primaryVol.getSize());
            if (primaryPool.getType() == StoragePoolType.RBD) {
                newTemplate.setFormat(ImageFormat.RAW);
            } else {
                newTemplate.setFormat(ImageFormat.QCOW2);
            }
            data = newTemplate;
        } else if (destData.getObjectType() == DataObjectType.VOLUME) {
            final VolumeObjectTO volumeObjectTO = new VolumeObjectTO();
            volumeObjectTO.setPath(primaryVol.getName());
            volumeObjectTO.setSize(primaryVol.getSize());
            if (primaryVol.getFormat() == PhysicalDiskFormat.RAW) {
                volumeObjectTO.setFormat(ImageFormat.RAW);
            } else if (primaryVol.getFormat() == PhysicalDiskFormat.QCOW2) {
                volumeObjectTO.setFormat(ImageFormat.QCOW2);
            }
            data = volumeObjectTO;
        }
        return new CopyCmdAnswer(data);
    } catch (final CloudRuntimeException e) {
        return new CopyCmdAnswer(e.toString());
    } finally {
        try {
            if (secondaryPool != null) {
                secondaryPool.delete();
            }
        } catch (final Exception e) {
            s_logger.debug("Failed to clean up secondary storage", e);
        }
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) NfsTO(com.cloud.agent.api.to.NfsTO) 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) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) DataTO(com.cloud.agent.api.to.DataTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 22 with PrimaryDataStoreTO

use of org.apache.cloudstack.storage.to.PrimaryDataStoreTO 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 23 with PrimaryDataStoreTO

use of org.apache.cloudstack.storage.to.PrimaryDataStoreTO in project cloudstack by apache.

the class KVMStorageProcessor method dettachVolume.

@Override
public Answer dettachVolume(final DettachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final VolumeObjectTO vol = (VolumeObjectTO) disk.getData();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) vol.getDataStore();
    final String vmName = cmd.getVmName();
    final String serial = resource.diskUuidToSerial(vol.getUuid());
    try {
        final Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
        final KVMPhysicalDisk phyDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        attachOrDetachDisk(conn, false, vmName, phyDisk, disk.getDiskSeq().intValue(), serial, vol.getBytesReadRate(), vol.getBytesWriteRate(), vol.getIopsReadRate(), vol.getIopsWriteRate());
        storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        return new DettachAnswer(disk);
    } catch (final LibvirtException e) {
        s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        return new DettachAnswer(e.toString());
    } catch (final InternalErrorException e) {
        s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        return new DettachAnswer(e.toString());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DettachAnswer(org.apache.cloudstack.storage.command.DettachAnswer) Connect(org.libvirt.Connect) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) InternalErrorException(com.cloud.exception.InternalErrorException) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 24 with PrimaryDataStoreTO

use of org.apache.cloudstack.storage.to.PrimaryDataStoreTO in project cloudstack by apache.

the class VmwareStorageProcessor method deleteVolume.

@Override
public Answer deleteVolume(DeleteCommand cmd) {
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource DeleteCommand: " + _gson.toJson(cmd));
    }
    try {
        VmwareContext context = hostService.getServiceContext(null);
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
        VolumeObjectTO vol = (VolumeObjectTO) cmd.getData();
        DataStoreTO store = vol.getDataStore();
        PrimaryDataStoreTO primaryDataStoreTO = (PrimaryDataStoreTO) store;
        Map<String, String> details = primaryDataStoreTO.getDetails();
        boolean isManaged = false;
        String managedDatastoreName = null;
        if (details != null) {
            isManaged = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));
            if (isManaged) {
                managedDatastoreName = getManagedDatastoreNameFromPath(vol.getPath());
            }
        }
        ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, isManaged ? managedDatastoreName : store.getUuid());
        if (morDs == null) {
            String msg = "Unable to find datastore based on volume mount point " + store.getUuid();
            s_logger.error(msg);
            throw new Exception(msg);
        }
        DatastoreMO dsMo = new DatastoreMO(context, morDs);
        ManagedObjectReference morDc = hyperHost.getHyperHostDatacenter();
        ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
        ClusterMO clusterMo = new ClusterMO(context, morCluster);
        if (vol.getVolumeType() == Volume.Type.ROOT) {
            String vmName = vol.getVmName();
            if (vmName != null) {
                VirtualMachineMO vmMo = clusterMo.findVmOnHyperHost(vmName);
                if (vmMo == null) {
                    // Volume might be on a zone-wide storage pool, look for VM in datacenter
                    DatacenterMO dcMo = new DatacenterMO(context, morDc);
                    vmMo = dcMo.findVm(vmName);
                }
                if (vmMo != null) {
                    if (s_logger.isInfoEnabled()) {
                        s_logger.info("Destroy root volume and VM itself. vmName " + vmName);
                    }
                    VirtualMachineDiskInfo diskInfo = null;
                    if (vol.getChainInfo() != null)
                        diskInfo = _gson.fromJson(vol.getChainInfo(), VirtualMachineDiskInfo.class);
                    HostMO hostMo = vmMo.getRunningHost();
                    List<NetworkDetails> networks = vmMo.getNetworksWithDetails();
                    // tear down all devices first before we destroy the VM to avoid accidently delete disk backing files
                    if (VmwareResource.getVmState(vmMo) != PowerState.PowerOff) {
                        vmMo.safePowerOff(_shutdownWaitMs);
                    }
                    // call this before calling detachAllDisksExcept
                    // when expunging a VM, we need to see if any of its disks are serviced by managed storage
                    // if there is one or more disk serviced by managed storage, remove the iSCSI connection(s)
                    // don't remove the iSCSI connection(s) until the supported disk(s) is/are removed from the VM
                    // (removeManagedTargetsFromCluster should be called after detachAllDisksExcept and vm.destroy)
                    List<VirtualDisk> virtualDisks = vmMo.getVirtualDisks();
                    List<String> managedIqns = getManagedIqnsFromVirtualDisks(virtualDisks);
                    List<String> detachedDisks = vmMo.detachAllDisksExcept(vol.getPath(), diskInfo != null ? diskInfo.getDiskDeviceBusName() : null);
                    VmwareStorageLayoutHelper.moveVolumeToRootFolder(new DatacenterMO(context, morDc), detachedDisks);
                    if (isManaged) {
                        vmMo.unregisterVm();
                    } else {
                        vmMo.destroy();
                    }
                    // this.hostService.handleDatastoreAndVmdkDetach(iScsiName, storageHost, storagePort);
                    if (managedIqns != null && !managedIqns.isEmpty()) {
                        removeManagedTargetsFromCluster(managedIqns);
                    }
                    for (NetworkDetails netDetails : networks) {
                        if (netDetails.getGCTag() != null && netDetails.getGCTag().equalsIgnoreCase("true")) {
                            if (netDetails.getVMMorsOnNetwork() == null || netDetails.getVMMorsOnNetwork().length == 1) {
                                resource.cleanupNetwork(hostMo, netDetails);
                            }
                        }
                    }
                }
                /*
                    if (s_logger.isInfoEnabled()) {
                        s_logger.info("Destroy volume by original name: " + vol.getPath() + ".vmdk");
                    }

                    VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vol.getPath(), new DatacenterMO(context, morDc));
                     */
                return new Answer(cmd, true, "Success");
            }
            if (s_logger.isInfoEnabled()) {
                s_logger.info("Destroy root volume directly from datastore");
            }
        }
        VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vol.getPath(), new DatacenterMO(context, morDc));
        return new Answer(cmd, true, "Success");
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            hostService.invalidateServiceContext(null);
        }
        String msg = "delete volume failed due to " + VmwareHelper.getExceptionMessage(e);
        s_logger.error(msg, e);
        return new Answer(cmd, false, msg);
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) NetworkDetails(com.cloud.hypervisor.vmware.mo.NetworkDetails) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) ClusterMO(com.cloud.hypervisor.vmware.mo.ClusterMO) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) VirtualDisk(com.vmware.vim25.VirtualDisk) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) ResignatureAnswer(org.apache.cloudstack.storage.command.ResignatureAnswer) Answer(com.cloud.agent.api.Answer) SnapshotAndCopyAnswer(org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) VirtualMachineDiskInfo(org.apache.cloudstack.utils.volume.VirtualMachineDiskInfo) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) RemoteException(java.rmi.RemoteException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Example 25 with PrimaryDataStoreTO

use of org.apache.cloudstack.storage.to.PrimaryDataStoreTO in project cloudstack by apache.

the class LibvirtRevertSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
    SnapshotObjectTO snapshot = command.getData();
    VolumeObjectTO volume = snapshot.getVolume();
    PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
    DataStoreTO snapshotImageStore = snapshot.getDataStore();
    if (!(snapshotImageStore instanceof NfsTO)) {
        return new Answer(command, false, "revert snapshot on object storage is not implemented yet");
    }
    NfsTO nfsImageStore = (NfsTO) snapshotImageStore;
    String secondaryStoragePoolUrl = nfsImageStore.getUrl();
    String volumePath = volume.getPath();
    String snapshotPath = null;
    String snapshotRelPath = null;
    KVMStoragePool secondaryStoragePool = null;
    try {
        final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
        secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
        String ssPmountPath = secondaryStoragePool.getLocalPath();
        snapshotRelPath = snapshot.getPath();
        snapshotPath = ssPmountPath + File.separator + snapshotRelPath;
        KVMPhysicalDisk snapshotDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volumePath);
        KVMStoragePool primaryPool = snapshotDisk.getPool();
        if (primaryPool.getType() == StoragePoolType.RBD) {
            return new Answer(command, false, "revert snapshot to RBD is not implemented yet");
        } else {
            Script cmd = new Script(libvirtComputingResource.manageSnapshotPath(), libvirtComputingResource.getCmdsTimeout(), s_logger);
            cmd.add("-v", snapshotPath);
            cmd.add("-n", snapshotDisk.getName());
            cmd.add("-p", snapshotDisk.getPath());
            String result = cmd.execute();
            if (result != null) {
                s_logger.debug("Failed to revert snaptshot: " + result);
                return new Answer(command, false, result);
            }
        }
        return new Answer(command, true, "RevertSnapshotCommand executes successfully");
    } catch (CloudRuntimeException e) {
        return new Answer(command, false, e.toString());
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) Answer(com.cloud.agent.api.Answer) Script(com.cloud.utils.script.Script) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO)

Aggregations

PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)33 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)24 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)21 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)20 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)19 NfsTO (com.cloud.agent.api.to.NfsTO)19 InternalErrorException (com.cloud.exception.InternalErrorException)19 DataTO (com.cloud.agent.api.to.DataTO)17 SnapshotObjectTO (org.apache.cloudstack.storage.to.SnapshotObjectTO)11 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)10 LibvirtException (org.libvirt.LibvirtException)10 Connection (com.xensource.xenapi.Connection)9 SR (com.xensource.xenapi.SR)9 XenAPIException (com.xensource.xenapi.Types.XenAPIException)9 VDI (com.xensource.xenapi.VDI)9 URI (java.net.URI)9 XmlRpcException (org.apache.xmlrpc.XmlRpcException)9 IOException (java.io.IOException)8 RadosException (com.ceph.rados.exceptions.RadosException)7 RbdException (com.ceph.rbd.RbdException)7