用于管理kvm主机的api。

kvm的Python项目详细描述


此模块旨在管理kvm管理程序。为此,请使用 unix module允许管理 本地和远程的类Unix系统,通过重载类 实例。这个模块只是virsh命令的包装器。它解析 virsh命令的输出(XML和文本)。命令分组在 可通过属性访问的子对象。

安装

此模块与python2.7和python 3.*兼容。模块是 在pypi上,以便可以使用pip命令安装它。

例如,在virtualenv中使用kvm

$ virtualenv env/ --prompt "(myprog)"
$ . ./env/bin/activate
(myprog) $ pip install kvm

否则,源位于github上:https://github.com/fmenabe/python-kvm

用法

您需要从unix模块导入必要的类。管理程序是 由hypervisor对象表示,并且必须包装类型为的对象 unix.Localunix.Remote。它理论上支持任何unix系统,但是 磁盘操作需要加载nbd模块,因此最好使用 unix.linux.Linux主机。

>>>fromuniximportLocal,Remote,UnixError>>>fromunix.linuximportLinux>>>importkvm>>>importjson>>>localhost=kvm.Hypervisor(Linux(Local()))>>>localhost.hypervisor.nodeinfo(){'nb_cpu':1,'nb_threads_per_core':2,'memory':16331936,'numa_cells':1,'cpu_model':'x86_64','nb_cores_per_cpu':4,'nb_cores':8,'cpu_freq':1340}>>>localhost.list_domains(all=True){'guest1':{'id':-1,'state':'shut off'}}{'guest2':{'id':1,'state':'running'}}>>>localhost.domain.start('guest1')# Wait a few seconds for the domain to start.>>>localhost.domain.state('guest1')'running'>>>localhost.domain.id('guest1')2>>>print(json.dumps(localhost.domain.conf('guest1'),indent=2))# json is use for pretty printing the dictionnary containing the# configuration.{"@type":"kvm","name":"guest1","uuid":"ed68d942-5d4b-7bba-4d74-7d44d73779d3","memory":{"@unit":"KiB","#text":"2097152"},...}>>>localhost.list_networks(){'default':{'autostart':True,'persistent':True,'state':'active'}}>>>host=Remote()>>>host.connect('hypervisor1')>>>host=kvm.Hypervisor(Linux(host))>>>host.hypervisor.nodeinfo(){'cores_per_socket':12,'cpu_frequency':'2200 MHz','cpu_model':'x86_64','cpu_sockets':2,'cpus':24,'memory_size':'98974432 kB','numa_cells':1,'threads_per_core':1}>>>host.list_domains(all=True){'guest1':{'id':1,'state':'running'}}{'guest2':{'id':2,'state':'running'}}>>>host.domain.shutdown('guest2')# Wait for the domain to stop.>>>host.domain.state('guest1')'shut off'# Using the context manager for the connecion.>>>fromunix.linuxaslinux,kvm>>>withlinux.connect('hypervisor1')ashost:...host=kvm.Hypervisor(host)...host.hypervisor.node_info()

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Frida Java windows的逆向工程   Java术语中的对象初始化   java在何处放置maven参数化junit输入xml文件   java如何在Swift中创建以日期为键、布尔值为值的字典/地图?   java Velocity动态属性访问   java Maven站点我如何找到它们?   java JavaFX按钮在显示阶段时显示为选中状态   文件io字符串方法java赋值   java为SOAP头生成证书   java Android更改EditText的setError弹出消息的背景色   java格式存储为字符串的数字   java Testcontainers+dbrider:无法清除表X,消息:启用自动提交时无法提交。,原因:无效   java程序应该可以运行,但仍然没有输出?   Java在列表末尾添加一个节点?   java类对象中的默认构造函数做什么?   java如何更快地从映射返回键和值?