Libvirt virtualization Database Analysis

Source: Internet
Author: User

Speaking of outward scaling computing (such as cloud computing), libvirt may be one of the most important Libraries you have never heard. Libvirt provides an unknown API for Virtual Machine Monitoring programs to securely manage guest operating systems running on hosts. LibvirtItselfIt is not a tool, but an API that allows you to create a tool to manage the guest operating system. Libvirt is built on an abstract concept. It provides common APIs for common functions implemented by supported virtual machine monitoring programs. Libvirt was initially a management API specifically designed for xen and later expanded to support multiple virtual machine monitoring programs.

Basic Architecture

First, let's discuss libvirt from the perspective of case model, and then explore its architecture and usage. Libvirt exists in the form of a group of APIS for managing applications (see figure 1 ). Libvirt communicates with each valid Virtual Machine monitor program through a mechanism specific to the Virtual Machine monitor program to complete API requests. Later, I will discuss how to implement this function through qemu.

Figure 1. libvirt comparison and Case Model

The comparison of libvirt terms is also shown in the figure. These terms are important because they are used for API naming. The two fundamental differences are that libvirt calls a physical hostNodeThe guest operating system is calledDomain. It is worth noting that libvirt (and its applications) runs in the host Linux operating system (domain 0.

Control Mode

Using libvirt, we have two different control methods. The first is shown in figure 1, where the management application and domain are located on the same node. In this example, the management application works through libvirt to control the local region. When the management application and domain are located on different nodes, another control method is created. In this example, remote communication is required (see figure 2 ). This mode runs on a remote node namedLibmongod. When libvirt is installed on a new node, the program starts automatically, and the local Virtual Machine Monitoring Program can be automatically identified and the driver can be installed for it (discussed later ). The management application connects to the remote libmongod from the local libvirt through a common protocol. For qemu, the protocol ends at the qemu monitor. Qemu contains a monitoring console that allows you to check the operating guest operating system and control all parts of a Virtual Machine (VM.

Figure 2. Use libmongod to control the remote Virtual Machine Monitoring Program

Support for Virtual Machine Monitoring programs

To support the scalability of various virtual machine monitoring programs, libvirt implements a driver-based architecture that allows a common API to provide services for a large number of potential Virtual Machine Monitoring programs in a common way. This means that some professional functions of some virtual machine monitoring programs are invisible in the API. In addition, some virtual machine monitors may not be able to implement all the API functions, so they are defined as not supported in a specific driver. Figure 3 shows the hierarchy of libvirt APIs and related drivers. Here, you also need to note that libmongod provides a way to access the local region from a remote application.

Figure 3. Driver-based libvirt Architecture

At the time of writing this article, libvirt implemented the driver for the Virtual Machine monitor listed in Table 1. With the emergence of new Virtual Machine Monitoring programs in the open-source community, other drivers will undoubtedly be available.

Table 1. Virtual Machine Monitoring programs supported by libvirt
Virtual Machine Monitoring Program Description
Xen Virtual Machine monitors for IA-32, IA-64 and PowerPC 970 Architectures
Qemu Platform simulators for various architectures
Kernel-based Virtual Machine (KVM) Linux platform Simulator
Linux containers (lxc) Linux (lightweight) containers used for operating system virtualization
Openvz Linux Kernel-based OS Virtualization
Virtualbox X86 Virtual Machine Monitoring Program
User Mode Linux Linux platform simulators for various architectures
Test Test drive for pseudo-Virtual Machine Monitoring Program
Storage Storage pool drive (Local disk, network disk, iSCSI volume)
 

Back to Top

Libvirt and Virtual Shell

The above section describes some libvirt architectures. Next, let's take a look at some examples of how to use the libvirt virtualization API. First, we will introduceVirsh(Virtual Shell) application, which is built on libvirt. This shell allows multiple libvirt functions to be used in interactive (shell-based) mode. In this section, I use virsh to demonstrate some VM operations.

The first step is to define the domain configuration file (as shown in Listing 1 below ). This Code specifies all the options required for the definition domain-from the Virtual Machine monitor program (simulator) to the resources used by the domain and the peripheral configuration (such as the network ). Note that this is only a simple configuration, and libvirt actually supports more diverse attributes. For example, you can specify bios and host boot programs, resources to use in the domain, and devices to use-from floppy disks and CD-ROM to USB and PCI devices.

The domain configuration file defines some basic metadata for the qemu domain, including the domain name, maximum memory, initial available memory (current), and the number of virtual processors available for the domain. You do not need to assign the universally unique idenifier (UUID), but libvirt. You need to define the machine type to be simulated for the platform-in this example, the 686 processor is fully virtualized (hvm. You need to define the simulator location for the domain (for use when multiple similar simulators are supported) and virtual disks. Note that the VM is a reactos operating system in the Virtual Machine disk (vmdk) format. Finally, you must specify the default network settings and use the graphic-Oriented Virtual Network Computing (VNC ).

List 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>

After completing the domain configuration file, start the domain using the virsh tool. The virsh tool uses command parameters for specific actions to be executed. When starting a new domain, usecreateCommand and domain configuration file:

Listing 2. Start 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 that (qemu:///system). The local URI is connected to the system mode daemprocess of the local qemu driver. To connect to the remote qemu VM monitor through the Secure Shell (SSH) Protocol on the host shinchan, you can use URL qemu + SSH: // shinchan /.

Next, you can uselistCommand to list the active domains on a given host. In this way, you can list the active domains, domain IDs, and their statuses, 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 defined in the metadata of the domain configuration file. The domain name is 1 and running.

You can also usesuspendCommand to abort the domain. This command can stop the scheduling domain, but the domain still exists in the memory and can be quickly resumed. The following example shows how to abort a domain, view the status of the execution list, and restart the domain:

Listing 4. Abort the domain, check the status, and restart
[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 tool also supports many other commands, such as the command to save the domain (save) To restore the existing domain commands (restore), Restart the domain command (reboot) And other commands. You can also (dumpxml) Create a domain configuration file.

So far, we have started and operated on the domain. But how can we connect to the domain to view the current active domain? This can be achieved through VNC. To create a window that represents a graphic desktop in a specific domain, you can use VNC:

Listing 5. connecting to a domain
[email protected]:~/libvtest$ xvnc4viewer 127.0.0.1 0
 

Back to Top

Libvirt and Python

The previous example shows how to use the command line tool virsh to control the domain. Now let's look at an example of using python to control the domain. Python is a scripting language supported by libvirt. It provides full object-oriented interfaces to libvirt APIs.

In this example, I have studied some basic operations (list,suspend,resumeAnd so on. For the python sample script, see Listing 6. In this example, we start by importing the libvirt module. Connect to the local qemu VM monitor. From here, repeat the available domain IDs. Create a domain object for each ID, stop, continue, and delete the domain.

Listing 6. Example Python script (libvtest. py) used to control the domain)
import libvirtconn = libvirt.open(‘qemu:///system‘)for id in conn.listDomainsID():dom = conn.lookupByID(id)print "Dom %s  State %s" % ( dom.name(), dom.info()[0] )dom.suspend()print "Dom %s  State %s (after suspend)" % ( dom.name(), dom.info()[0] )dom.resume()print "Dom %s  State %s (after resume)" % ( dom.name(), dom.info()[0] )dom.destroy()

Although this is just a simple example, we can still see the powerful functions provided by libvirt through python. A simple script can repeat all local qemu domains, issue information about domains, and then control domains. The result of the script is as follows:Listing 7.

Listing 7. Python script output 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 Top

API Overview

The advanced libvirt API can be divided into five APIs: The Virtual Machine Monitoring Program connects APIs, domain APIs, network APIs, storage volume APIs, and storage pool APIs.

After a connection is created for a given VM monitor, all libvirt communication is generated (for exampleopenCall ). This connection provides a path for all other APIs to be used. InCAPI.virConnectOpenCall (and other authentication calls. The returned values of these functions arevirConnectPtrObject, which represents a connection to the VM monitoring program. As the basis of all other management functions, this object is a required statement to call concurrent APIs for a given Virtual Machine Monitoring Program. Important concurrent calls arevirConnectGetCapabilitiesAndvirNodeGetInfoThe former returns the functions of the VM Monitoring Program and the driver, and the latter obtains information about nodes. This information is returned as an XML document, so that you can understand possible behaviors through parsing.

After entering the Virtual Machine Monitoring Program, you can use a set of APIs to call functions to reuse various resources on the Virtual Machine Monitoring Program.virConnectListDomainsThe API call function returns a list of domain identifiers that represent the active domains on the VM monitor.

API implements a large number of domain-specific functions. To explore or manage a domain, you first needvirDomainPtrObject. You can obtain the handle in multiple ways (using ID, UUID, or domain name ). Let's continue to look at the example of repeated domains. You can use the index table returned by this function and callvirDomainLookupByIDTo obtain the domain handle. With this domain handle, you can perform a lot of operations from the exploration domain (virDomainGetUUID,virDomainGetInfo,virDomainGetXMLDesc,virDomainMemoryPeek) To the control domain (virDomainCreate,virDomainSuspend,virDomainResume,virDomainDestroyAndvirDomainMigrate).

You can also use APIs to manage and check virtual networks and storage resources. After creating an API model, you needvirNetworkPtrObject To manage and check the virtual network, andvirStoragePoolPtr(Storage pool) orvirStorageVolPtr(Volume) object to manage these resources.

API also supports an event mechanism that you can use to register as notified when a specific event (such as domain startup, stop, resume, or stop) occurs.

 

Back to Top

Language binding

Libvirt LibraryC(SupportedC++), Including direct support for python. However, it also supports a large number of language bindings. Currently™Language, Perl and ocaml are bound. InC#We have done a lot of work to call libvirt. Libvirt supports the most popular system programming languages (CAndC++), Multiple scripting languages, and even a unified functional language (objective caml ). Therefore, no matter which language you focus on, libvirt provides a path to help you control the domain.

 

Back to Top

Applications using libvirt

The powerful functions provided by libvirt can be seen only from a small part of the functions shown in this article. As you wish, a large number of applications are successfully built on libvirt. An interesting application is virsh (shown here), which is a virtual shell. There is also an application named virt-install that can be used to supply new domains from multiple operating system releases. Virt-clone can be used to copy a VM from another VM (including operating system replication and disk replication ). Some advanced applications include the multi-purpose desktop management tool virt-manager and the lightweight tool virt-Viewer that securely connects to the VM graphics console.

The most important tool built on libvirt isOvirt. The ovirt VM management application is designed to manage a large number of VMS on a single Vm or multiple hosts on a single node. In addition to simplifying the management of a large number of hosts and VMS, it can also be used for cross-platform and architecture automation clusters, load balancing and work.

In-depth exploration

From this short article, we can see that libvirt is a powerful library used to build applications. It can manage domains across system large networks in different Virtual Machine Monitoring Program environments. In view of the increasing popularity of cloud computing, libvirt will undoubtedly develop along with it and continue to acquire new applications and users. At the time of writing this article, libvirt has only four years of history, so it is relatively new in the field of large-scale Scalable Computing. Libvirt will certainly develop greatly in the future.

 

From: http://www.ibm.com/developerworks/cn/linux/l-libvirt/

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.