Search in sources :

Example 41 with Domain

use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method startDomain.

protected String startDomain(Connect conn, String vmName, String domainXML) throws LibvirtException, InternalErrorException {
    /* No duplicated vm, we will success, or failed */
    boolean failed = false;
    Domain dm = null;
    try {
        dm = conn.domainDefineXML(domainXML);
    } catch (final LibvirtException e) {
        /* Duplicated defined vm */
        s_logger.warn("Failed to define domain " + vmName + ": " + e.getMessage());
        failed = true;
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (final LibvirtException e) {
        }
    }
    /* If failed, undefine the vm */
    Domain dmOld = null;
    Domain dmNew = null;
    try {
        if (failed) {
            dmOld = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
            dmOld.undefine();
            dmNew = conn.domainDefineXML(domainXML);
        }
    } catch (final LibvirtException e) {
        s_logger.warn("Failed to define domain (second time) " + vmName + ": " + e.getMessage());
        throw e;
    } catch (Exception e) {
        s_logger.warn("Failed to define domain (second time) " + vmName + ": " + e.getMessage());
        throw new InternalErrorException(e.toString());
    } finally {
        try {
            if (dmOld != null) {
                dmOld.free();
            }
            if (dmNew != null) {
                dmNew.free();
            }
        } catch (final LibvirtException e) {
        }
    }
    /* Start the VM */
    try {
        dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        dm.create();
    } catch (LibvirtException e) {
        s_logger.warn("Failed to start domain: " + vmName + ": " + e.getMessage());
        throw e;
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (final LibvirtException e) {
        }
    }
    return null;
}
Also used : LibvirtException(org.libvirt.LibvirtException) InternalErrorException(com.cloud.exception.InternalErrorException) Domain(org.libvirt.Domain) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException)

Example 42 with Domain

use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method VifHotPlug.

private void VifHotPlug(Connect conn, String vmName, String vlanId, String macAddr) throws InternalErrorException, LibvirtException {
    NicTO nicTO = new NicTO();
    nicTO.setMac(macAddr);
    nicTO.setType(TrafficType.Public);
    if (vlanId == null) {
        nicTO.setBroadcastType(BroadcastDomainType.Native);
    } else {
        nicTO.setBroadcastType(BroadcastDomainType.Vlan);
        nicTO.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlanId));
    }
    InterfaceDef nic = createVif(conn, nicTO, InterfaceDef.nicModel.VIRTIO);
    Domain vm = getDomain(conn, vmName);
    vm.attachDevice(nic.toString());
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) Domain(org.libvirt.Domain) NicTO(com.cloud.agent.api.to.NicTO)

Example 43 with Domain

use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method rebootVM.

protected String rebootVM(Connect conn, String vmName) {
    Domain dm = null;
    String msg = null;
    try {
        dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        String vmDef = dm.getXMLDesc(0);
        s_logger.debug(vmDef);
        msg = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
        msg = startDomain(conn, vmName, vmDef);
        return null;
    } catch (LibvirtException e) {
        s_logger.warn("Failed to create vm", e);
        msg = e.getMessage();
    } catch (Exception e) {
        s_logger.warn("Failed to create vm", e);
        msg = e.getMessage();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (LibvirtException e) {
        }
    }
    return msg;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException)

Example 44 with Domain

use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

protected ManageSnapshotAnswer execute(final ManageSnapshotCommand cmd) {
    String snapshotName = cmd.getSnapshotName();
    String snapshotPath = cmd.getSnapshotPath();
    String vmName = cmd.getVmName();
    try {
        Connect conn = LibvirtConnection.getConnection();
        DomainInfo.DomainState state = null;
        Domain vm = null;
        if (vmName != null) {
            try {
                vm = getDomain(conn, cmd.getVmName());
                state = vm.getInfo().state;
            } catch (LibvirtException e) {
            }
        }
        KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(cmd.getPool().getUuid());
        KVMPhysicalDisk disk = primaryPool.getPhysicalDisk(cmd.getVolumePath());
        if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryPool.isExternalSnapshot()) {
            String vmUuid = vm.getUUIDString();
            Object[] args = new Object[] { snapshotName, vmUuid };
            String snapshot = SnapshotXML.format(args);
            s_logger.debug(snapshot);
            if (cmd.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
                vm.snapshotCreateXML(snapshot);
            } else {
                DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
                snap.delete(0);
            }
            /*
				 * libvirt on RHEL6 doesn't handle resume event emitted from
				 * qemu
				 */
            vm = getDomain(conn, cmd.getVmName());
            state = vm.getInfo().state;
            if (state == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) {
                vm.resume();
            }
        } else {
            /* VM is not running, create a snapshot by ourself */
            final Script command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
            if (cmd.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
                command.add("-c", disk.getPath());
            } else {
                command.add("-d", snapshotPath);
            }
            command.add("-n", snapshotName);
            String result = command.execute();
            if (result != null) {
                s_logger.debug("Failed to manage snapshot: " + result);
                return new ManageSnapshotAnswer(cmd, false, "Failed to manage snapshot: " + result);
            }
        }
        return new ManageSnapshotAnswer(cmd, cmd.getSnapshotId(), disk.getPath() + File.separator + snapshotName, true, null);
    } catch (LibvirtException e) {
        s_logger.debug("Failed to manage snapshot: " + e.toString());
        return new ManageSnapshotAnswer(cmd, false, "Failed to manage snapshot: " + e.toString());
    }
}
Also used : Script(com.cloud.utils.script.Script) LibvirtException(org.libvirt.LibvirtException) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) Connect(org.libvirt.Connect) DomainSnapshot(org.libvirt.DomainSnapshot) ManageSnapshotAnswer(com.cloud.agent.api.ManageSnapshotAnswer) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) DomainInfo(org.libvirt.DomainInfo) Domain(org.libvirt.Domain)

Example 45 with Domain

use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method getAllVms.

private HashMap<String, State> getAllVms() {
    final HashMap<String, State> vmStates = new HashMap<String, State>();
    String[] vms = null;
    int[] ids = null;
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
    } catch (LibvirtException e) {
        s_logger.debug("Failed to get connection: " + e.getMessage());
        return vmStates;
    }
    try {
        ids = conn.listDomains();
    } catch (final LibvirtException e) {
        s_logger.warn("Unable to listDomains", e);
        return null;
    }
    try {
        vms = conn.listDefinedDomains();
    } catch (final LibvirtException e) {
        s_logger.warn("Unable to listDomains", e);
        return null;
    }
    Domain dm = null;
    for (int i = 0; i < ids.length; i++) {
        try {
            dm = conn.domainLookupByID(ids[i]);
            DomainInfo.DomainState ps = dm.getInfo().state;
            final State state = convertToState(ps);
            s_logger.trace("VM " + dm.getName() + ": powerstate = " + ps + "; vm state=" + state.toString());
            String vmName = dm.getName();
            vmStates.put(vmName, state);
        } catch (final LibvirtException e) {
            s_logger.warn("Unable to get vms", e);
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (LibvirtException e) {
            }
        }
    }
    for (int i = 0; i < vms.length; i++) {
        try {
            dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vms[i].getBytes()));
            DomainInfo.DomainState ps = dm.getInfo().state;
            final State state = convertToState(ps);
            String vmName = dm.getName();
            s_logger.trace("VM " + vmName + ": powerstate = " + ps + "; vm state=" + state.toString());
            vmStates.put(vmName, state);
        } catch (final LibvirtException e) {
            s_logger.warn("Unable to get vms", e);
        } catch (Exception e) {
            s_logger.warn("Unable to get vms", e);
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (LibvirtException e) {
            }
        }
    }
    return vmStates;
}
Also used : LibvirtException(org.libvirt.LibvirtException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Connect(org.libvirt.Connect) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) State(com.cloud.vm.VirtualMachine.State) DomainInfo(org.libvirt.DomainInfo) Domain(org.libvirt.Domain)

Aggregations

Domain (org.libvirt.Domain)67 LibvirtException (org.libvirt.LibvirtException)54 Connect (org.libvirt.Connect)30 InternalErrorException (com.cloud.exception.InternalErrorException)24 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)17 URISyntaxException (java.net.URISyntaxException)17 IOException (java.io.IOException)16 ConfigurationException (javax.naming.ConfigurationException)16 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)15 FileNotFoundException (java.io.FileNotFoundException)14 Test (org.junit.Test)14 DomainInfo (org.libvirt.DomainInfo)14 ExecutionException (java.util.concurrent.ExecutionException)13 Answer (com.cloud.agent.api.Answer)11 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)11 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)11 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)11 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)11 NicTO (com.cloud.agent.api.to.NicTO)10 ArrayList (java.util.ArrayList)10