Libvirt virtualization Database Analysis

Source: Internet
Author: User
Tags ovirt

The libvirt library is a Linux API that implements Linux virtualization. It supports various virtual machine monitoring functions.ProgramIncluding xen and KVM, as well as qemu and some virtual products for other operating systems. This article mainly discusses libvirt, its usage and architecture.

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 uses a mechanism specific to the Virtual Machine monitor program and

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. As new Virtual Machine Monitoring programs become open-sourceCommunityOther 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)

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 ). TheCodeSpecifies all options required for the definition domain-from the Virtual Machine Monitoring Program (simulator) to the resources used by the domain and peripheral configurations (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 allocate universallyunique 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>
<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


Mtj @ mtj-desktop :~ /Libvtest $Virsh create react-qemu.xml
Connecting to Uri: qemu: // System
Domain reactos-on-qemu created from react-qemu.xml

 

Mtj @ mtj-desktop :~ /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


Mtj @ mtj-desktop :~ /Libvtest $Virsh list
Connecting to Uri: qemu: // System
ID name state
----------------------------------
1 reactos-on-qemu running

 

Mtj @ mtj-desktop :~ /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


Mtj @ mtj-desktop :~ /Libvtest $Virsh suspend 1
Connecting to Uri: qemu: // System
Domain 1 suincluded

 

Mtj @ mtj-desktop :~ /Libvtest $Virsh list
Connecting to Uri: qemu: // System
ID name state
----------------------------------
1 reactos-on-qemu paused

Mtj @ mtj-desktop :~ /Libvtest $Virsh resume 1
Connecting to Uri: qemu: // System
Domain 1 resumed

Mtj @ mtj-desktop :~ /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


Mtj @ mtj-desktop :~ /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 libvirt

 

Conn = 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 script result is shown in listing 7.


Listing 7. Python script output in Listing 6


Mtj @ mtj-desktop :~ /Libvtest $Python libvtest. py
Dom reactos-on-qemu state 1
Dom reactos-on-qemu state 3 (after suspend)
Dom reactos-on-qemu state 1 (after resume)
Mtj @ mtj-desktop :~ /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, you have bound Ruby, Java, Perl, and ocaml. InC #We have done a lot of work to call libvirt. Libvirt supports the most popular systemsProgramming Language(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 shortArticleIt can be seen 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.

Check the libvirt web site to learn the latest libvirt information and download the latest version. You will also find the full api reference manual, which details the core interface and error handling interface.

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.