libvirtsAPI
2020-12-13 03:08
标签:des style blog http color get 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 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
{ 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
{ 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
{ 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 = " 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 = " dom = virConnectDefineXML(conn,xmlconfig,0); if (!dom) { fprintf(stderr,"Domain definition failed"); return; } if (virDomainCreate(dom)
{ virDomainFree(dom); fprintf(stderr,"Cannot boot guest"); New guest provisioning techniques } CMROM/ISO image provisioning const char *xml = " virDomainPtr dom; dom = virDomainDefineXML(conn,xml); if (!dom) { fprintf(stderr,"Unable to define persistent guest configuration"); return; } if (virDomainCreate(dom)
{ fprintf(stderr,"Unable to boot guest configuration"); } Paravirtualized (full virtualization)准虚拟化 technologies Paravirtualization ()半虚拟化 host side bootloader xen paravirtualized BIOS boot setup fully virtualized guest stopping a guest can be stopped by two methods shutdown and destroy virDomainPtr dom; int id; const char *filename = "/var/lib/libvirt/save/demo-guest.img"; if ((id = virDomainRestore(conn,filename))
{ 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; virInterfaceDefineXML virInterfaceGetXMLDesc xml definition of an ethernet interface using DHCP xml definition of an ethernet interface with static ip xml definition of a bridge device with eth0 and eth1 attached xml definition of a vlan interface associated with eth0 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) getting a list of inactive ("down") interfaces on a host 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 libvirtsAPI,搜素材,soscw.com libvirtsAPI 标签:des style blog http color get 原文地址:http://www.cnblogs.com/ruiy/p/ap.html
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.const char *xml = "
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);
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");
}
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 )
{
printf("%s \n",ifaceNames[i]);
free(ifaceNames[i]);
}
free(ifaceNames);
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 )
{
prientf("%s \n",ifaceNames[i]);
free(ifaceNames[i]);
}
free(faceNames);
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);
}