www.cnblogs.com/ruiyqinrui

开源、架构、Linux C/C++/python AI BI 运维开发自动化运维。 春风桃李花 秋雨梧桐叶。“力尽不知热 但惜夏日长”。夏不惜,秋不获。@ruiY--秦瑞

python爬虫,C编程,嵌入式开发.hadoop大数据,桉树,onenebula云计算架构.linux运维及驱动开发.

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  2911 随笔 :: 9 文章 :: 51 评论 :: 185万 阅读

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 * sizeofchar *));//分配内存空间
 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);
}
复制代码

 

posted on   秦瑞It行程实录  阅读(750)  评论(0编辑  收藏  举报
编辑推荐:
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
阅读排行:
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· 语音处理 开源项目 EchoSharp
· 《HelloGitHub》第 106 期
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 使用 Dify + LLM 构建精确任务处理应用
www.cnblogs.com/ruiyqinrui
点击右上角即可分享
微信分享提示