Search in sources :

Example 1 with Connect

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

the class LibvirtComputingResource method execute.

private Answer execute(RebootCommand cmd) {
    Long bytesReceived = null;
    Long bytesSent = null;
    synchronized (_vms) {
        _vms.put(cmd.getVmName(), State.Starting);
    }
    try {
        Connect conn = LibvirtConnection.getConnection();
        final String result = rebootVM(conn, cmd.getVmName());
        if (result == null) {
            Integer vncPort = null;
            try {
                vncPort = getVncPort(conn, cmd.getVmName());
            } catch (Exception e) {
            }
            get_rule_logs_for_vms();
            return new RebootAnswer(cmd, null, bytesSent, bytesReceived, vncPort);
        } else {
            return new RebootAnswer(cmd, result);
        }
    } catch (LibvirtException e) {
        return new RebootAnswer(cmd, e.getMessage());
    } finally {
        synchronized (_vms) {
            _vms.put(cmd.getVmName(), State.Running);
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) RebootAnswer(com.cloud.agent.api.RebootAnswer) 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 2 with Connect

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

the class LibvirtComputingResource method execute.

private Answer execute(SecurityGroupRulesCmd cmd) {
    String vif = null;
    String brname = null;
    try {
        Connect conn = LibvirtConnection.getConnection();
        List<InterfaceDef> nics = getInterfaces(conn, cmd.getVmName());
        vif = nics.get(0).getDevName();
        brname = nics.get(0).getBrName();
    } catch (LibvirtException e) {
        return new SecurityGroupRuleAnswer(cmd, false, e.toString());
    }
    boolean result = add_network_rules(cmd.getVmName(), Long.toString(cmd.getVmId()), cmd.getGuestIp(), cmd.getSignature(), Long.toString(cmd.getSeqNum()), cmd.getGuestMac(), cmd.stringifyRules(), vif, brname);
    if (!result) {
        s_logger.warn("Failed to program network rules for vm " + cmd.getVmName());
        return new SecurityGroupRuleAnswer(cmd, false, "programming network rules failed");
    } else {
        s_logger.debug("Programmed network rules for vm " + cmd.getVmName() + " guestIp=" + cmd.getGuestIp() + ",ingress numrules=" + cmd.getIngressRuleSet().length + ",egress numrules=" + cmd.getEgressRuleSet().length);
        return new SecurityGroupRuleAnswer(cmd);
    }
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) SecurityGroupRuleAnswer(com.cloud.agent.api.SecurityGroupRuleAnswer)

Example 3 with Connect

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

the class LibvirtComputingResource method execute.

protected CreatePrivateTemplateAnswer execute(CreatePrivateTemplateFromVolumeCommand cmd) {
    String secondaryStorageURL = cmd.getSecondaryStorageUrl();
    KVMStoragePool secondaryStorage = null;
    try {
        Connect conn = LibvirtConnection.getConnection();
        String templateFolder = cmd.getAccountId() + File.separator + cmd.getTemplateId() + File.separator;
        String templateInstallFolder = "/template/tmpl/" + templateFolder;
        secondaryStorage = _storagePoolMgr.getStoragePoolByURI(secondaryStorageURL);
        KVMStoragePool primary = _storagePoolMgr.getStoragePool(cmd.getPrimaryStoragePoolNameLabel());
        KVMPhysicalDisk disk = primary.getPhysicalDisk(cmd.getVolumePath());
        String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateInstallFolder;
        _storage.mkdirs(tmpltPath);
        Script command = new Script(_createTmplPath, _cmdsTimeout, s_logger);
        command.add("-f", disk.getPath());
        command.add("-t", tmpltPath);
        command.add("-n", cmd.getUniqueName() + ".qcow2");
        String result = command.execute();
        if (result != null) {
            s_logger.debug("failed to create template: " + result);
            return new CreatePrivateTemplateAnswer(cmd, false, result);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(StorageLayer.InstanceConfigKey, _storage);
        Processor qcow2Processor = new QCOW2Processor();
        qcow2Processor.configure("QCOW2 Processor", params);
        FormatInfo info = qcow2Processor.process(tmpltPath, null, cmd.getUniqueName());
        TemplateLocation loc = new TemplateLocation(_storage, tmpltPath);
        loc.create(1, true, cmd.getUniqueName());
        loc.addFormat(info);
        loc.save();
        return new CreatePrivateTemplateAnswer(cmd, true, null, templateInstallFolder + cmd.getUniqueName() + ".qcow2", info.virtualSize, info.size, cmd.getUniqueName(), ImageFormat.QCOW2);
    } catch (LibvirtException e) {
        s_logger.debug("Failed to get secondary storage pool: " + e.toString());
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (InternalErrorException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (IOException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (ConfigurationException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } catch (CloudRuntimeException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
    } finally {
        if (secondaryStorage != null) {
            secondaryStorage.delete();
        }
    }
}
Also used : Script(com.cloud.utils.script.Script) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) Processor(com.cloud.storage.template.Processor) LibvirtException(org.libvirt.LibvirtException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) ConfigurationException(javax.naming.ConfigurationException) TemplateLocation(com.cloud.storage.template.TemplateLocation) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) FormatInfo(com.cloud.storage.template.Processor.FormatInfo)

Example 4 with Connect

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

the class LibvirtComputingResource method execute.

protected GetVncPortAnswer execute(GetVncPortCommand cmd) {
    try {
        Connect conn = LibvirtConnection.getConnection();
        Integer vncPort = getVncPort(conn, cmd.getName());
        return new GetVncPortAnswer(cmd, _privateIp, 5900 + vncPort);
    } catch (Exception e) {
        return new GetVncPortAnswer(cmd, e.toString());
    }
}
Also used : GetVncPortAnswer(com.cloud.agent.api.GetVncPortAnswer) 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)

Example 5 with Connect

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

the class LibvirtComputingResource method execute.

private AttachVolumeAnswer execute(AttachVolumeCommand cmd) {
    try {
        Connect conn = LibvirtConnection.getConnection();
        KVMStoragePool primary = _storagePoolMgr.getStoragePool(cmd.getPoolUuid());
        KVMPhysicalDisk disk = primary.getPhysicalDisk(cmd.getVolumePath());
        attachOrDetachDisk(conn, cmd.getAttach(), cmd.getVmName(), disk, cmd.getDeviceId().intValue());
    } catch (LibvirtException e) {
        return new AttachVolumeAnswer(cmd, e.toString());
    } catch (InternalErrorException e) {
        return new AttachVolumeAnswer(cmd, e.toString());
    }
    return new AttachVolumeAnswer(cmd, cmd.getDeviceId());
}
Also used : AttachVolumeAnswer(com.cloud.agent.api.AttachVolumeAnswer) LibvirtException(org.libvirt.LibvirtException) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.exception.InternalErrorException)

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