libvirt 简介

libvirt 是目前使用最为广泛的对 KVM 虚拟机进行管理的工具和应用程序接口。
 
 
libvirt 可以支持多种 hypervisor,包括 Xen,Hyper-V 和 qemu-kvm 等。同时,相比于使用 qemu 通过参数指定创建虚拟机的方式,使用 libvirt 创建虚机更简单,更人性化。
 
libvirt 由应用程序接口 API,守护进程 libvirtd 和 libvirt 的默认管理工具组成。其中,libvirt API 为其它虚机管理工具,如 virsh,virt-manager 等提供应用程序接口,使得这些虚机管理工具通过调用 libvirt 的 API 实现虚机的管理。libvirtd 负责执行节点上虚机(domain) 的管理工作。libvirt 的默认管理工具是 virsh,它是 libvirt 默认提供的虚机的管理工具,通过 virsh 提供的一系列接口命令可以实现对虚机的管理。

libvirt XML 配置

libvirt 有几个重要概念,分别是:
  • 节点 Node:节点是虚机(domain) 运行的物理机器,hypervisor 也运行在节点之上。
  • domain: 虚机在 libvirt 中表示为 domain(域),一个 domain 就是一个虚机。
  • hypervisor:虚拟机监控器,KVM 就是使用硬件辅助的全虚拟化方案的 hepervisor。
 
在 libvirt 中创建 domain 的流程是:通过 XML 文件定义好 domain 的配置,然后,virsh 根据定义好的 XML 文件创建指定 domain。
 
domain XML 配置
disk(磁盘)
任何磁盘设备,包括软盘(floppy)、硬盘(hard disk)、光驱(cdrom)或者半虚拟化驱动都使用 <disk> 元素来定义。方式:
<disk type='**' device='**'>。其中:
type 用来指定device source 的类型:"file", "block", "dir", "network", 或者 "volume"。具体的 source 由 <source> 标签定义。
device 用来指定 device target 的类型:"floppy", "disk", "cdrom", and "lun", 默认为 "disk" 。具体的 target 由 <target> 标签定义。
(1)volume 类型的 disk
<disk type='volume' device='disk'>
<driver name='qemu' type='raw'/>
<source pool='blk-pool0' volume='blk-pool0-vol0'/>
<target dev='hdk' bus='ide'/>
</disk>
(2)file 类型的 disk
<disk type='file' snapshot='external'>
<driver name="tap" type="aio" cache="default"/>
<source file='/var/lib/xen/images/fv0' startupPolicy='optional' />
<target dev='hda' bus='ide'/>
</disk>
(3)block 类型的 disk
<disk type='block' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='hdd' bus='ide' tray='open'/>
<readonly/>
</disk>
(4)network 类型的 disk
<disk type='network' device='cdrom'>
<driver name='qemu' type='raw'/>
<source protocol="http" name="url_path">
<host name="hostname" port="80"/>
</source>
<target dev='hde' bus='ide' tray='open'/>
<readonly/>
</disk>
Host device assignment (主机设备分配)
<hostdev mode='subsystem' type='usb'> #USB 设备直接分配
<source startupPolicy='optional'>
<vendor id='0x1234'/>
<product id='0xbeef'/>
</source>
<boot order='2'/>
</hostdev>
<hostdev mode='subsystem' type='pci' managed='yes'> #PCI 设备直接分配
<source>
<address domain='0x0000' bus='0x06' slot='0x02' function='0x0'/>
</source>
<boot order='1'/>
<rom bar='on' file='/etc/fake/boot.bin'/>
</hostdev>
Network interface (网卡)
有几种 interface 类型:
(1)type = ‘network’ 定义一个连接 Virtual network 的 interface
<devices>
<interface type='network'>
<source network='default'/> #虚拟网络的名称为 ‘default’
</interface>
...
<interface type='network'>
<source network='default' portgroup='engineering'/>
<target dev='vnet7'/>
<mac address="00:11:22:33:44:55"/>
<virtualport>
<parameters instanceid='09b11c53-8b5c-4eeb-8f00-d84eaa0aaa4f'/>
</virtualport> </interface>
</devices>
(2)type=‘birdge’ 定义一个 Bridge to LAN(桥接到物理网络)的interface:前提是主机上存在一个 bridge,该 bridge 已经连到物理 LAN
<interface type='bridge'> #连接到 br0
<source bridge='br0'/>
</interface>
<interface type='bridge'> #连接到br1
<source bridge='br1'/>
<target dev='vnet7'/>
<mac address="00:11:22:33:44:55"/>
</interface>
<interface type='bridge'> #连接到 Open vSwith bridge ovsbr
<source bridge='ovsbr'/>
<virtualport type='openvswitch'>
<parameters profileid='menial' interfaceid='09b11c53-8b5c-4eeb-8f00-d84eaa0aaa4f'/>
</virtualport>
</interface>
(3)type=‘ethernet’定义一个使用指定脚本连接到 LAN 的 interface
<devices>
<interface type='ethernet'>
<target dev='vnet7'/>
<script path='/etc/qemu-ifup-mynet'/>
</interface>
</devices>
(4)type=‘direct’ 定义一个直接连到物理网卡(Direct attachment to physical interface)的 interface:需要 Linux macvtap 驱动支持
<interface type='direct' trustGuestRxFilters='no'>
<source dev='eth0' mode='vepa'/>
</interface>
(5)type=‘hostdev’ 定义一个由主机 PCI 网卡直接分配(PCI Passthrough)的 interface:分配主机上的网卡给虚机
<devices>
<interface type='hostdev' managed='yes'>
<driver name='vfio'/>
<source>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</source>
<mac address='52:54:00:6d:90:02'/>
<virtualport type='802.1Qbh'>
<parameters profileid='finance'/>
</virtualport>
</interface>
</devices>
metadata(元数据)
domain 的 metadata,用来表示 domain 的属性,从而区别于其它 domain:
<metadata>
<nova:instance xmlns:nova="http://openstack.org/xmlns/libvirt/nova/1.0">
<nova:package version="20.0.2-0.20191230035951.27bfd0b.el8ost"/>
<nova:name>ROBOT-TEST-IPV6-01-OAM-001</nova:name>
<nova:creationTime>2020-07-11 17:55:37</nova:creationTime>
<nova:flavor name="demo">
<nova:memory>32768</nova:memory>
<nova:disk>40</nova:disk>
<nova:swap>0</nova:swap>
<nova:ephemeral>0</nova:ephemeral>
<nova:vcpus>6</nova:vcpus>
</nova:flavor>
<nova:owner>
<nova:user uuid="a511dc27164a476c9b28c4323d76b3a9">admin</nova:user>
<nova:project uuid="8acc3ea78b924f77a30d0389303f6818">admin</nova:project>
</nova:owner>
<nova:root type="image" uuid="2d3a3798-208b-4a61-b2fa-171be08a7a3b"/>
</nova:instance>
</metadata>
CPU
CPU 配置,cpu mode 有三种模式:host-model,custom 和 host-passthrough。
host-model:根据 node 上的 CPU 特性,在 domain 上选择一个最接近的标准 CPU 型号,如果不指定 cpu mode,默认就是 host-model。
custom: 表示基于某个基础 CPU 做个性化定制。
host-passthrough:直接将物理 CPU 特性暴露给虚拟机使用。
<cpu mode='host-model' check='partial'>
<model fallback='allow'/>
<topology sockets='3' cores='1' threads='2'/> # CPU topology,socker,core and threads
<numa>
<cell id='0' cpus='0-5' memory='33554432' unit='KiB' memAccess='shared'/>
</numa>
</cpu>
cputune
cputune 标签可对 cpu 进行更多调节:
<cputune>
<shares>6144</shares>
<vcpupin vcpu='0' cpuset='11'/>
<vcpupin vcpu='1' cpuset='39'/>
<vcpupin vcpu='2' cpuset='41'/>
<vcpupin vcpu='3' cpuset='13'/>
<vcpupin vcpu='4' cpuset='34'/>
<vcpupin vcpu='5' cpuset='6'/>
<emulatorpin cpuset='6,11,13,34,39,41'/> # vcpu 和 物理 cpu 的映射
</cputune>
os
os 标签定义操作系统架构,hvm 表示硬件辅助的虚拟机
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.6.0'>hvm</type>
<boot dev='hd'/>
<smbios mode='sysinfo'/>
</os>
更多关于 libvirt XML 的配置可看这里

virsh 创建 domain

virsh 是用于管理 domain 和 hypervisor 的命令行工具。
virsh 通过一系列命令实现对 domain 的管理和监控,宿主机和 hypersivor 的管理,网络/存储池和存储卷的管理等等。可通过 help 命令查看详细的解释。
 
通用的 virsh 命令主要有:
virsh list 查看本地 node 的 domain:
[root@compute-1 admin]# virsh list
Id Name State
----------------------------------------------------
1331 instance-00001c38 running
 
virsh dominfo 查看本地 node 的 domain 信息:
[root@compute-1 admin]# virsh dominfo 1331
Id: 1331
Name: instance-00001c38
UUID: 50df6317-642b-48f3-9a53-fbc3c440f56f
OS Type: hvm
State: running
CPU(s): 6
CPU time: 73898.6s
Max memory: 33554432 KiB
Used memory: 33554432 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0
 
virsh vcpuinfo 查看本地 node 上 domain 的 vcpu 信息:
[root@compute-1 admin]# virsh vcpuinfo 1331
VCPU: 0
CPU: 11
State: running
CPU time: 12235.4s
CPU Affinity: -----------y-------------------------------------------- VCPU: 1
CPU: 39
State: running
CPU time: 12081.0s
CPU Affinity: ---------------------------------------y----------------
 
virsh edit <domainID> 查看并且编辑 domain 的 XML 信息:
[root@compute-1 admin]# virsh edit 1331
 
virsh define 根据 XML 文件定义一个 domain,但是不启动 domain:
[root@compute-1 admin]# virsh edit demo.xml
 
virsh create 根据 XML 文件创建一个 domain:
[root@compute-1 admin]# virsh create demo.xml

libvirt API

libvirt API 是一套 C 语言实现的管理虚拟机的,稳定高效的程序接口。virsh 就是通过 libvirt API 来实现 domain 的管理监控等操作的。
libvirt API 分为对本地的 API 调用和对远端 node 的 API 调用。管理工具通过本地 URI 和 远程 URI 实现这些调用。
本地 URI 的格式是:qemu:///session,连接本地 session 实例,该连接仅能管理当前用户的 domain 资源;qemu:///system,连接本地 system 实例,管理特权用户的 domain 资源。远程 URI 的格式是: qemu+ssh:///root@example.com/system 和 qemu+ssh:///user@example.com/session 等。
[admin@compute-1 ~]$ virsh -c qemu:///system list
error: failed to connect to the hypervisor
error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': Permission denied
[admin@compute-1 ~]$ virsh -c qemu:///session list
Id Name State
---------------------------------------------------- [admin@compute-1 ~]$ sudo su
[root@compute-1 admin]# virsh -c qemu:///system list
Id Name State
----------------------------------------------------
1331 instance-00001c38 running
1332 instance-00001c3a running [root@compute-1 admin]# virsh list # virsh list 默认 list 当前用户的 domain
Id Name State
----------------------------------------------------
1331 instance-00001c38 running
1332 instance-00001c3a running
 
libvirt API 提供了很多接口供管理工具调用。许多语言都提供了对 libvirt 的绑定,python 也实现了对 libvirt 的绑定,用户可通过导入 libvirt 包实现对 libvirt API 的调用(需要事先安装好 libvirt-python 软件包),安装好之后代码可在 /usr/lib64/python2.7/site-packages/libvirt* 找到:
[lianhua@controller-0 ~]$ ll /usr/lib64/python2.7/site-packages/libvirt
libvirt_lxc.py libvirt_lxc.pyo libvirtmod_qemu.so libvirt.py libvirt.pyo libvirt_qemu.pyc
libvirt_lxc.pyc libvirtmod_lxc.so libvirtmod.so libvirt.pyc libvirt_qemu.py libvirt_qemu.pyo
 
通过一个简单的 python 代码实现对 libvirt API 的调用:
#!/usr/bin/python
# Get domain info via libvirt python API
# Test env: python2.7 and libvirt-python-2.0.0 on KVM host import libvirt
import sys def createConnection():
conn = libvirt.openReadOnly(None)
if conn == None:
print "Failed to open connection to QEMU/KVM"
sys.exit(1)
else:
print "connection successfully"
return conn def getDomInfoByName(conn, name):
try:
localDom = conn.lookupByName(name)
except:
print 'Failed to get the domain info with name "%s"' % name
return 1 print "domain id: %d name: %s " % (localDom.ID(), localDom.name())
print "domain state: %s " % (localDom.state(0))
print "domain info: %s " % (localDom.info())
print "vCPUS: %d " % localDom.maxVcpus() def getDomInfoByID(conn, id):
try:
localDom = conn.lookupByID(id)
except:
print 'Failed to get the domain info with id "%d"' % id
return 1 print "lookup domain id: %d, name: %s" % (localDom.ID(), localDom.name()) def closeConnection(conn):
print "close connection session"
try:
conn.close()
except:
print "Failed to close the session"
return 1 if __name__ == '__main__':
name1 = "instance-00002d2e"
name2 = "notExist"
id1 = 321
id2 = 999
conn = createConnection()
getDomInfoByName(conn, name1)
getDomInfoByName(conn, name2)
getDomInfoByID(conn, id1)
getDomInfoByID(conn, id2)
closeConnection(conn)
 
查看执行结果,检查是否调用到 libvirt API:
[root@compute-1 qemu-kvm]# virsh list
Id Name State
----------------------------------------------------
321 instance-00002d2e running [root@compute-1 qemu-kvm]# python libvirt-test.py
connection successfully
domain id: 321 name: instance-00002d2e
domain state: [1, 5]
domain info: [1, 33554432L, 33554432L, 3, 1546499830000000L]
vCPUS: 3
libvirt: QEMU Driver error : Domain not found: no domain with matching name 'notExist'
Failed to get the domain info with name "notExist"
lookup domain id: 321, name: instance-00002d2e
libvirt: QEMU Driver error : Domain not found: no domain with matching id 999
Failed to get the domain info with id "999"
close connection session
 
 

KVM 管理工具:libvirt的更多相关文章

  1. KVM管理工具 WebVirtMgr

    WEB管理工具 WebVirtMgr WebVirtMgr是一个基于libvirt的Web界面,用于管理虚拟机.它允许您创建和配置新域,并调整域的资源分配.VNC查看器为来宾域提供完整的图形控制台.K ...

  2. 虚拟化技术之kvm管理工具virsh常用基础命令(一)

    在上一篇博客中,我们了解了KVM基础架构和部署以及图形管理工具virt-manager安装虚拟机的过程,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13499 ...

  3. KVM管理工具webvirtmgr的使用

    WebVirtMgr的日常配置:添加宿主机,创建虚拟机,磁盘扩容,快照等具体操作记录如下: 一.创建虚拟机 1.创建存储池 点击创建的宿主机,进入虚拟机部署界面 点击“存储池”按钮,创建存储池(即创建 ...

  4. 虚拟化技术之kvm管理工具virsh常用基础命令(二)

    上一篇博客我们主要聊了下virsh 管理kvm虚拟机的命令相关用法和说明,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13508231.html:今天我们来继 ...

  5. kvm管理工具Webvirtmgr安装

    虚拟机版本vmware workstation 15.5.0 pro   (也就是linux版) cat /etc/redhat-release CentOS Linux release 7.4.17 ...

  6. Mac安装Linux的KVM管理工具virt-manager

    安装: brew tap jeffreywildman/homebrew-virt-manager brew install virt-manager virt-viewer 中途会碰到很多问题,可以 ...

  7. KVM管理工具

    Ovirt:功能强大,RHEV的开源版本 WebVirtMgr:virt-manager的WEB模式的替代品 ConVirt:分为开源版.商业版 Openstack:开源框架,复杂程度较高

  8. 虚拟化技术之kvm WEB管理工具kimchi

    在前面的博客中,我们介绍了kvm的各种工具,有基于图形管理的virt-manager.有基于命令行管理的virt-install .qemu-kvm.virsh等等:今天我们来介绍一款基于web界面的 ...

  9. virsh命令行管理工具

    virsh命令行管理工具 Libvirt有两种控制方式,命令行和图形界面 图形界面: 通过执行名virt-manager,启动libvirt的图形界面,在图形界面下可以一步一步的创建虚拟机,管理虚拟机 ...

  10. 基于KVM、Xen、OpenVZ等虚拟化技术的WEB在线管理工具

    1.Proxmox proxmox是一个开源的虚拟化管理平台,支持集群管理和HA.在存储方面,proxmox除了支持常用的lvm,nfs,iscsi,还支持集群存储glusterfs和ceph,这也是 ...

随机推荐

  1. 华为防火墙1day?

    背景信息 缺省情况下,FW通过8887端口提供内置的本地Portal认证页面,用户可以主动访问或HTTP重定向至认证页面(https://接口IP地址:8887)进行本地Portal认证. 当企业部署 ...

  2. iMessage群发,iMessage群发基础知识,iMessage群发源代码分享

    在当今的数字化时代,即时通讯已经成为我们日常生活和工作中不可或缺的一部分,其中,苹果的iMessage服务因其出色的用户体验和无缝的设备间同步而备受用户喜爱. 然而,你是否想过如何利用iMessage ...

  3. 使用cgroup控制内存

    关键文件 memory.limit_in_bytes memory.soft_limit_in_bytes memory.memsw.limit_in_bytes tasks cgroup.procs ...

  4. OpenSSL命令总结

    OpenSSL命令总结 疑今者察之古,不知来者视之往. 导航 介绍 对称加密 公钥加密 信息摘要 数字证书 杂项 介绍 密码学标准和互联网协议一样,是一种大家都遵守的约定和标准,比如PKCS#1中规定 ...

  5. CSS3学习笔记引言

    开始我们要来介绍css: CSS(全称为Cascading Style Sheets)是一种用于描述HTML.XML等文档样式的样式语言,它能够定义元素的显示方式,如字体.颜色.布局等. CSS可以把 ...

  6. vue图片由小放大

    原生效果 <template> <div> <transition> <img :src="imageUrl" class="i ...

  7. JavaScript异步编程1——Promise的初步使用

    目录 1. 概述 2. 详论 3. 参考 1. 概述 Promise对象是ES6提出的的异步编程的规范.说到异步编程,就不得不说说同步和异步这两个概念. 从字面意思理解同步编程的话,似乎指的是两个任务 ...

  8. C# / VB.NET 获取PDF文档的数字签名信息

    文档中的数字签名具有不可否认性,可有效防伪防篡改.对文档中已有的数字签名信息,可通过一定方法获取,下面通过程序代码介绍如何来实现.程序中,使用了Spire.PDF.dll,版本:6.11.6,可自行在 ...

  9. 从java到JavaScript(2):对比Java/Go/Swift/Rust看Dart

    Dart与Java的一些直观区别 Dart和java以及C#都差不多,基本上不用学习可以直接使用,从这里可以你可以了解Dart有些特别之处.其实对于Java开发人员来说Dart,还是相对好理解的 基本 ...

  10. Axure 页面交互