码迷,mamicode.com
首页 > Windows程序 > 详细

libvirtsAPI

时间:2014-06-28 19:38:48      阅读:431      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   get   

mongodb远程服务器连接

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

bubuko.com,布布扣

基于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);
}

 

libvirtsAPI,布布扣,bubuko.com

libvirtsAPI

标签:des   style   blog   http   color   get   

原文地址:http://www.cnblogs.com/ruiy/p/ap.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!