为环境CentOS5.5
从官方网站上下载了文档 libvirt 0.7.5 Application Development Guide
由于CentOS自带libvirt版本为0.6.3的,于是自己卸载了libvirt 0.6.3自己下载了一个0.8.1的版本装上,步骤如下:
libvirt 8.0下载地址:ftp://libvirt.org/libvirt/ 下载檔案:libvirt-0.8.0.tar.gz
解压后./configure 提示 You must install the GnuTLS library in order to compile and run libvirt
然后到ftp://ftp.gnu.org/gnu/gnutls/下载檔案:gnutls-2.10.0.tar.bz2
用tar jvfx gnutls-2.10.0.tar.bz2解压缩
进入目录 ./configure
make
make install
安装好GnuTLS library后继续上面安装libvirt 8.0
./configure
make
make install
安装成功!
于是乎继续编译如下代码
/* example ex1.c */
/* compile with: gcc -g -Wall ex1.c -o ex -lvirt */
#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>
int main(int argc, char *argv[])
{
virConnectPtr conn;
conn = virConnectOpen("qemu:///system");
if (conn == NULL) {
fprintf(stderr, "Failed to open connection to qemu:///system/n");
return 1;
}
virConnectClose(conn);
return 0;
}
编译:gcc -g -Wall ex1.c -o ex -lvirt 生成:ex
[root@localhost program]# ./ex
./ex: error while loading shared libraries: libvirt.so.0: cannot open shared object file: No such file or directory
后从新用centos5.5光盘自带rpm包安装libvirt CIM 重启电脑,编译通过了!
gcc -g -Wall ex1.c -o ex -lvirt 命令解释 -g 编译时生成调试信息 -Wall 生成所有警告信息 -o 生成指定可执行文件 -lvirt 还不清楚
API学习
libvirt API分为连接API,域API,网络API,存储卷API,存储池API
连接API
- virConnectPtr virConncetOpen(const char *name)
- virConnectPtr virConnectOpenReaderOnly(const char *name)
- virConnectPtr virConnectOpenAuth(const char *name,virConnectAuthPtr auth, int flags)
name URI of hypervisor,为NULL代表本地连接
一个相当于xm list命令的程序
//name : listvirt.c
//compile with : gcc -g listvirt.c -o listvirt -lvirt
#include<stdio.h>
#include<stdlib.h>
#include<libvirt/libvirt.h>
#include<string.h>
#define MAXID 50
static virConnectPtr con=NULL;
void freeDomain(virDomainPtr dom)
{
if(dom!=NULL)
virDomainFree(dom);
}
void closeConn()
{
if(con!=NULL)
virConnectClose(con);
}
void getDomainInfo(int id)
{
virDomainPtr dom=NULL;
virDomainInfo info;
int ref;
dom=virDomainLookupByID(con, id);
if(dom==NULL)
{
fprintf(stderr,"Failed to find Domain %d/n",id);
freeDomain(dom);
closeConn();
}
ref=virDomainGetInfo(dom,&info);
if(ref<0)
{
fprintf(stderr, "Failed to get information for Domain %d/n", id);
freeDomain(dom);
closeConn();
}
printf("%d/t%lu/t%lu/t%hu/t%0X/t%s/n", id, info.memory / 1024,
info.maxMem / 1024, info.nrVirtCpu, info.state, virDomainGetName(dom));
virDomainFree(dom);
}
int main(int argc,char *argv[])
{
int idCount=0;
int i;
int id;
int ids[MAXID];
printf("--------------------------------------------------/n");
printf(" XEN Domain List /n");
printf(" Build by Song YongMing /n");
printf(" 2010/11/08 /n");
printf("--------------------------------------------------/n");
con=virConnectOpen("");
if(con==NULL)
{
fprintf(stderr,"Failed to connect to hypervisor/n");
return 1;
}
idCount=virConnectListDomains(con,&ids[0],MAXID);
if(idCount<1)
{
fprintf(stderr,"Failed to List Domains/n");
virConnectClose(con);
return 0;
}
printf("Domain Totals:%d/n",idCount);
printf("ID/tMEM/tMaxMEM/tVCPUs/tState/tNAME/n");
for(i=0;i<idCount;i++)
{
id=ids[i];
getDomainInfo(id);
}
return 0;
}