Search in sources :

Example 61 with Connect

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

the class LibvirtStorageAdaptor method deleteStoragePool.

@Override
public boolean deleteStoragePool(String uuid) {
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
    } catch (LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
    StoragePool sp = null;
    try {
        sp = conn.storagePoolLookupByUUIDString(uuid);
    } catch (LibvirtException e) {
        return true;
    }
    try {
        sp.destroy();
        sp.undefine();
        sp.free();
        return true;
    } catch (LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) StoragePool(org.libvirt.StoragePool) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connect(org.libvirt.Connect)

Example 62 with Connect

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

the class LibvirtComputingResource method execute.

private synchronized Answer execute(MigrateCommand cmd) {
    String vmName = cmd.getVmName();
    State state = null;
    String result = null;
    synchronized (_vms) {
        state = _vms.get(vmName);
        _vms.put(vmName, State.Stopping);
    }
    Domain dm = null;
    Connect dconn = null;
    Domain destDomain = null;
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
        dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        dconn = new Connect("qemu+tcp://" + cmd.getDestinationIp() + "/system");
        /*
			 * Hard code lm flags: VIR_MIGRATE_LIVE(1<<0) and
			 * VIR_MIGRATE_PERSIST_DEST(1<<3)
			 */
        destDomain = dm.migrate(dconn, (1 << 0) | (1 << 3), vmName, "tcp:" + cmd.getDestinationIp(), _migrateSpeed);
    } catch (LibvirtException e) {
        s_logger.debug("Can't migrate domain: " + e.getMessage());
        result = e.getMessage();
    } catch (Exception e) {
        s_logger.debug("Can't migrate domain: " + e.getMessage());
        result = e.getMessage();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
            if (dconn != null) {
                dconn.close();
            }
            if (destDomain != null) {
                destDomain.free();
            }
        } catch (final LibvirtException e) {
        }
    }
    if (result != null) {
        synchronized (_vms) {
            _vms.put(vmName, state);
        }
    } else {
        destroy_network_rules_for_vm(conn, vmName);
        cleanupVM(conn, vmName, getVnetId(VirtualMachineName.getVnet(vmName)));
    }
    return new MigrateAnswer(cmd, result == null, result, null);
}
Also used : MigrateAnswer(com.cloud.agent.api.MigrateAnswer) LibvirtException(org.libvirt.LibvirtException) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) 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 63 with Connect

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

the class CloudZonesComputingResource method configure.

@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    boolean success = super.configure(name, params);
    if (!success) {
        return false;
    }
    _parent = (String) params.get("mount.path");
    try {
        _dhcpTimeout = Long.parseLong((String) params.get("dhcp.timeout"));
    } catch (Exception e) {
        _dhcpTimeout = 1200000;
    }
    _hostIp = (String) params.get("host.ip");
    _hostMacAddress = (String) params.get("host.mac.address");
    try {
        Connect conn;
        conn = LibvirtConnection.getConnection();
        setupDhcpManager(conn, _guestBridgeName);
    } catch (LibvirtException e) {
        s_logger.debug("Failed to get libvirt connection:" + e.toString());
        return false;
    }
    _dhcpSnooper.configure(name, params);
    _vmDataServer.configure(name, params);
    return true;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ConfigurationException(javax.naming.ConfigurationException) LibvirtException(org.libvirt.LibvirtException)

Example 64 with Connect

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

the class CloudZonesComputingResource method execute.

@Override
protected synchronized StartAnswer execute(StartCommand cmd) {
    VirtualMachineTO vmSpec = cmd.getVirtualMachine();
    String vmName = vmSpec.getName();
    LibvirtVMDef vm = null;
    State state = State.Stopped;
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
        synchronized (_vms) {
            _vms.put(vmName, State.Starting);
        }
        vm = createVMFromSpec(vmSpec);
        createVbd(conn, vmSpec, vmName, vm);
        createVifs(conn, vmSpec, vm);
        s_logger.debug("starting " + vmName + ": " + vm.toString());
        startDomain(conn, vmName, vm.toString());
        NicTO[] nics = vmSpec.getNics();
        for (NicTO nic : nics) {
            if (nic.isSecurityGroupEnabled()) {
                if (vmSpec.getType() != VirtualMachine.Type.User) {
                    default_network_rules_for_systemvm(conn, vmName);
                } else {
                    nic.setIp(null);
                    default_network_rules(conn, vmName, nic, vmSpec.getId());
                }
            }
        }
        // attached disk
        for (DiskDef disk : vm.getDevices().getDisks()) {
            if (disk.isAttachDeferred()) {
                attachOrDetachDevice(conn, true, vmName, disk.toString());
            }
        }
        if (vmSpec.getType() == VirtualMachine.Type.User) {
            for (NicTO nic : nics) {
                if (nic.getType() == TrafficType.Guest) {
                    InetAddress ipAddr = _dhcpSnooper.getIPAddr(nic.getMac(), vmName);
                    if (ipAddr == null) {
                        s_logger.debug("Failed to get guest DHCP ip, stop it");
                        StopCommand stpCmd = new StopCommand(vmName);
                        execute(stpCmd);
                        return new StartAnswer(cmd, "Failed to get guest DHCP ip, stop it");
                    }
                    s_logger.debug(ipAddr);
                    nic.setIp(ipAddr.getHostAddress());
                    post_default_network_rules(conn, vmName, nic, vmSpec.getId(), _dhcpSnooper.getDhcpServerIP(), _hostIp, _hostMacAddress);
                    _vmDataServer.handleVmStarted(cmd.getVirtualMachine());
                }
            }
        }
        state = State.Running;
        return new StartAnswer(cmd);
    } catch (Exception e) {
        s_logger.warn("Exception ", e);
        if (conn != null) {
            handleVmStartFailure(conn, vmName, vm);
        }
        return new StartAnswer(cmd, e.getMessage());
    } finally {
        synchronized (_vms) {
            if (state != State.Stopped) {
                _vms.put(vmName, state);
            } else {
                _vms.remove(vmName);
            }
        }
    }
}
Also used : LibvirtVMDef(com.cloud.agent.resource.computing.LibvirtVMDef) DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) StopCommand(com.cloud.agent.api.StopCommand) StartAnswer(com.cloud.agent.api.StartAnswer) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) InetAddress(java.net.InetAddress) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) ConfigurationException(javax.naming.ConfigurationException) LibvirtException(org.libvirt.LibvirtException) NicTO(com.cloud.agent.api.to.NicTO)

Example 65 with Connect

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

the class LibvirtComputingResource method getRealPowerState.

protected State getRealPowerState(String vm) {
    int i = 0;
    s_logger.trace("Checking on the HALTED State");
    Domain dm = null;
    for (; i < 5; i++) {
        try {
            Connect conn = LibvirtConnection.getConnection();
            dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vm.getBytes()));
            DomainInfo.DomainState vps = dm.getInfo().state;
            if (vps != null && vps != DomainInfo.DomainState.VIR_DOMAIN_SHUTOFF && vps != DomainInfo.DomainState.VIR_DOMAIN_NOSTATE) {
                return convertToState(vps);
            }
        } catch (final LibvirtException e) {
            s_logger.trace(e.getMessage());
        } catch (Exception e) {
            s_logger.trace(e.getMessage());
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (final LibvirtException e) {
            }
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
    }
    return State.Stopped;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) DomainInfo(org.libvirt.DomainInfo) 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)

Aggregations

Connect (org.libvirt.Connect)113 LibvirtException (org.libvirt.LibvirtException)112 Answer (com.cloud.agent.api.Answer)47 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)42 Test (org.junit.Test)40 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)39 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)39 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)39 InternalErrorException (com.cloud.exception.InternalErrorException)33 Domain (org.libvirt.Domain)30 URISyntaxException (java.net.URISyntaxException)25 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)24 NicTO (com.cloud.agent.api.to.NicTO)23 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)22 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)19 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)16 ConfigurationException (javax.naming.ConfigurationException)15 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)13 HashMap (java.util.HashMap)11