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

目录
相关文章
|
16天前
|
监控 前端开发 JavaScript
实战篇:商品API接口在跨平台销售中的有效运用与案例解析
随着电子商务的蓬勃发展,企业为了扩大市场覆盖面,经常需要在多个在线平台上展示和销售产品。然而,手工管理多个平台的库存、价格、商品描述等信息既耗时又容易出错。商品API接口在这一背景下显得尤为重要,它能够帮助企业在不同的销售平台之间实现商品信息的高效同步和管理。本文将通过具体的淘宝API接口使用案例,展示如何在跨平台销售中有效利用商品API接口,以及如何通过代码实现数据的统一管理。
|
3月前
|
缓存 JavaScript Serverless
一些可能被忽视的 Vue3 API 附带案例
一些可能被忽视的 Vue3 API 附带案例
110 0
|
2月前
|
API
GEE案例分析——利用sentinel-3数据计算空气污染指数(Air Pollution Index,简称API)
GEE案例分析——利用sentinel-3数据计算空气污染指数(Air Pollution Index,简称API)
104 0
|
9月前
|
JSON 监控 前端开发
python对接API二次开发高级实战案例解析:Zabbix API封装类实现获取认证密钥、所有主机组、所有主机、所有监控项和历史数据
python对接API二次开发高级实战案例解析:Zabbix API封装类实现获取认证密钥、所有主机组、所有主机、所有监控项和历史数据
293 0
|
9月前
|
数据可视化 搜索推荐 JavaScript
数据可视化大屏百度地图区域掩膜MapMask实现地图指定区域非省市县行政区显示的实战案例解析(JavaScript API GL、个性化地图定制、指定区域经纬度拾取转化)
数据可视化大屏百度地图区域掩膜MapMask实现地图指定区域非省市县行政区显示的实战案例解析(JavaScript API GL、个性化地图定制、指定区域经纬度拾取转化)
684 0
|
9月前
|
JavaScript 前端开发 API
python对接API二次开发高级实战案例解析:百度地图Web服务API封装函数(行政区划区域检索、地理编码、国内天气查询、IP定位、坐标转换)
python对接API二次开发高级实战案例解析:百度地图Web服务API封装函数(行政区划区域检索、地理编码、国内天气查询、IP定位、坐标转换)
222 0
|
3月前
|
缓存 供应链 安全
淘宝API接口调用:案例分析与最佳实践(续)
淘宝API接口是连接商家与淘宝平台强大功能的重要桥梁。通过案例分析和最佳实践的分享,我们希望商家能够更深入地理解如何有效地使用这些API来优化电商业务。随着技术的不断进步,淘宝API的功能将会越来越丰富,而商家面临的挑战也会越来越大。因此,商家需要不断地学习新技术、探索新方法,并且不断完善自己的API使用策略,以便更好地适应市场的变化,赢得竞争的优势。
|
3月前
|
供应链 搜索推荐 API
淘宝API接口调用:案例分析与最佳实践
在电子商务迅猛发展的今天,淘宝作为中国最大的在线购物平台之一,为商家们提供了强大的数据分析和市场洞察工具——淘宝API。有效的API调用不仅可以提升商家的运营效率,还可以帮助商家更好地理解消费者需求、优化商品布局、提高用户满意度等。本文将通过案例分析和最佳实践探讨如何高效利用淘宝API接口。
|
3月前
|
机器学习/深度学习 安全 数据挖掘
电商API接口的最佳实践与案例分析
随着电商行业的快速发展,越来越多的企业开始将业务拓展到线上。为了提高用户体验和运营效率,电商平台提供了丰富的API接口,方便商家进行商品管理、订单处理、营销活动等操作。本文将介绍电商API接口的最佳实践和案例分析。
|
9月前
|
移动开发 JavaScript 前端开发
数据可视化大屏百度地图手机端标注开发实战案例解析(jsAPI接口、标注分类图片、文本标签、分类筛选、自适应高度信息弹窗、PHP后端API)
数据可视化大屏百度地图手机端标注开发实战案例解析(jsAPI接口、标注分类图片、文本标签、分类筛选、自适应高度信息弹窗、PHP后端API)
147 0

热门文章

最新文章