libvirt_python

时间:2022-09-04 15:22:09

一、Connections

连接函数接口
libvirt.open(name);                   //可读写方式连接上QEMU 参数说明: name:连接名称
libvirt.openAuth(uri, auth, flags);          //认证方式连接上QEMU      参数说明: uri:连接到Hypervisor的入口地址
libvirt.openReadOnly(name)               //可读方式连接上QEMU          
# Example-.py
from __future__ import print_function
import sys
import libvirt
conn = libvirt.open('qemu:///system')                            //可读写方式连接上hypervisor(QEMU)
if conn == None:
  print('Failed to open connection to qemu:///system', file=sys.stderr)
exit()
conn.close()
exit()
# Example-.py
from __future__ import print_function
import sys
import libvirt
conn = libvirt.openReadOnly('qemu:///system')                      //只读方式连接上hypervisor(QEMU)                                                              
if conn == None:
  print('Failed to open connection to qemu:///system', file=sys.stderr)
exit()
conn.close()
exit() # Example-.py
from __future__ import print_function
import sys
import libvirt
SASL_USER = "my-super-user"
SASL_PASS = "my-super-pass"
def request_cred(credentials, user_data):
  for credential in credentials:
    if credential[] == libvirt.VIR_CRED_AUTHNAME:
      credential[] = SASL_USER
    elif credential[] == libvirt.VIR_CRED_PASSPHRASE:
      credential[] = SASL_PASS
  return
auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_cred, None]
conn = libvirt.openAuth('qemu+tcp://localhost/system', auth, )
if conn == None:
  print('Failed to open connection to qemu+tcp://localhost/system', file=sys.stderr)
exit()
conn.close()

二、Host information

getHostname                    //获取主机名
getMaxVcpus                    //获取支持最大虚拟cpu个数
getInfo     //获取主机内存,CPU 相关信息 **以列表形式存储(list[7])存储8个值 list[0]:cpu平台(x86_64/i382);list[1]:主机内存大小;list[2]:CPU个数;list[3]:CPU频率;list[4]:NUMA节点个数;list[5]:CPU sockets 个数;list[6]:CPU核数;list[7]:CPU超线程核数
Member Description     
list[0] CPU平台
list[1]  主机内存大小
list[2] CPU个数
list[3] CPU频率
list[4] NUMA节点个数
list[5] CPU Sockets 个数
list[6] CPU核数
list[7] CPU超线程核数


                         

getCellsFreeMemory            //每个节点(如 NUMA 节点)空闲内存大小
getType                  //获取虚拟化平台类型
getVersion                //获取版本号 **版本号计算方法:Versions numbers are integers: 1000000*major + 1000*minor + release.
getLibVersion              //获取libvirt 版本号        
getURI                   //获取 libvirt 连接URI 地址             
isEncrypted              //检测连接方式是否加密
isSecure                //检测连接是否安全
isAlive                 //检测是否是连接状态
compareCPU              //设定CPU模式与主机CPU进行对比
getFreeMemory            //返回空间节点内存大小(设定CPU与主机CPU对比之后)      
getFreePages            //获取指定空闲页表大小
getMemoryParameters        //获取内存参数类型
getMemoryStats            //获取节点内存统计信息
getSecurityModel          //获取当前使用的安全模型
getSysinfo             
getCPUMap              //获取主机节点CPU的CPU映射
getCPUStats             //获取CPU统计信息
getCPUModelNames          //获取与CPU体系结构匹配的名称列表
# Example-.py
from __future__ import print_function
import sys
import libvirt
conn = libvirt.open('qemu:///system')
if conn == None:
  print('Failed to open connection to qemu:///system', file=sys.stderr)
  exit() host = conn.getHostname()
print('Hostname:'+host) vcpus = conn.getMaxVcpus(None)
print('Maximum support virtual CPUs: '+str(vcpus)) nodeinfo = conn.getInfo()
print('Model: '+str(nodeinfo[]))
print('Memory size: '+str(nodeinfo[])+'MB')
print('Number of CPUs: '+str(nodeinfo[]))
print('MHz of CPUs: '+str(nodeinfo[]))
print('Number of NUMA nodes: '+str(nodeinfo[]))
print('Number of CPU sockets: '+str(nodeinfo[]))
print('Number of CPU cores per socket: '+str(nodeinfo[]))
print('Number of CPU threads per core: '+str(nodeinfo[])) nodeinfo = conn.getInfo()
numnodes = nodeinfo[]
memlist = conn.getCellsFreeMemory(, numnodes)
cell =
for cellfreemem in memlist:
  print('Node '+str(cell)+': '+str(cellfreemem)+' bytes free memory')
  cell += print('Virtualization type: '+conn.getType()) print('Version: '+str(conn.getVersion())) print('Libvirt Version: '+str(conn.getLibVersion())); print('Canonical URI: '+conn.getURI()) print('Connection is encrypted: '+str(conn.isEncrypted())) print('Connection is secure: '+str(conn.isSecure())) print("Connection is alive = " + str(conn.isAlive())) xml = '<cpu mode="custom" match="exact">' + \
'<model fallback="forbid">kvm64</model>' + \
'</cpu>'
retc = conn.compareCPU(xml)
if retc == libvirt.VIR_CPU_COMPARE_ERROR:
  print("CPUs are not the same or ther was error.")
elif retc == libvirt.VIR_CPU_COMPARE_INCOMPATIBLE:
  print("CPUs are incompatible.")
elif retc == libvirt.VIR_CPU_COMPARE_IDENTICAL:
  print("CPUs are identical.")
elif retc == libvirt.VIR_CPU_COMPARE_SUPERSET:
  print("The host CPU is better than the one specified.")
else:
  print("An Unknown return code was emitted.") print("Free memory on the node (host) is " + str(conn.getFreeMemory()) + " bytes.") pages = []
start =
cellcount =
buf = conn.getFreePages(pages, start, cellcount)
i =
for page in buf:
  print("Page Size: " + str(pages[i]) + " Available pages: " + str(page))
  ++i buf = conn.getMemoryParameters()
for parm in buf:
  print(parm) buf = conn.getMemoryStats(libvirt.VIR_NODE_MEMORY_STATS_ALL_CELLS)
for parm in buf:
  print(parm) model = conn.getSecurityModel()
print(model[] + " " + model[]) xmlInfo = conn.getSysinfo()
print(xmlInfo) map = conn.getCPUMap()
print("CPUs: " + str(map[]))
print("Available: " + str(map[])) stats = conn.getCPUStats()
print("kernel: " + str(stats['kernel']))
print("idle: " + str(stats['idle']))
print("user: " + str(stats['user']))
print("iowait: " + str(stats['iowait'])) models = conn.getCPUModelNames('x86_64')
for model in models:
  print(model) conn.close()
exit()

三、Guest Domains

libvirt_python的更多相关文章

  1. Package libvirt was not found in the pkg-config search path

    关于pip安装libvirt-python的时候提示Package libvirt was not found in the pkg-config search path的问题解决方法 1.一开始以为 ...

随机推荐

  1. iOS 学习 - 18&period;TextField 自定义菜单事件,复制和微信分享

    菜单事件包括,剪切.拷贝.全选.分享...,此 demo 只有 copy.share 1.定义 field 继承与 UITextField - (BOOL)canPerformAction:(SEL) ...

  2. 阿里云配置nginx&plus;php&plus;mysql

    #yum -y install php mysql mysql-server mysql-devel php-mysql php-cgi php-mbstring php-gd php-fastcgi ...

  3. Python 网页投票信息抓取

    最近学习python,为了巩固一下学过的知识,花了半天(主要还是因为自己正则表达式不熟)写了个小脚本来抓取一个网站上的投票信息,排名后进行输出. 抓取的网站网址是http://www.mudidi.n ...

  4. 面向对象 ---Java抽象类

    在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类除了不能实例化对象之外, ...

  5. POJ 2186&period;Popular Cows (强连通)

    强连通缩点,统计入度为1的缩点后的点的个数 个数1的话输出这个强连通分量的点的数量 否则输出0: code /* Kosaraju算法,无向图的强连通分量,时间复杂度O(n+m) 思路: 按照图G的深 ...

  6. laravel Scout包在elasticsearch中的应用

    laravel Scout包在elasticsearch中的应用 laravel的Scout包是针对自身的Eloquent模型开发的基于驱动的全文检索引擎.意思就是我们可以像使用ORM一样使用检索功能 ...

  7. a span做成按钮样式不选中文字

    a,span做成按钮样式时,文字会被选中.加以下CSS可以让其不选中.测试三大浏览器都可以 .button { display: inline-block; -moz-user-select: non ...

  8. React框架简介

    React的基本认识 Facebook开源的一个js库,一个用来动态构建用户界面的js库 英文官网,中文官网 React的特点 Declarative(声明式编码),Component-Based(组 ...

  9. casperjs get开头的几个dom操作使用

    getCurrentUrl() Signature: getCurrentUrl() Retrieves current page URL. Note that the url will be url ...

  10. iframe元素的学习&lpar;笔记&rpar;

    什么是iframe:iframe元素即内联框架,iframe是内联的并且承前启后,对于外围的页面,iframe是一个普通的元素,对于iframe里面的内容,又是一个五脏俱全的页面.重下面的写法可以看出 ...

相关文章