Search in sources :

Example 31 with State

use of com.cloud.vm.VirtualMachine.State 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)

Example 32 with State

use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method initialize.

@Override
public StartupCommand[] initialize() {
    Map<String, State> changes = null;
    synchronized (_vms) {
        _vms.clear();
        changes = sync();
    }
    final List<Object> info = getHostInfo();
    final StartupRoutingCommand cmd = new StartupRoutingCommand((Integer) info.get(0), (Long) info.get(1), (Long) info.get(2), (Long) info.get(4), (String) info.get(3), HypervisorType.KVM, RouterPrivateIpStrategy.HostLocal);
    cmd.setStateChanges(changes);
    fillNetworkInformation(cmd);
    _privateIp = cmd.getPrivateIpAddress();
    cmd.getHostDetails().putAll(getVersionStrings());
    cmd.setPool(_pool);
    cmd.setCluster(_clusterId);
    cmd.setGatewayIpAddress(_localGateway);
    StartupStorageCommand sscmd = null;
    try {
        KVMStoragePool localStoragePool = _storagePoolMgr.createStoragePool(_localStorageUUID, "localhost", _localStoragePath, StoragePoolType.Filesystem);
        com.cloud.agent.api.StoragePoolInfo pi = new com.cloud.agent.api.StoragePoolInfo(localStoragePool.getUuid(), cmd.getPrivateIpAddress(), _localStoragePath, _localStoragePath, StoragePoolType.Filesystem, localStoragePool.getCapacity(), localStoragePool.getUsed());
        sscmd = new StartupStorageCommand();
        sscmd.setPoolInfo(pi);
        sscmd.setGuid(pi.getUuid());
        sscmd.setDataCenter(_dcId);
        sscmd.setResourceType(Storage.StorageResourceType.STORAGE_POOL);
    } catch (CloudRuntimeException e) {
    }
    if (sscmd != null) {
        return new StartupCommand[] { cmd, sscmd };
    } else {
        return new StartupCommand[] { cmd };
    }
}
Also used : StartupStorageCommand(com.cloud.agent.api.StartupStorageCommand) StartupCommand(com.cloud.agent.api.StartupCommand) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) State(com.cloud.vm.VirtualMachine.State) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand)

Example 33 with State

use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method getVmState.

protected State getVmState(Connect conn, final String vmName) {
    int retry = 3;
    Domain vms = null;
    while (retry-- > 0) {
        try {
            vms = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
            State s = convertToState(vms.getInfo().state);
            return s;
        } catch (final LibvirtException e) {
            s_logger.warn("Can't get vm state " + vmName + e.getMessage() + "retry:" + retry);
        } catch (Exception e) {
            s_logger.warn("Can't get vm state " + vmName + e.getMessage() + "retry:" + retry);
        } finally {
            try {
                if (vms != null) {
                    vms.free();
                }
            } catch (final LibvirtException e) {
            }
        }
    }
    return State.Stopped;
}
Also used : LibvirtException(org.libvirt.LibvirtException) State(com.cloud.vm.VirtualMachine.State) 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 34 with State

use of com.cloud.vm.VirtualMachine.State in project cloudstack by apache.

the class AffinityGroupServiceImpl method postStateTransitionEvent.

@Override
public boolean postStateTransitionEvent(StateMachine2.Transition<State, Event> transition, VirtualMachine vo, boolean status, Object opaque) {
    if (!status) {
        return false;
    }
    State newState = transition.getToState();
    if ((newState == State.Expunging) || (newState == State.Error)) {
        // cleanup all affinity groups associations of the Expunged VM
        SearchCriteria<AffinityGroupVMMapVO> sc = _affinityGroupVMMapDao.createSearchCriteria();
        sc.addAnd("instanceId", SearchCriteria.Op.EQ, vo.getId());
        _affinityGroupVMMapDao.expunge(sc);
    }
    return true;
}
Also used : State(com.cloud.vm.VirtualMachine.State)

Example 35 with State

use of com.cloud.vm.VirtualMachine.State in project cloudstack by apache.

the class Ovm3HypervisorSupport method getAllVmStates.

/**
     * getAllVmStates: Get the state of all the VMs
     *
     * @return
     * @throws Ovm3ResourceException
     */
private Map<String, State> getAllVmStates(Map<String, State> vmStateMap) throws Ovm3ResourceException {
    Map<String, Xen.Vm> vms = getAllVms();
    final Map<String, State> states = new HashMap<String, State>();
    for (final Map.Entry<String, Xen.Vm> entry : vms.entrySet()) {
        Xen.Vm vm = entry.getValue();
        State ns = State.Running;
        String as = vm.getVmState();
        if (vm.isControlDomain() || as == null) {
            continue;
        }
        /* need a more exact match! */
        if (as.contains("r")) {
            ns = State.Running;
        /* The domain is blocked, and not running or runnable. */
        } else if (as.contains("b")) {
            ns = State.Running;
        /* The domain has been paused */
        } else if (as.contains("p")) {
            ns = State.Running;
        /* The guest has requested to be shutdown, still migrating... */
        } else if (as.contains("s")) {
            if (vmStateMap.get(vm.getVmName()) == State.Migrating) {
                ns = State.Migrating;
            } else {
                ns = State.Stopped;
            }
        /* The domain has crashed */
        } else if (as.contains("c")) {
            ns = State.Error;
        /*
                 * The domain is in process of dying (if we see this twice we
                 * have a problem ?)
                 */
        } else if (as.contains("d")) {
            ns = State.Stopping;
        } else {
            ns = State.Unknown;
        }
        LOGGER.trace("state " + ns + " for " + vm.getVmName() + " based on " + as);
        states.put(vm.getVmName(), ns);
    }
    return states;
}
Also used : Xen(com.cloud.hypervisor.ovm3.objects.Xen) HashMap(java.util.HashMap) PowerState(com.cloud.vm.VirtualMachine.PowerState) State(com.cloud.vm.VirtualMachine.State) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

State (com.cloud.vm.VirtualMachine.State)45 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)14 ConfigurationException (javax.naming.ConfigurationException)11 HashMap (java.util.HashMap)9 PowerState (com.cloud.vm.VirtualMachine.PowerState)8 LibvirtException (org.libvirt.LibvirtException)8 CheckVirtualMachineAnswer (com.cloud.agent.api.CheckVirtualMachineAnswer)7 StartAnswer (com.cloud.agent.api.StartAnswer)7 StopAnswer (com.cloud.agent.api.StopAnswer)7 Map (java.util.Map)7 Connect (org.libvirt.Connect)7 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 Ovm3ResourceException (com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException)5 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 VmState (com.cloud.agent.api.StartupRoutingCommand.VmState)4 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)4 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)4 Answer (com.cloud.agent.api.Answer)3 NicTO (com.cloud.agent.api.to.NicTO)3