你好,游客 登录 注册 搜索
背景:
阅读新闻

应用Libvirt连接KVM虚拟化平台

[日期:2013-02-25] 来源:Linux社区  作者:my2005lb [字体: ]

发布一段C代码,用于连接指定的KVM宿主机器,获得该宿主机器的配置信息,以及该主机上所有的虚拟主机列表、状态及配置信息:

#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>

void ListDom(virConnectPtr conn)
{
    char **inactiveDom;
    virDomainPtr *allDom;
    int nInActiveDoms = virConnectNumOfDefinedDomains(conn); 
    allDom = malloc(sizeof(virDomainPtr)* nInActiveDoms);
    int i = 0;
    char str[200];
    virDomainInfo t;
    if(nInActiveDoms != 0)
    {
        printf("inactive num: %d\n",nInActiveDoms);
        inactiveDom = malloc(sizeof(char*) * (nInActiveDoms));
        nInActiveDoms  = virConnectListDefinedDomains(conn , inactiveDom , nInActiveDoms); 
        printf("numOfInactiveDom*************:%d\n", nInActiveDoms);
 
        for(i=0 ; i<nInActiveDoms; i++)
        {
            printf("name:%s\n",inactiveDom[i]);
            allDom[i] = virDomainLookupByName(conn , inactiveDom[i]);
            virDomainGetUUIDString(allDom[i],str);
            printf("uuid:%s | os:%s\n",str,virDomainGetOSType(allDom[i]));   
            virDomainGetInfo(allDom[i],&t);
            printf("HostName:%s\n",virDomainGetName(allDom[i]));
            printf("state:%d|maxmem:%d|memused:%d|cpunum:%d|cputime:%ld\n",t.state,t.maxMem,t.memory,t.nrVirtCpu,t.cpuTime);
          }
    }
}
void ListActiveDom(virConnectPtr conn)
{
    int numOfActiveDom = virConnectNumOfDomains(conn);
    int activeDom[numOfActiveDom];
    virDomainPtr *allDomains;     
    virDomainInfo t;
    int i ;
    char str[200];
    allDomains = malloc(sizeof(virDomainPtr)* numOfActiveDom); 
    numOfActiveDom = virConnectListDomains(conn , activeDom , numOfActiveDom); 
   
    printf("Active Dom: \n"); 
    for(i=0 ; i<numOfActiveDom ; i++){
        printf("id:%d\n",activeDom[i]); 
        allDomains[i] = virDomainLookupByID(conn ,activeDom[i]);  //ͨ¹ýidÈ¡Domain 
        virDomainGetUUIDString(allDomains[i],str);
        printf("uuid:%s | os:%s\n",str,virDomainGetOSType(allDomains[i]));   
        printf("HostName:%s\n",virDomainGetName(allDomains[i]));
        virDomainGetInfo(allDomains[i],&t);
        printf("state:%d|maxmem:%d|memused:%d|cpunum:%d|cputime:%ld\n",t.state,t.maxMem,t.memory,t.nrVirtCpu,t.cpuTime);
    }
}
int main(int argc, char *argv[])
{
    virConnectPtr conn;
    char* caps;
    virNodeInfo nodeinfo;   
    char str[200];
    char *struuid[200];
    int max = 0;
    int i = 0;
    virNodeMemoryStats *pMem;
    int pN[100];
    while(1)
    {
      printf("Input KVM Host:>");
      gets(str);
      printf("\n");
      if(strcmp(str,"quit") == 0)
      {
          printf("Bye-Bye\n");
          exit(0);
      }
      conn = virConnectOpen(str);
      if (conn == NULL) {
          fprintf(stderr, "Failed to open connection to qemu:///system\n");
          continue;
      }
      caps = virConnectGetCapabilities(conn);
      fprintf(stdout, "Capabilities:\n%s\n", caps);
      free(caps);

      printf("HostName: %s\n",virConnectGetHostname(conn));
      fprintf(stdout, "Virtualization type: %s\n", virConnectGetType(conn));

      virNodeGetInfo(conn, &nodeinfo);
      fprintf(stdout, "Model: %s\n", nodeinfo.model);
      fprintf(stdout, "Memory size: %lukb\n", nodeinfo.memory);
      fprintf(stdout, "Number of CPUs: %u\n", nodeinfo.cpus);
      fprintf(stdout, "MHz of CPUs: %u\n", nodeinfo.mhz);
      fprintf(stdout, "Number of NUMA nodes: %u\n", nodeinfo.nodes);
      fprintf(stdout, "Number of CPU sockets: %u\n", nodeinfo.sockets);
      fprintf(stdout, "Number of CPU cores per socket: %u\n", nodeinfo.cores);
      fprintf(stdout, "Number of CPU threads per core: %u\n", nodeinfo.threads);
    /*  printf("Sysinfor :[%s]\n",virConnectGetSysinfo(conn,0));

      pMem = malloc(sizeof(virNodeMemoryStats)* 100);
      virNodeGetMemoryStats(conn,VIR_NODE_MEMORY_STATS_ALL_CELLS,pMem,pN,0);
        for(i = 0; i < 10; i++)
        {
            printf("MEM STAT\n%s - %ld\n",pMem[i].field,pMem[i].value);
        }*/
      ListDom(conn);
      ListActiveDom(conn);
      virConnectClose(conn);
    }
    return 0;
}

 

执行效果如下:

[root@kvm T]# ./t
Input KVM Host:>qemu+ssh:///system

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款