Search in sources :

Example 26 with PrimaryDataStoreTO

use of com.cloud.legacymodel.to.PrimaryDataStoreTO in project cosmic by MissionCriticalCloud.

the class LibvirtDeleteVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final DeleteVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
    final String vmName = cmd.getVmName();
    final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    Domain dm = null;
    DomainSnapshot snapshot = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnection();
        dm = libvirtComputingResource.getDomain(conn, vmName);
        snapshot = dm.snapshotLookupByName(cmd.getTarget().getSnapshotName());
        // only remove this snapshot, not children
        snapshot.delete(0);
        return new DeleteVMSnapshotAnswer(cmd, cmd.getVolumeTOs());
    } catch (final LibvirtException e) {
        final String msg = " Delete VM snapshot failed due to " + e.toString();
        if (dm == null) {
            s_logger.debug("Can not find running vm: " + vmName + ", now we are trying to delete the vm snapshot using qemu-img if the format of root volume is QCOW2");
            VolumeObjectTO rootVolume = null;
            for (final VolumeObjectTO volume : cmd.getVolumeTOs()) {
                if (volume.getVolumeType() == VolumeType.ROOT) {
                    rootVolume = volume;
                    break;
                }
            }
            if (rootVolume != null && ImageFormat.QCOW2.equals(rootVolume.getFormat())) {
                final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) rootVolume.getDataStore();
                final KvmPhysicalDisk rootDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), rootVolume.getPath());
                final String command = "qemu-img snapshot -l " + rootDisk.getPath() + " | tail -n +3 | awk -F ' ' '{print $2}' | grep ^" + cmd.getTarget().getSnapshotName() + "$";
                final String qemu_img_snapshot = Script.runSimpleBashScript(command);
                if (qemu_img_snapshot == null) {
                    s_logger.info("Cannot find snapshot " + cmd.getTarget().getSnapshotName() + " in file " + rootDisk.getPath() + ", return true");
                    return new DeleteVMSnapshotAnswer(cmd, cmd.getVolumeTOs());
                }
                final int result = Script.runSimpleBashScriptForExitValue("qemu-img snapshot -d " + cmd.getTarget().getSnapshotName() + " " + rootDisk.getPath());
                if (result != 0) {
                    return new DeleteVMSnapshotAnswer(cmd, false, "Delete VM Snapshot Failed due to can not remove snapshot from image file " + rootDisk.getPath() + " : " + result);
                } else {
                    return new DeleteVMSnapshotAnswer(cmd, cmd.getVolumeTOs());
                }
            }
        } else if (snapshot == null) {
            s_logger.debug("Can not find vm snapshot " + cmd.getTarget().getSnapshotName() + " on vm: " + vmName + ", return true");
            return new DeleteVMSnapshotAnswer(cmd, cmd.getVolumeTOs());
        }
        s_logger.warn(msg, e);
        return new DeleteVMSnapshotAnswer(cmd, false, msg);
    } finally {
        if (dm != null) {
            try {
                dm.free();
            } catch (final LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) PrimaryDataStoreTO(com.cloud.legacymodel.to.PrimaryDataStoreTO) Connect(org.libvirt.Connect) DomainSnapshot(org.libvirt.DomainSnapshot) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) DeleteVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.DeleteVMSnapshotAnswer) Domain(org.libvirt.Domain) KvmPhysicalDisk(com.cloud.agent.resource.kvm.storage.KvmPhysicalDisk)

Aggregations

PrimaryDataStoreTO (com.cloud.legacymodel.to.PrimaryDataStoreTO)26 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)22 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)19 CopyCmdAnswer (com.cloud.legacymodel.communication.answer.CopyCmdAnswer)17 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)17 NfsTO (com.cloud.legacymodel.to.NfsTO)17 DataTO (com.cloud.legacymodel.to.DataTO)15 DataStoreTO (com.cloud.legacymodel.to.DataStoreTO)14 SnapshotObjectTO (com.cloud.legacymodel.to.SnapshotObjectTO)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 LibvirtException (org.libvirt.LibvirtException)9 TemplateObjectTO (com.cloud.legacymodel.to.TemplateObjectTO)8 IOException (java.io.IOException)7 QemuImgException (com.cloud.agent.resource.kvm.storage.utils.QemuImgException)6 FileNotFoundException (java.io.FileNotFoundException)6