标签:
学习 KVM 的系列文章:
为什么需要Libvirt?
Libvirt提供了什么?
目前,libvirt 已经成为使用最为广泛的对各种虚拟机进行管理的工具和应用程序接口(API),而且一些常用的虚拟机管理工具(如virsh、virt-install、virt-manager等)和云计算框架平台(如OpenStack、OpenNebula、Eucalyptus等)都在底层使用libvirt的应用程序接口。
(SLE 11)
对象 | 解释 |
Domain (域) | 指运行在由Hypervisor提供的虚拟机器上的一个操作系统实例(常常是指一个虚拟机)或者用来启动虚机的配置。 |
Hypervisor | 一个虚拟化主机的软件层 |
Node (主机) | 一台物理服务器。 |
Storage pool (存储池) | 一组存储媒介的集合,比如物理硬盘驱动器。一个存储池被划分为小的容器称作卷。卷会被分给一个或者多个虚机。 |
Volume (卷) | 一个从存储池分配的存储空间。一个卷会被分给一个或者多个域,常常成为域里的虚拟硬盘。 |
对象名称 | 对象 | Python 类 | 描述 |
Connect | 与 Hypervisor的连接 |
virConnectPtr
|
在调用任何 API 去管理一个本地或者远端的Hypervisor前,必须建立和这个Hypervisor的连接。 |
Domain | Guest domain |
virDomainPtr
|
用于列举和管理已有的虚机,或者创建新的虚机。唯一标识:ID,Name,UUID。一个域可能是暂时性的或者持久性的。暂时性的域只能在它运行期间被管理。持久性的域在主机上保存了它的配置。 |
Virtual Network | 虚拟网络 |
virNetworkPtr
|
用于管理虚机的网络设备。唯一标识:Name,UUID。一个虚拟网络可能是暂时性的或者持久性的。每个主机上安装libvirt后,它都有一个默认的网络设备“default”。它向该主机上运行的虚机提供DHCP服务,以及通过NAT连接到主机上。 |
Storage Pool | 存储池 |
virStoragePoolPtr
|
用于管理虚拟机内的所有存储,包括 local disk, logical volume group, iSCSI target, FibreChannel HBA and local/network file system。唯一标识:Name,UUID。一个存储池可能是暂时性的或者持久性的。Pool 的 type 可以是 dir , fs , netfs , disk , iscsi , logical , scsi ,mpath , rbd , sheepdog , gluster 或者 zfs。 |
Storage Volume | 存储卷 |
virStorageVolPtr
|
用于管理一个存储池内的存储块,包括一个池内分配的块、磁盘分区、逻辑卷、SCSI/iSCSI Lun,或者一个本地或者网络文件系统内的文件等。唯一标识:Name,Key,Path。 |
Host device | 主机设备 |
virNodeDevPtr
|
用于管理主机上的物理硬件设备,包括 the physical USB or PCI devices and logical devices these provide, such as a NIC, disk, disk
controller, sound card, etc。唯一标识:Name。
|
Libvirt API 就是对各种对象的各种操作,包括基本的增、删、改、查操作和其它操作。
对象 | 增 | 删 | 改 | 查 | 其它 |
Connect | virConnectOpen virConnectOpenAuth virConnectOpenReadOnly |
virConnectClose | virConnectSetKeepAlive | ||
Strorage pool | virStoragePoolBuild virStoragePoolCreate virStoragePoolCreateXML virStoragePoolDefineXML |
virStoragePoolDelete virStoragePoolDestroy virStoragePoolFree virStoragePoolUndefine |
virStoragePoolRefresh virStoragePoolSetAutostart |
virConnectFindStoragePoolSources virConnectListAllStoragePools virConnectListDefinedStoragePools virConnectListStoragePools virConnectNumOfDefinedStoragePools virConnectNumOfStoragePools virStoragePoolGetInfo/Name/UUID/UUIDString/XMLDesc virStoragePoolIsActive/Persistent virStoragePoolLookupByName/UUID/UUIDString/Volume virStoragePoolRef |
virStoragePoolGetAutostart virStoragePoolGetConnect virStoragePoolNumOfVolumes virStoragePoolListAllVolumes
virStoragePoolListVolumes
|
Storage volume | virStorageVolCreateXML virStorageVolCreateXMLFrom |
virStorageVolDelete virStorageVolFree |
virStorageVolResize virStorageVolUpload virStorageVolWipe virStorageVolWipePattern |
virStorageVolGetConnect/Info/Key/Name/Path/XMLDesc virStorageVolLookupByKey/Name/Path virStorageVolRef |
virStorageVolDownload |
Network | virNetworkCreate virNetworkCreateXML virNetworkDefineXML |
virNetworkDestroy virNetworkFree virNetworkUndefine |
virNetworkSetAutostart virNetworkUpdate |
virConnectListAllNetworks virConnectListDefinedNetworks virConnectListNetworks virConnectNumOfDefinedNetworks virConnectNumOfNetworks virNetworkGetBridgeName/DHCPLeases/Name/UUID/UUIDString/XMLDesc virNetworkIsActive/Persistent virNetworkLookupByName/UUID/UUIDString virNetworkGetAutostart virNetworkGetConnect
|
virConnectNetworkEventDeregisterAny virConnectNetworkEventGenericCallback virNetworkDHCPLeaseFree |
Domain snapshot | virDomainSnapshotCreateXML | virDomainSnapshotDelete virDomainSnapshotFree |
virDomainRevertToSnapshot | virDomainHasCurrentSnapshot virDomainListAllSnapshots virDomainSnapshotCurrent virDomainSnapshotGetConnect/Domain/Name/Parent/XMLDesc virDomainSnapshotHasMetadata virDomainSnapshotIsCurrent virDomainSnapshotListAllChildren virDomainSnapshotListChildrenNames virDomainSnapshotListNames virDomainSnapshotLookupByName virDomainSnapshotNum virDomainSnapshotNumChildren virDomainSnapshotRef |
|
Host | virInitialize virNodeSetMemoryParameters virNodeSuspendForDuration |
virConnectBaselineCPU virConnectCompareCPU virConnectGetCPUModelNames/Capabilities/Hostname/LibVersion/MaxVcpus/Sysinfo/Type/URI/Version virConnectIsAlive virConnectIsEncrypted virConnectIsSecure virGetVersion virNodeGetCPUMap/CPUStats /CellsFreeMemory/FreeMemory/Info/MemoryParameters/MemoryStats / virNodeGetSecurityModel |
virTypedParamsAddBoolean virTypedParamsAddDouble virTypedParamsAddFromString virTypedParamsAddInt virTypedParamsAddLLong virTypedParamsAddString virTypedParamsAddUInt virTypedParamsAddULLong virTypedParamsClear virTypedParamsFree virTypedParamsGet |
||
Interface | virInterfaceCreate virInterfaceDefineXML |
virInterfaceDestroy virInterfaceFree virInterfaceUndefine |
virInterfaceChangeBegin virInterfaceChangeCommit virInterfaceChangeRollback |
virConnectListAllInterfaces virConnectListDefinedInterfaces virConnectListInterfaces virConnectNumOfDefinedInterfaces virConnectNumOfInterfaces virInterfaceGetConnect virInterfaceGetMACString virInterfaceGetName virInterfaceGetXMLDesc virInterfaceIsActive virInterfaceLookupByMACString virInterfaceLookupByName virInterfaceRef |
|
Net Filter | virNWFilterDefineXML |
virNWFilterFree virNWFilterUndefine |
virConnectListAllNWFilters virConnectListNWFilters virConnectNumOfNWFilters virNWFilterGetName virNWFilterGetUUID virNWFilterGetUUIDString virNWFilterGetXMLDesc virNWFilterLookupByName virNWFilterLookupByUUID virNWFilterLookupByUUIDString virNWFilterRef |
||
Domain Event | virConnectDomainEventDeregister virConnectDomainEventDeregisterAny virConnectDomainEventDeviceAddedCallback virConnectDomainEventDeviceRemovedCallback |
virConnectDomainEventAgentLifecycleCallback virConnectDomainEventBalloonChangeCallback virConnectDomainEventBlockJobCallback virConnectDomainEventCallback virConnectDomainEventDiskChangeCallback virConnectDomainEventGenericCallback virConnectDomainEventGraphicsCallback virConnectDomainEventIOErrorCallback virConnectDomainEventIOErrorReasonCallback virConnectDomainEventPMSuspendCallback virConnectDomainEventPMSuspendDiskCallback |
|||
Domain | virDomainCreate virDomainCreateLinux virDomainCreateWithFiles virDomainCreateWithFlags virDomainCreateXML virDomainCreateXMLWithFiles virDomainDefineXML virDomainDefineXMLFlags |
virDomainDestroy virDomainDestroyFlags virDomainFree virDomainUndefine virDomainUndefineFlags virDomainUpdateDeviceFlags |
virDomainAbortJob virDomainAddIOThread virDomainDelIOThread
virDomainAttachDevicevirDomainAttachDeviceFlags virDomainDetachDevice virDomainDetachDeviceFlags virDomainBlockCommit virDomainBlockCopy virDomainBlockJobAbort virDomainBlockJobSetSpeed virDomainBlockPeek virDomainBlockPull virDomainBlockRebase virDomainBlockResize virDomainCoreDump virDomainCoreDumpWithFormat virDomainFSFreeze virDomainFSInfoFree virDomainFSThaw virDomainFSTrim virDomainInjectNMI virDomainInterfaceFree virDomainManagedSave virDomainManagedSaveRemove virDomainMigrate virDomainMigrate2 virDomainMigrate3 virDomainMigrateSetCompressionCache virDomainMigrateSetMaxDowntime virDomainMigrateSetMaxSpeed virDomainMigrateToURI virDomainMigrateToURI2 virDomainMigrateToURI3 virDomainOpenChannel virDomainOpenConsole virDomainOpenGraphics virDomainOpenGraphicsFD virDomainPMSuspendForDuration virDomainPMWakeup virDomainReboot virDomainReset virDomainRestore virDomainRestoreFlags virDomainResume virDomainSave virDomainSaveFlags virDomainSaveImageDefineXML virDomainScreenshot virDomainSendKey virDomainSendProcessSignal virDomainShutdown
virDomainSetAutostartvirDomainShutdownFlags virDomainSuspend virDomainSetBlkioParameters virDomainSetBlockIoTune virDomainSetInterfaceParameters virDomainSetMaxMemory virDomainSetMemory virDomainSetMemoryFlags virDomainSetMemoryParameters virDomainSetMemoryStatsPeriod virDomainSetMetadata virDomainSetNumaParameters virDomainSetSchedulerParameters virDomainSetSchedulerParametersFlags virDomainSetTime virDomainSetUserPassword virDomainSetVcpus virDomainSetVcpusFlags |
virConnectGetAllDomainStats virConnectGetDomainCapabilities virConnectListAllDomains virConnectListDefinedDomains virConnectListDomains virConnectNumOfDefinedDomains virConnectNumOfDomains virDomainBlockStats virDomainBlockStatsFlags virDomainGetAutostart virDomainGetBlkioParameters virDomainGetBlockInfo virDomainGetBlockIoTune virDomainGetBlockJobInfo virDomainGetCPUStats virDomainGetConnect virDomainGetControlInfo virDomainGetDiskErrors virDomainGetEmulatorPinInfo virDomainGetFSInfo virDomainGetHostname virDomainGetID virDomainGetIOThreadInfo virDomainGetInfo virDomainGetInterfaceParameters virDomainGetJobInfo virDomainGetJobStats virDomainGetMaxMemory virDomainGetMaxVcpus virDomainGetMemoryParameters virDomainGetMetadata virDomainGetName virDomainGetNumaParameters virDomainGetOSType virDomainGetSchedulerParameters virDomainGetSchedulerParametersFlags virDomainGetSchedulerType virDomainGetSecurityLabel virDomainGetSecurityLabelList virDomainGetState virDomainGetTime virDomainGetUUID virDomainGetUUIDString virDomainGetVcpuPinInfo virDomainGetVcpus virDomainGetVcpusFlags virDomainGetXMLDesc virDomainHasManagedSaveImage virDomainIOThreadInfoFree virDomainInterfaceAddresses virDomainInterfaceStats virDomainIsActive virDomainIsPersistent virDomainIsUpdated virDomainListGetStats virDomainLookupByID virDomainLookupByName virDomainLookupByUUID virDomainLookupByUUIDString virDomainMemoryPeek virDomainMemoryStats virDomainMigrateGetCompressionCache virDomainMigrateGetMaxSpeed virDomainPinEmulator ? virDomainPinIOThread virDomainPinVcpu virDomainPinVcpuFlags virDomainSaveImageGetXMLDesc virDomainStatsRecordListFree |
virConnectDomainXMLFromNative virConnectDomainXMLToNative |
Secret | virSecretDefineXML | virSecretFree virSecretUndefine |
virSecretSetValue | virConnectListAllSecrets virConnectListSecrets virConnectNumOfSecrets virSecretGetConnect/UUID/UUIDString/UsageID/UsageType/Value/XMLDesc virSecretLookupByUUID/UUIDString/Usage virSecretRef |
|
Stream | virStreamNew | virStreamFree | virStreamFinish virStreamAbort
virStreamRecv
virStreamRecvAll
virStreamSend
virStreamSendAll
|
virStreamSinkFunc virStreamSourceFunc |
Libvirt 使用 XML 来定义各种对象,其中,与 OpenStack Nova 关系比较密切的有:
disk (磁盘) | 任何磁盘设备,包括软盘(floppy)、硬盘(hard disk)、光驱(cdrom)或者半虚拟化驱动都使用 <disk> 元素来定义。 方式:
<disk type=‘**‘ device=‘**‘>。其中:
(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> <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> <disk type=‘block‘ device=‘cdrom‘> <driver name=‘qemu‘ type=‘raw‘/> <target dev=‘hdd‘ bus=‘ide‘ tray=‘open‘/> <readonly/> </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> #virsh:attach-interface --domain d-2 --type network --source isolatednet1 --mac 52:53:00:4b:75:6f --config
(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 vSwithc bridge ovsbr <source bridge=‘ovsbr‘/> <virtualport type=‘openvswitch‘> <parameters profileid=‘menial‘ interfaceid=‘09b11c53-8b5c-4eeb-8f00-d84eaa0aaa4f‘/> </virtualport> </interface> #virsh:attach-interface --domain d-2 --type bridge --source virbr0 --mac 52:22:33:44:55:66 --config (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> |
|
network (网络) |
<bridge name="virbr0" stp="on" delay="5" macTableManager="libvirt"/> <domain name="example.com" localOnly="no"/> <forward mode="nat" dev="eth0"/> 1. bridge:定义一个用于构造该虚拟网络的网桥。 2. domain:定义 DHCP server 的 DNS domain。 3. forward: 定义虚拟网络直接连到物理 LAN 的方式. ”mode“指转发模式。 (1) mode=‘nat’:所有连接到该虚拟网络的虚拟的网络都会经过物理机器的网卡,并转换成物理网卡的地址。 <network> <name>default</name> <bridge name="virbr0" /> <forward mode="nat"/> <ip address="192.168.122.1" netmask="255.255.255.0"> <dhcp> <range start="192.168.122.2" end="192.168.122.254" /> </dhcp> </ip> <ip family="ipv6" address="2001:db8:ca2:2::1" prefix="64" /> </network> 也可以指定公共的IP地址和端口号。 <forward mode=‘nat‘><nat><address start=‘1.2.3.4‘ end=‘1.2.3.10‘/> </nat> </forward> <forward mode=‘nat‘><nat><port start=‘500‘ end=‘1000‘/></nat></forward> <network> <name>local</name> <bridge name="virbr1" /> <forward mode="route" dev="eth1"/> <ip address="192.168.122.1" netmask="255.255.255.0"> <dhcp> <range start="192.168.122.2" end="192.168.122.254" /> </dhcp> </ip> <ip family="ipv6" address="2001:db8:ca2:2::1" prefix="64" /> </network> <network> <name>host-bridge</name> <forward mode="bridge"/> <bridge name="br0"/> </network> <forward mode=‘passthrough‘> <interface dev=‘eth10‘/> <interface dev=‘eth11‘/> <interface dev=‘eth12‘/> <interface dev=‘eth13‘/> <interface dev=‘eth14‘/> </forward> <forward mode=‘hostdev‘ managed=‘yes‘> <driver name=‘vfio‘/> <address type=‘pci‘ domain=‘0‘ bus=‘4‘ slot=‘0‘ function=‘1‘/> <address type=‘pci‘ domain=‘0‘ bus=‘4‘ slot=‘0‘ function=‘2‘/> <address type=‘pci‘ domain=‘0‘ bus=‘4‘ slot=‘0‘ function=‘3‘/> </forward> |
详细的 XML 定义说明在 https://libvirt.org/format.html。
libvirt API 的实现是在各个 Hypervisor driver 和 Storage dirver 内。Hypervisor 驱动包括:
#C API int virConnectNumOfDomains (virConnectPtr conn); int virDomainSetMaxMemory (virDomainPtr domain, unsigned long memory);
#Python APIvirConnect::numOfDomains(self)
virDomain::setMaxMemory(self, memory)
因此,libvirt 官网并没有提供详细的 python API 描述。
virsh command | Public API | QEMU driver function | Monitor command |
---|---|---|---|
virsh create XMLFILE | virDomainCreateXML() | qemudDomainCreate() | info cpus, cont, change vnc password, balloon (all indirectly) |
virsh suspend GUEST | virDomainSuspend() | qemudDomainSuspend() | stop |
virsh resume GUEST | virDomainResume() | qemudDomainResume() | cont |
virsh shutdown GUEST | virDomainShutdown() | qemudDomainShutdown() | system_powerdown |
virsh setmem GUEST MEM-KB | virDomainSetMemory() | qemudDomainSetMemory() | balloon (indirectly) |
virsh dominfo GUEST | virDomainGetInfo() | qemudDomainGetInfo() | info balloon (indirectly) |
virsh save GUEST FILENAME | virDomainSave() | qemudDomainSave() | stop, migrate exec |
virsh restore FILENAME | virDomainRestore() | qemudDomainRestore() | cont |
virsh dumpxml GUEST | virDomainDumpXML() | qemudDomainDumpXML() | info balloon (indirectly) |
virsh attach-device GUEST XMLFILE | virDomainAttachDevice() | qemudDomainAttachDevice() | change, eject, usb_add, pci_add (all indirectly) |
virsh detach-device GUEST XMLFILE | virDomainDetachDevice() | qemudDomainDetachDevice() | pci_del (indirectly) |
virsh migrate GUEST DEST-URI | virDomainMigrate() | qemudDomainMigratePerform() | stop, migrate_set_speed, migrate, cont |
virsh domblkstat GUEST | virDomainBlockStats() | qemudDomainBlockStats() | info blockstats |
- | virDomainBlockPeek() | qemudDomainMemoryPeek() | memsave |
有三种方式来安装 libvirt:
(1)下载 libvirt 的源代码,然后编译和安装
(2)从各 Linux 的发行版中直接安装,比如 Ubuntu 上运行 apt-get install libvirt-bin
(3)从 git 上克隆 libvirt 的代码,然后编译和安装
这里只描述基本的过程。具体的过程,下一篇文章会具体分析 Nova 中 libvirt 的使用。
标签:
原文地址:http://www.cnblogs.com/sammyliu/p/4558638.html