Search in sources :

Example 61 with LibvirtException

use of org.libvirt.LibvirtException in project cloudstack by apache.

the class LibvirtConnection method getConnectionByVmName.

public static Connect getConnectionByVmName(String vmName) throws LibvirtException {
    HypervisorType[] hypervisors = new HypervisorType[] { HypervisorType.KVM, Hypervisor.HypervisorType.LXC };
    for (HypervisorType hypervisor : hypervisors) {
        try {
            Connect conn = LibvirtConnection.getConnectionByType(hypervisor.toString());
            if (conn.domainLookupByName(vmName) != null) {
                return conn;
            }
        } catch (Exception e) {
            s_logger.debug("Can not find " + hypervisor.toString() + " connection for Instance: " + vmName + ", continuing.");
        }
    }
    s_logger.warn("Can not find a connection for Instance " + vmName + ". Assuming the default connection.");
    // return the default connection
    return getConnection();
}
Also used : HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) Connect(org.libvirt.Connect) LibvirtException(org.libvirt.LibvirtException)

Example 62 with LibvirtException

use of org.libvirt.LibvirtException in project cloudstack by apache.

the class KVMStorageProcessor method attachVolume.

@Override
public Answer attachVolume(final AttachCommand 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);
        storagePoolMgr.connectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath(), disk.getDetails());
        final KVMPhysicalDisk phyDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        attachOrDetachDisk(conn, true, vmName, phyDisk, disk.getDiskSeq().intValue(), serial, vol.getBytesReadRate(), vol.getBytesWriteRate(), vol.getIopsReadRate(), vol.getIopsWriteRate());
        return new AttachAnswer(disk);
    } catch (final LibvirtException e) {
        s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        return new AttachAnswer(e.toString());
    } catch (final InternalErrorException e) {
        s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        return new AttachAnswer(e.toString());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) Connect(org.libvirt.Connect) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) InternalErrorException(com.cloud.exception.InternalErrorException) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Example 63 with LibvirtException

use of org.libvirt.LibvirtException in project cloudstack by apache.

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 e) {
        return new Answer(cmd, false, e.toString());
    } catch (final URISyntaxException e) {
        return new Answer(cmd, false, e.toString());
    } catch (final InternalErrorException e) {
        return new Answer(cmd, false, e.toString());
    }
    return new Answer(cmd);
}
Also used : CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) ResignatureAnswer(org.apache.cloudstack.storage.command.ResignatureAnswer) Answer(com.cloud.agent.api.Answer) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) DettachAnswer(org.apache.cloudstack.storage.command.DettachAnswer) 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) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) TemplateObjectTO(org.apache.cloudstack.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(org.apache.cloudstack.storage.command.AttachAnswer)

Example 64 with LibvirtException

use of org.libvirt.LibvirtException in project cloudstack by apache.

the class LibvirtResizeVolumeCommandWrapper method execute.

@Override
public Answer execute(final ResizeVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final String volid = command.getPath();
    final long newSize = command.getNewSize();
    final long currentSize = command.getCurrentSize();
    final String vmInstanceName = command.getInstanceName();
    final boolean shrinkOk = command.getShrinkOk();
    final StorageFilerTO spool = command.getPool();
    final String notifyOnlyType = "NOTIFYONLY";
    if (currentSize == newSize) {
        // nothing to do
        s_logger.info("No need to resize volume: current size " + currentSize + " is same as new size " + newSize);
        return new ResizeVolumeAnswer(command, true, "success", currentSize);
    }
    try {
        final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
        KVMStoragePool pool = storagePoolMgr.getStoragePool(spool.getType(), spool.getUuid());
        final KVMPhysicalDisk vol = pool.getPhysicalDisk(volid);
        final String path = vol.getPath();
        String type = notifyOnlyType;
        if (pool.getType() != StoragePoolType.RBD) {
            type = libvirtComputingResource.getResizeScriptType(pool, vol);
            if (type.equals("QCOW2") && shrinkOk) {
                return new ResizeVolumeAnswer(command, false, "Unable to shrink volumes of type " + type);
            }
        } else {
            s_logger.debug("Volume " + path + " is on a RBD storage pool. No need to query for additional information.");
        }
        s_logger.debug("Resizing volume: " + path + "," + currentSize + "," + newSize + "," + type + "," + vmInstanceName + "," + shrinkOk);
        /* libvirt doesn't support resizing (C)LVM devices, and corrupts QCOW2 in some scenarios, so we have to do these via Bash script */
        if (pool.getType() != StoragePoolType.CLVM && vol.getFormat() != PhysicalDiskFormat.QCOW2) {
            s_logger.debug("Volume " + path + " can be resized by libvirt. Asking libvirt to resize the volume.");
            try {
                final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
                final Connect conn = libvirtUtilitiesHelper.getConnection();
                final StorageVol v = conn.storageVolLookupByPath(path);
                int flags = 0;
                if (conn.getLibVirVersion() > 1001000 && vol.getFormat() == PhysicalDiskFormat.RAW && pool.getType() != StoragePoolType.RBD) {
                    flags = 1;
                }
                if (shrinkOk) {
                    flags = 4;
                }
                v.resize(newSize, flags);
            } catch (final LibvirtException e) {
                return new ResizeVolumeAnswer(command, false, e.toString());
            }
        }
        s_logger.debug("Invoking resize script to handle type " + type);
        final Script resizecmd = new Script(libvirtComputingResource.getResizeVolumePath(), libvirtComputingResource.getCmdsTimeout(), s_logger);
        resizecmd.add("-s", String.valueOf(newSize));
        resizecmd.add("-c", String.valueOf(currentSize));
        resizecmd.add("-p", path);
        resizecmd.add("-t", type);
        resizecmd.add("-r", String.valueOf(shrinkOk));
        resizecmd.add("-v", vmInstanceName);
        final String result = resizecmd.execute();
        if (result != null) {
            if (type.equals(notifyOnlyType)) {
                return new ResizeVolumeAnswer(command, true, "Resize succeeded, but need reboot to notify guest");
            } else {
                return new ResizeVolumeAnswer(command, false, result);
            }
        }
        /* fetch new size as seen from libvirt, don't want to assume anything */
        pool = storagePoolMgr.getStoragePool(spool.getType(), spool.getUuid());
        pool.refresh();
        final long finalSize = pool.getPhysicalDisk(volid).getVirtualSize();
        s_logger.debug("after resize, size reports as " + finalSize + ", requested " + newSize);
        return new ResizeVolumeAnswer(command, true, "success", finalSize);
    } catch (final CloudRuntimeException e) {
        final String error = "Failed to resize volume: " + e.getMessage();
        s_logger.debug(error);
        return new ResizeVolumeAnswer(command, false, error);
    }
}
Also used : Script(com.cloud.utils.script.Script) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) StorageVol(org.libvirt.StorageVol) LibvirtException(org.libvirt.LibvirtException) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) Connect(org.libvirt.Connect) ResizeVolumeAnswer(com.cloud.agent.api.storage.ResizeVolumeAnswer) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 65 with LibvirtException

use of org.libvirt.LibvirtException in project cloudstack by apache.

the class LibvirtRestoreVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RestoreVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
    String vmName = cmd.getVmName();
    List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
    VirtualMachine.PowerState vmState = VirtualMachine.PowerState.PowerOn;
    Domain dm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        Connect conn = libvirtUtilitiesHelper.getConnection();
        dm = libvirtComputingResource.getDomain(conn, vmName);
        if (dm == null) {
            return new RestoreVMSnapshotAnswer(cmd, false, "Restore VM Snapshot Failed due to can not find vm: " + vmName);
        }
        String xmlDesc = dm.getXMLDesc(0);
        List<VMSnapshotTO> snapshots = cmd.getSnapshots();
        Map<Long, VMSnapshotTO> snapshotAndParents = cmd.getSnapshotAndParents();
        for (VMSnapshotTO snapshot : snapshots) {
            VMSnapshotTO parent = snapshotAndParents.get(snapshot.getId());
            String vmSnapshotXML = libvirtUtilitiesHelper.generateVMSnapshotXML(snapshot, parent, xmlDesc);
            s_logger.debug("Restoring vm snapshot " + snapshot.getSnapshotName() + " on " + vmName + " with XML:\n " + vmSnapshotXML);
            try {
                // VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE = 1
                int flags = 1;
                if (snapshot.getCurrent()) {
                    // VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT = 2
                    flags += 2;
                }
                dm.snapshotCreateXML(vmSnapshotXML, flags);
            } catch (LibvirtException e) {
                s_logger.debug("Failed to restore vm snapshot " + snapshot.getSnapshotName() + " on " + vmName);
                return new RestoreVMSnapshotAnswer(cmd, false, e.toString());
            }
        }
        return new RestoreVMSnapshotAnswer(cmd, listVolumeTo, vmState);
    } catch (LibvirtException e) {
        String msg = " Restore snapshot failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new RestoreVMSnapshotAnswer(cmd, false, msg);
    } finally {
        if (dm != null) {
            try {
                dm.free();
            } catch (LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
            ;
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) Domain(org.libvirt.Domain) VirtualMachine(com.cloud.vm.VirtualMachine)

Aggregations

LibvirtException (org.libvirt.LibvirtException)176 Connect (org.libvirt.Connect)109 Answer (com.cloud.agent.api.Answer)63 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)58 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)55 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)55 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)55 Test (org.junit.Test)55 Domain (org.libvirt.Domain)53 InternalErrorException (com.cloud.exception.InternalErrorException)41 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)41 URISyntaxException (java.net.URISyntaxException)32 StoragePool (org.libvirt.StoragePool)27 NicTO (com.cloud.agent.api.to.NicTO)25 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)23 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)22 ConfigurationException (javax.naming.ConfigurationException)22 IOException (java.io.IOException)21 ArrayList (java.util.ArrayList)21 FileNotFoundException (java.io.FileNotFoundException)17