libvirtsAPI

简介: mongodb远程服务器连接 mongo -uroot -p321 master.puppet.org:27017/admin 基于libvcirt的虚拟化guest domain overview: A guest domain may be transient, or persistent.

mongodb远程服务器连接

mongo -uroot -p321 master.puppet.org:27017/admin

基于libvcirt的虚拟化guest domain overview:

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 turned into a persistent guest on the fly by defining a configuration for
it.

 

virDomainLookupByID,virDomainLookupByName,virDomainLokkupByUUID

Example:Fetching a domain object from ID

int domainID = 6;

virDomainPtr dom;

dom = virDomainLookupByID(conn,domainID);

example:fetching a domain object from Name

int domainName = "byRuiy";

virDomainPtr dom;

dom = virDomainLookupByName(conn,domainName);

example:fetching a domain object from an UUID

char *domainUUID = "byrui-1990-04-03-15055198367-5160-59158"

virDomainPtr dom

dom = virDomainLookupByUUIDString(conn,domainUUID);

 

virConnectListDomains

virConnectNumOfDomains

int i;

int numDomains;

int *activeDomains;

numDomains = virConnectNumOfDomains(conn);

activeDomains = virConnectListDomains(conn,activeDomains,numDomains);

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

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

{

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

}

free(activeDomains);

 

Exampe:Listing active domais

there may be some persistent inactive domain configurations stored on the host.

virConnectListDefinedDomains

virConnectNumOfDefinedDomains

int i;

int numDomains;

char **inactiveDomains;

numDomains = virConnectNumOfDefinedDomains(conn);

inactiveDomains = malloc(sizeof(char *) * numDomains);

numDomains = virConnectListDomains(conn,inactiveDomains,numDomains);

printf("Inactive domain names:\n");

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

{

printf("%s \n",inactiveDomains[i]);

free(inactiveDomains[i]);

}

free(inactiveDomains);

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

virDomainPtr *allDomains;

int numDomains;

int numActiveDomains,numInactiveDomains;

char *inactiveDomains;

int *activeDomains;

numActiveDomains = virConnectNumOfDomains(conn);

numInactiveDomains = virConnectNumOfDefinedDomains(conn);

allDomains = malloc(sizeof(virDomainPtr) * numActiveDomains + numInactiveDomains);

inactiveDomains = malloc(sizeof(char *) * numDomains);

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

numActiveDomains = virConnectListDomains(conn,activeDomains,numActiveDomains);

numInactiveDomains = virConnectListDomains(conn,activeDomains,numActiveDomains);

numInactiveDomains = virConnectListDomains(conn,inactiveDomains,NumInactiveDomains);

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

{

allDomains[numDomains] = virDomainLoopupByID(aciveDomains[i]);

numDomains++

}

基于libvirt虚拟化 Guest domains lifecycle c ontrol API overview;

libvirt can control the entire lifecycle of guest domains,guest domains can transition through several states throughout their

virDomainCreateXML create and immediately boot a new transient guest domain,when this guest domain shuts down,all trace of it will disappear

virDomainDefineXML store the configuration for a persisent guest domain

virDomainCreate boot a previously defined guest domain from its persistent configuration,

virDomainDefineXML command can be used to turn a previously booted transient guest domain into a persistent domain

booting a transient guest domain

to boot a transient guest domain,simply requires a connection to libvirt and a string containing the XML document describing the required

configuration,the following example assumes that conn is

virDomainPtr dom;

const char *xmlconfig = "<domain>...</domain>";

dom = virConnectCreateXML(conn,xmlconfig,0);

if (!dom) {

fprintf(stderr,"Domain creation failed");

return;

}

fprintf(stderr,"Guest %s has booted",virDomainName(dom));

virDomainFree(dom);

Defining and booting a persistent guest domain

virDomainPtr dom;

const char *xmlconfig  = "<domain>..</domain>";

dom = virConnectDefineXML(conn,xmlconfig,0);

if (!dom)

{

fprintf(stderr,"Domain definition failed");

return;

}

if (virDomainCreate(dom) < 0)

{

virDomainFree(dom);

fprintf(stderr,"Cannot boot guest");

New guest provisioning techniques

<domain type='kvm'>

<name>demo</name>

<graphics type='vnc' port='-1' listen='localhost'>

 

}

CMROM/ISO image provisioning

<os>

<type arch='x86_64' machine='pc'>hvm</type>

<boot dev='hd'/>

<boot dev='cdrom'/>

</os>

<disk type='file' device='cdrom'>

<source file='/var/lib/libvirt/images/rhel5-x86_64-dvd.iso'/>

<target dev='hdc' bus='ide'>

</disk>

const char *xml = "<domain>...</domain>";

virDomainPtr dom;

dom = virDomainDefineXML(conn,xml);

if (!dom)

{

fprintf(stderr,"Unable to define persistent guest configuration");

return;

}

if (virDomainCreate(dom) < 0)

{

fprintf(stderr,"Unable to boot guest configuration");

}

Paravirtualized (full virtualization)准虚拟化 technologies

Paravirtualization ()半虚拟化

<os>

<type arch='x86_64' machine='pc'>hvm</type>

<kernel>/var/lib/libvirt/boot/f11-x86_64-vmlinuz</kernel>

<initrd>/var/lib/libvirt/boot/f11-x86_64-initrd.img</initrd>

<cmdline>method=http://download.fedoraproject.org/pub/fedora/linux/releases/11/x86_64/osconsole=ttySo console=tty<cmdline>

</os>

host side bootloader xen paravirtualized

<bootloader>/usr/bin/pygrub</bootloader>

<os>

<type arch='x86_64' machine='pc'>xen</type>

</os>

BIOS boot setup fully virtualized guest

const char *xml = "<domain>...</domain>"
virDomainPtr dom;
dom = virDomainCreateXML(conn,xml);
if (!dom)
{
fprintf(stderr,"Unable to define persistent guest configuration \n");
return;
}
if (virDomainCreate(dom) < 0) {
fprintf("stderr,"Unable to boot persistent guest \n"");
return;
}
fprintf(stderr,"Guest provisoning complete,OS is running \n");

stopping

a guest can be stopped by two methods

shutdown and destroy

virDomainPtr dom;
virDomainInfoPtr info;
const char *filename = "/var/lib/libvirt/save/demo-guest.img";
dom = virDomainLookupByName(conn,"demo-guest");
//id,uuid,name/第二个参数;
//virDomainPtr,virDomainLokkupByID,virDomainByName,virDomainByUUID
if (!dom)
{
fprintf(stderr,"Cannot find guest to be saved");
return;
}
if (virDomainGetInfo(dom,&info) < 0)
{
fprintf(stderr,"Cannot check guest state");
return;
}
if (info.state == VIR_DOMAIN_SHUTOFF)
{
fprintf(stderr,"Not saving guest that isn't  running");
}
if (virDomainSave(dom,filename) < 0)
{
fprintf(stderr,"Unable to save guest to %s ",filename);
}
fprintf(stderr,"Guest state saved to %s",filename);

virDomainPtr dom;

int id;

const char *filename = "/var/lib/libvirt/save/demo-guest.img";

if ((id = virDomainRestore(conn,filename)) < 0)

{

fprintf(stderr,"Unable to restore guest from %s",filename);

}

dom = virDomainLookupByID(conn,id);

if (!dom) {

fprintf(stderr,"Cannot find guest that was restored");

return;

}

virDomainMigrate established hypervisor connection

domains are defined in libvirt using XML  Everything related only to the domain

monitoring performance

statistical metrics are available for monitoring the utilization rates of domains,

vCPUS,Vmemory,block devices ,netWork interfaces;

virNodeDevicePtr de= get virNodeDevicePtr for the PCI device
if (virNodeDeviceDettach(dev) < 0)
{
fprintf(stderr,"Device cannot be dettached from the host OS drivers \n");
return;
}
if (virNodeDeviceReset(dev) < 0)
{
fprintf(stderr,"Device cannot be safely reset without affecting other devices \n");
}

virInterfaceDefineXML virInterfaceGetXMLDesc

xml definition of an ethernet interface using DHCP

<interface type='ethernet' name='eth0'>

<start mode='onboot'/>

<mac address=''/>

<protocol family='ipv4'>

 

xml definition of an ethernet interface with static ip

<interface type='ethernet' name='eth0'>

<start mode='onboot'/>

<mac address=''>

<protocol family=''>

<ip address="" prefix="24"/>

<route gateway="">

</protocol>

</interface>

xml definition of a bridge device with eth0 and eth1 attached

<interface type="bridge" name="br0">

<start mode="onboot"/>

<mtu size="1500"/>

<protocol family="ipv4">

<dhcp/>

</protocol>

<bridge stp="off" delay="0.01">

<interface type="ethernet" name="eth0">

<mac address=""/>

</interface>

<interface type="ethernet" name="eth1"/>

</bridge>

</interface>

xml definition of a vlan interface associated with eth0

<interface type="value" name="eth0.042">

<start mode="onboot"/>

<protocol family="ipv4">

<dhcp peerdns="no"/>

</protocol>

<vlan tag="42">

<interface name="eth0"/>

</vlan>

</interface>

retrieving information about interfaces

enumerating interfaces

have a connection to a host,respresented by a virConnectPtr,determine the number of interface on the host with virConnectNumOfInterfaces

and virConnectNumOfDefinedInterfaces

a list of those interfaces names can be obtained with virConnectListInterfaces and virConnectListDefinedInterfaces

("defined"  interfaces are those that have been defined,but are currently inactive)

getting a list of active("up" interfaces on a host)

int numIfaces,i
char *ifaceNames;
numIfaces = virConnectNumOfInterfaces(conn);
ifaceNames = malloc(numIfaces * sizeof(char *));
numIfaces = virConnectListInterfaces(conn,names,ct);
printf("Active host interfaces:\n");
for(i = 0;i < numIfaces;i++)
{
printf("%s \n",ifaceNames[i]);
free(ifaceNames[i]);
}
free(ifaceNames);

getting a list of inactive ("down") interfaces on a host

int numIfaces,i;
char *ifaceNames;
numIfaces = virConnectNumOfDefinedInterfaces(conn);
ifaceNames = malloc(numIfaces * sizeof(char *));//分配内存空间
 numIfaces = virConnectListDefinedInterfaces(conn,names,ct);
printf("Inactive host interfaces:\n");
for (i = 0;i < numIfaces;i++)
{
prientf("%s \n",ifaceNames[i]);
free(ifaceNames[i]);
}
free(faceNames);

altrnative method of enumerating interfaces

virNodeDevice

virNodeListDevices virNodeDeviceLookupByName virNodeDeviceGetXMLDesc 

obtaining a virInterfacePtr for an interface

many operations require that you have a virInterfacePtr,but you may only have the name or MAC address of the interface

use virInterfaceLookupByName and virInterfaceLookupByMACString to get the virInterfacePtr

virInterfacePtr iface;

const char *name = "eth0";

iface = virInterfaceLookupByName(name);

if (iface)

{

/*  use the virInterfacePtr*/

virInterfaceFree(iface);

}

else

{

printf("Interface '%s' not found. \n",name);

}

virInterfacePtr iface;

const char *mac ="";

iface = virInterfaceLookupByMACString(mac);

if (iface) {

//use the virInterfacePtr;

virInterfaceFree(iface);

}

else

{

printf("No interface found with MAC address '%s'.\n",mac);

}

virInterfaceGetName virInterfaceMACString virInterfaxceGetXMLDesc

const char *name;

const char *mac;

name = virInterfaceGetName(iface);

mac = virInterfaceGetMACString(iface);

printf("Interface %s has MAC address %s",name,mac);

virInterfaceGetXMLDesc

const char *xml;

name = virInterfaceGetXMLDesc(iface,0);

printf("Interface configuration:\n%s \n",xml);

free(xml);

//xml is a char * containing the description

virInterfacePtr iface;

iface = virInterfaceDefineXML(xml,0);

if (!iface)

{

fprintf(stderr,"Failed to define interface. \n");

//other error handling

goto cleanup;

virInterfaceFree(iface);

cleanup;

}

virInterfacePtr iface;

char *xml = NULL;

iface = virInterfaceLookupByName("br0");

if (!iface)

{

printf("Interface br0 not found. \n");

}

else

{

xml = virInterfaceGetXMLDesc(iface,0);

virInterfaceUndefine(iface);

virInterfaceFree(iface);

}

//you must also free the buffer at xml when you're finished with it

activating/deactivating an interface

temporarily bring down eth2,then bring it back up

virInterfaqcePtr iface;
iface = virInterfaceLookupByName("eth2");
if (!iface){
printf("Interface eth2 not found. \n");
}
else{
if (virInterfaceDestroy(iface) != 0)
{
fprintf(stderr,"failed to destroy (deactive) interface eth2. \n");
}
else{
//
if (virInterfaceCreate(iface) != 0){
fprintf(stderr,"failed to create (active) interface eth2. \n");
}
}
free(iface);
}

 

目录
相关文章
|
Java 数据库连接 Spring
Spring Boot命令行启动添加参数
Spring Boot命令行启动添加参数
2246 0
|
SQL 数据库 容器
软件体系结构 - 元组演算
【4月更文挑战第7天】软件体系结构 - 元组演算
514 2
|
5月前
|
数据采集 存储 前端开发
某乎爬虫进阶:爬取问题+回答+用户信息,构建知识图谱数据源
本文详解如何爬取知乎问题、回答及用户信息构建知识图谱:突破SSR渲染与反爬限制,采用API直调+Cookie认证+UA池+隧道代理方案;设计三表实体模型(问题/回答/用户),支持关系抽取与Neo4j存储,兼顾合规性与实用性。(239字)
493 2
|
5月前
|
存储 人工智能 API
2026企业智能体平台选型全指南
2026企业智能体平台选型全指南:聚焦全域管控、低门槛开发、工作流编排、生态集成与多Agent协同五大维度,结合企业规模与行业特性提供精准适配策略,助力规模化落地。(239字)
|
5月前
|
人工智能 自然语言处理 搜索推荐
多源交叉验证体系在本地生活 GEO 优化中的技术落地与实践
摘要 随着生成式大模型在本地生活服务领域的深度应用,用户消费决策入口逐步向 AI 问答迁移,生成式引擎优化(GEO)成为实体商家数字化运营的新方向。当前行业普遍存在信源单一、实体识别歧义、地域权重失真等问题,导致优化效果不稳定、合规风险高。本文提出一套基于多源交叉验证的 GEO 技术体系,从政商实体、地理空间、内容语义、行为反馈四大维度构建置信度评分模型,并结合贵阳本地市场完成落地实践。数据表明,该体系可将商家实体置信度平均提升 42%,AI 推荐触发率提升 37%,同时全程符合白帽合规要求,为本地生活类 GEO 优化提供了可复用的技术框架。 关键词:生成式引擎优化;多源交叉验证;本地生活服务
424 0
|
6月前
|
运维 API 数据库
哪个IP查询工具更新更及时?实测对比:日更库 vs周更/月更库
IP归属地动态变化,周/月更库易过时,导致广告错投、风控失效。本文实测对比发现:日更商业库新IP段24小时内精准入库,而月更纯真库等严重滞后。附三大验证方法——跟踪新IP段、多工具交叉比对、WHOIS事实核查。(239字)
1200 1
|
7月前
|
人工智能 Java 开发者
Java做AI不行?2026年最大的认知误区
2026年,Java做AI已成现实:Spring官方集成DeepSeek,JBoltAI等框架支持MCP协议与Agent工程化落地。Java凭借稳定性、类型安全与现有系统优势,正成为企业级AI应用的首选底座——不换语言,即可构建可运行的AI服务。
679 1
|
传感器 存储 边缘计算
定位与专长的分野:ThingsBoard 物联网平台与 MyEMS 能源管理系统的深度对比
ThingsBoard 与 MyEMS 是两款数据驱动的开源技术平台,分别聚焦物联网全域管理与能源垂直领域。前者以泛在物联为核心,具备设备接入、规则引擎、可视化与多租户管理能力,适用于智慧城市、工业物联网等场景;后者专注能源管理,提供能源数据治理、能效优化与碳排分析功能,广泛应用于制造、建筑与新能源场景。两者在技术架构与应用场景上各具特色,分别体现了“广度连接”与“深度专精”的技术路径。
532 2
|
机器学习/深度学习 人工智能 运维
运维不背锅,从“自动修锅”开始:AI自动化运维是怎么回事?
运维不背锅,从“自动修锅”开始:AI自动化运维是怎么回事?
954 49
|
编解码 Java 计算机视觉
探索 JavaCV:开启计算机视觉与多媒体处理新世界
JavaCV 是基于 OpenCV 和 FFmpeg 的 Java 接口库,助力开发者实现视频处理、图像分析等功能。支持多种音视频格式编解码、GPU 加速及跨平台运行,适用于直播录制、摄像头捕获、美颜相机等场景,是多媒体开发的利器。
1444 1