libvirt 基于C API基本使用案例

玩开源分享,需要有干到底的精神,今晚随便逛逛技术论坛突发有感;
Ruiy不足之处,需跟进了;

最近变的较懒了,干活有点没劲,也不怪干来干去收获不大,缺少鼓励!

现在玩的技术大多是上不了台面了,想过没,你在使用别人定义封装好的class(JAVA,C++等面向对象),感觉简单不,那就类似于洗衣机中预设的定时洗涤程序,伟大是人家设计者;

想要有搞头,那就自个搞搞class def(维度于CPP,.....)

C,CPP中你看那个软件包的devel中包含的header file文件,技术含量在那里面哈,你简单玩的那个简直上不了台面,伟大也在人家;

仅仅只想想简单玩玩(敷衍差事,瞎忽悠),那你就不照了哈;

Ruiy哥曾经试图阅读C include下 的相关header files,至今精力还未能投入其中;
Ruiy哥追求的就是尽量完成手头活,不妥不拉;

1,virConnectGetLibVersion

API call obtain the version of libvirt software in use on the host

it takes a connection pointer and unsigned long pointer as input,

2,virConnectGetVersion

API call obtain version of the host virtualization software in use

3,virConnectGetURI

API call obtain URI current connection

4,virConnectIsEncrypted

API call 判断以建立的virtualizationHypervisors 是否是加密的

5,virConnectIsSecure

API call 同上判断virtual conn 是否加密

if succesful returns 1 for a secure connection and 0 for an insecure connection

if an error occurred -1 will be returned;

6,libvirt Enevt loop integration;

libvirt APIs use a basic request/response architecture that is generally synchronous

libvirt application calls a libvirt API (the request) which doesn't return until the action is complete (the response)

a libvirtd server can also generate asynchronous messages and send them to
the libvirt application

 7,by default when an error occurs,libvirt will call the virDefaultErrorFunc function which will print the error information to stderr,

  virSetErrorFunc API call can be used to set a custom global error function that libvirt will call instead,it takes a viod

* pointer as input,and returns nothing,the custom error function should have function signature

typedef void (*virErrorFunc) (void *userData,virErrorPtr error);

following code demonstrates use virSetErrorFunc

//compile with: gcc -g -Wall virSerErrorFunc.c -o virSetErrorFunc -lvirt;
//by Ruiy
//stderr 0
//stdin 1
//stdout 2

#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>
#include <libvirt/libvirt-qemu.h>
#include <libvirt/virterror.h>

static void customErrorFunc(void *userdata,virErrorPtr err)
{
fprintf(stderr,"Failure of libvirt library cal: \n");
fprintf(stderr,"Code:%d \n",err->code);
fprintf(stderr,"Domain:%d\n",err->domain);
fprintf(stderr,"Message:%s \n",err->message);
fprintf(stderr,"Level:%d \n",err->level);
fprintf(stderr,"str1:%s \n",err->str1);
fprintf(stderr,"str2:%s \n",err->str2);
fprintf(stderr,"str3:%s \n",err->str3);
fprintf(stderr,"int1:%d \n",err->int1);
fprintf(stderr,"int2:%d \n",err->int2);
}
int main(int argc,char *argv[])
{
 virConnectPtr conn;
 virSetErrorFunc(NULL,customErrorFunc);
 conn = virConnectOpen("qemu+tcp://root@192.168.1.143/system");
if (conn == NULL)
{
 fprintf(stderr,"Failed to open connection to qemu+tcp://root@192.168.1.143/system");
return 1;   
}      
if (virConnectGetVersion(conn,NULL) < 0)
fprintf(stderr,"virConnectGetVersion failed \n");l
virConnectClose(conn);
return 0;

 8,virConnSetErrorFunc

  API call can be used to set a per-connection custom error handling function,if present,this per-connection error handling function will take precendence over the global

 

continue upGrade;

if (conn->handler)
    conn->handler;
else if (global_handler)
    global_handler;
else
    virDefaultErrorFunc;

typedef void (*virErrorFunc) (void *userData,virErrorPtr error);

//example demonstrates
//cimpile with: gcc -g -Wall *.c -o execfiles -lvirt;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <>
using namespace std;
#include <libvirt/libvirt.h>
#include <libvirt/libvirt-qemu.h>
#include <libvirt/viererror.h>

static void customConnErrorFunc(void *userdata,virErrorPtr err)
{
fprintf(stderr,"Connection handler,failure of libvirt library call: \n");
fprintf(stderr,"Code:%d \n",err->code);
fprintf(stderr,"Domain:%d \n",err->domain);
fprintf(stderr,"Message:%s \n",err->message);
fprintf(stderr,"Level:%d \n",err->level);
fprintf(stderr,"str1:%s \n",err->str1);
fprintf(stderr,"str2:%s \n",err->str2);
fprintf(stderr,"str3:%s \n",err->str3);
fprintf(stderr,"int1:%d \n",err->int1);
fprintf(stderr,"int2:%d \n",err->int2);
}

static void customGlobalErrorFunc(void *userdata,virErrorPtr err)
{
fprintf(stderr,"Global handler,failer of libvirt library call: \n");
fprintf(stderr,"Code:%d \n",err->code);
fprintf(stderr,"Domain:%d \n",err->domain);
fprintf(stderr,"Message:%s \n",err->message);
fprintf(stderr,"Level:%d \n",err->level);
fprintf(stderr,"str1:%s \n",err->str1);
fprintf(stderr,"str2:%s \n",err->str2);
fprintf(stderr,"str3:%s \n",err->str3);
fprintf(stderr,"int1:%d \n",err->int1);
fprintf(stderr,"int2:%d \n",err->int2);
}

int main(int argc,char *argc[])
{
virConnectPtr conn1;
virConnectPtr conn2;
//set a global error function for all connection
virSetErrorFunc(NULL,customGlobalErrorFunc);    
}

 9,virCopyLastError

API call obtain a copy last error reported from libvirt

error object is keep in thread local storage so separate threads can safely use this function concurrently,

//compile with: gcc -g -g -Wall *.c -o virCopyLastError -lvirt

#include <stdio.h>

#include <stdib.h>

#include <libvirt/libvirt.h>

#include <libvirt/virterror.h>

#include <libvirt/libvirt-qemu.h>

//dummy error function to suppress virDefaultErrorFunc

static void customErrorFunc(void *userdata,virErrorPtr err)

{}

int main(int argc,char *argv[])

{

virConnectPtr conn;

virError err;

virSetErrorFunc()

}

10,virResetError

api call to clear and free any memory associated with an virError object;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libvirt/libvirt.h>
#include <libvirt/libvirt-qemu.h>
#include <libvirt/virterror.h>

//dummy error function to suppress virDefaultErrorFunc
static void customErrorFunc(void *userdata,virErrorPtr err)
{
}

int main(int argc,char *argc[])
{
virConnectPtr conn;
virError err;
virSetErrorFunc(NULL,customErrorFunc);
conn = virConnectOpen("qemu:///system");
conn = virConnectOpen("qemu+tcp://localhost/system");
if (conn == NULL)
{
fprintf(stderr,"Failed to open connection to qemu:///system \n");
return 1;
}
if (virConnectGetVersion(conn,NULL) < 0)
{
virCopyLastError(&err);
fprintf(stderr,"virConnectGetVersion Failed:%s \n",err.message);
virResetError(&err);
}
virConnectClose(conn);
return 0;
}

11.virGetLastError

API call obtain a pointer to the last error reported from libvirt;

#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
#include <libvirt/libbvirt-qemu.h>

static void customErrorFunc(void *userdata,virerrorPtr err)
{}
int main(int argc,char *argv[])
{
virConnectPtr conn;
virErrorPtr err;
virSetErrorfunc(NULL,customErrorfunc);

conn = virConnectOpen("qemu+tcp://libvirt.org/system");
if (conn == NULL)
{
fprintf(stderr,"Failed to open connection to qemu+tcp://libvirt.org/systsem" \n);
return 1;
}
if (virconnectGetVersion(conn,NULL) < 0)
{
//this is a vailed way to use virgetLastErrorerr v= virgetLastError();
err = virGetLasteError();
fprintf(stderr,"virconnectGetVersion failed:%s \n",err->message);
}
if (virConnectGetVersion(conn,NULL) < 0)
{
//this is an invailed way to use virGetLastError; the error message will not reresent the error from virConnectGetVersion
}
err = virgetLastError();
virNodeGetFreeMemory(NULL);
frpintf(stderr,"virConnectGetVersion failed:%s \n",err->message);
}

12.virSaveLastError

API call to allocate and obtain a copy of the last error reported fromn libvirt;

/*compile with: gcc -g -Wall virSaveLastError.c -o virSaveLastError -lvirt*/
//dummy虚拟;
#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>
#include <libvirt/libvirt-qemu.h>
#include <libvirt/virterror.h>

//dummy function to suppress virDefaultErrorFunc
static void customErrorFunc(void *userdata,virErrorPtr)
{
}

int main(int argc,char *argv[])
{
virConnectPtr conn;
virErrorPtr err;
virSetErrorFunc(NULL,customErrorFunc);

}

 13,debug/logging

log messages,log filters,log outputs;

priority level:

1 debug

2 info

3 warn

4 error

3,log outputs - where to send the message once it has passwd through filters,the format for a log output is one of the forms:

x:stderr - log to stderr

x:syslog:name - log to syslog with a prefix of "name"

x:file:file_path - log to a file specified by "file_path"

where "X" is the minimal priority level,for instance,to log all warnings and errors to syslog with a prefix of "libvirt",the following output cann be used:

3:syslog:libvirtd

14,integrated example

this example daemonstrates many of the concepts from the chapter together,including error checking,

while still not a "real" program (which would likely be multi-threaded),it's a good example of how to write a libvirt program from end to end;

 15,guest domains

domain is an instance of an operating system running on a virtualized machine,a guest domain can refer to either a running virtual machine or a configuration which can be used launch a virtual machine,the connection object provides APIs to enumerate the guest domains,create new guest domains and manage existing domains,A guest domain is represented with the virDomainPtr object and has a number of unique identifiers;

Unique identifiers:

1. id:positive integer,unique amongstrunning guest domains on a single host,an inactive domain does not have an id,if the host OS is a virtual it is given a id of zero by default.for example,with the xen hypervisor,Dom0 indicates a guest domain,other domain ids will be allocated starting at 1,and incrementing each time a new domain starts,typically domain IDs will not be-used until the entire ID space warps around.the domain id space is at 16 bits in size,but often extends 32 bits

2.name:short string,unique amongst all guest domains on a single host,both running and inactive for maximum portability between hypervisors applications should only rely on being able to use the characters a-z,0-9 in names,many hypervisors will store inactive domain configurations as files on disk,based on the domain name;

uuid:16unsigned bytes,guaranteed to be unique amongst all guest domains on any host,RFC 4122 defines the format for UUIDs and provides a recommended algorithm for generating UUids with guaranteed uniqueness.if the host OS is itself a virtual domain,then by convention it will be given a uuid of all zeros,this is the case with the xen hypervisors,

a guest domain may be transient,or persistent,a transient guest domain can only be managed while it is running on the host and,when powered off,all trace of it will disappear.a persistent guest domain has its configuration maintained in a data store on the host by the hypervisor,in an implementation defined format,thus when a persistent guest is powered off,it is still possible to manage its inactive config,a transient guest can be truned into a persistent guest on the fly by defining a configuration for it

once an application has a unique identifier for a domain,it will often want to obtain the corresponding virDomainPtr object.there are three,imaginatively named,methods to do lookup existing domains,

virDomainLoopupByID,virDomainLookupByName and virDomainLookByUUID

each of these takes a connection object as first parameter,and the domain identifier as the other.

they will return NULL if no matching domain exists,the connections error object be queried to find specific detials of the error if required;

int domainID = 6;

virDomainPtr dom;

dom = virDomainLoopupByID(conn,domainID);

15.Example4.1 fetching a domain object from an ID

int domainName = "byRuiy";

virDomainPtr dom;

dom = virDomainLookupByName(conn,domainName);

Example 4.2 Fetch a domain object from an name

char *domainUUID = "";

virDomainPtr dom;

dom  = virDomainLookupByUUIDString(conn,domainUUID);

example 4.3 Fetch a domain object from an UUID

for convenience of this document,the UUID example used the printable format of UUID,there is an equivalent method which accepts the raw bytes unsigned char[]

获取VMs域信息API函数

int domainID = 6;

virdomainPtr dom;

virDomainLoopupByUUIDString(conn,domainUUID);

virDomainLoopupByName(conn,domainName);

virDomainLookupByID(conn,domainID);

16.listing domains

the libvirt API exposes two lists of domains,the first contains running domains,while the second contains inactive,persistent domains.the lists are intended to be non-overlapping,exclusive sets,though there is always a small possibility that a domain can stop or start in between the querying of each set.the events API described later in this section provides a way to track all lifecycle changes avoiding this potential race condition;

API for listing active domains,returns a list of domain IDs.Every running domain has a positive integer ID,uniquely identifying it amongst all running domains on the host.the API for listing active domains,virConnectListDomains,requires the caller to pass in a pre-allocated int array which will be filled in domain IDs, the return value will be -1 upon error,or the total number of array elements filed,to determine how large to make the ID array,the application can use the API call virConnectNumofDomains.putting these two calls together,a fragment of code which printfs a list running domain IDs would be

int i;

int numDomains;

int *activeDomains;

numDomains = virConnectNumOfDomains(conn);

activeDomains = malloc(sizeof(int) * numDomains);

numDomains = virConnectListDomains(conn,activeDomains,numDomains);

printf("Active domain IDs: \n");

for (i = 0;i < numDomains;i++)

{

printf("%d \n",activeDomains[i]);

}

free(activeDomains);

Example 4.4 listing active domains

in addition to the running domains,there may be some persistent inactive domain configurations stored on the host,since an inactive domain not hace any ID identifier,the listing of inactive domains is exposed as a list of name strings,in a similar style to the API just discussed

inactive domains is exposed as a list of name strings,in a similar style to the API just discused

the virConnectListDefinedDomains API requires the callers to provide a pre-allocated

char * array which will beb filled with domain name strings.the return value will be -1 upon error ,or the total number of array elements filled with names it si the callers responsibility free mempry

Example4.5 listing inactive domains

the APIs for for listing domains do not directly return the full virDomainPtr objects,since this may incur

posted on 2014-05-30 11:24 秦瑞It行程实录 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/ruiy/p/libAPI.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值