开发者社区> cloud_ruiy> 正文

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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,大概有三种登录方式:
9991 0
阿里云服务器ECS登录用户名是什么?系统不同默认账号也不同
阿里云服务器Windows系统默认用户名administrator,Linux镜像服务器用户名root
13944 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,云吞铺子总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系统盘、创建快照、配置安全组等操作如何登录ECS云服务器控制台? 1、先登录到阿里云ECS服务器控制台 2、点击顶部的“控制台” 3、通过左侧栏,切换到“云服务器ECS”即可,如下图所示 通过ECS控制台的远程连接来登录到云服务器 阿里云ECS云服务器自带远程连接功能,使用该功能可以登录到云服务器,简单且方便,如下图:点击“远程连接”,第一次连接会自动生成6位数字密码,输入密码即可登录到云服务器上。
33594 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
25225 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
18972 0
腾讯云服务器 设置ngxin + fastdfs +tomcat 开机自启动
在tomcat中新建一个可以启动的 .sh 脚本文件 /usr/local/tomcat7/bin/ export JAVA_HOME=/usr/local/java/jdk7 export PATH=$JAVA_HOME/bin/:$PATH export CLASSPATH=.
14105 0
阿里云服务器怎么设置密码?怎么停机?怎么重启服务器?
如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
20683 0
阿里云服务器ECS远程登录用户名密码查询方法
阿里云服务器ECS远程连接登录输入用户名和密码,阿里云没有默认密码,如果购买时没设置需要先重置实例密码,Windows用户名是administrator,Linux账号是root,阿小云来详细说下阿里云服务器远程登录连接用户名和密码查询方法
21671 0
+关注
cloud_ruiy
爱技术,爱开源,爱linux! 在技术成长道路上,能拉一把就拉一把,不藏着不掖着! 我由衷希望能和有理想敢吃苦的it人一起共同进步,共同成长! 虽然我现在没有大牛的能力,但是我有大牛的心态. 技术领域:shell编程,C编程,嵌入式开发.hadoop大数据,桉树,onenebul
1715
文章
1
问答
文章排行榜
最热
最新
相关电子书
更多
JS零基础入门教程(上册)
立即下载
性能优化方法论
立即下载
手把手学习日志服务SLS,云启实验室实战指南
立即下载