Libvirt Section API Introduction

Source: Internet
Author: User
Tags uuid identifier

Thank your friends for supporting this blog. Welcome to discuss the exchange together, because the ability and time is limited, the mistake is unavoidable, welcome correction!


If reproduced, please retain the author's information.


Blog Address: http://blog.csdn.net/qq_21398167

Original post address: http://blog.csdn.net/qq_21398167/article/details/46422583

Http://libvirt.org/html/index.html Official website includes all the APIs
API Overview

The Advanced Libvirt API can be divided into 5 API parts: Hypervisor connection API, domain API, network API, storage volume API, and storage pool API.

Creating a connection for a given hypervisor generates all LIBVIRT traffic (for example, the open call seen in Listing 6). This connection provides the path for all other APIs to be used. In the C API, this behavior is provided through Virconnectopen calls (and other authentication calls). The return value of these functions is a Virconnectptr object that represents a connection to the hypervisor. This object serves as the basis for all other administrative functions and is necessary for a given hypervisor to make concurrent API calls. Important concurrent calls are virconnectgetcapabilities and Virnodegetinfo. The former returns the functionality of the hypervisor and the driver, which obtains information about the node.

This information is returned as an XML document, so that you can understand the behavior that might occur by parsing.

After entering the hypervisor, you can use a set of API call functions to reuse the various resources on the hypervisor. The Virconnectlistdomains API call function returns a list of domain identifiers that represent the active domain on the hypervisor.

The API implements a large number of domain-specific functions. To explore or manage domains. First, a Virdomainptr object is required. There are several ways you can get the handle (using ID, UUID, or domain name). To continue with the example of a repeating field, you can use the index table returned by the function and call Virdomainlookupbyid to get the domain handle.

With this domain handle, you can run very many operations, from the Explore domain (Virdomaingetuuid, virdomaingetinfo, Virdomaingetxmldesc, Virdomainmemorypeek) to the control domain (virdomaincreate, virdomainsuspend, Virdomainresume, Virdomaindestroy, and VirDomainMigrate).

You can also use the API to manage and examine virtual networks and storage resources. After the API model has been established. A Virnetworkptr object is required to manage and examine the virtual network, and a virstoragepoolptr (storage pool) or virstoragevolptr (volume) object is required to manage these resources.

The API also supports an event mechanism. You can use this mechanism to register to be notified when a particular event occurs, such as when a domain is started, aborted, resumed, or stopped.



(1) Connect Hypervisor related APIs: A series of functions starting with virconnect.


Only after the connection with Hypervisor has been established, the ability to perform virtual machine management operations, so the API to connect Hypervisor is a precondition for all other APIs to use. The connection established with Hypervisor provides a path for the operation of other APIs and is the basis for other virtualization management functions.

The Virconnectopen function can establish a connection. The return value is a Virconnectptr object that represents a connection to hypervisor, and a null value (NULL) if there is an error in the connection.
The Virconnectopenreadonly function creates a read-only connection. The ability to use some queries on the connection is not used, such as creation, churn, and so on.
The Virconnectopenauth function provides a more authentication-established connection.
The Virconnectgetcapabilities function is an XML-formatted string that returns a descriptive narrative of the Hypervisor and the drive's functionality. The Virconnectlistdomains function returns a list of field identifiers that represent the active domain on the Hypervisor.
Virconnectgethostname
Virconnectgetmaxvcpus
Virconnectgettype
Virconnectgetversion
Virconnectgetlibversion
Virconnectgeturi
virconnectisencrypted
Virconnectissecure
Virconnectclose Closing the connection

(2) domain-managed API: A series of functions that begin with Virdomain.


Virtual machine management, the most important function is the management of the domain on each node, so Libvirt API implemented a lot of domain management functions.

To manage a domain, you first get the Virdomainptr domain object. Then the ability to manipulate the domain. There are very many ways to get domain objects, such as Virdomainptr

The Virdomainlookupbyid (virconnectptr conn, int id) function finds the corresponding domain based on the ID value of the domain to conn this connection. The functions of Virdomainlookupbyname, virdomainlookupbyuuid and so on are respectively based on the name and UUID of the domain to find the corresponding domain.

After you get the object for a domain, you can do a lot of work. Can be a query for domain information
Virdomaingethostname
Virdomaingetinfo
Virdomaingetvcpus
Virdomaingetvcpusflags
Virdomaingetcpustats
can also be the life cycle of the control domain. Such as:
Virdomaincreate
Virdomainsuspend
Virdomainresume
Virdomaindestroy
Virdomainmigrate
Create a virtual machine
Virdomaindefinexml ()
Virfilereadall () The function prototype is intvirfilereadall (const char *path, int maxlen, char **buf). The function is to read the file contents of the path specified by the parameter "path" into a buffer. and the buffer address is recorded in the parameter "*buf". The number of references "MaxLen" specifies the maximum length of the file. Take advantage of this API. We were able to put the XML configuration file into a buffer. To facilitate the next use

Virdomaincreatexml () The function prototype is Virdomainptr virdomaincreatexml (virconnectptrconn, const char * xmldesc, unsigned i NT flags), a function that creates a domain and returns a pointer to the domain according to the configuration defined by the "Xmldesc" parameter. The reference "conn" is a pointer to the virtual Machine Manager. And by setting a different "flags" flag. Enables you to have different properties for the created domain.


(3) node-managed API: A series of functions that begin with Virnode.
The domain is executed on top of the physical node, Libvirt also provides the ability to query and control the information of the node. Most functions of node management need to use a connection Hypervisor object as one of the incoming parameters in order to be able to query or change the information to the nodes on that connection.

The Virnodegetinfo function is the physical hardware information that gets the node, http://my.oschina.net/u/274153/blog/55011
The Virnodegetcpustats function can obtain usage statistics for each CPU on a node
The Virnodegetmemorystats function can get the usage statistics of the memory on the node
The Virnodegetfreememory function can get the spare memory size available on the node.

There are also functions that set or control the node Virnodesetmemoryparameters function to set the parameters of memory scheduling on the node
The Virnodesuspendforduration function allows a node (host) to pause for a period of time
Virnodegetcellsfreememory


(4) network management API: A series of functions beginning with virnetwork and some functions beginning with virinterface.
Libvirt also provides a rich API for network management in virtualized environments. Libvirt first need to create a Virnetworkptr object, then ability to query or control the virtual network. Some functions that query network-related information. Such as:

Virnetworkgetname (Virnetworkptr Network) function to get the name of the net
Network:a Network Object
Returns:a Pointer to the name or NULL, the string need not is deallocated its lifetime would be is the same as the network Object.

The Virnetworkgetbridgename function can get the name of the bridge in the network
Virnetworkgetuuid function to get the UUID identifier of the network
The VIRNETWORKGETXMLDESC function can obtain the descriptive narrative information of the network in XML format.
The Virnetworkisactive function can query whether the network is in use. Some of the functions that control or change network settings are:
int virnetworkcreate (VIRNETWORKPTR network) Get the Network property

Virnetworkcreatexml (virconnectptr conn,const char * xmldesc) function to create a network (return Virnetworkptr object) based on the supplied XML-formatted string
Conn:pointer to the hypervisor connection
Xmldesc:an XML Description of the network
RETURNS:A new Network object or NULL in case of failure
Virdomaininterfaceptr
The Virnetworkdestroy function destroys a network (the domain that uses the network is also closed at the same time)
The Virnetworkfree function can reclaim a network (but does not close the executing domain)
The Virnetworkupdate function updates an existing network based on the network configuration provided by the XML format.

Other than that
Virinterfacecreate,
Virinterfacefree,
Virinterfacedestroy,
Virinterfacegetname,
Functions such as virinterfaceisactive can be used to create, release, and destroy network interfaces, and to query the name and activation status of network interfaces.


(5) Storage volume Management API: A series of functions beginning with Virstoragevol.

Libvirt Management of Storage volumes (volume). The main is the management of the domain image files, these images may be raw, qcow2, VMDK, QED and other formats. Libvirt the management of a storage volume, you first need to create an object that virstoragevolptr this storage volume. Then the ability to query or control the operation. Libvirt provides 3 functions to obtain a storage volume object in different ways. Such as:
The Virstoragevollookupbykey function is able to obtain a storage volume object based on globally unique key values
The Virstoragevollookupbyname function can obtain a storage volume object in a storage resource pool (storage pool) by name Virstoragevollookupbypath The function can obtain a storage volume object based on its path on the node. Some functions are used to query information about a stored volume, such as: The Virstoragevolgetinfo function can query the usage of a storage volume
Virstoragevolgetname function to get the name of the storage volume
Virstoragevolgetpath function to get the path of the storage volume
The Virstoragevolgetconnect function can query the connection of a storage volume. Some functions are used to create and alter storage volumes. Such as:
The Virstoragevolcreatexml function can create a storage volume based on the XML descriptive narrative provided
The Virstoragevolfree function frees the handle to the storage volume (but the storage volume still exists)
The Virstoragevoldelete function can delete a storage volume, and the Virstoragevolresize function can resize the storage volume.


(6) Storage pool Management API: A series of functions that begin with Virstoragepool.

Libvirt management of the storage pool, including the local basic file system, common network shared file system, iSCSI shared file system, LVM partition and other management.

Libvirt needs to query and control operations based on VIRSTORAGEPOOLPTR, the storage pool object talent. Some functions can obtain a storage pool object through a query. Such as:
The Virstoragepoollookupbyname function can obtain a storage pool object based on the name of the storage pool
Virstoragepoollookupbyvolume can return its corresponding storage pool object based on a storage volume
The Virstoragepoolcreatexml function can create a storage pool based on an XML descriptive narrative (which is activated by default)
The Virstoragepooldefinexml function can statically define a storage pool based on XML descriptive narrative information (not yet activated)
The Virstoragepoolcreate function can activate a storage pool.
Virstoragepoolgetinfo,
Virstoragepoolgetname,
The Virstoragepoolgetuuid function can obtain the information, name, and UUID identifier of the storage pool, respectively.
The Virstoragepoolisactive function can query whether the storage pool is in use state.


The Virstoragepoolfree function frees up storage-pool-related memory (but does not change its state in the host)
The Virstoragepooldestroy function can be used to destroy a storage pool (but it does not release the Virstoragepoolptr object, and then it can be activated again with the Virstoragepoolcreate function).
The Virstoragepooldelete function can physically delete a storage pool resource, which is not recoverable.


(7) API for event management: A series of functions beginning with virevent.

Libvirt supports the event mechanism, which is used to obtain customized notifications when specific events occur, such as starting, pausing, resuming, stopping, and so on.

(8) API for Data Flow Management: A series of functions beginning with Virstream.

Libvirt also provides a series of functions for the transmission of data streams.

(9) Error handing
Virseterrorfunc
Virconnseterrorfunc
Vircopylasterror
Virgetlasterror
Virsavelasterror
Virreseterror
Virfreeerror
Virconnreseterror
Virconncopylasterror
Virconngetlasterror

Virdomainmigrate Virtual Machine Migration
Virdomainreboot Virtual Machine Restart
int virdomainref (virdomainptr domain)
int Virdomainreset (virdomainptr domain,
unsigned int flags)
int Virdomainrestore (Virconnectptr conn,
const char * from)
int Virdomainrestoreflags (Virconnectptr conn,
const char * from,
const char * dxml,

Libvirt Section API Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.