初识libvirt

想要学习libvirt,首先要学习如何使用libvirt。具体的使用方式在官网可以查询到完整的资料,因此本文不会对此进行详细的讲解,本文目的是让读者快速地、低成本地学会libvirt的基本使用并学会如何查询相应资料。

1、libvirt是什么

官方文档上对libvirt的描述:

The libvirt project:

wikipedia对libvirt的描述:

libvirt is a C library with bindings in other languages, notably in Python,Perl, OCaml,Ruby,Java,JavaScript (via Node.js)and PHP.libvirt for these programming languages is composed of wrappers around another class/package called libvirtmod. libvirtmod’s implementation is closely associated with its counterpart in C/C++ in syntax and functionality.

libvirt在虚拟化中的位置:

libvirt

简而言之,libvirt就是一个中间件,屏蔽了底层不同hypervisor的区别,向上层提供统一的管理接口。

2、第一件事

安装好libvirt之后,想要使用它,要做的第一件事是什么?

​ 如果你曾接触过libvirt,你也许会认为要使用libvirt首先要开启libvirtd服务。但事实上这不是必要的。要使用libvirt首先要做的第一件事是连接(Connection)一个hypervisor

2.1、virsh CLI

​ virsh是libvirt项目中内建的一个CLI(command line interface)工具,通过它可以调用libvirt的API,它是最纯净的一个基于libvirt的虚拟资源管理器,它的命令和libvirtAPI基本上是一一对应的,我推荐使用它来学习研究libvirt。

使用virsh调用libvirtAPI:

1
2
3
4
5
6
7
8
9
# virsh
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # uri
error: failed to connect to the hypervisor
error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory

​ 若未开启libvirtd服务,会报一个failed to connect to the hypervisor的错误,在本文后续章节中将会进行解释。下面开启libvirtd再进入virsh。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# systemctl start libvirtd
# virsh
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # uri
qemu:///system

virsh # version
Compiled against library: libvirt 4.5.0
Using library: libvirt 4.5.0
Using API: QEMU 4.5.0
Running hypervisor: QEMU 2.11.2

virsh # 

2.2、URI

​ virsh终端一进去就会自动连接到qemu:///system,这是由于在libvirt中将这个uri设置成了默认的连接(connection)。我们可以在/etc/libvirt/libvirt.conf中修改默认的连接。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# cat /etc/libvirt/libvirt.conf
#
# This can be used to setup URI aliases for frequently
# used connection URIs. Aliases may contain only the
# characters  a-Z, 0-9, _, -.
#
# Following the '=' may be any valid libvirt connection
# URI, including arbitrary parameters

#uri_aliases = [
#  "hail=qemu+ssh://root@hail.cloud.example.com/system",
#  "sleet=qemu+ssh://root@sleet.cloud.example.com/system",
#]

#
# These can be used in cases when no URI is supplied by the application
# (@uri_default also prevents probing of the hypervisor driver).
#
#uri_default = "qemu:///system"

​ 在libvirt中uri的意义是什么。uri指定了libvirt想要连接的hypervisor具体位置及类型信息。结构及意义如下:

1
driver[+transport]://[username@][hostname][:port]/[path][?extraparameters]
Component Description
driver The name of the libvirt hypervisor driver to connect to. This is the same as that used in a local URI. Some examples are xen, qemu, lxc, openvz, and test. As a special case, the psuedo driver name remote can be used, which will cause the remote daemon to probe for an active hypervisor and pick one to use. As a general rule if the application knows what hypervisor it wants, it should always specify the explicit driver name and not rely on automatic probing.
transport The name of one of the data transports described earlier in this section. Possible values include tls, tcp, unix, sshand ext. If omitted, it will default to tls if a hostname is provided, or unix if no hostname is provided.
username When using the SSH data transport this allows choice of a username that differs from the client’s current login name.
hostname The fully qualified hostname of the remote machine. If using TLS with x509 certificates, or SASL with the GSSAPI/Keberos plug-in, it is critical that this hostname match the hostname used in the server’s x509 certificates / Kerberos principle. Mis-matched hostnames will guarantee authentication failures.
port Rarely needed, unless SSH or libvirtd has been configured to run on a non-standard TCP port. Defaults to 22 for the SSH data transport, 16509 for the TCP data transport and16514 for the TLS data transport.
path The path should be the same path used for the hypervisor driver’s local URIs. For Xen, this is always just /, while for QEMU this would be /system.
extraparameters The URI query parameters provide the mean to fine tune some aspects of the remote connection, and are discussed in depth in the next section.

例如uri:qemu+tcp://127.0.0.1/system,即代表通过tcp连接本地libvirtd。更多相关的信息可以参考官方文档

2.3 、连接一个hypervisor

​ 现在我们可以来连接一个hypervisor,同时来解释为什么libvirtd服务不是必要的,同样,不开启libvirtd,进入virsh终端:

1
2
3
4
5
6
virsh # connect test:///default

virsh # uri
test:///default

virsh #

​ test是一个测试用的hypervisor,并没有实际的作用,只是用来测试接口。为什么可以不开启libvirtd守护进程,就涉及到libvirt的两种HypervisorDriver。 libvirt连接方式

​ 只有当需要连接到远程(remote)的hypervisor才需要连接libvirtd,而部分本地的Driver是不需要远程连接的(上图有部分错误,qemu是必须远程连接的)。详情见代码。

2.4、libvirt的结构

​ libvirt是标准的linux系统编程,它主要是使用C语言编写成的,是模块化编程。以简单的vmware为例: ​ TODO

3、配置文件与日志

​ 配置文件与日志主要影响libvirtd,这些文件和日志都是在libvirtd初始化的时候被获取的。 TODO