Anatomy of the libvirt virtualization Library

Source: Internet
Author: User
Tags ovirt

The libvirt library is a Linux API over the specified alization capabilities of Linux that supports a variety of hypervisors, including xen and KVM, as well as qemu and some other alization products for other operating systems. this article es libvirt, its use, and its architecture.

When it comes to scale-out computing (such as cloud computing), libvirt may be one of the most important Libraries you 've never heard. libvirt provides a hypervisor-agnostic API to securely manage guest operating systems running on a host. libvirt isn' t a toolPer seBut an API to build tools to manage guest operating systems. libvirt itself is built on the idea of your action. it provides a common API for common functionality that the supported hypervisors implement. libvirt was originally designed as a management API for xen, but it has since been extended to support a number of hypervisors. basic Architecture join the Green Groups on my developerworks

Discuss topics and share resources about energy, efficiency, and the environment on the green it report space and the green Computing Group on my developerworks.

Let's start our discussion of libvirt with a view of the use model, then dig into its architecture and use. libvirt exists as a set of Apis designed to be used by a management application (see figure 1 ). libvirt, through a hypervisor-specific mechanic, communicates with each available hypervisor to perform the API requests. I have e how this is done with qemu later in the article. figure 1. comparison and use model of libvirt

Also shown is a comparison of the terminology that libvirt uses. This terminology is important, as these terms are used in API naming. The two fundamental differences are that libvirt CILS the physical hostNode,And the guest operating system is calledDomain.Note here that libvirt (and its Application) runs in the domain of the host Linux operating system (domain 0). Means of control

With libvirt, you have two distinct means of control. the first is demonstrated in Figure 1, where the management application and domains exist on the same node. in this case, the management application works through libvirt to control the local domains. the other means of control exist when the management application and the domains are on separate nodes. in this case, remote communication is required (see figure 2 ). this mode uses a special daemon calledLibmongodThat runs on remote nodes. this daemon is started automatically when libvirt is installed on a new node and can automatically determine the local hypervisors and set up drivers for them (to be discussed shortly ). the management application communicates through the local libvirt to the remote lib1_d through a custom protocol. for qemu, the protocol ends at the qemu monitor. qemu nodes des a monitor console that allows you to inspect a running guest operating system as well as control various aspects of the Virtual Machine (VM ). figure 2. control of remote hypervisors with libmongod hypervisor support

To support extensibility over a wide variety of hypervisors, libvirt implements a driver-based architecture, which allows a common API to service a large number of underlying hypervisors in a common fashion. this means that certain specialized functionality of some hypervisors is not visible through the API. additionally, some hypervisors may not implement all API functions, which are then defined as unsupported within the specific driver. figure 3 into strates the layering of the libvirt API and associated drivers. note also here that libmongod provides the means to access local domains from remote applications. figure 3. driver-based architecture of libvirt

As of this writing, libvirt implements drivers for the hypervisors listed in Table 1. Other drivers will no doubt be available as new hypervisors emerge from the open source communities. Table 1. hypervisors that libvirt supports


Hypervisor for IA-32, IA-64, and PowerPC 970 ubuntures

Platform emulator for varous ubuntures

Kernel-based Virtual Machine (KVM)
Linux platform emulator

Linux containers (lxc)
Linux (lightweight) containers for operating system virtualization alization

Operating System-level virtualization based on the Linux Kernel

Hypervisor for x86 Virtualization

User Mode Linux
Linux platform emulator for various ubuntures

Test driver for a fake hypervisor

Storage pool drivers (Local disk, network disk, iSCSI volume)

Back to Top libvirt and the virtualization Shell

Now that I 've covered some of the architecture of libvirt, let's look at some examples of the use of the libvirt using alization API. I start by using an application calledVirsh(Virtualization shell), which is built on top of libvirt. this shell permits use of much of the libvirt functionality but in an interactive (shell-based) fashion. in this section, I demonstrate some of the aspects of VM manipulation using virsh.

The first step is to define the domain configuration file (shown in Listing 1, below ). this Code specifies all the necessary options for defining a domain-from the hypervisor (emulator) to the resources that the domain uses and peripheral configuration (such as the network ). note that this is a very simple configuration: the actual attributes that libvirt supports are much more diverse. for example, you can specify a BIOS and host bootloader, resources to be used by the domain, and devices to be used-from floppy disks and CD-ROMs to USB and PCI devices.

The domain configuration file defines some of the basic metadata to be used for this qemu domain, including the domain name, maximum memory, and initially available memory (current) as well as the number of virtual processors to be made available to this domain. you don't assign a universally unique identifier (UUID); instead, you allow libvirt to assign one. you define the type of machine to emulate for this platform-in this case, a 686 processor that is fully virtualized (hvm ). you define the location of the emulator (in case you need to support multiple of the same type) and the virtual disk for the domain. note here that you indicate the VM, which is a reactos operating system in Virtual Machine disk (vmdk) format. finally, you specify the default networking configuration, and you use virtual network computing (VNC) for graphics. listing 1. domain Configuration File

<xml version="1.0"?><domain type=‘qemu‘>  <name>ReactOS-on-QEMU<name>  <uuid<uuid>  <memory>131072<memory>  <currentMemory>131072<currentMemory>  <vcpu>1<vcpu>  <os>    <type arch=‘i686‘ machine=‘pc‘>hvm<type>  <os>  <devices>    <emulator>usr/bin/qemu<emulator>    <disk type=‘file‘ device=‘disk‘>      <source file=‘/home/mtj/libvtest/ReactOS.vmdk‘/>      <target dev=‘hda‘/>    <disk>    <interface type=‘network‘>      <source network=‘default‘/>    <interface>    <graphics type=‘vnc‘ port=‘-1‘/>  <devices><domain>

Now, with the domain configuration file complete, let's start a domain with the virsh tool. the virsh tool takes a command argument for the participating action to be taken. in the case of starting a new domain, you usecreateCommand and the domain configuration file: Listing 2. Starting a new domain

[email protected]:~/libvtest$ virsh create react-qemu.xmlConnecting to uri: qemu:///systemDomain ReactOS-on-QEMU created from react-qemu.xml[email protected]:~/libvtest$

Note here the Universal Resource indicator (URI) used to attach to the domain (qemu:///system). This local URI attaches to the system mode daemon for the local qemu driver. to attach to a remote qemu hypervisor over the Secure Shell (SSH) Protocol on host shinchan, you can use the URI qemu + SSH: // shinchan /.

Next, you can list the active domains on a given host usinglistCommand within virsh. Doing so lists the active domains, their domain IDs, and their State, as shown below: Listing 3. Listing active Domains

[email protected]:~/libvtest$ virsh listConnecting to uri: qemu:///system Id Name                 State----------------------------------  1 ReactOS-on-QEMU      running[email protected]:~/libvtest$

Note that the name defined here is the name you defined in your domain configuration file metadata. You can see that this domain has a domain ID of 1 and is currently running.

You can also suspend a domain withsuspendCommand. this command stops the domain from being scheduled, but the domain continues to reside in memory and can be quickly resumed. the following example extends strates suspending the domain, specify a list to see status, and then restarting the domain: Listing 4. suspending a domain, checking status, and restarting

[email protected]:~/libvtest$ virsh suspend 1Connecting to uri: qemu:///systemDomain 1 suspended[email protected]:~/libvtest$ virsh listConnecting to uri: qemu:///system Id Name                 State----------------------------------  1 ReactOS-on-QEMU      paused[email protected]:~/libvtest$ virsh resume 1Connecting to uri: qemu:///systemDomain 1 resumed[email protected]:~/libvtest$

The virsh utility also supports a number of other commands, such as saving a domain (save), Restoring a saved domain (restore), Rebooting a domain (reboot), And define others. You can also create a domain configuration file from a running domain (dumpxml).

So far, you 've started and manipulated a domain. but what about attaching to it so that you can see the domain in action. you can do this using VNC. to create a window representing the graphical desktop of the particle domain, you can use VNC as: Listing 5. attaching to a domain

[email protected]:~/libvtest$ xvnc4viewer 0

Back to toplibvirt and Python

The previous example was strated the control of domains using the command-line utility virsh. let's now look an example of domain control using python. python was the libvirt-supported scripting language and provides a clean, object-oriented interface to the libvirt API.

In this example, I need e some of the same operations that I demonstrated with the virsh utility (list,suspend,resume, And so on ). the Python example script is provided in Listing 6. in this example, you begin by importing the libvirt module. you then connect to the local qemu hypervisor. from here, you iterate through the domain IDs that are available; for each one, you create a domain object, and then suspend, resume, and finally destroy the domain. listing 6. sample Python script for domain control (libvtest. PY)

import libvirtconn =‘qemu:///system‘)for id in conn.listDomainsID():dom = conn.lookupByID(id)print "Dom %s  State %s" % (,[0] )dom.suspend()print "Dom %s  State %s (after suspend)" % (,[0] )dom.resume()print "Dom %s  State %s (after resume)" % (,[0] )dom.destroy()

Although this is a simple example, you can see the power that libvirt provides through python. through a simple script, you are able to iterate through all of the local qemu domains, emit some information about the domain, and then control the domain. the output of this script is shown in listing 7. listing 7. output from the Python script in Listing 6

[email protected]:~/libvtest$ python libvtest.pyDom ReactOS-on-QEMU  State 1Dom ReactOS-on-QEMU  State 3 (after suspend)Dom ReactOS-on-QEMU  State 1 (after resume)[email protected]:~/libvtest$

Back to topapi Overview

At a high level, the libvirt API can be divided into five API sections: the hypervisor connection API, the domain API, the Network API, the storage volume API, and finally the storage pool API.

All libvirt communication occurs after a connection is created for a given hypervisor (for example, as shown withopenCall in Listing 6). The Connection provides a path for all other APIs to work through. InCAPI, this behavior is provided throughvirConnectOpenCall (as well as others for authentication). The response of these functions isvirConnectPtrObject, which represents a connection to a hypervisor. This object serves as the basis for all other management functionality and is therefore a required argument for subsequent API callto a given hypervisor. Important subsequent callarevirConnectGetCapabilities, Which returns the capabilities of the hypervisor and driver, andvirNodeGetInfo, Which retrieves information about the node. This information is returned as an XML document that can be parsed to understand which behaviors are possible.

Now, having access to a hypervisor, You Can iterate through the varous resources on that hypervisor with a set of api cils.virConnectListDomainsAPI call returns a list of domain identifiers representing the active domains on that hypervisor.

The API implements a large number of functions targeted toward domains. To operate e or manage a domain, you first needvirDomainPtrObject. you can get this handle in a number of ways (using either the ID, UUID, or domain name ). continuing with the example of iterating domains, you can use the index list that this function returns and callvirDomainLookupByIDTo get the domain handle. With the domain handle in hand, you can now perform a large number of operations, from processing the domain (virDomainGetUUID,virDomainGetInfo,virDomainGetXMLDesc,virDomainMemoryPeek) To controlling the domain (virDomainCreate,virDomainSuspend,virDomainResume,virDomainDestroy, AndvirDomainMigrate).

You can also use the API to manage and inspect virtual networks and storage resources. Following the model of the API,virNetworkPtrObject is necessary to manage and inspect virtual networks, andvirStoragePoolPtr(Storage pool) orvirStorageVolPtr(Volume) object is necessary to manage these resources.

The API also supports an event mechanic with which you can register to be notified of particle events (such as a domain being booted, suincluded, resumed, or stopped ).

Back to toplanguage bindings

The libvirt library was implemented inC(SupportingC++) And supported des direct support for python. But it also supports a number of language bindings. bindings have been implemented for Ruby, the Java? Language, Perl, and ocaml. Work has also been done for calling libvirt fromC#. Libvirt supports the most popular system programming languages ages (CAndC++), A variety of scripting languages, and even a uniied functional language (objective caml). So whatever your language focus, libvirt provides a path to control your domains.

Back to topapplications using libvirt

From just the small amount of capabilities that I 've demonstrated in this article, you can see the power that libvirt provides. and as you can perform CT, there are a number of applications that are being successfully built on libvirt. one of the interesting applications is virsh (demonstrated here), which is a implements alization shell. there's also virt-install, which can be used to provision new domains from operating system distributions. the utility virt-clone can be used to clone a VM from another VM (covering both operating system and disk replication ). some of the higher-level applications include virt-manager, which is a general-purpose desktop-management tool, and virt-viewer, which is a lightweight tool for securely attaching to the graphical console of VMS.

One of the most important tools built on libvirt is calledOvirt.The ovirt VM management application was designed to manage a single VM on a single node or thousands of VMS over hundreds of hosts. in addition to simplifying management of large numbers of hosts and VMS, it can be used to automate clustering and load balancing and works against SS platforms and ubuntures.

Back to topgoing further

As you can see from this short article, libvirt is a great library for building applications that manage domains in your different hypervisor environments over large networks of systems. given the growing popularity of cloud computing, libvirt will no doubt grow along with it, finding new applications and users. as of this writing, libvirt is only just over four years old, so it's relatively new in the massively Scalable Computing space. more is most certainly to come. resourceslearn

  • Check out the libvirt web site for the latest news about libvirt and to download the latest version. You'll also find a complete api reference manual that covers the core interfaces and error-handling interfaces.
  • Libvirt supports a large number of hypervisors, including:
    • Xen
    • Qemu
    • KVM
    • Lxc
    • Openvz
    • Virtualbox
    • User-mode Linux
  • In "virtual Linux: An Overview of virtualization methods, ubuntures, and implementations" (developerworks, December 2006), learn more about the various types of virtualization. cloud computing relies on Virtualization alization for optimal use of server-available resources. with parameter alization, servers can be used to host multiple operating systems and application sets.
  • In "lxc: Linux container Tools" (developerworks, February 2009), you can learn more about the management tools built specifically for lxc. you'll notice some parallels here with the management methods for libvirt.
  • Virtual Network Computing, or VNC, is a method for sharing graphical tables tops over a network. this variable action is ideal for massive scale-out computing, as it means a single station can manage limit distributed clients.
  • The Linux subsystem for managing file systems is large and complex. You can learn more about the larger File System Subsystem in "Anatomy of the Linux File System" (developerworks, October 2007 ).
  • In the developerworks Linux zone, find more resources for Linux developers, and scan our most popular articles and tutorials.
  • See all Linux articles and Linux tutorials on developerworks.
  • Stay current with developerworks technical events and webcasts.
Get products and technologies
  • Red Hat's ovirt open VM management platform is a user of libvirt and demonstrates what kinds of applications can be built. you can use ovirt to manage large numbers of hosts, and the platform easily scales to support thousands of VMS.
  • In this article, you used the reactos as a means to demonstrate a domain on qemu. This article provides more information on reactos (a free windows? Clone), which is easily executed on the qemu platform.
  • With IBM trial software, available for download directly from developerworks, build your next development project on Linux.

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: 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.