libvirt 基于C API基本使用案例

简介: 玩开源分享,需要有干到底的精神,今晚随便逛逛技术论坛突发有感;Ruiy不足之处,需跟进了; 最近变的较懒了,干活有点没劲,也不怪干来干去收获不大,缺少鼓励! 现在玩的技术大多是上不了台面了,想过没,你在使用别人定义封装好的class(JAVA,C++等面向对象),感觉简单不,那就类似于洗衣机中预设的定时洗涤程序,伟大是人家设计者; 想要有搞头,那就自个搞搞class def(维度于CPP,.

玩开源分享,需要有干到底的精神,今晚随便逛逛技术论坛突发有感;
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

目录
相关文章
|
7月前
|
监控 前端开发 JavaScript
实战篇:商品API接口在跨平台销售中的有效运用与案例解析
随着电子商务的蓬勃发展,企业为了扩大市场覆盖面,经常需要在多个在线平台上展示和销售产品。然而,手工管理多个平台的库存、价格、商品描述等信息既耗时又容易出错。商品API接口在这一背景下显得尤为重要,它能够帮助企业在不同的销售平台之间实现商品信息的高效同步和管理。本文将通过具体的淘宝API接口使用案例,展示如何在跨平台销售中有效利用商品API接口,以及如何通过代码实现数据的统一管理。
|
7月前
|
缓存 JavaScript Serverless
一些可能被忽视的 Vue3 API 附带案例
一些可能被忽视的 Vue3 API 附带案例
146 0
|
7月前
|
API
GEE案例分析——利用sentinel-3数据计算空气污染指数(Air Pollution Index,简称API)
GEE案例分析——利用sentinel-3数据计算空气污染指数(Air Pollution Index,简称API)
207 0
|
22天前
|
JSON BI API
商城上货API接口的实战案例
在商城上货过程中,API接口扮演着至关重要的角色。以下是对商城上货API接口的实战分析,涵盖其主要功能、类型、安全性以及实战案例等方面。
|
19天前
|
XML 数据可视化 API
商品详情数据实战案例,API接口系列
淘宝商品详情数据在电商领域具有广泛的应用价值,而淘宝商品详情API接口则为开发者提供了获取这些数据的重要途径。通过合理利用这些接口和数据,可以提升业务效率、优化用户体验,为电商行业的发展注入新的活力。
|
7月前
|
SQL Java 程序员
Java 8中的Stream API:简介与实用案例
【5月更文挑战第23天】本文将深入探讨Java 8中的Stream API,这是一种能够极大提升Java程序员生产力的新特性。我们将从基础概念开始,然后通过一些实用的案例来展示如何使用Stream API进行数据处理和操作。无论你是Java的初学者还是经验丰富的开发者,本文都将为你提供有价值的信息。
|
4月前
|
存储 Linux API
Linux源码阅读笔记08-进程调度API系统调用案例分析
Linux源码阅读笔记08-进程调度API系统调用案例分析
|
4月前
|
JSON 数据管理 关系型数据库
【Dataphin V3.9】颠覆你的数据管理体验!API数据源接入与集成优化,如何让企业轻松驾驭海量异构数据,实现数据价值最大化?全面解析、实战案例、专业指导,带你解锁数据整合新技能!
【8月更文挑战第15天】随着大数据技术的发展,企业对数据处理的需求不断增长。Dataphin V3.9 版本提供更灵活的数据源接入和高效 API 集成能力,支持 MySQL、Oracle、Hive 等多种数据源,增强 RESTful 和 SOAP API 支持,简化外部数据服务集成。例如,可轻松从 RESTful API 获取销售数据并存储分析。此外,Dataphin V3.9 还提供数据同步工具和丰富的数据治理功能,确保数据质量和一致性,助力企业最大化数据价值。
190 1
|
4月前
|
开发者
告别繁琐代码,JSF标签库带你走进高效开发的新时代!
【8月更文挑战第31天】JSF(JavaServer Faces)标准标签库为页面开发提供了大量组件标签,如`&lt;h:inputText&gt;`、`&lt;h:dataTable&gt;`等,简化代码、提升效率并确保稳定性。本文通过示例展示如何使用这些标签实现常见功能,如创建登录表单和展示数据列表,帮助开发者更高效地进行Web应用开发。
45 0
|
4月前
|
UED 存储 自然语言处理
【语言无界·体验无疆】解锁Vaadin应用全球化秘籍:从代码到文化,让你的应用畅游世界每一个角落!
【8月更文挑战第31天】《国际化与本地化实战:构建多语言支持的Vaadin应用》详细介绍了如何使用Vaadin框架实现应用的国际化和本地化,提升用户体验和市场竞争力。文章涵盖资源文件的创建与管理、消息绑定与动态加载、日期和数字格式化及文化敏感性处理等方面,通过具体示例代码和最佳实践,帮助开发者构建适应不同语言和地区设置的Vaadin应用。通过这些步骤,您的应用将更加灵活,满足全球用户需求。
59 0