Search in sources :

Example 11 with Connect

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

the class LibvirtComputingResource method execute.

private Answer execute(NetworkRulesSystemVmCommand cmd) {
    boolean success = false;
    Connect conn;
    try {
        conn = LibvirtConnection.getConnection();
        success = default_network_rules_for_systemvm(conn, cmd.getVmName());
    } catch (LibvirtException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return new Answer(cmd, success, "");
}
Also used : FenceAnswer(com.cloud.agent.api.FenceAnswer) ConsoleProxyLoadAnswer(com.cloud.agent.api.proxy.ConsoleProxyLoadAnswer) DeleteSnapshotBackupAnswer(com.cloud.agent.api.DeleteSnapshotBackupAnswer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) GetHostStatsAnswer(com.cloud.agent.api.GetHostStatsAnswer) CheckSshAnswer(com.cloud.agent.api.check.CheckSshAnswer) GetVncPortAnswer(com.cloud.agent.api.GetVncPortAnswer) RebootAnswer(com.cloud.agent.api.RebootAnswer) ManageSnapshotAnswer(com.cloud.agent.api.ManageSnapshotAnswer) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) AttachVolumeAnswer(com.cloud.agent.api.AttachVolumeAnswer) ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) StartAnswer(com.cloud.agent.api.StartAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) MigrateAnswer(com.cloud.agent.api.MigrateAnswer) CreateVolumeFromSnapshotAnswer(com.cloud.agent.api.CreateVolumeFromSnapshotAnswer) CheckNetworkAnswer(com.cloud.agent.api.CheckNetworkAnswer) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) StopAnswer(com.cloud.agent.api.StopAnswer) NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer) Answer(com.cloud.agent.api.Answer) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) CheckHealthAnswer(com.cloud.agent.api.CheckHealthAnswer) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) PrepareForMigrationAnswer(com.cloud.agent.api.PrepareForMigrationAnswer) SecurityGroupRuleAnswer(com.cloud.agent.api.SecurityGroupRuleAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect)

Example 12 with Connect

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

the class CloudZonesComputingResource method execute.

protected Answer execute(StopCommand cmd) {
    final String vmName = cmd.getVmName();
    Long bytesReceived = new Long(0);
    Long bytesSent = new Long(0);
    State state = null;
    synchronized (_vms) {
        state = _vms.get(vmName);
        _vms.put(vmName, State.Stopping);
    }
    try {
        Connect conn = LibvirtConnection.getConnection();
        try {
            Domain dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        } catch (LibvirtException e) {
            state = State.Stopped;
            return new StopAnswer(cmd, null, 0, bytesSent, bytesReceived);
        }
        String macAddress = null;
        if (vmName.startsWith("i-")) {
            List<InterfaceDef> nics = getInterfaces(conn, vmName);
            if (!nics.isEmpty()) {
                macAddress = nics.get(0).getMacAddress();
            }
        }
        destroy_network_rules_for_vm(conn, vmName);
        String result = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
        try {
            cleanupVnet(conn, cmd.getVnet());
            _dhcpSnooper.cleanup(macAddress, vmName);
            _vmDataServer.handleVmStopped(cmd.getVmName());
        } catch (Exception e) {
        }
        state = State.Stopped;
        return new StopAnswer(cmd, result, 0, bytesSent, bytesReceived);
    } catch (LibvirtException e) {
        return new StopAnswer(cmd, e.getMessage());
    } finally {
        synchronized (_vms) {
            if (state != null) {
                _vms.put(vmName, state);
            } else {
                _vms.remove(vmName);
            }
        }
    }
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) Domain(org.libvirt.Domain) StopAnswer(com.cloud.agent.api.StopAnswer) ConfigurationException(javax.naming.ConfigurationException) LibvirtException(org.libvirt.LibvirtException)

Example 13 with Connect

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

the class LibvirtComputingResource method stop.

@Override
public boolean stop() {
    try {
        Connect conn = LibvirtConnection.getConnection();
        conn.close();
    } catch (LibvirtException e) {
    }
    return true;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect)

Example 14 with Connect

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

the class LibvirtComputingResource method execute.

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());
        Script.runSimpleBashScript("virsh schedinfo " + vmName + " --set cpu_shares=" + vmSpec.getCpus() * vmSpec.getSpeed());
        NicTO[] nics = vmSpec.getNics();
        for (NicTO nic : nics) {
            if (nic.getIsolationUri() != null && nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString())) {
                if (vmSpec.getType() != VirtualMachine.Type.User) {
                    default_network_rules_for_systemvm(conn, vmName);
                    break;
                } else {
                    default_network_rules(conn, vmName, nic, vmSpec.getId());
                }
            }
        }
        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 : StartAnswer(com.cloud.agent.api.StartAnswer) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) 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) NicTO(com.cloud.agent.api.to.NicTO)

Example 15 with Connect

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

the class LibvirtComputingResource method execute.

public Answer execute(IpAssocCommand cmd) {
    String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    String[] results = new String[cmd.getIpAddresses().length];
    Connect conn;
    try {
        conn = LibvirtConnection.getConnection();
        List<InterfaceDef> nics = getInterfaces(conn, routerName);
        Map<String, Integer> vlanAllocatedToVM = new HashMap<String, Integer>();
        Integer nicPos = 0;
        for (InterfaceDef nic : nics) {
            if (nic.getBrName().equalsIgnoreCase(_linkLocalBridgeName)) {
                vlanAllocatedToVM.put("LinkLocal", nicPos);
            } else {
                String vlanId = getVlanIdFromBridge(nic.getBrName());
                if (vlanId != null) {
                    vlanAllocatedToVM.put(vlanId, nicPos);
                } else {
                    vlanAllocatedToVM.put(Vlan.UNTAGGED, nicPos);
                }
            }
            nicPos++;
        }
        IpAddressTO[] ips = cmd.getIpAddresses();
        int i = 0;
        String result = null;
        int nicNum = 0;
        for (IpAddressTO ip : ips) {
            if (!vlanAllocatedToVM.containsKey(ip.getVlanId())) {
                /* plug a vif into router */
                VifHotPlug(conn, routerName, ip.getVlanId(), ip.getVifMacAddress());
                vlanAllocatedToVM.put(ip.getVlanId(), nicPos++);
            }
            nicNum = vlanAllocatedToVM.get(ip.getVlanId());
            networkUsage(routerIp, "addVif", "eth" + nicNum);
            result = _virtRouterResource.assignPublicIpAddress(routerName, routerIp, ip.getPublicIp(), ip.isAdd(), ip.isFirstIP(), ip.isSourceNat(), ip.getVlanId(), ip.getVlanGateway(), ip.getVlanNetmask(), ip.getVifMacAddress(), ip.getGuestIp(), nicNum);
            if (result != null) {
                results[i++] = IpAssocAnswer.errorResult;
            } else {
                results[i++] = ip.getPublicIp() + " - success";
                ;
            }
        }
        return new IpAssocAnswer(cmd, results);
    } catch (LibvirtException e) {
        return new IpAssocAnswer(cmd, results);
    } catch (InternalErrorException e) {
        return new IpAssocAnswer(cmd, results);
    }
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) LibvirtException(org.libvirt.LibvirtException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.exception.InternalErrorException) IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef)

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