Search in sources :

Example 6 with Connect

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

the class LibvirtComputingResource 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();
        List<DiskDef> disks = getDisks(conn, vmName);
        destroy_network_rules_for_vm(conn, vmName);
        String result = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
        if (result == null) {
            for (DiskDef disk : disks) {
                if (disk.getDeviceType() == DiskDef.deviceType.CDROM && disk.getDiskPath() != null)
                    cleanupDisk(conn, disk);
            }
        }
        final String result2 = cleanupVnet(conn, cmd.getVnet());
        if (result != null && result2 != null) {
            result = result2 + result;
        }
        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 : DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) LibvirtException(org.libvirt.LibvirtException) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) StopAnswer(com.cloud.agent.api.StopAnswer)

Example 7 with Connect

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

the class LibvirtComputingResource method execute.

private synchronized Answer execute(PrepareForMigrationCommand cmd) {
    VirtualMachineTO vm = cmd.getVirtualMachine();
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Preparing host for migrating " + vm);
    }
    NicTO[] nics = vm.getNics();
    try {
        Connect conn = LibvirtConnection.getConnection();
        for (NicTO nic : nics) {
            String vlanId = null;
            if (nic.getBroadcastType() == BroadcastDomainType.Vlan) {
                URI broadcastUri = nic.getBroadcastUri();
                vlanId = broadcastUri.getHost();
            }
            if (nic.getType() == TrafficType.Guest) {
                if (nic.getBroadcastType() == BroadcastDomainType.Vlan && !vlanId.equalsIgnoreCase("untagged")) {
                    createVlanBr(vlanId, _pifs.first());
                }
            } else if (nic.getType() == TrafficType.Control) {
                /* Make sure the network is still there */
                createControlNetwork(conn);
            } else if (nic.getType() == TrafficType.Public) {
                if (nic.getBroadcastType() == BroadcastDomainType.Vlan && !vlanId.equalsIgnoreCase("untagged")) {
                    createVlanBr(vlanId, _pifs.second());
                }
            }
        }
        /* setup disks, e.g for iso */
        VolumeTO[] volumes = vm.getDisks();
        for (VolumeTO volume : volumes) {
            if (volume.getType() == Volume.Type.ISO) {
                getVolumePath(conn, volume);
            }
        }
        synchronized (_vms) {
            _vms.put(vm.getName(), State.Migrating);
        }
        return new PrepareForMigrationAnswer(cmd);
    } catch (LibvirtException e) {
        return new PrepareForMigrationAnswer(cmd, e.toString());
    } catch (InternalErrorException e) {
        return new PrepareForMigrationAnswer(cmd, e.toString());
    } catch (URISyntaxException e) {
        return new PrepareForMigrationAnswer(cmd, e.toString());
    }
}
Also used : VolumeTO(com.cloud.agent.api.to.VolumeTO) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.exception.InternalErrorException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) NicTO(com.cloud.agent.api.to.NicTO) PrepareForMigrationAnswer(com.cloud.agent.api.PrepareForMigrationAnswer)

Example 8 with Connect

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

the class LibvirtComputingResource method execute.

private Answer execute(CheckVirtualMachineCommand cmd) {
    try {
        Connect conn = LibvirtConnection.getConnection();
        final State state = getVmState(conn, cmd.getVmName());
        Integer vncPort = null;
        if (state == State.Running) {
            vncPort = getVncPort(conn, cmd.getVmName());
            synchronized (_vms) {
                _vms.put(cmd.getVmName(), State.Running);
            }
        }
        return new CheckVirtualMachineAnswer(cmd, state, vncPort);
    } catch (LibvirtException e) {
        return new CheckVirtualMachineAnswer(cmd, e.getMessage());
    }
}
Also used : CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) LibvirtException(org.libvirt.LibvirtException) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect)

Example 9 with Connect

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

the class LibvirtComputingResource method getHostInfo.

protected List<Object> getHostInfo() {
    final ArrayList<Object> info = new ArrayList<Object>();
    long speed = 0;
    long cpus = 0;
    long ram = 0;
    String cap = null;
    try {
        Connect conn = LibvirtConnection.getConnection();
        final NodeInfo hosts = conn.nodeInfo();
        boolean result = false;
        try {
            BufferedReader in = new BufferedReader(new FileReader("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"));
            speed = Long.parseLong(in.readLine()) / 1000;
            result = true;
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } catch (NumberFormatException e) {
        }
        if (!result) {
            speed = hosts.mhz;
        }
        cpus = hosts.cpus;
        ram = hosts.memory * 1024L;
        LibvirtCapXMLParser parser = new LibvirtCapXMLParser();
        parser.parseCapabilitiesXML(conn.getCapabilities());
        ArrayList<String> oss = parser.getGuestOsType();
        for (String s : oss) {
            /*
				 * Even host supports guest os type more than hvm, we only
				 * report hvm to management server
				 */
            if (s.equalsIgnoreCase("hvm")) {
                cap = "hvm";
            }
        }
    } catch (LibvirtException e) {
    }
    if (isSnapshotSupported()) {
        cap = cap + ",snapshot";
    }
    info.add((int) cpus);
    info.add(speed);
    info.add(ram);
    info.add(cap);
    // save a maximum
    long dom0ram = Math.min(ram / 10, 768 * 1024 * 1024L);
    // of 10% of
    // system ram or
    // 768M
    dom0ram = Math.max(dom0ram, _dom0MinMem);
    info.add(dom0ram);
    s_logger.debug("cpus=" + cpus + ", speed=" + speed + ", ram=" + ram + ", dom0ram=" + dom0ram);
    return info;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) NodeInfo(org.libvirt.NodeInfo) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 10 with Connect

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

the class LibvirtComputingResource method execute.

private Answer execute(AttachIsoCommand cmd) {
    try {
        Connect conn = LibvirtConnection.getConnection();
        attachOrDetachISO(conn, cmd.getVmName(), cmd.getIsoPath(), cmd.isAttach());
    } catch (LibvirtException e) {
        return new Answer(cmd, false, e.toString());
    } catch (URISyntaxException e) {
        return new Answer(cmd, false, e.toString());
    } catch (InternalErrorException e) {
        return new Answer(cmd, false, e.toString());
    }
    return new Answer(cmd);
}
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) URISyntaxException(java.net.URISyntaxException) 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